Skip to content

feat(prompt-cache): add opt-in system-prompt root snapshot - #2950

Merged
inureyes merged 3 commits into
mainfrom
feat/issue-2885-system-root-snapshot
Nov 11, 2026
Merged

inureyes merged 3 commits into
mainfrom
feat/issue-2885-system-root-snapshot

Conversation

@inureyes

Copy link
Copy Markdown
Member

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

  • PrefillPlan takes a PrefillSplits set instead of one Option<usize> boundary. Each split ends one unpadded piece, so a hit at either split forwards exactly the cold plan's remaining pieces. Existing Option callers compile unchanged.
  • The root is rendered as the leading system messages plus a placeholder user turn, then cut where the placeholder starts. The Qwen3.8 template raises on an empty message list, so rendering the system messages alone does not work. The scheduler clips the root to the common token prefix minus one seam token and keeps it strictly before the history boundary.
  • The classic plan, paged reservation and both Gemma 4 MTP prefill mirrors use the same splits (fix: Gemma 4 MTP never engages on CUDA (GB10), verify probe fails #2160).
  • 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 --verify on Qwen3.8-Flash-Next-OptiQ-2bit, M3 Ultra. Full write-up: docs/benchmark_results/qwen4-exp-system-root-replay-m3ultra-2026-11-11.md.

Root off Root on
Total TTFT, 16 requests 51.16 s 38.37 s
New chat, first turn (B1, C1) 5.70 s, 5.60 s 1.53 s, 1.44 s
History-editing turn (A8) 14.55 s 10.34 s
Cached tokens 122,978 137,210

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

Refs #2885

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
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review labels Nov 10, 2026
@inureyes
inureyes merged commit a6f36b8 into main Nov 11, 2026
31 checks passed
@inureyes
inureyes deleted the feat/issue-2885-system-root-snapshot branch November 11, 2026 05:32
@inureyes inureyes added status:done Completed and removed status:review Under review labels 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
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant