Repository navigation
refactor: extend scheduler-owned layer state to composite per-layer state (DeepSeek V4, IQuest-Coder Loop) - #3641
Merged
Conversation
…layer state (#3329) Introduce LayerStateKind::Composite and LayerState::Composite in mlxcel-core and recursive composite handling in state_snapshot, then migrate DeepSeek V4 and IQuest-Coder Loop off ModelOwnedSequenceState onto scheduler-owned layer state. - Add LayerStateKind::Composite(Vec<LayerStateKind>) and LayerState::Composite(Vec<LayerState>) with recursive matches_kind, stored_mode, offset, visible_len, nbytes, atomic rewind checks, and decode-undo flushing. - Add LanguageModel::layer_state_paged_lengths (defaulting to LayerState::visible_len) so DeepSeek V4 mirrors monotonic local.offset into paged admission accounting. - Extend server::prompt_cache::state_snapshot (snapshot, restore, truncation, checkpoints, KV validation) to handle Composite layers with flat prefixes for disjoint categories and .part{j} disambiguation for duplicate categories. - Migrate DeepSeekV4Model and IQuestLoopCoderWrapper to scheduler-owned state using zero-copy LentV4Caches / LentLoopCaches borrow guards, preserving DeepSeek V4 eval_state barriers and keeping IQuest-Coder Loop snapshot reuse disabled by design. - Record the composite representation decision and rejected alternatives in docs/adr/0010-scheduler-owned-sequence-state.md.
inureyes
force-pushed
the
refactor/issue-3329-composite-layer-state
branch
from
December 1, 2026 16:25
d1286f9 to
5a2afa3
Compare
inureyes
marked this pull request as ready for review
December 1, 2026 16:26
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.
Closes #3329
Summary
Extends scheduler-owned per-layer sequence state (ADR 0010) to composite per-layer storages and migrates
DeepSeekV4ModelandIQuestLoopCoderWrapperoffModelOwnedSequenceState.Composite representation decision
LayerStateKind::Composite(Vec<LayerStateKind>)andLayerState::Composite(Vec<LayerState>), recorded indocs/adr/0010-scheduler-owned-sequence-state.md. Preserves the 1-to-1layer_state.len() == num_layers()invariant, adds zero indirection to single-storage families, and composes recursively acrossmatches_kind,stored_mode,offset,visible_len,nbytes, atomicrewind_padded_prefill/rewind_decode_appends,flush_decode_undo, andstate_snapshot.Vec<LayerState>with a per-layer group (Vec<Vec<LayerState>>/Vec<LayerGroup>) acrossSequenceCacheSetandforward_layer_state(forces extra indirection and trait churn onto all single-storage families), or flattening composite layers intolen() > num_layers()(breaks paged shadow accounting, storage-mode reporting, andlayer{i}snapshot prefixes).LanguageModel::layer_state_paged_lengths(defaulting toLayerState::visible_len) so DeepSeek V4 mirrors monotoniclocal.offsetinto paged admission accounting for its growing pooling caches.Changes
src/lib/mlxcel-core/src/cache/layer_state.rs&layer_state_tests.rs: AddedLayerStateKind::CompositeandLayerState::Compositeplus unit tests.src/lib/mlxcel-core/src/generate.rs,engine/mod.rs,language_model_ref.rs,src/loaded_model.rs,src/models/pooled_model_language_model.rs: Added and forwardedlayer_state_paged_lengths.src/server/prompt_cache/state_snapshot.rs&state_snapshot_tests.rs: Added recursiveCompositehandling for whole snapshot, restore, truncation, turn-boundary checkpoints, and KV tensor validation (flatlayer{i}prefix when part storage categories are disjoint,.part{j}disambiguation when categories repeat), with tests pinning exact tensor names against the individual encoders.src/models/deepseek_v4.rs,deepseek_v4_layer_state.rs,deepseek_v4_compress.rs,deepseek_v4_tests.rs: MigratedDeepSeekV4Modelto scheduler-owned composite state via zero-copyLentV4Caches, preservingPoolingCache::eval_state()barriers andlocal.offsetpaged lengths, and moved by-id tests ontoPooled.src/models/iquestloopcoder_model.rs,iquestloopcoder_tests.rs: MigratedIQuestLoopCoderWrapperto scheduler-owned composite state ([Full, Sliding]) via zero-copyLentLoopCaches, keepingsupports_snapshot_reuse() == falseby design, and moved by-id tests ontoPooled.src/server/batch/scheduler_model_owned_cache_tests.rs: Added paged-override (DecodeStorageBackend::Paged) scheduler tests for both DeepSeek V4 and IQuest-Coder Loop.docs/adr/0010-scheduler-owned-sequence-state.md: Recorded the composite representation decision and rejected alternatives.Verification
rg 'ModelOwnedSequenceState' src/models/deepseek_v4.rs src/models/iquestloopcoder_model.rs: 0 matches.cargo fmt --all -- --check: passed.cargo clippy --profile test-fast --features metal,accelerate -p mlxcel-core --lib --tests -- -D warnings: passed (0 warnings). Root-crate clippy passed for all touched files (only 1 pre-existing unrelatedmanual_containswarning insrc/models/lfm3_omni_config.rs).cargo test --profile test-fast --features metal,accelerate -p mlxcel-core cache::layer_state::tests: 11/11 passed.cargo test --profile test-fast --features metal,accelerate --lib state_snapshot::tests -- --test-threads=1: 12/12 passed.cargo test --profile test-fast --features metal,accelerate --lib deepseek_v4::deepseek_v4_tests -- --test-threads=1: 52/52 passed.cargo test --profile test-fast --features metal,accelerate --lib iquestloopcoder_tests -- --test-threads=1: 21/21 passed.cargo test --profile test-fast --features metal,accelerate --lib scheduler_model_owned_cache_tests -- --test-threads=1: 6/6 passed.python3 scripts/ci/check_cross_repo_refs.py: passed.Unmeasured