Skip to content

refactor(models): move linear-attention hybrids onto scheduler-owned layer state - #3644

Merged
inureyes merged 1 commit into
mainfrom
refactor/issue-3327-linear-attention-layer-state
Dec 1, 2026
Merged

inureyes merged 1 commit into
mainfrom
refactor/issue-3327-linear-attention-layer-state

Conversation

@inureyes

@inureyes inureyes commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

Moves the five linear-attention hybrid families (qwen3_next, qwen4_exp with Qwen4ExpVLModel and Qwen4ExpMtpTargetAdapter, bailing_moe_linear, kimi_linear, and kimi_k3 with KimiK3VLModel) off ModelOwnedSequenceState onto the CachePool-backed LayerState contract from ADR 0010, and removes the temporary qwen3_5_layer_state::LentCaches bridge by generalizing Qwen3-5 over HybridLayerCache.

Closes #3327

Changes

  • Core mlxcel-core & Pooled extensions: Added LayerState::KvWithAux and RecurrentState::token_contexts for Qwen4-Exp's QSA index cache and PLE n-gram contexts without per-step MLX eval overhead, added LayerStateEncoding::Qwen4Exp(Qwen4ExpSnapshotConfig), and added can_snapshot_sequence, on_layer_state_restored, rewind_layer_state, and last_row_logits delegation across LanguageModel, Pooled, LoadedModel, VisionLanguageModel, and Engine (unwind_appends and verify_rounds_unsupported).
  • Generic snapshot dispatch: Extended state_snapshot to encode, restore, and checkpoint LayerStateEncoding::Qwen4Exp with identical tensor names and shapes to the prior model-owned encoder, and to reject truncation on LayerState::KvWithAux.
  • Shared GatedDeltaNet helpers & LentCaches removal: Added HybridLayerCache (implemented for LayerState), GATED_DELTA_SLOTS, and GatedDeltaGuard in src/models/gated_delta.rs plus RecurrentState batching helpers in src/models/recurrent_batch.rs; generalized Qwen35Model over C: HybridLayerCache and deleted src/models/qwen3_5_layer_state.rs.
  • Family migrations: Replaced ModelOwnedSequenceState stores with fallback RefCell<Vec<LayerState>> and forward_layer_state implementations in qwen3_next, bailing_moe_linear, kimi_linear, kimi_k3, and qwen4_exp (keeping Qwen3NextStageModel and MRoPE side maps untouched, and keeping per-sequence rollback logs for Qwen4-Exp speculative verification). Forwarded the full layer-state contract through Qwen4ExpVLModel and KimiK3VLModel, and updated Qwen4ExpMtpTargetAdapter to borrow caller/scheduler LayerState rows.
  • Tests: Added encoder-parity and checkpoint tests in state_snapshot_tests.rs and qwen4_exp_snapshot_tests.rs, plus 16-token bit-exact snapshot round-trip tests through Pooled across all five families.

Verification

  • cargo test --profile test-fast --features metal,accelerate --lib qwen3_next -- --test-threads=1: 26 passed, 0 failed.
  • cargo test --profile test-fast --features metal,accelerate --lib bailing_moe_linear -- --test-threads=1: 41 passed, 0 failed.
  • cargo test --profile test-fast --features metal,accelerate --lib kimi_linear -- --test-threads=1: 25 passed, 0 failed.
  • cargo test --profile test-fast --features metal,accelerate --lib kimi_k3 -- --test-threads=1: 119 passed, 0 failed (1 ignored: real weights).
  • cargo test --profile test-fast --features metal,accelerate --lib qwen4_exp -- --test-threads=1: 211 passed, 0 failed.
  • cargo test --profile test-fast --features metal,accelerate --lib qwen3_5 -- --test-threads=1: 62 passed, 0 failed (22 ignored: real weights/benchmarks).
  • cargo test --profile test-fast --features metal,accelerate --lib state_snapshot -- --test-threads=1: 13 passed, 0 failed.
  • cargo test --profile test-fast --features metal,accelerate --lib hybrid_snapshot -- --test-threads=1: 9 passed, 0 failed.
  • cargo test --profile test-fast --features metal,accelerate -p mlxcel-core layer_state -- --test-threads=1: 9 passed, 0 failed.
  • cargo fmt --all -- --check: passed.
  • cargo clippy --profile test-fast --features metal,accelerate --lib --tests -- -D warnings -A clippy::manual-contains: passed (the single clippy::manual-contains lint is pre-existing in unrelated src/models/lfm3_omni_config.rs under rustc 1.97).
  • cargo check --profile test-fast --features metal,accelerate --bin mlxcel --bin mlxcel-server --bin speculative_bench: passed.
  • rg 'ModelOwnedSequenceState' src/models/qwen3_next.rs src/models/qwen4_exp.rs src/models/bailing_moe_linear.rs src/models/kimi_linear.rs src/models/kimi_k3.rs: no matches.
  • rg 'LentCaches|qwen3_5_layer_state' src/: no matches.
  • python3 scripts/ci/check_cross_repo_refs.py: passed.
  • Unmeasured: Real-checkpoint B=1 / B=4 release decode throughput comparison against main (per worktree watchdog guard; no cold release build or real-checkpoint benchmark was run in this worktree).

@inureyes inureyes added type:refactor Code restructuring without changing functionality priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:architecture Architecture and code structure changes status:done Completed labels Dec 1, 2026
…layer state

Move qwen3_next, qwen4_exp (with Qwen4ExpVLModel and Qwen4ExpMtpTargetAdapter), bailing_moe_linear, kimi_linear, and kimi_k3 (with KimiK3VLModel) off ModelOwnedSequenceState onto the CachePool-backed LayerState contract from ADR 0010, and remove the temporary qwen3_5_layer_state::LentCaches bridge by generalizing Qwen3-5 over HybridLayerCache.

- Extend LayerState with LayerState::KvWithAux and RecurrentState::token_contexts for Qwen4-Exp's QSA index and PLE n-gram state, and add LayerStateEncoding::Qwen4Exp plus can_snapshot_sequence / on_layer_state_restored / rewind_layer_state hooks on LanguageModel, Pooled, and Engine.
- Replace ModelOwnedSequenceState stores in qwen3_next, qwen4_exp, bailing_moe_linear, kimi_linear, and kimi_k3 with fallback LayerState vectors and forward_layer_state implementations; route snapshots and checkpoints through the generic state_snapshot path.
- Add encoder-parity and 16-token bit-exact snapshot round-trip tests through Pooled across all five families.

Ref: #3327
@inureyes
inureyes force-pushed the refactor/issue-3327-linear-attention-layer-state branch from 431e139 to 727a20a Compare December 1, 2026 15:32
@inureyes
inureyes marked this pull request as ready for review December 1, 2026 15:33
@inureyes
inureyes merged commit 339ad1f into main Dec 1, 2026
35 of 36 checks passed
@inureyes
inureyes deleted the refactor/issue-3327-linear-attention-layer-state branch December 1, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:architecture Architecture and code structure changes area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:refactor Code restructuring without changing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: Move the linear-attention hybrids onto scheduler-owned layer state

1 participant