Skip to content

[V1] Allocate kv_cache with stride order for V1 #18775

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 2 commits into from
May 29, 2025

Conversation

NickLucche
Copy link
Contributor

Basically a port of #16605 for V1. The same motivations still stand, plus we will need to have a different kv cache layout to optimize nixl xfer for the P/D case.

Signed-off-by: nicklucche <nlucches@redhat.com>
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 May 27, 2025
@NickLucche
Copy link
Contributor Author

cc @LucasWilkinson

Comment on lines +2021 to +2028
try:
kv_cache_stride_order = self.attn_backends[
i].get_kv_cache_stride_order()
assert len(kv_cache_stride_order) == len(
kv_cache_shape)
except (AttributeError, NotImplementedError):
kv_cache_stride_order = tuple(
range(len(kv_cache_shape)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

The try/catch seems a little sketchy. but I don't see a better way.

I thought about handling it in the abstract AttentionBackend, but there we don't know how many axes the kv_cache_shape is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep same thought

Comment on lines +365 to +368
if default_stride == rnd_stride:
assert all(kv.is_contiguous() for kv in model_runner.kv_caches)
else:
assert all(not kv.is_contiguous() for kv in model_runner.kv_caches)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might be a little better to check the exact stride order -- although this is ambiguous if one of the dims is length 1

def stride_order(tensor: torch.Tensor):
    # returns a tuple of axis indices sorted from largest stride to smallest
    # i.e. slowest-varying dim first, fastest-varying dim last
    strides = tensor.stride()
    return tuple(sorted(range(tensor.dim()), key=lambda i: strides[i], reverse=True))

@tlrmchlsmth tlrmchlsmth added the ready ONLY add when PR is ready to merge/full CI is needed label May 29, 2025
@NickLucche
Copy link
Contributor Author

Pre-commit is actually fine in this PR, issue is with CI itself when running the step

Failed to fetch http://archive.ubuntu.com/ubuntu/pool/universe/g/game-music-emu/libgme0_0.6.3-2_amd64.deb  403  Forbidden [IP: 185.125.190.82 80]

Might be worth another spin.

@tlrmchlsmth tlrmchlsmth enabled auto-merge (squash) May 29, 2025 15:10
@tlrmchlsmth tlrmchlsmth merged commit 32ce3cf into vllm-project:main May 29, 2025
69 of 72 checks passed
amitm02 pushed a commit to amitm02/vllm that referenced this pull request Jun 1, 2025
Signed-off-by: nicklucche <nlucches@redhat.com>
Signed-off-by: amit <amit.man@gmail.com>
amitm02 pushed a commit to amitm02/vllm that referenced this pull request Jun 1, 2025
Signed-off-by: nicklucche <nlucches@redhat.com>
Signed-off-by: amit <amit.man@gmail.com>
minpeter pushed a commit to minpeter/vllm that referenced this pull request Jun 24, 2025
Signed-off-by: nicklucche <nlucches@redhat.com>
Signed-off-by: minpeter <kali2005611@gmail.com>
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.

2 participants