Skip to content
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

[Misc][V1] Enhance performance of KVCacheManager._get_cached_block #13878

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

imkero
Copy link
Contributor

@imkero imkero commented Feb 26, 2025

Enhance KVCacheManager cached block dict retrieve by:

  • fusing key in dict check and dict getitem operation
  • cache dict.get fn (cache the fn itself, rather than access dict.get every time) will get better performance
  • use for _ in dict.values() for better performance than next(iter(dict.values())) because for loop use native GET_ITER and FOR_ITER instruction with better performance

A slight time cost grow in case of "unknown block", but we may expect one or more "cached block" hit when enable_prefix_cache=True, and it would act like cache_hit, cache_hit, cache_hit, ..., cache_hit, cache_miss. So I think it's worth to choose this impl?

Benchmark

repeat 10000 times

code unknown block cached block
main 0.00107s 0.00427s
ver 0 (worse performance when cache miss) 0.00139s (+29.9%) 0.00225s (-47.3%)
ver 1 (this PR) 0.00114s (+6.5%) 0.00179s (-58.1%)

Reference

10+ years performance problem of dict.get

https://www.reddit.com/r/Python/comments/1atp5s/comment/c90onqq/

Iteration instruction compare

def v0(dict):
    return next(iter(dict.values()), None)
---
  1           0 RESUME                   0

  2           2 LOAD_GLOBAL              1 (NULL + next)
             12 LOAD_GLOBAL              3 (NULL + iter)
             22 LOAD_FAST                0 (dict)
             24 LOAD_ATTR                5 (NULL|self + values)
             44 CALL                     0
             52 CALL                     1
             60 LOAD_CONST               0 (None)
             62 CALL                     2
             70 RETURN_VALUE
def v1(dict):
    for v in dict.values():
        return v
    return None
---
  1           0 RESUME                   0

  2           2 LOAD_FAST                0 (dict)
              4 LOAD_ATTR                1 (NULL|self + values)
             24 CALL                     0
             32 GET_ITER
             34 FOR_ITER                 5 (to 48)
             38 STORE_FAST               1 (v)

  3          40 LOAD_FAST                1 (v)
             42 SWAP                     2
             44 POP_TOP
             46 RETURN_VALUE

  2     >>   48 END_FOR

  4          50 RETURN_CONST             0 (None)

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 Feb 26, 2025
@imkero imkero force-pushed the feat/get-cached-block-enhance branch from 40c90fe to 50226bf Compare February 26, 2025 07:57
Signed-off-by: imkero <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant