Repository navigation
refactor(models): move linear-attention hybrids onto scheduler-owned layer state - #3644
Merged
Merged
Conversation
…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
force-pushed
the
refactor/issue-3327-linear-attention-layer-state
branch
from
December 1, 2026 15:32
431e139 to
727a20a
Compare
inureyes
marked this pull request as ready for review
December 1, 2026 15:33
2 of 8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Moves the five linear-attention hybrid families (
qwen3_next,qwen4_expwithQwen4ExpVLModelandQwen4ExpMtpTargetAdapter,bailing_moe_linear,kimi_linear, andkimi_k3withKimiK3VLModel) offModelOwnedSequenceStateonto theCachePool-backedLayerStatecontract from ADR 0010, and removes the temporaryqwen3_5_layer_state::LentCachesbridge by generalizing Qwen3-5 overHybridLayerCache.Closes #3327
Changes
mlxcel-core&Pooledextensions: AddedLayerState::KvWithAuxandRecurrentState::token_contextsfor Qwen4-Exp's QSA index cache and PLE n-gram contexts without per-step MLX eval overhead, addedLayerStateEncoding::Qwen4Exp(Qwen4ExpSnapshotConfig), and addedcan_snapshot_sequence,on_layer_state_restored,rewind_layer_state, andlast_row_logitsdelegation acrossLanguageModel,Pooled,LoadedModel,VisionLanguageModel, andEngine(unwind_appendsandverify_rounds_unsupported).state_snapshotto encode, restore, and checkpointLayerStateEncoding::Qwen4Expwith identical tensor names and shapes to the prior model-owned encoder, and to reject truncation onLayerState::KvWithAux.LentCachesremoval: AddedHybridLayerCache(implemented forLayerState),GATED_DELTA_SLOTS, andGatedDeltaGuardinsrc/models/gated_delta.rsplusRecurrentStatebatching helpers insrc/models/recurrent_batch.rs; generalizedQwen35ModeloverC: HybridLayerCacheand deletedsrc/models/qwen3_5_layer_state.rs.ModelOwnedSequenceStatestores with fallbackRefCell<Vec<LayerState>>andforward_layer_stateimplementations inqwen3_next,bailing_moe_linear,kimi_linear,kimi_k3, andqwen4_exp(keepingQwen3NextStageModeland MRoPE side maps untouched, and keeping per-sequence rollback logs for Qwen4-Exp speculative verification). Forwarded the full layer-state contract throughQwen4ExpVLModelandKimiK3VLModel, and updatedQwen4ExpMtpTargetAdapterto borrow caller/schedulerLayerStaterows.state_snapshot_tests.rsandqwen4_exp_snapshot_tests.rs, plus 16-token bit-exact snapshot round-trip tests throughPooledacross 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 singleclippy::manual-containslint is pre-existing in unrelatedsrc/models/lfm3_omni_config.rsunder 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.main(per worktree watchdog guard; no cold release build or real-checkpoint benchmark was run in this worktree).