Skip to content

epic: Prompt-prefix cache reuse for sliding-window families #3259

Done
10 of 10 issues completed
Done

Description

@inureyes

Problem / Background

Sliding-window families miss the prompt-prefix cache whenever a request diverges before the end of the stored prefix. Measured on GB10 with mlxcel-server 0.8.1, gemma-4-12b-it-4bit, fp16 KV and about 5.8k-token prompts:

  • An exact multi-turn continuation hits: 5808/5823 tokens cached, TTFT about 4.9 s down to 0.4-0.56 s.
  • An edit, a regenerate, or a new chat with the same system prompt reuses nothing (snapshot_diverged). The sliding ring (window 1024) has wrapped, and the history-boundary snapshot cannot be truncated.
  • 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 root cause is ownership. Sliding layers are model-owned rings, and every reuse feature is written per family. Among the sliding-window families, only Gemma 3, Gemma 4, AFMoE and Muse Glimmer have snapshot hooks. By code reading on origin/main, gpt-oss, Exaone 4, Ministral 3, Mellum, Laguna, Step 3.5, MiMo V2 Flash and DeepSeek V4 get no prefix reuse at all (see #3254).

Per-token KV in gemma-4-12b-it (40 sliding layers with 8 KV heads x 256, 8 full layers with 1 KV head x 512, K and V both stored) is 320 KiB sliding and 16 KiB full. The computed whole snapshot at 5.8k is about 431 MB, against snapshot_bytes_per_entry 435 MB measured.

Context Sliding ring Full-size sliding Full-attention KV
8k 320 MiB 2.5 GiB 128 MiB
32k 320 MiB 10 GiB 512 MiB
128k 320 MiB 40 GiB 2 GiB

Goal

Rotating families reuse prompt prefixes at any block boundary while the prompt-cache budget holds the sliding blocks, and at the nearest anchor (system root, previous turn) beyond that: across sessions that share a system prompt, and after edits, regenerates and branches. Live memory stays window-bounded, and save, restore, truncation, checkpoints and APC are implemented once in the scheduler instead of per model.

Sub-issues

Phase 1

Phase 2

Phase 3

Phase 4

Acceptance Criteria

Technical Considerations

Run wrap-up — 2026-11-25

#3255 merged in #3283 and #3254 merged in #3305. The isolated execution-overhead optimization #3385 merged in #3406 (50ac7182), with the owner accepting the small-model performance tradeoff documented there. This does not complete the sliding-window work.

#3256 remains open with draft #3334 stopped at its second B=1 performance gate (about 6% slower on the measured Gemma 3 case). Server paged-cache acceptance, retention/eviction checks, the real-model live-block bound, Gemma 4 migration and replay/parity acceptance remain incomplete. #3257 is still blocked by #3256. The epic remains open.

Family follow-ups were filed as #3324–#3329. #3386 tracks the OpenAI-route ignore_eos propagation bug and #3387 tracks other pool-slab kernels. #3250 must retain its separate quantized-state acceptance: this optimization neither adds Turbo4 prefix cloning/trimming nor proves quantized sliding-cache reuse.

Sub-issues

Sub-issues

10 of 10 Issues completed
Loading 10 sub-issues

Activity

  1. added
    status:ready
    priority:high
    area:architecture
    area:inference
    type:epic
    on Nov 22, 2026
  2. 1 remaining item

  3. added
    status:blocked
    and removed
    status:ready
    on Nov 24, 2026
  4. inureyes commented on Dec 1, 2026

    @inureyes
    MemberAuthor

    Epic implementation complete

    Verification highlights

    • Real-model smoke & prompt-cache checks (Apple Silicon Metal): verified 14 real checkpoints across Wave 0 PRs before merging, plus --swa-full on gemma-4-e2b-it-4bit and qwen3-0.6b-4bit.
    • gemma-4-12b-it-4bit replay (#3256): scripts/bench_prompt_cache_replay.py --edit-target 3 at the default 2 GiB budget achieved 15/16 hits (136,000 / 175,605 cached tokens, 77.4%), reducing total TTFT from 543.79 s cold to 127.19 s (4.28x speedup), with reject_sliding_window_evicted = 0 and cold-identical SHAs on edited Turn 8 and cross-session system-root hits (Sessions B and C). Recorded in docs/benchmark_results/paged-sliding-decode-apple-silicon-2026-12-02.md.
    • gemma-3-1b-it-4bit interleaved decode (#3256, Metal release): B=1 256 +28.68%, B=1 8192 +6.64%, B=4 256 +16.08%, B=4 8192 -11.01% (2-slab gather fallback above 1024-block default slab).
    • Post-wave contract gate (dd6a628f9): cargo fmt --all -- --check, check_cross_repo_refs.py, check_llama_compat_manifest.py, make verify-used-by, llama_compat_manifest, dead_doc_pointers, mlx_test_guard_coverage, cli_help_consistency, vlm_wrapper_capability_delegation (40/40), and cargo check --examples all passed.
    • Unmeasured / follow-up: GB10 CUDA re-measurement of feat: Paged sliding-window KV with prefix reuse at any block boundary #3256 after rebasing onto perf(cache): reduce paged attention execution overhead #3406 + Wave 0, gemma-4-12b-it-4bit B=1/B=4 interleaved decode cells, and feat: support quantized KV cache modes in model-state snapshots for prompt-cache reuse #3250 quantized sliding-window re-evaluation remain open.
  5. added
    status:done
    and removed
    status:blocked
    on Dec 1, 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:architecturearea:inferencepriority:highstatus:donetype:epic

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions