Repository navigation
fix(qwen35): refuse a dense snapshot longer than the KV cache rows - #847
Merged
davide221 merged 3 commits intoOct 18, 2026
Merged
Conversation
A KVFlash pool allocates fewer KV rows than max_ctx, but restore_target_cache only checked snap.cur_pos against max_ctx. A dense disk-cache entry written by a server with no pool (or a larger one) passed that check and was written past the end of the pool tensor, aborting the server in ggml_backend_tensor_set. Check the destination tensor rows before any write and fail the restore.
davide221
reviewed
Oct 15, 2026
davide221
left a comment
Contributor
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed 7281314f · 0 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again
… destination capacity (red: with Luce-Org#847 alone the restore fails the request with InvalidSnapshotSlot instead of counting a miss)
…n hold it (same change as 0bcbdc5, moved into a pure helper): a larger pool's snapshot is a miss, not an InvalidSnapshotSlot failure
dusterbloom
added a commit
to dusterbloom/lucebox-hub
that referenced
this pull request
Oct 17, 2026
c0a311a (1018/0/7 on 7281314) and the GPU reproduction; an 11.6K dense .dkv read by --kvflash 8192 returns 500 invalid_snapshot_slot twice on 7281314 and 200 twice on c0a311a, the first answer equal to an empty-cache run RTX 3090, Qwen3.8-27B UD-Q4_K_M, --max-ctx 131072. On c0a311a the log shows the adoption refused, then disk_hit=false and a normal prefill. The red log is c5d727c's RestoreBoundsFixture run (2 passed, 1 failed at test_restore_bounds.cpp:104 and :105). Binaries and the 553 MB .dkv entries stay in /mnt/kimi-k3/build/pr847_adopt/; md5s are in each provenance.txt. The PR branch kvflash/fix-dense-restore-bounds now fast-forwards to c0a311a.
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.
Why
A server started with
--kvflash Naborts with this assertion when the on-disk KV cache offers a dense entry longer than N tokens:The entry can come from an earlier run of the same model and KV type without
--kvflash, because the disk cache is on by default and its layout key does not include the pool size. With--kvflash Nthe KV tensors hold only N rows, but the context limit stays at--max-ctx. Two checks looked only at that logical length:snapshot_adopt) compared the snapshot's rows with the position to restore, not with the destination's rows;restore_target_cachechecked the length against--max-ctx.So a longer snapshot was adopted and copied past the end of the tensor.
Reproduction against main 6f2b179 (RTX 3090, Qwen3.8-27B UD-Q4_K_M,
--max-ctx 131072):luce_serverwith--kv-cache-dir <empty dir>and no--kvflash. Send one chat request of about 11.6K tokens and wait for it to finish. The server writes one.dkventry of 11600 positions (553.5 MB).luce_serverwith the same model, the same--kv-cache-dirand--kvflash 8192.[disk-cache] hit policy=full len=11600, then the assertion above, and the process aborts.What it does
Two checks, in the order the request meets them:
attn_snapshot_adoptableininternal.h, requires the same KV type and head shape, rows equal to the restore position, and that position at most the destination's row count.snapshot_adoptinqwen35_backend.cppuses it for K and V. A refused adoption frees the payload, and the disk lookup counts a miss. The request then prefills from scratch and succeeds.restore_target_cachekeeps a bounds check as the last line. It compares the snapshot length with the row count of the destination K and V tensors before it writes anything. If the snapshot is longer, it setsrestore_target_cache: snapshot longer than the KV cache rows, returns false and writes nothing. With check 1 in place this path no longer fires on the reproduction. It guards any other caller.On the reproduction, both the first request and a follow-up return 200. The log shows the adoption refused, then
disk_hit=false. The first answer's text equals that of a server with an empty cache dir at--kvflash 8192. The Qwen3.5 MoE backend restores through the same function, so check 2 covers it too.The disk cache layout hash is left as it is on purpose. Its tensor part sets the sequence-length dimension of each tensor to 1, and its identity salt covers the model file,
--max-ctx, the chat template and the key rotation, but not the pool size. Dense entries are meant to be shared between servers that differ only in that length, and an entry that fits the pool is still usable. Adding the pool size to the hash would make every existing entry unusable after a change of--kvflash, and it would not fix the missing bound checks, which are the actual defect.Tests
server/test/unit/test_restore_bounds.cpp, on CPU-backend K and V tensors:attn_snapshot_adoptable: 12 rows into 8 is refused, 8 into 8 is adopted, rows that differ from the restore position (7) are refused, 12 into 16 is adopted.test_server_unitaborts in this test with the sametensor write out of boundsassertion. With the old adoption logic, the adoption test fails (test_restore_bounds.cpp:104,:105).test_server_unit(CUDA build, RTX 3090): main has 1016 passed, 0 failed, 7 skipped. This branch has 1019 passed, 0 failed, 7 skipped.invalid_snapshot_slot.This is two bounds checks, on adoption and on a copy. Not benchmarked.
Limits
An oversized entry stays on disk. Each request that finds it pays one refused adoption and then a full prefill, until the entry is deleted or expires. The KVFlash session-restore work (stacked PRs, not part of this one) makes the restore plan check the pool size and re-prefill from token 0 instead, and its tests cover that case.