-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Resolved the extremely large block_size problem #20977
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
base: main
Are you sure you want to change the base?
Conversation
👋 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 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 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @nadathurv, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses a critical memory inefficiency within the KV cache by implementing a dynamic block size calculation. My primary goal is to optimize memory allocation for heterogeneous model architectures, such as those integrating SSM and attention layers, which is expected to lead to substantial memory savings. The core of this change involves calculating an optimal block size based on an aggregate constraint across attention layers and then applying this size during the KV cache configuration process, thereby minimizing wasted memory.
Highlights
- KV Cache Optimization for Heterogeneous Models: I've introduced a new function,
calculate_optimal_block_size
, which dynamically computes the most efficient block size for the KV cache. This optimization is specifically designed for heterogeneous model architectures (e.g., those combining SSM and attention layers), aiming to significantly reduce memory waste by up to 75% while preserving 16-byte alignment for FlashAttention compatibility. - Automated Block Size Configuration: The system now automatically applies the newly calculated optimal block size during KV cache configuration. I've updated
get_kv_cache_config
to invoke_get_kv_cache_config_optimal_block_size
for relevant scenarios, ensuring that hybrid models benefit from optimized memory utilization without requiring manual tuning. - Focused Memory Efficiency: The changes are concentrated on a mathematical optimization of the block size. I've ensured that this improvement does not necessitate alterations to the underlying GPU allocators or the fundamental KV cache memory layout, which helps in reducing implementation complexity and potential side effects.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The code changes introduce an optimization for calculating the block_size
in hybrid models (Attention + SSM) to reduce memory usage. There are a couple of critical issues that will prevent the code from running correctly: a syntax error in get_kv_cache_config
where an else
block follows a raise
statement, making the new logic unreachable, and the calculate_optimal_block_size
function attempts to access a non-existent attribute state_size_bytes
on MambaSpec
, which will cause a runtime error. There are also some suggestions to improve code clarity.
Signed-off-by: nadathurv <work.vnadathur@gmail.com> Signed-off-by: Srreyansh Sethi <srreyansh.sethi@gmail.com> Co-Authored-By: Srreyansh Sethi <107075589+WorldExplored@users.noreply.github.com> Co-Authored-By: nadathurv <218520480+nadathurv@users.noreply.github.com>
1121cf6
to
5e6ac22
Compare
Signed-off-by: nadathurv <work.vnadathur@gmail.com> Signed-off-by: Srreyansh Sethi <srreyansh.sethi@gmail.com> Co-Authored-By: Srreyansh Sethi <107075589+WorldExplored@users.noreply.github.com> Co-Authored-By: nadathurv <218520480+nadathurv@users.noreply.github.com>
This pull request has merge conflicts that must be resolved before it can be |
Related to the To-Do item
Our Changes
A fresh aggregate‑constraint algorithm picks the smallest block size that satisfies padding for all attention layers at once.
In typical hybrid models, the number falls almost in proportion to the layer count, so memory waste drops to roughly one‑quarter. FlashAttention still works because every block keeps its 16‑byte alignment.
Integration with the KV Cache
The helper
calculate_optimal_block_size()
is invoked insideget_kv_cache_config()
. Mixed SSM‑plus‑attention networks call it automatically; uniform architectures follow their original path, preserving backward compatibility.Memory Layout Remains Stable
Underlying page allocation stays exactly as before. GPU allocators and metadata builders need no edits, which confines the patch to a compact mathematical change that is easy to audit.
Assistance Requested
Validation
Edge‑case fallbacks should be exercised, and full runs on Granite‑4.0 plus Falcon‑H1 would be especially helpful. Results from A100 or H100 hardware are also welcome.
Benchmarking
Please gather numbers on memory savings, first‑token latency, per‑token throughput, and overall speed at several batch sizes, then compare those results with the previous allocator and with a hand‑tuned baseline.