Repository navigation
dsa: widen the fp8 k-cache quant kernel's token_id to int64 - #30859
Conversation
There was a problem hiding this comment.
Code Review
This pull request casts token_id to tl.int64 in the _quantize_k_cache_fast_kernel to prevent integer overflow. The reviewer recommends applying this same fix to other Triton kernels in dequant_k_cache.py and dsv4/quant_k_cache.py to ensure consistency and prevent similar overflow issues.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| @@ -283,7 +283,7 @@ def _quantize_k_cache_fast_kernel( | |||
| FP8_MIN: tl.constexpr, | |||
| FP8_MAX: tl.constexpr, | |||
| ): | |||
| token_id = tl.program_id(0) | |||
| token_id = tl.program_id(0).to(tl.int64) | |||
There was a problem hiding this comment.
While this change correctly prevents integer overflow in the quantization kernel when the token count exceeds the int32 range, there are other Triton kernels in the codebase that suffer from the exact same issue and should be updated for consistency and correctness:\n\n1. python/sglang/srt/layers/attention/dsa/dequant_k_cache.py:\n - In _dequantize_k_cache_fast_kernel (line 135), token_id = tl.program_id(0) should be cast to tl.int64 to prevent overflow in pointer arithmetic like token_id * input_nope_q_stride_0.\n\n2. python/sglang/srt/layers/attention/dsv4/quant_k_cache.py:\n - In _quant_k_cache_fused_kernel (line 29), token_id = tl.program_id(0) should also be cast to tl.int64 to prevent overflow in pointer arithmetic like token_id * k_bf16_stride_0 at scale.\n\nApplying the same hardening to these files will ensure full correctness across all FP8 KV cache quantization/dequantization paths.
|
/rerun-failed-ci |
|
Tested commit |
67a2838 to
d78b76f
Compare
In _quantize_k_cache_fast_kernel, token_id = tl.program_id(0) is int32 by default, but it scales every pointer offset into the k/scale buffers (k_nope_ptr + token_id * k_nope_stride_0, and the output/scale pointers). On a large KV cache where the token index exceeds the int32 range, those offsets wrap and the kernel reads/writes the wrong memory. Cast to int64 so the pointer arithmetic holds at scale.
Use int64 token offsets in the matching DSA dequant and DSV4 quant kernels so large cache rows cannot overflow int32 pointer arithmetic.
d78b76f to
088252f
Compare
|
/rerun-failed-ci |
Same int32-overflow vector as the quant side: token_id * output_stride_0 and token_id_paged * pool strides are int32 multiplies that wrap on large pools before widening. Cover the paged dequant kernel and the dsv4 dequant kernel so the whole fp8 k-cache quant/dequant family is int64.
|
/rerun-failed-ci |
…n' into xinyuan/quant-int64-up
The DSA index-pool gather kernels compute source addresses as page_index * buf_numel_per_page with page_index loaded from an int32 page table. The product overflows int32 once the pool exceeds 2^31 elements (page 254,203 with 8,448-element pages), producing a wild address and an illegal memory access at runtime. Same class of bug as sgl-project#30859; widen the _GetK/_GetS/_GetKAndS triton kernels used by every DSA index-K/S gather on the non-aiter path.
What
Widen
token_id = tl.program_id(0)to int64 in the three FP8 k-cache kernels that use it to scale pointer offsets:python/sglang/kernels/ops/attention/dsa/quant_k_cache.pypython/sglang/kernels/ops/attention/dsa/dequant_k_cache.pypython/sglang/kernels/ops/attention/dsv4/quant_k_cache.pyWhy
token_idscales pointer offsets into the k/scale buffers, for examplek_nope_ptr + token_id * k_nope_stride_0. Leaving the program id at the default int32 lets the multiplication wrap after the offset exceeds 2^31. With a typical stride of 512, that can happen at about 4.2M tokens. Casting the program id to int64 keeps the pointer arithmetic correct at large offsets.Scope
Three one-line numerical-correctness hardenings. The surrounding kernel bodies and kernel interfaces are unchanged.
Verification
py_compile, and diff-check pass.CI States
Latest PR Test (Base): ✅ Run #30063972255
Latest PR Test (Extra): ❌ Run #30063972097