Skip to content

Conversation

Jialin
Copy link
Contributor

@Jialin Jialin commented Jul 21, 2025

Essential Elements of an Effective PR Description Checklist

  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

Purpose

With #21222, we noticed that _maybe_evict_cached_block became the new bottleneck for allocate_new_block.
Screenshot 2025-07-20 at 9 47 09 PM

And we notice that, we do duplicated key lookup against the same dict 4 times, and we could easily cut it down into 2.

Test Plan

Compare trace with and without the change on top of #21222

Test Result

Quite significant improvements 0.13ms -> 0.03ms

After
Screenshot 2025-07-20 at 9 46 23 PM

Before
Screenshot 2025-07-20 at 9 47 09 PM

(Optional) Documentation Update

Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@mergify mergify bot added the v1 label Jul 21, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes _maybe_evict_cached_block by reducing dictionary lookups, which was identified as a performance bottleneck. The changes are logically sound, achieving the intended performance improvement by using .get() and .pop() to minimize lookups. The new implementation is also more robust by handling cases where a block ID might not be present in the cache, avoiding potential KeyError exceptions. The code is correct under the assumption of single-threaded access, which is consistent with the vLLM scheduler's design. Overall, this is a solid improvement.

@Jialin
Copy link
Contributor Author

Jialin commented Jul 21, 2025

Resolve #21141

Copy link
Member

@njhill njhill left a comment

Choose a reason for hiding this comment

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

Minor comment, otherwise lgtm

Comment on lines 256 to 257
if len(blocks_by_id) == 0:
self.cached_block_hash_to_block.pop(block_hash)
Copy link
Member

Choose a reason for hiding this comment

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

Why not del?

Suggested change
if len(blocks_by_id) == 0:
self.cached_block_hash_to_block.pop(block_hash)
if blocks_by_id:
del cached_block_hash_to_block.pop[block_hash]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • For del, it makes a lot sense, as we don't rely on the returned values;
  • But for the bool check, I thought len(blocks_by_id) == 0 would be alightly more efficient? I could imagine the bool stack would be bool -> len(blocks_by_id) == 0. So maybe the original approach would avoid one less function call.

However, the difference of the second bullet might be very minimum, so updating the commit per you comments.

Copy link
Member

Choose a reason for hiding this comment

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

yeah the bool check is more a style thing, only a suggestion

Copy link
Collaborator

@houseroad houseroad left a comment

Choose a reason for hiding this comment

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

Overall, looks good. Only two comments to address.

@houseroad houseroad added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 21, 2025
Jialin added 3 commits July 21, 2025 15:00
Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
@houseroad houseroad enabled auto-merge (squash) July 21, 2025 22:02
@simon-mo simon-mo disabled auto-merge July 22, 2025 05:37
@simon-mo simon-mo merged commit af376ca into vllm-project:main Jul 22, 2025
64 of 66 checks passed
return False
block.reset_hash()
blocks_by_id.pop(block.block_id, None)
if blocks_by_id:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is wrong, fixed in #21357

Copy link
Member

Choose a reason for hiding this comment

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

Sorry about this :( had meant to suggest not blocks_by_id above.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting this didn’t trigger any unit test failure 🤣? I think we should fix that @Jialin

Copy link
Contributor Author

@Jialin Jialin Jul 22, 2025

Choose a reason for hiding this comment

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

Thanks @simon-mo for the fix forward. My bad. I should be more careful when updating the logic.

There you go, at least #21400 should help capturing some basic failures.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I will take some blame as well, didn't notice the issue in the review.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No worries, thx for adding the tests.

yeqcharlotte pushed a commit to yeqcharlotte/vllm that referenced this pull request Jul 23, 2025
zixi-qi pushed a commit to zixi-qi/vllm that referenced this pull request Jul 23, 2025
…llm-project#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: qizixi <qizixi@meta.com>
LyrisZhong pushed a commit to LyrisZhong/vllm that referenced this pull request Jul 23, 2025
avigny pushed a commit to avigny/vllm that referenced this pull request Jul 31, 2025
…llm-project#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: avigny <47987522+avigny@users.noreply.github.com>
wenscarl pushed a commit to wenscarl/vllm that referenced this pull request Aug 4, 2025
…llm-project#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: shuw <shuw@nvidia.com>
x22x22 pushed a commit to x22x22/vllm that referenced this pull request Aug 5, 2025
…llm-project#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: x22x22 <wadeking@qq.com>
Pradyun92 pushed a commit to Pradyun92/vllm that referenced this pull request Aug 6, 2025
npanpaliya pushed a commit to odh-on-pz/vllm-upstream that referenced this pull request Aug 6, 2025
jinzhen-lin pushed a commit to jinzhen-lin/vllm that referenced this pull request Aug 9, 2025
…llm-project#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: Jinzhen Lin <linjinzhen@hotmail.com>
paulpak58 pushed a commit to paulpak58/vllm that referenced this pull request Aug 13, 2025
…llm-project#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: Paul Pak <paulpak58@gmail.com>
taneem-ibrahim pushed a commit to taneem-ibrahim/vllm that referenced this pull request Aug 14, 2025
diegocastanibm pushed a commit to diegocastanibm/vllm that referenced this pull request Aug 15, 2025
…llm-project#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
Signed-off-by: Diego-Castan <diego.castan@ibm.com>
epwalsh pushed a commit to epwalsh/vllm that referenced this pull request Aug 28, 2025
googlercolin pushed a commit to googlercolin/vllm that referenced this pull request Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants