Repository navigation
Description
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=0on 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
- refactor: Generalize prompt-cache state ownership from models to the scheduler #3254 (depends on feat: Support --ctx-checkpoints and --checkpoint-min-step for sliding-window families #3255; merged in refactor: move per-layer sequence state from models to the scheduler #3305)
Phase 3
- feat: Paged sliding-window KV with prefix reuse at any block boundary #3256 (depends on refactor: Generalize prompt-cache state ownership from models to the scheduler #3254)
Phase 4
- feat: Implement --swa-full as an opt-in that keeps every sliding block #3257 (depends on feat: Paged sliding-window KV with prefix reuse at any block boundary #3256)
Acceptance Criteria
- All sub-issues completed and merged
- On gemma-4-12b-it-4bit, the edited-previous-user-message replay (
scripts/bench_prompt_cache_replay.py --verify) reuses up to the edit with outputs equal to cold reruns, recorded indocs/benchmark_results/ - Follow-up issues filed for every family group still model-owned after refactor: Generalize prompt-cache state ownership from models to the scheduler #3254, linked here
- feat: support quantized KV cache modes in model-state snapshots for prompt-cache reuse #3250 re-evaluated against the scheduler-owned layer, with the decision recorded on that issue
Technical Considerations
- refactor: Generalize prompt-cache state ownership from models to the scheduler #3254 is the foundation. It is sequenced after feat: Support --ctx-checkpoints and --checkpoint-min-step for sliding-window families #3255 because both touch Gemma 3,
state_checkpoints.rsand the scheduler prompt-cache code, and refactor: Generalize prompt-cache state ownership from models to the scheduler #3254 moves feat: Support --ctx-checkpoints and --checkpoint-min-step for sliding-window families #3255's ring-only checkpoint into the generic path. It adds a scheduler-owned per-layer-kind state abstraction (paged blocks for full attention, window-bounded blocks for sliding, chunked, checkpointed recurrent state), with ADR 0010 and Gemma 3 and Qwen 3.5 as reference migrations. - feat: Support --ctx-checkpoints and --checkpoint-min-step for sliding-window families #3255 is the interim fix and is recommended to ship first. It works on the current snapshot path and is built from what main already has (feat(prompt-cache): add opt-in system-prompt root snapshot #2950, feat(prompt-cache): add opt-in periodic prefill checkpoints #2973, feat(prompt-cache): keep the previous turn's restore point #3037, feat(prompt-cache): checkpoint only recurrent state at turn boundaries for hybrid models #3047). After feat: Paged sliding-window KV with prefix reuse at any block boundary #3256 it stays useful for recurrent families and any family feat: Paged sliding-window KV with prefix reuse at any block boundary #3256 does not cover.
- feat: Paged sliding-window KV with prefix reuse at any block boundary #3256 is the main piece.
- feat: Implement --swa-full as an opt-in that keeps every sliding block #3257 is the opt-in after it.
- feat: support quantized KV cache modes in model-state snapshots for prompt-cache reuse #3250 (quantized KV in snapshots) applies today to the snapshot path. Int8 and Turbo sequences are never pool-backed, so the paged sliding design covers FP16 only until feat: support quantized KV cache modes in model-state snapshots for prompt-cache reuse #3250 is revisited against refactor: Generalize prompt-cache state ownership from models to the scheduler #3254.
- fix: Prometheus prompt-cache counters double-count every lookup #3246 (Prometheus prompt-cache double count) affects
/metricstotals. All acceptance here readscached_tokensand/v1/cache/statsinstead. - App side. The client-side fix, keeping chat history append-only, is tracked separately in lablup/backend.ai-go. It is still needed for remote providers.
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.
Activity
1 remaining item
perf(cache): reduce paged attention execution overhead (#3406)
refactor: move Gemma 4 and AFMoE onto scheduler-owned layer state (#3643
refactor: move remaining sliding-window families to scheduler-owned s…
refactor(llama4): move Llama 4 onto scheduler-owned layer state with …
refactor: move SSM and convolution hybrids onto scheduler-owned layer…
refactor: move SSM and convolution hybrids onto scheduler-owned layer…
feat(cache): paged sliding-window KV with prefix reuse at any block b…
feat(cache): implement --swa-full opt-in to keep every sliding block (#…
fix(examples): update logit_trace Gemma 4 adapter call after #3324 (#…
inureyes commentedon Dec 1, 2026
Epic implementation complete
wt-epic-3259-issue-*, all removed)Verification highlights
--swa-fullongemma-4-e2b-it-4bitandqwen3-0.6b-4bit.gemma-4-12b-it-4bitreplay (#3256):scripts/bench_prompt_cache_replay.py --edit-target 3at the default 2 GiB budget achieved 15/16 hits (136,000 / 175,605cached tokens, 77.4%), reducing total TTFT from543.79 scold to127.19 s(4.28x speedup), withreject_sliding_window_evicted = 0and cold-identical SHAs on edited Turn 8 and cross-session system-root hits (Sessions B and C). Recorded indocs/benchmark_results/paged-sliding-decode-apple-silicon-2026-12-02.md.gemma-3-1b-it-4bitinterleaved decode (#3256, Metal release): B=1256+28.68%, B=18192+6.64%, B=4256+16.08%, B=48192-11.01% (2-slab gather fallback above 1024-block default slab).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), andcargo check --examplesall passed.gemma-4-12b-it-4bitB=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.