Skip to content

[Feature] Support custom set kv buffer kernel#8884

Merged
merrymercy merged 3 commits intosgl-project:mainfrom
DarkSharpness:dev_store_kv
Aug 12, 2025
Merged

[Feature] Support custom set kv buffer kernel#8884
merrymercy merged 3 commits intosgl-project:mainfrom
DarkSharpness:dev_store_kv

Conversation

@DarkSharpness
Copy link
Copy Markdown
Collaborator

Motivation

nsys profile shows that set_kv_cache will lead to a little overhead in H100. We write a kernel to fuse storing into key cache and value cache.

Modifications

Before:

0f6bb65ce198afe19fad7b4a6c16a449

After:

5756b8a9e4125d0ef36bb71536922faf

This PR will bring around 3% e2e performance improvement for llama 3.1 8B in small batch size on H100. For large TP where the KVCache per rank is smaller (e.g. TP=8 for 70B model, 1 head per GPU), this should works even better.

Accuracy Test

Benchmark & Profiling

Checklist

Copy link
Copy Markdown
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 @DarkSharpness, 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!

I've implemented a significant performance enhancement by introducing a custom CUDA kernel to optimize the set_kv_cache operation. This change directly addresses an identified overhead in H100 environments by fusing the storage of key and value caches into a single, more efficient operation. The primary goal is to improve the end-to-end performance of large language models, particularly for small batch sizes and large tensor parallelism configurations, while ensuring a robust fallback to existing PyTorch implementations if the custom kernel is not applicable.

Highlights

  • Custom CUDA Kernel for KV Cache: I've introduced a new custom CUDA kernel specifically designed to fuse the storage operations for Key (K) and Value (V) caches. This kernel replaces the previous, less efficient method of copying K and V caches separately, which was identified as a performance bottleneck.
  • Performance Optimization: The integration of this custom kernel is expected to yield significant performance improvements. Initial profiling indicates an approximate 3% end-to-end performance gain for the Llama 3.1 8B model on H100 GPUs, particularly for small batch sizes. For configurations with large Tensor Parallelism (TP), where KVCache per rank is smaller, even greater improvements are anticipated.
  • Robust Fallback Mechanism: To ensure robustness and compatibility, the new set_kv_buffer_kernel includes a fallback mechanism. If the optimized CUDA kernel cannot be utilized (e.g., due to specific hardware limitations, non-CUDA environments, or explicit configuration), the system will gracefully revert to the original PyTorch-based tensor assignment for K and V caches.
  • System Integration: The new kernel and its Python wrapper have been seamlessly integrated into the existing memory management system. This involves adding the CUDA source file to the build system, registering the C++ function as a PyTorch operator, and updating the memory_pool.py to call the new optimized kernel.
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 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 or fill out our survey 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.

@DarkSharpness DarkSharpness changed the title feat: support custom set kv buffer kernel [Feature] Support custom set kv buffer kernel Aug 7, 2025
Copy link
Copy Markdown
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

This pull request introduces a custom CUDA kernel, set_kv_buffer_kernel, to optimize the process of setting key-value caches. The new kernel fuses the memory copy operations for both key and value caches, which, according to the description, yields about a 3% end-to-end performance improvement on H100 GPUs for Llama 3.1 8B with small batch sizes. The changes span across Python, C++, and CMake files to integrate this new kernel.

My review focuses on the implementation of the new kernel and its integration. I've identified a couple of areas for improvement:

  • In the CUDA kernel implementation (store.cu), there's significant code duplication for handling different data types of the out_loc tensor. I've suggested a refactoring to improve maintainability.
  • The Python wrapper for the kernel (memory.py) has a broad exception handler that could silently swallow important errors. I've recommended adding logging to aid in debugging potential kernel failures.

Overall, the changes are well-structured and address a valid performance concern. The suggestions aim to make the new code more robust and easier to maintain.

@merrymercy merrymercy merged commit 86a0be6 into sgl-project:main Aug 12, 2025
33 of 56 checks passed
narutolhy pushed a commit to narutolhy/sglang that referenced this pull request Aug 17, 2025
MahmoudAshraf97 pushed a commit to MahmoudAshraf97/sglang that referenced this pull request Sep 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants