Repository navigation
kv-cache : fix iSWA seq_pos_max answering from the SWA cache only - #29480
charan-rathore wants to merge 2 commits into
Conversation
llama_kv_cache_iswa::seq_pos_max answered from kv_swa alone. After a partial seq_rm below the sliding window (e.g. keeping only the last few tokens of a long prompt), the SWA cache becomes empty while the base cache still holds cells, so seq_pos_max returned -1 for a sequence that is not empty. Callers that derive positions from seq_pos_max (llama_batch_get_one via llama_batch_allocr) then restarted at position 0 and silently overwrote live base-cache cells, corrupting the KV state. Take the maximum across both caches so -1 keeps meaning "the sequence is empty", matching the contract of llama_kv_cache::seq_pos_max. Fixes ggml-org#29045
|
Hi @charan-rathore, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
Thanks for looking into this @charan-rathore, and thanks @mitoyosh for confirming it. |
|
Thanks, Julius. That iOS path is helpful context, especially the nextToken() use of llama_batch_get_one. The PR is still under review, so I can't promise when it will be synced, but an on-device KV-reuse result when it is available would be useful. |
|
Thanks for looking. I read #19225 and the discussion there. It switched seq_pos_min and seq_pos_max to the base cache, and ggerganov closed it because these functions intentionally work with the SWA cache. This PR is narrower: seq_pos_min is untouched, and seq_pos_max still reads the SWA cache but takes the max with the base cache, so it only changes the case where the SWA cache is empty and the base cache is not (it returned -1 for a non-empty sequence). I have not tested whether that still conflicts with the intended behavior. You are right that it does not solve the whole llama.rn problem. It only restores that -1 means an empty sequence for batches without positions, and I have only run test-batch-alloc, no iSWA-specific test. If the maintainers consider this by design and prefer callers to pass explicit positions or handle reuse in server-context.cpp, I am fine with closing it. |
|
The problem with this change is that if a SWA checkpoint is not restored and the inference continues from the new max pos, it will be subtly incorrect (because of the missing SWA data). I think we have to add some assertion in addition to the proposed change in order to prevent that from silently happening. |
|
Thanks, that makes sense. If the SWA cache is empty and the base cache is not, continuing from the base max position would leave the SWA layers attending to missing data without any signal. I can see two places for a check, and I would like to pick the one you prefer before I push anything:
I have not built or run either variant yet, and I have only run test-batch-alloc so far. If you think this is better handled in the caller (server-context) instead, I am fine with closing the PR. Prepared with AI assistance; I am responsible for the change. |
Try this first. But I am not sure it is going to work. It depends if we call |
…ot in iSWA seq_pos_max
|
Pushed d0158f4: On your question about call order: I only read the code, I did not run a model. What I ran: build of libllama and test-batch-alloc, which passes (0 failures). I have not run any iSWA model or the server, so the assertion itself is untested at runtime. Prepared with AI assistance; I am responsible for the change. Happy to close or change it if you would rather keep the query side-effect free. |
|
Yes, the assertion has to be in a different place. But it would be easier if I take a look at this myself instead of navigating you, or if someone else takes over. The idea is now clear. |
|
Thanks for the guidance and for taking the time on this. Happy for you or anyone else to take it over from here, and fine to close this PR if you prefer. I will not push anything more to it. |
Overview
llama_kv_cache_iswa::seq_pos_maxanswered from the SWA cache alone. After a partialseq_rmbelow the sliding window (e.g. keeping only the last few tokens of a long prompt), the SWA cache becomes empty while the base cache still holds cells, soseq_pos_maxreturned-1for a sequence that is not empty. Callers that derive positions from it (llama_batch_get_oneviallama_batch_allocr) then restart at position 0 and silently overwrite live base-cache cells - the KV corruption reported in #29045.The base cache is a superset of the SWA cache, so the fix reports the maximum across both caches:
This restores the documented contract that
-1means "the sequence is empty".seq_pos_minalready reports the minimum across both caches and is unchanged.Additional information
Fixes #29045. The fix direction matches what @juliusbangert proposed in the issue thread; the repro was confirmed on current master by @mitoyosh.
Validation: CPU-only build (
-DLLAMA_CURL=OFF);test-batch-alloc(thellama_batch_allocrconsumer ofseq_pos_max) passes - 50 tests, 327 assertions, 0 failures. The repo has no dedicated iSWA unit-test harness, so no new regression test is included; the issue body has the minimal runtime repro. Happy to add a targeted test if a maintainer points at the preferred harness.Requirements