Skip to content

ISWA: Switch to base cache for position tracking - #19225

Closed
cmp-nct wants to merge 1 commit into
ggml-org:masterfrom
cmp-nct-org:iswa_patch
Closed

cmp-nct wants to merge 1 commit into
ggml-org:masterfrom
cmp-nct-org:iswa_patch

Conversation

@cmp-nct

@cmp-nct cmp-nct commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

seq_pos_min/max should report logical range: updated position tracking to use base cache instead of SWA cache

The API should return seq position range but returns internal SWA-cache state instead of logical sequence range

During kv cache manipulations (seq_rm etc) I've ran into the bug that alternating swa model (gpt-oss-20b) wrongly freed up tokens from kv_swa that were in use (before the 128 window

This fix solved my issues (no more token losses) but I'm not familiar with that area of the codebase.

Updated position tracking to use base cache instead of SWA cache to avoid kv cache token losses.
Copilot AI review requested due to automatic review settings January 31, 2026 04:57
@cmp-nct
cmp-nct requested a review from ggerganov as a code owner January 31, 2026 04:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@cmp-nct

cmp-nct commented Feb 1, 2026

Copy link
Copy Markdown
Contributor Author

Another related PR: cmp-nct-org#9
That one is against find_slot corrupting the kv cache of other sequences when restoring a kv state.
It solved the corruption in my case. Though someone familiar with iswa should take a look if I am hunting ghosts.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return kv_swa->seq_pos_min(seq_id);
// base cache preserves all positions (no SWA cell reuse), making it the ground truth for position tracking
// SWA cache can lose older positions when cells are reused for new tokens outside the SWA window
return kv_base->seq_pos_min(seq_id);

Copilot AI Feb 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seq_pos_min() now unconditionally queries kv_base. However, this class intentionally skips reading/writing kv_base when LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY is set (see state_read/state_write later in this file). After restoring a partial sequence state, kv_base may be empty while kv_swa contains valid positions, causing seq_pos_min() to return -1 even though the sequence exists. Consider deriving the result from whichever cache currently contains the sequence (e.g., prefer kv_base when it has data, otherwise fall back to kv_swa).

Suggested change
return kv_base->seq_pos_min(seq_id);
// However, in partial-only state restores, kv_base may be empty while kv_swa still contains valid positions.
// In that case, fall back to kv_swa so that existing sequences are still reported correctly.
llama_pos pos_min_base = kv_base->seq_pos_min(seq_id);
if (pos_min_base != -1) {
return pos_min_base;
}
return kv_swa->seq_pos_min(seq_id);

Copilot uses AI. Check for mistakes.
Comment on lines 115 to +117
llama_pos llama_kv_cache_iswa::seq_pos_max(llama_seq_id seq_id) const {
return kv_swa->seq_pos_max(seq_id);
// base cache preserves all positions (no SWA cell reuse), making it the ground truth for position tracking
return kv_base->seq_pos_max(seq_id);

Copilot AI Feb 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seq_pos_max() also unconditionally queries kv_base, which can be unset/empty after restoring a partial sequence state (LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY skips kv_base state_read). This can incorrectly report -1 for existing sequences and can break callers that rely on seq_pos_max for continuity checks or error recovery. Consider computing max from the cache that actually has the sequence (prefer kv_base when populated, otherwise use kv_swa).

Copilot uses AI. Check for mistakes.
@ggerganov

Copy link
Copy Markdown
Member

The change is not valid - these functions intentionally work with the SWA cache since we have lost information about all tokens that are not in the SWA cache.

@cmp-nct

cmp-nct commented Feb 1, 2026 •

Copy link
Copy Markdown
Contributor Author

The change is not valid - these functions intentionally work with the SWA cache since we have lost information about all tokens that are not in the SWA cache.

Thanks for your review.
I guess the problem I ran into is that I just can hardcode p_min to be always zero in application level programming.
p_max is already the end in both kv_base and kv_swa.
The returned seq_min is invalid to be used for application level (because it reports swa min, not the actual min_pos of kv_base
I overcomplicated it., though a comment in the API might be useful to inform that ISWA min_pos is SWA POV

@ggerganov If you have a spare minute:
cmp-nct-org#9
I had find_slot corrupt my KV on state restoration when multiple sequences exist.
Maybe also wrong, but that change solved the corruption.

@cmp-nct cmp-nct closed this Feb 1, 2026
@ggerganov

Copy link
Copy Markdown
Member

Yes, the current logic of restoring sequences can erase other "out-of-SWA-scope" sequence data when SWA is involved. This is by design - one of the main advantages of SWA is that it reduces the KV cache, so the logic is implemented around that.

You can use split KV cache (--no-kv-unifed --swa-full) if you really want to keep the full SWA sequences.

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.

3 participants