Repository navigation
perf(sdk): add context checkpoints for hybrid and recurrent models - #1695
Open
Erin Genovy (jefgen) wants to merge 1 commit into
Open
Erin Genovy (jefgen) wants to merge 1 commit into
Erin Genovy (jefgen) wants to merge 1 commit into
Conversation
A hybrid or recurrent model's state can't be cut back to an earlier token, so when a prompt departs from the cache before its end, generate() resets and reads the whole prompt again. Qwen3.5's template drops the thinking of turns before the last user message, so in an agent session every user message departs where the previous turn's first reply began. On such models, prefill now stops 4 tokens short of each prompt's end to save the state seq_rm can't cut back, with LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY, as llama.cpp's server does. When seq_rm fails, generate() restores the newest checkpoint at or before the divergence, cuts the attention cache there, and reads only the rest. It keeps up to 4, and when full drops the one closest to the checkpoint before it, the cheapest one to fall back past. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Signed-off-by: Erin Genovy <29107334+jefgen@users.noreply.github.com>
Erin Genovy (jefgen)
force-pushed
the
feat/context-checkpoints
branch
from
November 24, 2026 09:04
88b9ba3 to
9895dbd
Compare
This branch has not been deployed
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.
Thanks MAN$I VERMA 🦀 (@mansiverma897993) for passing
reasoning_contentthrough to the chat template in #1673! 😊 With it, Qwen3.5's tool steps extend the cache. This PR covers the other half: user messages.I worked with Copilot to implement context checkpoints for #1677.
This PR adds context checkpoints to
LlamaLlm, for hybrid and recurrent models such as Qwen3.5 and LFM. It doesn't cover sliding-window models (#1675, which #1678 resets for now) or the VLM path invlm.cpp.A hybrid model's recurrent state can't be cut back to an earlier token, so when a new prompt departs from the cache before its end,
llama_memory_seq_rmfails, andgenerateresets and reads the whole prompt again. Qwen3.5's template drops the thinking of turns before the last user message, so in an agent session every user message departs from the cache 2 tokens before the end of the previous turn's first prompt, at its<think>\n.As in llama.cpp's server (
create_checkpointintools/server/server-context.cpp), prefill now stops 4 tokens short of each prompt's end and saves the stateseq_rmcan't cut back, withLLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY: 50.25 MiB for Qwen3.5 4B, at any prompt length. Whenseq_rmfails,generaterestores the newest checkpoint at or before the divergence, cuts the attention cache there, and reads only the rest, resetting only when none fits. With a drafter, the draft context's state and the speculator's are saved and restored alongside.It keeps up to 4, and when full drops the checkpoint closest to the one before it, the cheapest to fall back past. Tool steps sit closer together than turns, so a turn's first checkpoint outlasts its tool steps.
Summary
LlamaLlm::generatesaves context checkpoints on hybrid and recurrent models and restores one whenseq_rmfails, loggingprefix match: rollback to token N failed; restored the checkpoint at token M. Reset,load_kv_cacheand a context shift drop them.test_llm_prefix_cache_fallbacknow expects LFM3's repeated prompt to restore a checkpoint and read only its last tokens; its other prompts still read everything.test_llm_hybrid_checkpoint_after_user_messagechecks, with Qwen3.5 0.8B on CPU, that a user message after 5 tool steps reads only the turn, with the same replies as an empty cache. It setsn_ubatch=1, since other ubatch sizes flip a greedy near-tie within its 32 tokens, and takes about 30 s.Test plan
test_llm_hybrid_checkpoint_after_user_message: FIFO eviction, restoring the oldest checkpoint's state, and skipping the attention cache's cut. Theapi or (llama_cpp and device_cpu)cells pass, 38 tests, and so do the NPU cells oftest_llm_prefix_cacheandtest_mtp_qwen35_cache_lifecycle.qualcomm-ai-hub-community/Qwen3.5-4B-NPU-fraQtl:Q4_0and a 53,891-token prompt: after a user message it read 104 tokens in 1.3 s, wheremainread them all in 66.0 s, and a retry read 4 tokens in 0.9 s, against 67.3 s. The replies matched an empty cache's.Note: splitting off a prompt's last 4 tokens costs about one decode step per request. On the NPU, a 36-token tool step's first token came in 0.18 s instead of 0.09 s.
Closes #1677
Co-authored by Copilot + Human.