Skip to content

kv-cache : iSWA seq_rm returns false when the SWA cache cannot satisfy the cut - #31483

Open
kukirin-ui wants to merge 1 commit into
ggml-org:masterfrom
kukirin-ui:fix-31457-iswa-seq-rm
Open

kukirin-ui wants to merge 1 commit into
ggml-org:masterfrom
kukirin-ui:fix-31457-iswa-seq-rm

Conversation

@kukirin-ui

Copy link
Copy Markdown
Contributor

Overview

Fixes #31457

With swa_full=false, llama_kv_cache_iswa::seq_rm returned true for a tail cut even when the SWA cache had already overwritten the rows needed after the cut. The caller then thinks the cache is still valid and keeps going with broken SWA state.

Now, before touching either cache, seq_rm checks if the SWA cache still has positions [p0 - n_swa + 1, p0). If not, it returns false and leaves the sequence untouched, so the caller can reprocess. The check is skipped when swa_full=true, seq_id < 0 or p0 <= 0. This is the same threshold the server already uses (pos_min_thold), so the server path does not change.

Additional information

Added tests/test-memory-iswa-seq-rm.cpp. It uses the random gemma3 model from test-llama-archs (generate-models fixture). It fails on master (seq_rm returns true) and passes with the fix. test-llama-context still passes for all 139 models.

Related: #29480 touches the same file (seq_pos_max) but does not overlap, #28007 is a server side fallback and works together with this.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. I wrote the fix and the test myself. I used an AI assistant only to help me find and fix errors along the way. I can explain every change.

…y the cut

With swa_full = false the SWA cache reuses the cells of masked positions.
A tail removal at p0 needs the positions [p0 - n_swa + 1, p0) to still be
present, otherwise the next decode silently runs without them. Return false
in that case (before touching either cache), as documented in llama.h.

Fixes ggml-org#31457
@kukirin-ui
kukirin-ui requested review from a team and ggerganov as code owners October 31, 2026 20:46
@github-actions github-actions Bot added the testing Everything test related label Oct 31, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

Hi @kukirin-ui, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • Multiple open PRs from a new contributor: We limit new contributors (those without a previously merged PR) to 1 open PR at a time. You currently have 2 open PRs.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@JohnnyTeutonic

Copy link
Copy Markdown

Tested it against my repro at every cut point and it refuses exactly the broken ones. Thanks

@kukirin-ui

Copy link
Copy Markdown
Contributor Author

:)

@kukirin-ui
kukirin-ui marked this pull request as draft November 14, 2026 15:02
@kukirin-ui
kukirin-ui marked this pull request as ready for review November 15, 2026 06:48

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

testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iSWA: llama_memory_seq_rm returns true after a cut into overwritten SWA rows (swa_full=false), next decode silently wrong

2 participants