Skip to content

[v1][P/D] Fix a edge case in kv cache schedule #19182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vllm/v1/core/sched/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,8 @@ def _update_waiting_for_remote_kv(self, request: Request) -> bool:
# Now that the blocks are ready, actually cache them.
block_ids = self.kv_cache_manager.get_block_ids(request.request_id)[0]
num_computed_tokens = len(block_ids) * self.block_size
# Handle the case where num request tokens less then one block.
num_computed_tokens = min(num_computed_tokens, request.num_tokens)
Comment on lines +1009 to +1010
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current comment "# Handle the case where num request tokens less then one block." is a bit specific. While true for one scenario, the fix is more general: it ensures num_computed_tokens doesn't exceed request.num_tokens, regardless of whether request.num_tokens is less than one block or spans multiple blocks but is less than the total capacity of those blocks.

Could we make the comment slightly more general to better reflect the line's purpose? For example:

# Cap num_computed_tokens at the actual number of tokens in the request.

This would clarify that num_computed_tokens is being adjusted to the true length of the content being cached, rather than just the allocated block capacity.

Suggested change
# Handle the case where num request tokens less then one block.
num_computed_tokens = min(num_computed_tokens, request.num_tokens)
# Cap num_computed_tokens at the actual number of tokens in the request.

if num_computed_tokens == request.num_tokens:
num_computed_tokens -= 1
self.kv_cache_manager.single_type_manager.cache_blocks(
Expand Down