[Kernel Slimming] Migrate GPTQ-Marlin repack kernel to JIT#18543
[Kernel Slimming] Migrate GPTQ-Marlin repack kernel to JIT#18543BBuf merged 6 commits intosgl-project:mainfrom
Conversation
Summary of ChangesHello @celve, 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 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 GPTQ-Marlin repack kernel to a Just-In-Time (JIT) compiled version. The changes are well-structured, introducing new files for the JIT kernel, its Python wrapper, comprehensive tests, and benchmarks. The existing codebase is cleanly updated to utilize the new JIT kernel. The tests are particularly robust, verifying the JIT kernel's output against both a CPU reference and the original Ahead-Of-Time (AOT) compiled kernel, which ensures correctness. I've found one minor issue in the benchmark script that I've commented on. Overall, this is a high-quality contribution that improves kernel management flexibility.
| raise ValueError(f"Unknown provider: {provider}") | ||
|
|
||
| ms, min_ms, max_ms = triton.testing.do_bench_cudagraph(fn, quantiles=quantiles) | ||
| return 1000 * ms, 1000 * max_ms, 1000 * min_ms |
There was a problem hiding this comment.
The return values for y_min and y_max appear to be swapped. The triton.testing.do_bench_cudagraph function with quantiles=[0.5, 0.2, 0.8] returns (median, p20, p80). The perf_report decorator expects a tuple of (y, y_min, y_max). Consequently, min_ms (the 20th percentile) should be y_min, and max_ms (the 80th percentile) should be y_max. The current implementation will lead to inverted error bars in the benchmark plot.
| return 1000 * ms, 1000 * max_ms, 1000 * min_ms | |
| return 1000 * ms, 1000 * min_ms, 1000 * max_ms |
|
Test MMLU with following scripts: JIT: AOT: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0183bd0448
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| // Get ptrs | ||
| uint32_t const* b_q_weight_ptr = reinterpret_cast<uint32_t const*>(b_q_weight.data_ptr()); | ||
| uint32_t const* perm_ptr = reinterpret_cast<uint32_t const*>(perm.data_ptr()); |
There was a problem hiding this comment.
Validate perm tensor before launching repack kernel
The JIT port no longer validates perm (dtype, contiguity, or CUDA placement) before reinterpreting it as uint32_t*; when has_perm is true and callers pass common torch.argsort output (int64) or a CPU/non-contiguous tensor, the kernel will read wrong indices or invalid memory, producing corrupted repacked weights or a device fault. The previous AOT implementation explicitly rejected these inputs, so this is a regression in input safety for act-order repack paths.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pull request overview
Migrates the GPTQ-Marlin repack CUDA kernel from the AOT sgl_kernel extension to the sglang.jit_kernel JIT compilation path, and updates quantization callsites to use the new JIT implementation to support the kernel wheel slimming plan.
Changes:
- Add a JIT-compiled
gptq_marlin_repackCUDA kernel and Python wrapper undersglang.jit_kernel. - Update GPTQ/Marlin quantization codepaths to import
gptq_marlin_repackfromsglang.jit_kernelinstead ofsgl_kernel. - Add a unit test and a Triton-based benchmark for the new JIT repack kernel.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| python/sglang/srt/layers/quantization/marlin_utils_fp8.py | Switch repack import to the JIT kernel module. |
| python/sglang/srt/layers/quantization/gptq.py | Switch repack import to the JIT kernel module. |
| python/sglang/srt/layers/quantization/compressed_tensors/schemes/compressed_tensors_wNa16.py | Switch repack import to the JIT kernel module. |
| python/sglang/jit_kernel/gptq_marlin_repack.py | New Python wrapper that JIT-loads the kernel and allocates the output tensor. |
| python/sglang/jit_kernel/csrc/gemm/marlin/gptq_marlin_repack.cuh | New JIT CUDA implementation and host entrypoint for GPTQ-Marlin repack. |
| python/sglang/jit_kernel/tests/test_gptq_marlin_repack.py | New unit test covering correctness of the JIT repack output. |
| python/sglang/jit_kernel/benchmark/bench_gptq_marlin_repack.py | New benchmark comparing JIT vs AOT when available. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| bqw_dim1.set_value(size_n); | ||
| auto device_ = SymbolicDevice{}; | ||
| device_.set_options<kDLCUDA>(); | ||
| TensorMatcher({bqw_dim0, bqw_dim1}).with_dtype<int32_t>().with_device(device_).verify(b_q_weight); |
There was a problem hiding this comment.
The kernel assumes b_q_weight is contiguous row-major (it does b_q_weight_ptr[row * size_n + col]), but the current TensorMatcher checks only shape/dtype/device. Please also enforce expected strides/contiguity for b_q_weight to prevent incorrect results when a non-contiguous view is passed.
| TensorMatcher({bqw_dim0, bqw_dim1}).with_dtype<int32_t>().with_device(device_).verify(b_q_weight); | |
| TensorMatcher({bqw_dim0, bqw_dim1}) | |
| .with_dtype<int32_t>() | |
| .with_device(device_) | |
| .with_contiguous() | |
| .verify(b_q_weight); |
| out_dim0.set_value(size_k / device::marlin::tile_size); | ||
| out_dim1.set_value(size_n * device::marlin::tile_size / pack_factor); | ||
| TensorMatcher({out_dim0, out_dim1}).with_dtype<int32_t>().with_device(device_).verify(out); | ||
|
|
There was a problem hiding this comment.
Similarly, out is written via raw pointer arithmetic assuming a contiguous row-major layout. Consider enforcing expected strides/contiguity for out (or requiring contiguous) in the validation to avoid silent miswrites if a non-contiguous tensor is ever passed in.
| // Enforce that `out` is laid out contiguously in row-major order. | |
| // The kernel writes using raw pointer arithmetic assuming: | |
| // stride(1) == 1 | |
| // stride(0) == out_dim1 (i.e., contiguous rows) | |
| auto out_stride0 = out.stride(0); | |
| auto out_stride1 = out.stride(1); | |
| RuntimeCheck( | |
| out_stride1 == 1 && out_stride0 == out_dim1.value(), | |
| "Expected `out` to be a contiguous row-major tensor with shape (", | |
| out_dim0.value(), | |
| ", ", | |
| out_dim1.value(), | |
| "), but got strides (", | |
| out_stride0, | |
| ", ", | |
| out_stride1, | |
| ")."); |
| int64_t size_k, | ||
| int64_t size_n, | ||
| int64_t num_bits) { |
There was a problem hiding this comment.
This function takes size_k/size_n as int64_t, but the CUDA kernel parameters are typed as int. Add an explicit bounds check (or change the kernel signature) to prevent silent truncation for large sizes, which could lead to out-of-bounds accesses.
| import pytest | ||
| import torch | ||
| from sgl_kernel import gptq_marlin_repack as aot_gptq_marlin_repack | ||
| from sgl_kernel.scalar_type import scalar_types |
There was a problem hiding this comment.
The test imports sgl_kernel unconditionally for the AOT reference, so it will fail with ImportError when sgl_kernel isn’t installed. Consider try/except ImportError and skipping only the JIT-vs-AOT bitwise comparison when AOT isn’t available (the CPU reference check can still run).
| // Detect if there is act_order | ||
| bool has_perm = perm.size(0) != 0; | ||
|
|
||
| // Get ptrs | ||
| uint32_t const* b_q_weight_ptr = reinterpret_cast<uint32_t const*>(b_q_weight.data_ptr()); |
There was a problem hiding this comment.
has_perm is inferred from perm.size(0), but when it’s non-empty the code never verifies perm’s device/dtype/contiguity or that its length matches size_k. If a wrong tensor is passed, the kernel can read invalid memory. Add TensorMatcher/RuntimeChecks for perm and enforce perm.size(0) == 0 || perm.size(0) == size_k before launch.
|
Fix lint |
Done |
|
/rerun-failed-ci |
|
/rerun-failed-ci |
|
/tag-and-rerun-ci |
…ct#18543) Co-authored-by: Xiaoyu Zhang <35585791+BBuf@users.noreply.github.com>
Motivation
See #17865
Modifications
New files:
sgl-kernel/csrc/gemm/marlin/gptq_marlin_repack.cu
Modified files:
Accuracy Tests
Pass all tests defined in python/sglang/jit_kernel/tests/test_gptq_marlin_repack.py
Benchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci