Migrate renorm kernels from sgl-kernel to FlashInfer JIT#18854
Migrate renorm kernels from sgl-kernel to FlashInfer JIT#18854Johnsonms wants to merge 7 commits intosgl-project:mainfrom
Conversation
Migrate top_k_renorm_probs, top_p_renorm_probs, and top_k_mask_logits from compiled sgl-kernel CUDA implementations to FlashInfer's JIT-compiled public API. This reduces the sgl-kernel wheel size and leverages FlashInfer's optimized and maintained implementations. Changes: - Use flashinfer.sampling public API instead of torch.ops.sgl_kernel - Remove internal _*_internal() functions (45 lines) - Remove CUDA kernel bindings from common_extension.cc and common_extension_musa.cc - Remove function declarations from sgl_kernel_ops.h - Remove renorm.cu from build (keep sampling.cu for other functions) - Add comprehensive benchmark suite (bench_renorm.py)
Summary of ChangesHello @Johnsonms, 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 refactors the core probability renormalization and logit masking functionalities within the sgl-kernel. By transitioning from custom CUDA implementations to FlashInfer's highly optimized and JIT-compiled API, the project benefits from reduced binary size, enhanced performance, and simplified maintenance. The changes streamline the codebase and integrate with a well-established, performant library for these critical operations, as evidenced by the detailed benchmarks provided. 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 top_k_renorm_probs, top_p_renorm_probs, and top_k_mask_logits kernels from the internal sgl-kernel CUDA implementations to FlashInfer's JIT-compiled API. This is a great move to reduce the wheel size and leverage a well-maintained, optimized library. The changes are clean and consistent across the codebase, including build files and C++/Python bindings.
The addition of a comprehensive benchmark suite in bench_renorm.py is excellent for verifying correctness and performance. I've provided a few suggestions to improve the reference implementations within the benchmark file to make them more robust and efficient, which will strengthen the benchmark's reliability.
- Reorder imports in sampling.py (flashinfer before torch) - Fix string quotes and formatting in bench_renorm.py
- Use torch.topk instead of sort+pivot for exact k-element selection - Vectorize all three reference implementations for better performance - Handle edge cases: ties, k=0, variable k per batch - More robust and realistic baseline for benchmarking Changes: - torch_top_k_renorm_probs: Vectorized batch operations - torch_top_p_renorm_probs: Vectorized cumsum and masking - torch_top_k_mask_logits: Vectorized scatter operations This makes the performance comparison more meaningful by using efficient PyTorch operations instead of slow Python loops.
|
|
|
Verification source code: |
This change enables both CUDA and MUSA platforms to use appropriate kernel implementations: - CUDA: Uses FlashInfer JIT (requires NVRTC) - MUSA: Uses compiled sgl_kernel ops (NVRTC not available) Changes: - Add platform detection in sampling.py with _internal helper functions - Restore MUSA torch op registrations for renorm kernels - Include renorm.cu in MUSA build sources - Move bench_renorm.py to jit_kernel/benchmark/ - Add comprehensive unit tests for all three renorm kernels The platform dispatch checks device.type and routes to the appropriate implementation, ensuring compatibility across hardware platforms.
|
/tag-and-rerun-ci |
Add sgl_kernel to known_first_party and flashinfer to known_third_party in .isort.cfg so local and CI isort agree on import ordering.
76440c8 to
e245749
Compare
|
/tag-and-rerun-ci |
Migrate top_k_renorm_probs, top_p_renorm_probs, and top_k_mask_logits from compiled sgl-kernel CUDA implementations to FlashInfer's JIT-compiled public API. This reduces the sgl-kernel wheel size and leverages FlashInfer's optimized and maintained implementations.
Changes:
Motivation
#17865
move (external) flashinfer/csrc/renorm.cu: https://github.com/flashinfer-ai/flashinfer/blob/bc29697ba20b7e6bdb728ded98f04788e16ee021/csrc/renorm.cu (≈4MB)
Modifications
Accuracy Tests
python -m pytest sgl-kernel/tests/test_sampling.py -sBenchmarking and Profiling
python benchmark/bench_renorm.pyCorrectness check

Benchmarking
Used the data mades sumary as following:

Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci