Skip to content

refactor: extend scheduler-owned layer state to composite per-layer state (DeepSeek V4, IQuest-Coder Loop) - #3641

Merged
inureyes merged 1 commit into
mainfrom
refactor/issue-3329-composite-layer-state
Dec 1, 2026
Merged

inureyes merged 1 commit into
mainfrom
refactor/issue-3329-composite-layer-state

Conversation

@inureyes

@inureyes inureyes commented Dec 1, 2026

Copy link
Copy Markdown
Member

Closes #3329

Summary

Extends scheduler-owned per-layer sequence state (ADR 0010) to composite per-layer storages and migrates DeepSeekV4Model and IQuestLoopCoderWrapper off ModelOwnedSequenceState.

Composite representation decision

  • Chosen: LayerStateKind::Composite(Vec<LayerStateKind>) and LayerState::Composite(Vec<LayerState>), recorded in docs/adr/0010-scheduler-owned-sequence-state.md. Preserves the 1-to-1 layer_state.len() == num_layers() invariant, adds zero indirection to single-storage families, and composes recursively across matches_kind, stored_mode, offset, visible_len, nbytes, atomic rewind_padded_prefill/rewind_decode_appends, flush_decode_undo, and state_snapshot.
  • Rejected: Replacing Vec<LayerState> with a per-layer group (Vec<Vec<LayerState>> / Vec<LayerGroup>) across SequenceCacheSet and forward_layer_state (forces extra indirection and trait churn onto all single-storage families), or flattening composite layers into len() > num_layers() (breaks paged shadow accounting, storage-mode reporting, and layer{i} snapshot prefixes).
  • Paged accounting: Added LanguageModel::layer_state_paged_lengths (defaulting to LayerState::visible_len) so DeepSeek V4 mirrors monotonic local.offset into paged admission accounting for its growing pooling caches.

Changes

  • src/lib/mlxcel-core/src/cache/layer_state.rs & layer_state_tests.rs: Added LayerStateKind::Composite and LayerState::Composite plus 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 forwarded layer_state_paged_lengths.
  • src/server/prompt_cache/state_snapshot.rs & state_snapshot_tests.rs: Added recursive Composite handling for whole snapshot, restore, truncation, turn-boundary checkpoints, and KV tensor validation (flat layer{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: Migrated DeepSeekV4Model to scheduler-owned composite state via zero-copy LentV4Caches, preserving PoolingCache::eval_state() barriers and local.offset paged lengths, and moved by-id tests onto Pooled.
  • src/models/iquestloopcoder_model.rs, iquestloopcoder_tests.rs: Migrated IQuestLoopCoderWrapper to scheduler-owned composite state ([Full, Sliding]) via zero-copy LentLoopCaches, keeping supports_snapshot_reuse() == false by design, and moved by-id tests onto Pooled.
  • 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 unrelated manual_contains warning in src/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

  • Real-checkpoint B=1 and B=4 decode throughput benchmarks at 256 and 8192 prompt tokens for DeepSeek V4 and IQuest-Coder Loop were not run because neither checkpoint is present locally.

@inureyes inureyes added type:refactor Code restructuring without changing functionality status:done Completed area:models Model architectures, weights, loading, metadata area:architecture Architecture and code structure changes area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Dec 1, 2026
…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
inureyes force-pushed the refactor/issue-3329-composite-layer-state branch from d1286f9 to 5a2afa3 Compare December 1, 2026 16:25
@inureyes
inureyes marked this pull request as ready for review December 1, 2026 16:26
@inureyes
inureyes merged commit 973db48 into main Dec 1, 2026
36 of 37 checks passed
@inureyes
inureyes deleted the refactor/issue-3329-composite-layer-state branch December 1, 2026 16:26
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:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata status:done Completed type:refactor Code restructuring without changing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: Extend scheduler-owned layer state to composite per-layer state (DeepSeek V4, IQuest-Coder Loop)

1 participant