Conversation
Summary of ChangesHello, 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 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. Changelog
Activity
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 successfully migrates the per_token_quant_fp8 function to a JIT-compiled CUDA kernel, which is a great step for kernel slimming. The new implementation is well-structured with a dedicated CUDA kernel, a Python wrapper, comprehensive unit tests, and benchmarks. The code is clean and the approach is solid. I have a couple of suggestions to enhance the benchmark accuracy and further optimize the CUDA kernel's performance.
eefde07 to
1e2a37c
Compare
| out_arr[j] = static_cast<fp8_e4m3_t>(val); | ||
| } | ||
|
|
||
| if constexpr (kVecSize == 16) { |
There was a problem hiding this comment.
Why use reinterpret_cast here? Why not AlignedVector?
There was a problem hiding this comment.
Done. changed to AlignedVector
| constexpr int kBlockSize = 256; | ||
| constexpr int kMaxVecSize = 32 / sizeof(DType); | ||
|
|
||
| if (hidden_dim % kMaxVecSize == 0) { |
There was a problem hiding this comment.
Can we use some utility function to avoid passing the arguments same many times? The only difference here is the kernel.
e.g. use a local lambda functon which returns a pointer to the real kernel
There was a problem hiding this comment.
Done. Extracted common logic into a lambda.
| RuntimeCheck(hidden_dim % 4 == 0, "per_token_quant_fp8: hidden_dim must be divisible by 4, got ", hidden_dim); | ||
|
|
||
| constexpr int kBlockSize = 256; | ||
| constexpr int kMaxVecSize = 32 / sizeof(DType); |
There was a problem hiding this comment.
Why 32? Does this assume a Blackwell architecture?
There was a problem hiding this comment.
32 bytes here matches the AOT original version. In per_token_quant_fp8.cu L18, the default template parameter is kVecSize = 16. For float16 (2 bytes per element), that's 16 * 2 = 32 bytes per vector load.
The formula 32 / sizeof(DType) generalizes this across dtypes (e.g., float32 gives 32 / 4 = 8, also matching the AOT use_vec8 path).
From my understanding, 32-byte vector loads work correctly on all architectures. On pre-Blackwell GPUs the compiler splits them into two 16-byte load instructions. If #19794 gets merged first, I can update this to use device::kMaxVecBytes introduced by it directly.
There was a problem hiding this comment.
Let's wait for #19794. You may profile the performance first, just ensure changing from 32 -> 16 should not bring no performance regression.
There was a problem hiding this comment.
posted benchmark result in #19702 (comment)
There was a problem hiding this comment.
32 bytes here matches the AOT original version. In per_token_quant_fp8.cu L18, the default template parameter is
kVecSize = 16. Forfloat16(2 bytes per element), that's16 * 2 = 32bytes per vector load.The formula
32 / sizeof(DType)generalizes this across dtypes (e.g.,float32gives32 / 4 = 8, also matching the AOTuse_vec8path).From my understanding, 32-byte vector loads work correctly on all architectures. On pre-Blackwell GPUs the compiler splits them into two 16-byte load instructions. If #19794 gets merged first, I can update this to use
device::kMaxVecBytesintroduced by it directly.
@xingsy97 Yes. I think using device::kMaxVecBytes is better.
|
Regression benchmark: 32-byte -> 16-byte vector loads on Blackwell Conclusion: 32-byte -> 16-byte loads causes 3~22% regression on Blackwell, confirming that 32-byte loads should be kept. Once #19794 merged, this kernel will use |
2661a92 to
47165a1
Compare
|
Hi @DarkSharpness , I made some improvements to this PR (rebased to latest main branch to include #19794) Changes
Benchmark & TestAll unit tests pass. No regression. JIT matches or slightly outperforms AOT across all configs. |
|
Thanks @xingsy97! The JIT tree was relocated from |
Motivation
Migrate
per_token_quant_fp8kernel to JIT compilation (#17865).Modifications
Under
python/sglang/jit_kernel/:csrc/gemm/per_token_quant_fp8.cuh— CUDA kernel (ported fromsgl-kernel/csrc/gemm/per_token_quant_fp8.cu)per_token_quant_fp8.py— Python wrappertests/test_per_token_quant_fp8.py— Unit testsbenchmark/bench_per_token_quant_fp8.py— Benchmark (JIT vs AOT vs PyTorch)And
python/sglang/srt/layers/quantization/fp8_kernel.py— Switch import to JITAccuracy Tests
Pass all tests defined in
python/sglang/jit_kernel/tests/test_per_token_quant_fp8.pyBenchmarking and Profiling
JIT and AOT performance are essentially the same, no regression observed. Both are significantly faster than PyTorch.
Checklist