[KV Offload] Fix num_tokens_after_batch for different termination types - #49285
Merged
Merged
Conversation
Alex-ai-future
requested review from
ApostaC,
NickLucche,
ivanium,
orozery and
xuechendi
as code owners
July 21, 2026 06:02
…equest_finished Root-cause fix for issue vllm-project#49118, complementary to PR vllm-project#49146's clamp. Changes: 1. preempt path: sync clear offload_keys with block_ids - Maintains invariant: len(offload_keys) * blocks_per_chunk <= len(block_ids) - Prevents offload_keys residue after preempt 2. request_finished: check block_ids before calling update_offload_keys - Only populate offload_keys if request was actually scheduled - Prevents offload_keys without corresponding block_ids for queued aborts 3. test: update test_abort_queued_request_does_not_build_store_job - Assert offload_keys is empty for never-scheduled request - Reflects the source fix in request_finished Why this matters: - preempt sync clear: defensive fix for invariant consistency - request_finished check: source fix for queued abort (no longer need clamp) - mid-prefill abort: still benefits from PR vllm-project#49146's clamp (prefix cache reuse) Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Alex <jihuihuang@example.com> Signed-off-by: Alex <jihui.huang@daocloud.io> Signed-off-by: Alex <alex.tech.lab@outlook.com> Signed-off-by: Alex <jihui.huang@daocloud.io>
Root-cause fix for the semantic issue: aborted requests may have num_computed_tokens < num_tokens, but the code was using num_tokens (entire prompt) for all finished requests, potentially storing chunks without actual KV data (garbage data). Fix: Use num_computed_tokens for FINISHED_ABORTED requests (only store chunks with actual KV data), and keep num_tokens for other finished states (EOS, etc.) to handle scheduling delay where num_computed_tokens may not be updated yet. This is a fundamental semantic fix that ensures: - Aborted requests: only store chunks that were actually computed - Normal EOS: still store all chunks (handle scheduling delay) Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Alex <jihui.huang@daocloud.io> Signed-off-by: Alex <alex.tech.lab@outlook.com> Signed-off-by: Alex <jihui.huang@daocloud.io>
With the fix to distinguish abort from EOS (using num_computed_tokens for aborted requests), the min(num_chunks, num_allocated_chunks) clamp is no longer needed: - For abort: num_chunks = num_computed_tokens_chunks, which is always <= num_allocated_chunks (blocks are allocated for computed tokens) - For EOS: num_chunks = num_tokens_chunks, which equals num_allocated_chunks (all blocks allocated when request finishes) The clamp was a symptom fix for the root cause (not distinguishing abort from EOS). With the root cause fixed, the clamp becomes unnecessary. Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Alex <jihui.huang@daocloud.io> Signed-off-by: Alex <alex.tech.lab@outlook.com> Signed-off-by: Alex <jihui.huang@daocloud.io>
Alex-ai-future
force-pushed
the
fix/lazy-clip
branch
from
July 21, 2026 06:07
b6f04b5 to
7f091b1
Compare
Contributor
Author
|
@orozery PTAL |
Remove long comment from preempt path to satisfy 88-char line limit. Signed-off-by: Alex <jihui.huang@daocloud.io>
Preempted requests clear block_ids and offload_keys together in the next branch, so calling update_offload_keys() first just produces stale hashes that get immediately discarded. Move the call into the else branch so only non-preempted requests pay the cost. Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Alex <jihuihuang@users.noreply.github.com> Signed-off-by: Alex <jihui.huang@daocloud.io>
orozery
reviewed
Jul 21, 2026
Comment on lines
+982
to
+989
| if req.status == RequestStatus.FINISHED_ABORTED: | ||
| # For aborted requests, only store chunks with actual KV data | ||
| # (num_computed_tokens), not the entire prompt (num_tokens). | ||
| num_tokens_after_batch = req.num_computed_tokens | ||
| elif req.is_finished(): | ||
| # For other finished requests (EOS, etc.), use num_tokens to | ||
| # handle the case where num_computed_tokens hasn't been updated | ||
| # yet due to scheduling delay. |
Collaborator
There was a problem hiding this comment.
I think this is the only change that is relevant.
All other changes can be reverted.
Alex-ai-future
commented
Jul 22, 2026
Contributor
Author
|
@orozery |
Distinguish normal termination (STOPPED/LENGTH_CAPPED/REPETITION) from abnormal termination (ABORTED/ERROR) when computing num_tokens_after_batch: - Normal termination: use num_tokens to handle async scheduling delay - Abnormal termination: use num_computed_tokens (actual KV data) Keep clamp in storable_chunks as defense-in-depth for block_ids edge cases. Signed-off-by: Alex <jihui.huang@daocloud.io> Co-authored-by: Claude <noreply@anthropic.com>
Alex-ai-future
force-pushed
the
fix/lazy-clip
branch
from
July 22, 2026 07:21
9122bc4 to
36c4555
Compare
orozery
reviewed
Jul 22, 2026
Comment on lines
+933
to
+934
| else: | ||
| req_status.update_offload_keys() |
Collaborator
There was a problem hiding this comment.
Leftover unrelated change? Let's revert
Comment on lines
+984
to
+995
| if req.status in ( | ||
| RequestStatus.FINISHED_STOPPED, | ||
| RequestStatus.FINISHED_LENGTH_CAPPED, | ||
| RequestStatus.FINISHED_REPETITION, | ||
| ): | ||
| # Normal termination: use num_tokens to handle scheduling | ||
| # delay with async scheduling. | ||
| num_tokens_after_batch = req.num_tokens | ||
| elif req.is_finished(): | ||
| # Abnormal termination (abort/error): store all computed KV data. | ||
| # num_computed_tokens represents actual computed tokens. | ||
| num_tokens_after_batch = req.num_computed_tokens |
Collaborator
There was a problem hiding this comment.
This is more long and fragile than simply:
Suggested change
| if req.status in ( | |
| RequestStatus.FINISHED_STOPPED, | |
| RequestStatus.FINISHED_LENGTH_CAPPED, | |
| RequestStatus.FINISHED_REPETITION, | |
| ): | |
| # Normal termination: use num_tokens to handle scheduling | |
| # delay with async scheduling. | |
| num_tokens_after_batch = req.num_tokens | |
| elif req.is_finished(): | |
| # Abnormal termination (abort/error): store all computed KV data. | |
| # num_computed_tokens represents actual computed tokens. | |
| num_tokens_after_batch = req.num_computed_tokens | |
| if req.status is RequestStatus.FINISHED_ABORTED: | |
| num_tokens_after_batch = req.num_computed_tokens | |
| elif req.is_finished(): | |
| num_tokens_after_batch = req.num_tokens |
Distinguish aborted requests from other finished requests when computing num_tokens_after_batch: - Aborted: use num_computed_tokens (actual KV data) - Other finished (EOS, length cap, repetition, error): use num_tokens to handle async scheduling delay Signed-off-by: Alex <jihui.huang@daocloud.io> Co-authored-by: Claude <noreply@anthropic.com>
orozery
approved these changes
Jul 26, 2026
5 tasks
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.
Summary
Fix incorrect
num_tokens_after_batchcomputation in offloading scheduler by usingnum_computed_tokensfor aborted requests (where partial KV data may exist) instead of always usingnum_tokens.Problem
The original code used
num_tokensfor all finished requests:For aborted requests,
num_computed_tokens < num_tokens(partial KV data). Usingnum_tokenswould attempt to store chunks without actual KV data on GPU.Solution
Distinguish aborted requests from other finished requests:
Rationale:
num_computed_tokens(actual KV data), never store chunks without KV datanum_tokensto handle async scheduling delay correctlyThe clamp in
storable_chunks(min(num_chunks, num_allocated_chunks)) remains as defense-in-depth, handling allblock_idsedge cases (empty, partial capacity, etc.).Test Plan
All existing tests pass (112 passed):
test_abort_queued_request_does_not_build_store_job✅ (queued abort)test_request_preemption✅ (preempt handling)test_last_block_offloaded_at_request_finish✅ (normal EOS)test_two_groups_full_and_sliding_window✅ (multi-group)Impact
block_idsedge casesAI Assistance
Used Claude for code analysis, root cause identification, and test validation. All changes reviewed and approved by the human author.