Skip to content

refactor(llama4): move Llama 4 onto scheduler-owned layer state with chunked layers - #3642

Merged
inureyes merged 1 commit into
mainfrom
refactor/issue-3326-llama4-scheduler-owned-state
Dec 1, 2026
Merged

inureyes merged 1 commit into
mainfrom
refactor/issue-3326-llama4-scheduler-owned-state

Conversation

@inureyes

@inureyes inureyes commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

Closes #3326

Migrates Llama4Wrapper (src/models/llama4.rs) onto scheduler-owned layer state (ADR 0010, epic #3259) as the first consumer of LayerStateKind::Chunked { chunk } and LayerState::Chunked.

Changes

  • Added LayerStateEncoding::Chunked { standard, chunked } in src/lib/mlxcel-core/src/cache/layer_state_request.rs and wired LayerState::matches_kind (checking cache.chunk_size == chunk) plus as_chunked / as_chunked_mut in src/lib/mlxcel-core/src/cache/layer_state.rs.
  • Parameterized the chunked segment in KvSnapshotNames (src/models/kv_snapshot.rs) and implemented the chunked arms of StatePolicy::{snapshot, restore, truncatable_to, restore_truncated, state_checkpoint, restore_from_checkpoint} and check_kv_tensors in src/server/prompt_cache/state_snapshot.rs.
  • Generalized Llama 4's forward methods over Llama4LayerCache (implemented for both Llama4Cache and LayerState), removed ModelOwnedSequenceState and per-family snapshot/sequence hooks from Llama4Wrapper, implemented layer_state_kinds, make_layer_state, layer_state_snapshot_spec, and forward_layer_state, and kept supports_paged_decode_backend() == true.
  • Kept padded prefill declined (supports_padded_prefill() == false): LayerState::rewind_padded_prefill and LayerState::rewind_decode_appends continue to return an error for LayerState::Chunked rather than enabling a chunk-boundary rewind in this refactor.
  • Forwarded last_row_logits on Pooled<M> (src/models/pooled_model_language_model.rs) and moved Llama 4 model-level tests onto Pooled<Llama4Wrapper>.
  • Added unit, round-trip, encoder byte-compatibility, chunk-boundary truncation, and paged-override scheduler tests (src/models/llama4_layer_state_tests.rs, src/server/prompt_cache/state_snapshot_tests.rs, src/server/batch/scheduler_model_owned_cache_tests.rs, src/lib/mlxcel-core/src/cache/layer_state_tests.rs).

Validation

  • cargo fmt --all -- --check: passed.
  • cargo clippy --profile test-fast --features metal,accelerate -p mlxcel-core --lib --tests -- -D warnings: passed (0 warnings).
  • cargo clippy --profile test-fast --features metal,accelerate --lib --tests -- -D warnings: 0 warnings in all changed files (1 pre-existing clippy::manual_contains on main in src/models/lfm3_omni_config.rs:184).
  • cargo test --profile test-fast --features metal,accelerate -p mlxcel-core --lib layer_state::tests -- --test-threads=1: 9 passed.
  • cargo test --profile test-fast --features metal,accelerate --lib llama4::tests -- --include-ignored --test-threads=1: 25 passed.
  • cargo test --profile test-fast --features metal,accelerate --lib state_snapshot::tests -- --test-threads=1: 10 passed.
  • cargo test --profile test-fast --features metal,accelerate --lib kv_snapshot::tests -- --test-threads=1: passed.
  • cargo test --profile test-fast --features metal,accelerate --lib llama4_runs_under_paged_decode_override_with_scheduler_owned_state -- --test-threads=1: passed.
  • cargo test --profile test-fast --features metal,accelerate --lib llama4_helpers -- --test-threads=1: 9 passed (3 ignored serial MLX tests).
  • cargo test --profile test-fast --features metal,accelerate --lib snapshot_forwarding_tests -- --test-threads=1: 2 passed.
  • rg 'ModelOwnedSequenceState' src/models/llama4.rs: 0 matches.
  • python3 scripts/ci/check_cross_repo_refs.py: passed.

Unmeasured

  • Release-build B=1 and B=4 decode tok/s at 256 and 8192 prompt tokens (main vs branch, interleaved with null arm) on a real Llama 4 checkpoint was not run in this automated wave worker (pre-warmed test-fast profile only; cold release builds are deferred to post-wave / release validation).

@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 area:core mlxcel-core: MLX FFI, primitives, KV cache, layers status:done Completed labels Dec 1, 2026
@inureyes
inureyes force-pushed the refactor/issue-3326-llama4-scheduler-owned-state branch from 4e98c24 to 1b545e2 Compare December 1, 2026 15:18
@inureyes
inureyes marked this pull request as ready for review December 1, 2026 15:18
@inureyes
inureyes merged commit 45dc308 into main Dec 1, 2026
35 of 36 checks passed
@inureyes
inureyes deleted the refactor/issue-3326-llama4-scheduler-owned-state branch December 1, 2026 15:18
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 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 Llama 4 onto scheduler-owned layer state with chunked layers

1 participant