Skip to content

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

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

Conversation

nadathurv
Copy link

@nadathurv nadathurv commented Jul 15, 2025

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 inside get_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.

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.

🚀

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.

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

  1. 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.

@mergify mergify bot added the v1 label Jul 15, 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

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>
@nadathurv nadathurv force-pushed the large-block-size-fix branch from 1121cf6 to 5e6ac22 Compare July 15, 2025 10:44
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>
Copy link

mergify bot commented Jul 15, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @nadathurv.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Jul 15, 2025
@mergify mergify bot removed the needs-rebase label Jul 16, 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.

2 participants