Repository navigation
[Fix] Reject duplicate block ids in BlockedAllocator.free to prevent KV-cache double allocation - #8931
[Fix] Reject duplicate block ids in BlockedAllocator.free to prevent KV-cache double allocation#8931lkh-bug wants to merge 1 commit into
Conversation
…KV-cache double allocation BlockedAllocator.free() validated every block before mutating the free list, but the 'already free' check cannot detect duplicates within a single call: marking happens in the mutation loop, so both passes see the stale -1 marker for a repeated id. The duplicated block is pushed into the free list twice and a later allocate() hands one KV block to two sequences, silently corrupting cache state. Reject duplicates in the validation pass and normalize elements to int: callers pass tensors (e.g. DSStateManager.flush_sequence passes seq.all_block_ids()), and 0-dim tensors hash by identity, which would defeat a set-based duplicate check. Co-Authored-By: Kscc <noreply@owtffssent.com> Signed-off-by: likehong <likehong@users.noreply.github.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 09fc93030c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| # Normalize to int: callers may pass tensors (e.g. all_block_ids), | ||
| # and 0-dim tensors hash by identity, which would defeat | ||
| # duplicate detection below. | ||
| block = int(block) |
There was a problem hiding this comment.
Use normalized block IDs in the mutation pass
When block 1 is allocated and a caller passes free(True) (or a 0-D bool tensor), the conversion validates ID 1, but the mutation loop still uses the original True. PyTorch treats that as a boolean mask, so self._blocks[True] = ... overwrites the whole free-list tensor while only incrementing _free_blocks once, corrupting allocator state; materialize the normalized IDs and use them in both passes (or reject booleans).
Useful? React with 👍 / 👎.
Motivation
BlockedAllocator.free()validates every block for validity before mutating the free list, but the "already free" check cannot detect duplicates within a single call: marking happens in the second (mutation) loop, so both passes see the stale-1marker for a repeated id.Repro (pure CPU, no model or GPU needed):
The same call path is reachable from production code:
DSStateManager.flush_sequence()passesseq.all_block_ids()(a Tensor) intoBlockedKVCache.free()->BlockedAllocator.free(). Iterating that Tensor yields 0-dim tensors, which hash by identity -- so even a set-based duplicate check would be defeated without normalizing toint.Changes
free()call (Block X provided multiple times to free).int(block)before checking, so Tensor inputs go through the same code path.No behavior change for valid inputs: the free-list mutation loop is untouched.
Tests
Added
test_free_duplicate_block_rejectedintests/unit/inference/v2/ragged/test_blocked_allocator.py:free()raisesValueErrorfree_blocksstays consistent)Test commands / results
free([0, 0, 1])succeeded and a subsequentallocate(2)returned[1, 0]-- block 0 allocated twice.ValueError: Block 0 provided multiple times to free;free_blocksstays consistent.flake8on changed files: clean.inference_v2marker and only needsBlockedAllocatorconstruction (CPU with pinned-memory shim locally; will exercise natively on the GPU CI runners).Duplicate check
Searched issues and PRs for
BlockedAllocator, "already free", "allocated twice", "duplicate block" (both deepspeedai/DeepSpeed and legacy microsoft/DeepSpeed) -- no existing report. PR #8503 (fixing #5407) touches onlyengine_v2.pyand does not include this fix.AI assistance
This fix was prepared with AI coding assistance; the submitting human has reviewed every changed line and the verification above.
Signed-off-by: likehong likehong@kingsoft.com