Repository navigation
Description
Part of #3259
Problem / Background
Measured on GB10 with mlxcel-server 0.8.1 and gemma-4-12b-it-4bit (fp16 KV, prompts of about 5.8k tokens): an exact multi-turn continuation hits (5808/5823 cached, about 4.9 s down to 0.4-0.56 s). Any divergence before the prompt end is a full miss. The sliding ring (sliding_window 1024) has wrapped, so the history-boundary snapshot cannot be truncated, and the lookup records snapshot_diverged. A desktop client (AI:GO) that edits the previous user message saw cached_tokens=0 on every turn. Qwen3 under the same conditions reuses up to the divergence point.
The llama-server answer is a ring of per-slot context checkpoints (--ctx-checkpoints N, alias --swa-checkpoints, env LLAMA_ARG_CTX_CHECKPOINTS, default 32; --checkpoint-min-step N, env LLAMA_ARG_CHECKPOINT_MIN_SPACING_NT, default 8192, negative rejected), per common/arg.cpp:1687-1703 and common/common.h:614-615 at the pinned b10621 commit c1d0e7a0. mlxcel refuses both flags above 0 (src/cli/slot_args.rs:180-201).
Current Behavior
0.8.1 predates the multi-checkpoint machinery. origin/main (81001c7) now has it, behind environment switches only, for every supports_snapshot_reuse() family including Gemma 3, Gemma 4 and AFMoE:
MLXCEL_PROMPT_CACHE_SYSTEM_ROOT(feat(prompt-cache): add opt-in system-prompt root snapshot #2950) stores a whole snapshot at the end of the system block (policy.rs:117-197,system_root_leninscheduler/mod.rs:263).MLXCEL_PROMPT_CACHE_CHECKPOINT_INTERVAL(feat(prompt-cache): add opt-in periodic prefill checkpoints #2973) stores whole snapshots at absolute multiples of an interval of at least 256 (policy.rs:203-292,checkpoint_pointsinscheduler/mod.rs:292).MLXCEL_PROMPT_CACHE_KEEP_PREVIOUS_TURN(feat(prompt-cache): keep the previous turn's restore point #3037) exempts the previous turn's restore source from the session-chain supersede rule (policy.rs:294-325).- Turn-boundary state checkpoints (feat(prompt-cache): checkpoint only recurrent state at turn boundaries for hybrid models #3047,
src/server/prompt_cache/state_checkpoints.rs) store only the unrebuildable state and rebuild the attention rows from a longer donor snapshot of the same conversation. They have their own budget (state_checkpoint_capacity_bytes,policy.rs:85, sized byrecommend_state_checkpoint_capacityinsnapshot_sizing.rs:411) and their own/v1/cache/statsfields (state_checkpoint_{entries,bytes,inserts,hits,evictions},routes/cache.rs:210-222). Onlyqwen3_5,qwen3_next,qwen4_expand their VLM wrappers implementstate_checkpoint_from_snapshot/restore_sequence_state_from_checkpoint, so Gemma gets no state checkpoints.
The slot_args.rs refusal text and module doc are stale: they name capture_history_boundary_snapshot, which no longer exists, and say there is at most one snapshot per sequence.
Whole snapshots are expensive on Gemma 4. In gemma-4-12b-it (48 layers: 40 sliding with 8 KV heads x 256, 8 full with 1 KV head x 512), a sliding token costs 320 KiB and a full token 16 KiB, with K and V both stored. A whole snapshot at 5.8k is about 431 MB computed, against snapshot_bytes_per_entry 435 MB measured. A ring-only checkpoint is about 335 MB (320 MiB), so it saves only the full-attention part. That is about 22% at 5.8k (91 of 411 MiB) and about 62% at 32k (512 of 832 MiB).
Proposed Solution
Recommended as the interim fix on the current snapshot path, shippable before #3254 and #3256. Once #3256 lands, Gemma's sliding blocks reuse without checkpoints, and this mechanism stays for recurrent families and for families #3256 does not cover.
- Gemma 3, Gemma 4 and AFMoE state checkpoints. Implement
state_checkpoint_from_snapshotto keep only therotatingtensors and scalars of each sliding layer (thekv_snapshot::snapshot_rotatingnames). Implementrestore_sequence_state_from_checkpointto restore the sliding layers from the checkpoint and the full-attention layers from the donor, cut back withstandard_truncatable_toand the existing restore. Tag the checkpoint family as for Qwen 3.5. The donor rule instate_checkpoints.rsis unchanged: same session bucket, donor tokens extend the checkpoint, and lineage agrees. --ctx-checkpoints N(N >= 0). Unset leaves today's defaults.N > 0turns on keep-previous-turn, and also turns on state checkpoints where the family supports them. It caps the state checkpoints retained per conversation at N. It does not bound the periodic whole snapshots from--checkpoint-min-step, which stay under the snapshot byte budget andcheckpoint_points' ownmax. The oldest beyond N is dropped on insert, inPromptCacheStore::insert_state_checkpoint(state_checkpoints.rs:298), next toenforce_state_checkpoint_caps(:214).N == 0setsstate_checkpoint_capacity_bytes = 0and leaves keep-previous-turn off. This matches llama-server, but it is a change for Qwen 3.5 deployments that pass0today, so log it once atinfo. Negative values are refused.--checkpoint-min-step M(M >= 0).M > 0sets the periodic checkpoint interval to M, using main's absolute-multiple points, with the 256 floor applied as for the env switch:0 < M < 256is refused at startup with a message naming the floor, not clamped. Two checkpoints of one conversation closer than M are not both kept: the newer replaces the older.M == 0means no periodic checkpoints and no spacing rule. Rejected alternative: llama's spacing relative to the last checkpoint. It makes split points depend on history, which breaks the prefix-determinism thatcheckpoint_pointsrelies on to give the same partition as a cold run.- Precedence. A flag, or its
LLAMA_ARG_*variable through clap, overridesMLXCEL_PROMPT_CACHE_CHECKPOINT_INTERVALandMLXCEL_PROMPT_CACHE_KEEP_PREVIOUS_TURN.MLXCEL_PROMPT_CACHE_STATE_CHECKPOINT_CAPACITY_BYTESstill sets the byte budget.MLXCEL_DISABLE_BOUNDARY_SNAPSHOT=1still disables all of it, and the flag then logs awarnthat it has no effect. - Default for rotating families. Proposed: with the flags unset, Gemma 3, Gemma 4 and AFMoE get keep-previous-turn and state checkpoints with N = 2. That is at most about 640 MiB of rings per conversation on gemma-4-12b. Keep-previous-turn also retains one extra whole snapshot per conversation (about 431 MB at 5.8k, about 832 MiB at 32k), counted in the snapshot budget, which
model_aware_snapshot_target_entries(true)already raises by half (snapshot_sizing.rs:42-58). Both stay inside their model-aware budgets. Extendrecommend_state_checkpoint_capacity_from_configto size ring-only checkpoints fromkv_arch(window x sliding bytes per token). Other families keep today's defaults. Ship this default only if step 1 of the acceptance criteria shows the hit. - Rewrite the
slot_args.rsmodule doc and help text. Updatedocs/llama-server-compat.md.
Scope
In scope: src/cli/slot_args.rs and its tests, the startup wiring in src/server/startup.rs (around :3485), state_checkpoints.rs (per-conversation cap and spacing), snapshot_sizing.rs, the checkpoint hooks in gemma3.rs, gemma4.rs (text, VL and Unified wrappers) and afmoe.rs, and docs.
Out of scope: paged sliding blocks (#3256), --swa-full (#3257), gpt-oss and the other rotating families without snapshot hooks (#3254 follow-ups), quantized rings (#3250), and the eviction-by-gap policy, which was measured and not adopted (#2885 comment of 2026-11-12). This issue builds on #2885 Part B items 1-2 (#2950, #2973) and does not repeat them.
Implementation Notes
- Reuse:
state_checkpoints.rsdonor and drop logic,kv_snapshot.rsencoders,checkpoint_points, and the existing/v1/cache/statsfields (no new endpoint). - Edge cases: a ring that has not wrapped needs no checkpoint (the whole snapshot truncates), so skip it. A multimodal request takes no checkpoint, as today. A KV-shared layer stores nothing. A checkpoint with no donor is dropped (fix(prompt-cache): count turn-boundary checkpoints in store accounting and bound the orphan sweep #3058 rule).
- Errors: a refused restore discards the checkpoint, records
LayoutConstraints, and retries without checkpoints (prompt_cache.rs:153-168).
Acceptance Criteria
- First, measure on main with the env switches on (
MLXCEL_PROMPT_CACHE_KEEP_PREVIOUS_TURN=1, plus the checkpoint interval) usingscripts/bench_prompt_cache_replay.py --edit-target <n> --verifyon gemma-4-12b-it-4bit. Record whether the edited-previous-user-message turn already hits before changing code, indocs/benchmark_results/. -
--ctx-checkpoints 3is accepted. On Gemma 4, the edited-previous-user-message replay reportscached_tokens>= the previous history boundary, and/v1/cache/statsshowsstate_checkpoint_inserts > 0andstate_checkpoint_hits > 0. (PR feat: support --ctx-checkpoints and --checkpoint-min-step #3283: at edit target 7 the turn reaches the previous boundary, 14,481 tokens, through the keep-previous-turn whole snapshot, so that arm reports 0 checkpoint hits; the ring checkpoint is the hit at target 6, 12,853 tokens withstate_checkpoint_hits = 1, and at target 3 with--ctx-checkpoints 8.) - Output of every served turn equals its cold
--verifyrerun, under the fix(prompt-cache): restoring a completion snapshot diverges from a cold prefill #3040 comparison rules. - Unit test on a tiny Gemma 4 fixture with a wrapped ring: a checkpoint at boundary b plus a longer donor restores, and the decoded logits equal a cold run with the same partition. Negative control: restoring with the donor's ring instead of the checkpoint's makes the test fail.
-
slot_argstests:N > 0andM > 0accepted, negative values refused, the precedence over the env switches pinned, and the stalecapture_history_boundary_snapshottext gone. - A per-conversation cap test in
state_checkpoints_tests.rs: N + 1 boundaries keep the newest N.
Implement this in one PR, run the verification block once at the end, and keep pushes to a minimum.
Verification
make test-fast FILTER=slot_args
make test-fast FILTER=state_checkpoint
make test-fast FILTER=gemma4
make test-fast FILTER=prompt_cache
python3 scripts/bench_prompt_cache_replay.py --edit-target 3 --verify --out /tmp/replay.json
make verify-test # macOS; on Linux/NVIDIA: make verify-test-cuda
Activity
test: pin ring checkpoint sizing for Gemma 3 and AFMoE configs
fix: apply checkpoint cap and spacing over the whole conversation lin…
docs: note that the checkpoint budget variable enables Gemma and AFMo…
fix: document the cap across forked branches and pin it with a test
fix(models): return a load error on mismatched MoE expert shapes (#3304)
refactor: move per-layer sequence state from models to the scheduler (#…
feat: support --ctx-checkpoints and --checkpoint-min-step (lablup#3283)