Add prompt checkpoints for hybrid caches - #1667
Open
stale2000 wants to merge 1 commit into
Open
Conversation
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.
Why this matters
Modern hybrid models combine attention KV cache with recurrent state (for example, Mamba or Gated DeltaNet layers). A recurrent state at token T summarizes every preceding token, so it cannot be safely trimmed back to an arbitrary branch point the way attention KV can.
Without an earlier exact state, a request that diverges from a cached prompt must prefill from the beginning. This is most visible in multi-turn chat, tool-use, and agent workloads that share a long system prompt or conversation prefix.
Solution
For a cache containing any non-trimmable layer, split each prompt segment at the existing
prefill_step_sizeboundary. The server already saves a cache at each segment boundary and already knows how to select the nearest valid shorter prefix. This change gives it exact recurrent-state checkpoints to select:No recurrent state is trimmed, reset, or combined with KV state from a different history. Fully trimmable KV-only models keep their existing cache behavior.
Benefit
With the default 2,048-token prefill chunk, a branch can reuse the checkpoint immediately before its divergence and reprocess at most one chunk before the changed suffix. For example, a change after 30K tokens in a 32K-token prompt can reuse the 28,672-token checkpoint and prefill roughly 3.3K tokens instead of 32K: about 90% fewer prompt tokens processed for that request.
The exact saving depends on where the branch falls and cache capacity. The important guarantee is correctness: every restored recurrent state exactly matches the token prefix it represents.
Scope
prefill_step_sizeas the checkpoint interval; no new option or cache format.Tests
python -m py_compile mlx_lm/server.py tests/test_server.pygit diff --checkThe runtime MLX suite requires Apple Silicon.