Skip to content

perf(sdk): add context checkpoints for hybrid and recurrent models - #1695

Open
Erin Genovy (jefgen) wants to merge 1 commit into
qualcomm:mainfrom
jefgen:feat/context-checkpoints
Open

Erin Genovy (jefgen) wants to merge 1 commit into
qualcomm:mainfrom
jefgen:feat/context-checkpoints

Conversation

@jefgen

Copy link
Copy Markdown
Contributor

Thanks MAN$I VERMA 🦀 (@mansiverma897993) for passing reasoning_content through 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 in vlm.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_rm fails, and 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 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_checkpoint in tools/server/server-context.cpp), prefill now stops 4 tokens short of each prompt's end and saves the state seq_rm can't cut back, with LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY: 50.25 MiB for Qwen3.5 4B, at any prompt length. When seq_rm fails, generate restores 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::generate saves context checkpoints on hybrid and recurrent models and restores one when seq_rm fails, logging prefix match: rollback to token N failed; restored the checkpoint at token M. Reset, load_kv_cache and a context shift drop them.
  • test_llm_prefix_cache_fallback now 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_message checks, 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 sets n_ubatch=1, since other ubatch sizes flip a greedy near-tie within its 32 tokens, and takes about 30 s.

Test plan

  • Both tests fail without the change, and each of 3 breaks fails test_llm_hybrid_checkpoint_after_user_message: FIFO eviction, restoring the oldest checkpoint's state, and skipping the attention cache's cut. The api or (llama_cpp and device_cpu) cells pass, 38 tests, and so do the NPU cells of test_llm_prefix_cache and test_mtp_qwen35_cache_lifecycle.
  • Live on a Snapdragon X2 Elite NPU with qualcomm-ai-hub-community/Qwen3.5-4B-NPU-fraQtl:Q4_0 and a 53,891-token prompt: after a user message it read 104 tokens in 1.3 s, where main read 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.
  • In a GitHub Copilot app session on FrogNano 4B, a Qwen3.5-derived model, every user message restored a checkpoint, with the first token in 0.8 to 4.3 s on prompts of 54K to 56K tokens, where a build without checkpoints took 62 to 67 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.

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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qwen3.5: every user message re-reads the whole prompt, which context checkpoints would avoid

1 participant