Skip to content

fix(qwen35): refuse a dense snapshot longer than the KV cache rows - #847

Merged
davide221 merged 3 commits into
Luce-Org:mainfrom
dusterbloom:kvflash/fix-dense-restore-bounds
Oct 18, 2026
Merged

davide221 merged 3 commits into
Luce-Org:mainfrom
dusterbloom:kvflash/fix-dense-restore-bounds

Conversation

@dusterbloom

@dusterbloom dusterbloom commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Why

A server started with --kvflash N aborts with this assertion when the on-disk KV cache offers a dense entry longer than N tokens:

ggml-backend.cpp:369: GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds") failed

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 N the KV tensors hold only N rows, but the context limit stays at --max-ctx. Two checks looked only at that logical length:

  • adoption (snapshot_adopt) compared the snapshot's rows with the position to restore, not with the destination's rows;
  • restore_target_cache checked 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):

  1. Start luce_server with --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 .dkv entry of 11600 positions (553.5 MB).
  2. Stop it. Start luce_server with the same model, the same --kv-cache-dir and --kvflash 8192.
  3. Send the same request. The log shows [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:

  1. Adoption refuses a snapshot the destination cannot hold. A new pure helper, attn_snapshot_adoptable in internal.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_adopt in qwen35_backend.cpp uses 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.
  2. restore_target_cache keeps 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 sets restore_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

  • New server/test/unit/test_restore_bounds.cpp, on CPU-backend K and V tensors:
    • a 12-row snapshot restored into an 8-row cache is refused, and the cache is left untouched; an 8-row snapshot into 8 rows restores;
    • 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.
  • Without the restore guard, test_server_unit aborts in this test with the same tensor write out of bounds assertion. 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.
  • The reproduction above, run with each build:
    • The restore guard alone (7281314) no longer aborts, but both requests return 500 invalid_snapshot_slot.
    • With the adoption check (c0a311a), both return 200 with the empty-cache answer.

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.

View guided diff

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 davide221 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.

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.
@davide221
davide221 merged commit d1f1635 into Luce-Org:main Oct 18, 2026
8 checks passed
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.

2 participants