Repository navigation
feat(prompt-cache): add opt-in system-prompt root snapshot - #2950
Merged
Merged
Conversation
A snapshot-only family reuses a stored state only when a new prompt starts with exactly its tokens, so the history boundary (#1143) never helps a fresh chat that shares only the system prompt and tools, nor a turn that edits history behind the boundary. With MLXCEL_PROMPT_CACHE_SYSTEM_ROOT=1, a chat prompt of at least 2,048 tokens also stores a snapshot at the end of its system messages and tool list. - PrefillPlan takes a PrefillSplits set instead of one optional boundary; each split ends one unpadded piece, so a hit at either split reproduces the cold plan's pieces. Option<usize> callers are unchanged. - The root is rendered as the leading system messages plus a placeholder user turn, cut at the placeholder (the Qwen3.8 template refuses an empty message list). The scheduler clips it to the common token prefix minus the seam token and keeps it strictly before the history boundary. - The classic plan, the paged reservation estimate and both Gemma 4 MTP prefill mirrors take the same splits (#2160). - New SnapshotOrigin::SystemRoot; LRU skips roots while they hold at most half of the snapshot budget. Roots stay in the request's session bucket. - scripts/bench_prompt_cache_replay.py replays an agent-shaped workload with optional cold re-verification. On Qwen3.8-Flash-Next-OptiQ-2bit (M3 Ultra) the 16-request replay drops total TTFT from 51.2 s to 38.4 s, new chats from about 5.7 s to 1.5 s, and the history-editing turn from 14.6 s to 10.3 s; all 16 completions match their cold runs in both arms and match each other. Targeted tests, clippy and fmt pass. Refs #2885
This was referenced Nov 11, 2026
inureyes
added a commit
that referenced
this pull request
Nov 11, 2026
Second PR of #2885 Part B. An agent that rewrites or compacts its history deep into a long context loses the history-boundary snapshot, and the system-prompt root (#2950) is far behind the edit, so everything after the root is prefilled again. This adds opt-in periodic checkpoints. ## Behavior - `MLXCEL_PROMPT_CACHE_CHECKPOINT_INTERVAL=1` (or an interval of at least 256 tokens) splits a snapshot-family prefill at every multiple of 16,384 before its history boundary and stores a `Checkpoint` snapshot at each point. Off by default, and ignored when `MLXCEL_DISABLE_BOUNDARY_SNAPSHOT=1`. - Points are absolute multiples, so every prompt that shares the first `k * interval` tokens splits there. A hit on a checkpoint forwards the same pieces as the cold run of the same prompt. - `PrefillSplits` now holds up to 16 points instead of 4. The scheduler reserves room for the root and the history boundary and keeps the deepest checkpoints, so a very long prompt never drops its history boundary. - Checkpoints are exempt from the session-chain supersede rule, so a conversation keeps its 16K, 32K, and later checkpoints side by side. They are evicted by plain LRU. - The history segment, which used to be one unchunked forward, is now one forward per interval when the switch is on. ## Measurement Qwen3.8-Flash-Next-OptiQ-2bit on M3 Ultra, root on in both arms, 8 GiB snapshot budget. Session A turn 9 rewrites turn 6's user message, about 20K tokens into a 28K-token prompt: | | Root only | Root and checkpoints | |---|---|---| | A9 cached / TTFT | 4,744 / 22.16 s | 16,384 / 11.25 s | | Total TTFT, 16 requests | 70.18 s | 58.66 s | | Cached tokens of 268,730 | 197,007 | 208,647 | - A9 and every cold run are byte-identical across arms. - A5 to A8 differ from cold in the checkpoint arm. The checkpoints did not cause this: a probe on fresh servers gives the same served A5 with checkpoints off, and the gap also appears without them. These turns chain through earlier turns' boundary snapshots, so their partition differs from the cold run's. The details are in `docs/benchmark_results/qwen4-exp-prefill-checkpoints-replay-m3ultra-2026-11-12.md`. - At 0.5 to 1.2 GB per snapshot for this model, the 2 GB default budget would evict most checkpoints before an edit reaches back to them. ## Validation Unit tests cover the checkpoint positions, the policy parser, the store's supersede exemption, and a scheduler run where an edited history adopts the last checkpoint and reproduces its cold run (chunked and unchunked). Targeted lib tests, clippy `-D warnings` and fmt pass. The `chat_request` test helper now boxes its future, because one #2950 test overflowed the default 2 MiB test stack outside the Makefile's `RUST_MIN_STACK`. Not run: CUDA/ROCm, a small-memory Mac (#2883), the full workspace suite. Refs #2885
This was referenced Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First PR of #2885 Part B: the system-prompt root checkpoint, behind
MLXCEL_PROMPT_CACHE_SYSTEM_ROOT(off by default). Periodic checkpoints and gap eviction stay for separate PRs.A snapshot-only family reuses a stored state only when a new prompt starts with exactly its tokens. The history boundary (#1143) covers the next turn of the same chat. It does not cover a new chat that shares only the system prompt and tools, or a turn that rewrites history behind the boundary. With the switch on, a chat prompt of at least 2,048 tokens also stores a snapshot at the end of its system messages and tool list.
How
PrefillPlantakes aPrefillSplitsset instead of oneOption<usize>boundary. Each split ends one unpadded piece, so a hit at either split forwards exactly the cold plan's remaining pieces. ExistingOptioncallers compile unchanged.SnapshotOrigin::SystemRoot: LRU skips roots while they hold at most half of the snapshot budget. Roots stay in the request's session bucket. Cross-session sharing was left out on purpose.Measured
scripts/bench_prompt_cache_replay.py --verifyon Qwen3.8-Flash-Next-OptiQ-2bit, M3 Ultra. Full write-up:docs/benchmark_results/qwen4-exp-system-root-replay-m3ultra-2026-11-11.md.All 16 completions are byte-identical to their cache-reset runs in both arms, and identical across arms. The root snapshot is 254 MB at 4,744 tokens.
Validation
--lib --tests --bins -D warnings), fmt.Refs #2885