Skip to content

feat: Support --ctx-checkpoints and --checkpoint-min-step for sliding-window families #3255

Closed

Description

@inureyes

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:

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.

  1. Gemma 3, Gemma 4 and AFMoE state checkpoints. Implement state_checkpoint_from_snapshot to keep only the rotating tensors and scalars of each sliding layer (the kv_snapshot::snapshot_rotating names). Implement restore_sequence_state_from_checkpoint to restore the sliding layers from the checkpoint and the full-attention layers from the donor, cut back with standard_truncatable_to and the existing restore. Tag the checkpoint family as for Qwen 3.5. The donor rule in state_checkpoints.rs is unchanged: same session bucket, donor tokens extend the checkpoint, and lineage agrees.
  2. --ctx-checkpoints N (N >= 0). Unset leaves today's defaults. N > 0 turns 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 and checkpoint_points' own max. The oldest beyond N is dropped on insert, in PromptCacheStore::insert_state_checkpoint (state_checkpoints.rs:298), next to enforce_state_checkpoint_caps (:214). N == 0 sets state_checkpoint_capacity_bytes = 0 and leaves keep-previous-turn off. This matches llama-server, but it is a change for Qwen 3.5 deployments that pass 0 today, so log it once at info. Negative values are refused.
  3. --checkpoint-min-step M (M >= 0). M > 0 sets the periodic checkpoint interval to M, using main's absolute-multiple points, with the 256 floor applied as for the env switch: 0 < M < 256 is 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 == 0 means 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 that checkpoint_points relies on to give the same partition as a cold run.
  4. Precedence. A flag, or its LLAMA_ARG_* variable through clap, overrides MLXCEL_PROMPT_CACHE_CHECKPOINT_INTERVAL and MLXCEL_PROMPT_CACHE_KEEP_PREVIOUS_TURN. MLXCEL_PROMPT_CACHE_STATE_CHECKPOINT_CAPACITY_BYTES still sets the byte budget. MLXCEL_DISABLE_BOUNDARY_SNAPSHOT=1 still disables all of it, and the flag then logs a warn that it has no effect.
  5. 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. Extend recommend_state_checkpoint_capacity_from_config to size ring-only checkpoints from kv_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.
  6. Rewrite the slot_args.rs module doc and help text. Update docs/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.rs donor and drop logic, kv_snapshot.rs encoders, checkpoint_points, and the existing /v1/cache/stats fields (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) using scripts/bench_prompt_cache_replay.py --edit-target <n> --verify on gemma-4-12b-it-4bit. Record whether the edited-previous-user-message turn already hits before changing code, in docs/benchmark_results/.
  • --ctx-checkpoints 3 is accepted. On Gemma 4, the edited-previous-user-message replay reports cached_tokens >= the previous history boundary, and /v1/cache/stats shows state_checkpoint_inserts > 0 and state_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 with state_checkpoint_hits = 1, and at target 3 with --ctx-checkpoints 8.)
  • Output of every served turn equals its cold --verify rerun, 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_args tests: N > 0 and M > 0 accepted, negative values refused, the precedence over the env switches pinned, and the stale capture_history_boundary_snapshot text 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

  1. added
    status:ready
    type:enhancement
    priority:high
    area:models
    area:inference
    area:cli
    on Nov 22, 2026
  2. added
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Nov 22, 2026
  3. added
    status:done
    and removed
    status:review
    on Nov 22, 2026
  4. added a commit that references this issue on Nov 25, 2026
    bd931f7
  5. added this to the 0.9 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:cliarea:inferencearea:modelspriority:highstatus:donetype:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions