Skip to content

fix(sdk): read the prompt again when a rollback outruns the SWA cache - #1678

Open
Erin Genovy (jefgen) wants to merge 1 commit into
qualcomm:mainfrom
jefgen:fix/swa-rollback-window
Open

Erin Genovy (jefgen) wants to merge 1 commit into
qualcomm:mainfrom
jefgen:fix/swa-rollback-window

Conversation

@jefgen

Copy link
Copy Markdown
Contributor

Thanks again RemiliaForever (@RemiliaForever) for the safe reset in a099ea1, which this builds on! 😊

I worked with Copilot to generate a fix for #1675.


This PR makes LlamaLlm::generate read the prompt again when a rollback leaves a sliding-window model's SWA cache short of the window. It doesn't add context checkpoints, which would make that cheaper; that's #1677.

GenieX runs llama.cpp with swa_full = false, so a model such as Gemma 4 keeps only the newest n_swa + n_ubatch positions in its SWA cache, 1,536 on the NPU. When a new prompt departs from the cache behind those, llama_memory_seq_rm still succeeds, but leaves the SWA cache short of the window, or empty. llama_batch_get_one carries no positions, so llama.cpp numbers the next tokens from that cache's end: from 0 once it's empty. The server reported a normal prefix match while Gemma answered from a scrambled context.

After a successful seq_rm, generate now compares each context's llama_memory_seq_pos_min with the first re-decoded token's window, match_len - n_swa + 1, as llama.cpp's server does before it reuses a prefix (pos_min_thold), and resets when the cache falls short. With the window covered, the cache ends at match_len - 1, so llama.cpp's positions are right again. I kept llama_batch_get_one: after a context shift, process() decodes the same batch again, and llama.cpp recomputes its positions from memory, where explicit ones would be stale.

Summary

  • LlamaLlm::generate resets, with a prefix match: sliding-window cache doesn't reach back to token N's window warning, when a rollback leaves the target's or the draft's SWA cache short of the window.
  • test_llm_prefix_cache_swa_fallback checks, with Gemma 3 1B on CPU, that a reply after such a rollback matches one on an empty cache, and reads the whole prompt again.

Test plan

  • test_llm_prefix_cache_swa_fallback fails without the fix (the reply after the rollback was 63, on an empty cache 17) and passes with it. test_llm_prefix_cache, test_llm_prefix_cache_fallback and test_chat_template pass on CPU too.
  • Live on a Snapdragon X2 Elite NPU with qualcomm-ai-hub-community/Gemma-4-E4B-it-Hi-Fi-fraQtl:Q4_K_M: Gemma 4: a deep prefix-cache rollback empties the SWA cache and restarts positions at 0 #1675's script answered 50 after the rollback, as on an empty cache, reading the whole prompt again, where v0.11.0 answered None. A rollback near the end of the same 2,782-token prompt still reused 2,762 cached tokens.

Fixes #1675

Co-authored by Copilot + Human.

GenieX runs llama.cpp with swa_full off, so a sliding-window model's
SWA cache keeps only its newest positions. A rollback behind them still
succeeds, but leaves that cache short of the window, or empty, and
since llama_batch_get_one carries no positions, llama.cpp numbers the
next tokens from its end: from 0 once it's empty. Gemma 4 then answered
from a scrambled context while the server reported a normal prefix
match.

After seq_rm succeeds, reset unless each context's SWA cache still
reaches back to the first re-decoded token's window, as llama.cpp's
server checks before it reuses a prefix.

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.

Gemma 4: a deep prefix-cache rollback empties the SWA cache and restarts positions at 0

1 participant