Skip to content

[TPU] kv cache update kernel supports dynamic grid #20235

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
Jul 2, 2025

Conversation

yaochengji
Copy link
Collaborator

@yaochengji yaochengji commented Jun 30, 2025

Purpose

In the kv_cache_update TPU kernel, we pad the input tensor slices to prevent recompilation, but this resulted in numerous trivial DMA copies. We now use the dynamic grid features of a Pallas kernel to eliminate most of them. It can provide more than 1% e2e throughput gain usually.

Test Plan

pytest -s -v tests/v1/tpu/test_kv_cache_update_kernel.py

Test Result

Passed

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 @yaochengji, 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 aims to improve the end-to-end throughput of the kv_cache_update TPU kernel by leveraging dynamic grid features of Pallas. This change eliminates the need for padding input tensor slices, which previously led to inefficient trivial DMA copies, thereby optimizing performance.

Highlights

  • Performance Optimization: The primary goal is to achieve over 1% e2e throughput gain by switching the kv_cache_update TPU kernel from using padded input tensor slices to a dynamic grid approach. This change reduces unnecessary DMA copies.
  • Dynamic Grid Implementation: The Pallas kernel for KV cache updates now dynamically determines its grid size based on the actual number of slices (num_kv_update_slices) rather than a padded size, utilizing a new cdiv utility for ceiling division.
  • API Changes: The kv_cache_update_op and its underlying Pallas kernel (kv_cache_update) now accept an additional num_kv_update_slices parameter to facilitate the dynamic grid sizing.
  • Metadata Updates: The PallasMetadata struct has been extended to include num_kv_update_slices, ensuring this dynamic information is propagated through the system.
  • Test Suite Adaptation: The existing test for kv_cache_update_kernel has been updated to correctly pass the new num_kv_update_slices parameter, ensuring the changes are validated.
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.

@yaochengji yaochengji requested a review from vanbasten23 June 30, 2025 03:25
@mergify mergify bot added v1 tpu Related to Google TPUs labels Jun 30, 2025
@yaochengji
Copy link
Collaborator Author

@bythew3i , could you take a look?

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.

Code Review

The pull request introduces dynamic grid support for the kv_cache_update TPU kernel, aiming to eliminate unnecessary DMA copies and improve e2e throughput. The changes involve modifications to the test script and the kernel implementation, as well as updates to the worker to pass the number of KV update slices. The code appears well-structured and addresses the stated purpose. I've added a few comments to improve readability and highlight a potential platform-specific dependency.

Comment on lines 297 to +300
XLA_LIB.define(
"kv_cache_update_op(Tensor kv, Tensor slot_mapping, Tensor kv_cache, "
"int page_size, int num_slices_per_block) -> Tensor", )
"kv_cache_update_op(Tensor kv, Tensor slot_mapping, Tensor kv_cache," \
"Tensor num_kv_update_slices, int page_size, int num_slices_per_block)" \
"-> Tensor", )
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

It would be helpful to include a space after the concatenation operator (+) for improved readability.

    "Tensor num_kv_update_slices, int page_size, int num_slices_per_block)" \
    "-> Tensor", )

Comment on lines 714 to 717
# calcute the slot mapping
slot_mapping_metadata = self._get_slot_mapping_metadata(
num_reqs, num_scheduled_tokens_per_req)
num_kv_update_slices = slot_mapping_metadata.shape[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a brief comment to clarify what slot_mapping_metadata represents. This will improve code readability.

Suggested change
# calcute the slot mapping
slot_mapping_metadata = self._get_slot_mapping_metadata(
num_reqs, num_scheduled_tokens_per_req)
num_kv_update_slices = slot_mapping_metadata.shape[0]
# calculate the slot mapping
slot_mapping_metadata = self._get_slot_mapping_metadata(
num_reqs, num_scheduled_tokens_per_req)

Copy link
Collaborator

@vanbasten23 vanbasten23 left a comment

Choose a reason for hiding this comment

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

Thanks Chengji!

Assuming this pr is the same as https://github.com/vllm-project/tpu_commons/pull/87, then it LGTM

Signed-off-by: Chengji Yao <[email protected]>
Copy link
Member

@mgoin mgoin left a comment

Choose a reason for hiding this comment

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

Nice, LGTM!

@mgoin mgoin enabled auto-merge (squash) July 1, 2025 23:41
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 1, 2025
@mgoin mgoin merged commit 7da296b into vllm-project:main Jul 2, 2025
80 checks passed
huydhn pushed a commit to huydhn/vllm that referenced this pull request Jul 8, 2025
LyrisZhong pushed a commit to LyrisZhong/vllm that referenced this pull request Jul 23, 2025
avigny pushed a commit to avigny/vllm that referenced this pull request Jul 31, 2025
Pradyun92 pushed a commit to Pradyun92/vllm that referenced this pull request Aug 6, 2025
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 tpu Related to Google TPUs v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants