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

[V1] Implement sliding window attention in kv_cache_manager #14097

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

Conversation

heheda12345
Copy link
Collaborator

@heheda12345 heheda12345 commented Mar 2, 2025

Build on top of #14079, should be merged after it.

This pr supports “real” sliding window in v1:

  1. Support dropping blocks outside sliding window
  2. For prefix caching, only requires the last sliding_window tokens to be cached to achieve a prefix cache hit. e.g., for request ABCDE with sliding window size 2 & block_size 1, if DE are cached while ABC are not, we can still regard ABCDE as the cached prefix.

For models with global attention + sliding window attention, still regard as global-attention-only model in kv cache manager.

Some questions in #13296
It isn’t compatible with cascade attention yet.

Q: How does it work with chunked prefill?
A: It will allocate blocks for tokens that will be computed in the current step, and free the blocks that outside sliding window in the next step.
Assume window size 1k, chunk size 2k, prompt length 4k, block_size=1

  1. chunk prefill of 2k tokens: block table: [0:2k]
  2. chunk prefill of 2k tokens:
    1. Free the first 1k blocks as they won’t be used after step 1. block_table becomes [null_block*1000] + [1k:2k]
    2. Allocate blocks for the [2k:4k] tokens that will be computed. block_table becomes [null_block*1000] + [1k:4k]
  3. First decode step
    1. Free the [1k:3k] blocks as they won’t be used after step 2. block_table becomes [null_block*3000] + [3k:4k]
    2. Allocate a new slot for decoding, block_table becomes [null_block*3000] + [3k:4001]

Q: What's the shape of the block table for SWA? Is it append-only?
A: It is with the same length as global attention, but changes the blocks outside the sliding window to a special null_block. This replacement only happens in the kv_cache_manager side. As model_runner’s block_table is append-only, we do not replace existing blocks to null blocks in model runner. The result is correct because model runner won’t access the blocks outside sliding window.

This pr is part of the hybrid allocator #11382

Copy link

github-actions bot commented Mar 2, 2025

👋 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 Mar 2, 2025
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