Repository navigation
Conversation
Updated position tracking to use base cache instead of SWA cache to avoid kv cache token losses.
|
Another related PR: cmp-nct-org#9 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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).
| 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); |
| 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); |
There was a problem hiding this comment.
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).
|
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. @ggerganov If you have a spare minute: |
|
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 ( |
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.