[Feature] JIT Fused QK norm + qk norm clean up#15835
Conversation
Summary of ChangesHello @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! This pull request significantly enhances the performance of Query-Key (QK) normalization within the system by introducing a JIT fused kernel. This optimization addresses inefficiencies in the existing QK norm kernel, particularly for smaller batch sizes, and streamlines the codebase by refactoring common normalization logic into a reusable utility. The changes are expected to lead to faster prefill stages and improved GPU utilization. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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 by creating a comment using either
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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a JIT fused QK norm kernel, which significantly improves performance, especially for smaller batch sizes, as demonstrated by the provided benchmarks. The refactoring of the _apply_qk_norm logic into a shared utility function (sglang.srt.models.utils.apply_qk_norm) is a good step towards reducing redundant code and improving maintainability across different model implementations. The new CUDA C++ kernels and their Python wrappers are well-integrated. However, there is a critical issue identified in the CUDA kernel's pointer arithmetic for the k tensor, which could lead to memory access violations.
|
/rerun-failed-ci |
|
local precision test passed cc @BBuf |
|
/tag-and-rerun-ci |
8c29140 to
c796f52
Compare
BBuf
left a comment
There was a problem hiding this comment.
It seems that the kUsePDL template parameter in the JIT kernel doesn't automatically enable or disable itself based on GPU architecture?
| // NOTE: we offset the k here to reduce computation cost in the kernel | ||
| const auto params = QKNormParams{ | ||
| .q = q.data_ptr(), | ||
| .k = pointer::offset(k.data_ptr(), -2 * static_cast<int64_t>(num_qo_heads) * kHeadDim), |
There was a problem hiding this comment.
Can you add a comment for this line, I can't easily understand now.
| N_K = 2 | ||
| N_Q = 16 | ||
| DEVICE = "cuda" | ||
| DTYPE = torch.bfloat16 |
There was a problem hiding this comment.
Add a torch.float16 is better
|
/tag-and-rerun-ci |
|
Add a end2end model acc test? |
|
Merged with ci passed. https://github.com/sgl-project/sglang/actions/runs/20523661737/job/59001412891?pr=15835 |
| q_weight: torch.Tensor, | ||
| k_weight: torch.Tensor, | ||
| ) -> None: | ||
| from sgl_kernel import rmsnorm |
| q = q.view(-1, head_dim) | ||
| k = k.view(-1, head_dim) | ||
|
|
||
| current_stream = torch.cuda.current_stream() |
There was a problem hiding this comment.
Sometimes i found this function has a large CPU overhead. I use
sglang/python/sglang/srt/utils/common.py
Lines 3730 to 3735 in d90f9bf
|
|
||
| @cache_once | ||
| def can_use_fused_inplace_qknorm(head_dim: int) -> bool: | ||
| logger = logging.getLogger(__name__) |
There was a problem hiding this comment.
qq: could we move this line to the top of this file?
There was a problem hiding this comment.
no problem. it's just my personal preference to lazy init
Motivation
_apply_qk_norm.Modifications
_apply_qk_norm. Move them tosglang/srt/models/utils.py.Note: since our JIT kernel only support head_dim
[64,128,256], this can also be moved into sgl-kernel in the future without siginificantly increase binary size.Accuracy Tests
WIP.
Benchmarking and Profiling
E2E around 1~2% on Qwen3 models.
Kernel Benchmark Latency (us) on H200 (head_dim=128)
Checklist