Repository navigation
fix(sdk): read the prompt again when a rollback outruns the SWA cache - #1678
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
3 tasks done
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>
Erin Genovy (jefgen)
force-pushed
the
fix/swa-rollback-window
branch
from
November 24, 2026 09:03
3bc3abf to
9befe9d
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 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::generateread 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 newestn_swa + n_ubatchpositions in its SWA cache, 1,536 on the NPU. When a new prompt departs from the cache behind those,llama_memory_seq_rmstill succeeds, but leaves the SWA cache short of the window, or empty.llama_batch_get_onecarries 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,generatenow compares each context'sllama_memory_seq_pos_minwith 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 atmatch_len - 1, so llama.cpp's positions are right again. I keptllama_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::generateresets, with aprefix match: sliding-window cache doesn't reach back to token N's windowwarning, when a rollback leaves the target's or the draft's SWA cache short of the window.test_llm_prefix_cache_swa_fallbackchecks, 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_fallbackfails without the fix (the reply after the rollback was63, on an empty cache17) and passes with it.test_llm_prefix_cache,test_llm_prefix_cache_fallbackandtest_chat_templatepass on CPU too.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 answered50after the rollback, as on an empty cache, reading the whole prompt again, where v0.11.0 answeredNone. 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.