Skip to content

feat(prompt-cache): add opt-in periodic prefill checkpoints - #2973

Merged
inureyes merged 2 commits into
mainfrom
feat/issue-2885-periodic-checkpoints
Nov 11, 2026
Merged

inureyes merged 2 commits into
mainfrom
feat/issue-2885-periodic-checkpoints

Conversation

@inureyes

Copy link
Copy Markdown
Member

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

An agent that rewrites or compacts its history deep into a long context loses the history-boundary snapshot, and the system-prompt root is far behind the edit, so the whole span after the root is prefilled again. With MLXCEL_PROMPT_CACHE_CHECKPOINT_INTERVAL on, a snapshot-family prefill also splits at every multiple of the interval (16,384 by default) before its history boundary and stores a Checkpoint snapshot there. The points are absolute multiples, so a hit on a checkpoint forwards the same pieces as the cold run of the same prompt.

PrefillSplits now holds up to 16 points and the scheduler reserves room for the root and the history boundary. Checkpoints are exempt from the session-chain supersede rule and evicted by plain LRU. The chat_request test helper boxes its future so the root render tests pass on the default test stack.

Replay on Qwen3.8-Flash-Next-OptiQ-2bit (M3 Ultra, 8 GiB snapshot budget): the turn that edits history 20K tokens into a 28K prompt adopts 16,384 tokens instead of 4,744, TTFT 22.16 s to 11.25 s; total TTFT 70.18 s to 58.66 s. Targeted unit 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 11, 2026
@inureyes
inureyes merged commit d68a321 into main Nov 11, 2026
31 checks passed
@inureyes
inureyes deleted the feat/issue-2885-periodic-checkpoints branch November 11, 2026 15:35
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 11, 2026
@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