Skip to content

dsa: widen the fp8 k-cache quant kernel's token_id to int64 - #30859

Merged
Fridge003 merged 8 commits into
sgl-project:mainfrom
JustinTong0323:xinyuan/quant-int64-up
Aug 26, 2026
Merged

Fridge003 merged 8 commits into
sgl-project:mainfrom
JustinTong0323:xinyuan/quant-int64-up

Conversation

@JustinTong0323

@JustinTong0323 JustinTong0323 commented Jul 11, 2026 •

Copy link
Copy Markdown
Collaborator

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.py
  • python/sglang/kernels/ops/attention/dsa/dequant_k_cache.py
  • python/sglang/kernels/ops/attention/dsv4/quant_k_cache.py

Why

token_id scales pointer offsets into the k/scale buffers, for example k_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

  • Changed-file pre-commit, py_compile, and diff-check pass.
  • Focused H200 DSA/DSV4 tests: 34 passed, 18 skipped, 76 subtests passed.
  • DSA fast quant/dequant matches the reference bitwise on a focused GPU parity check.
  • Post-rebase 4xH200 TP4 + EAGLE full GSM8K with the non-fused store-cache path forced: 96.66% accuracy, 100.00% stop rate, 0.00% truncated, 0.00% request errors (1319 examples).

CI States

Latest PR Test (Base): ✅ Run #30063972255
Latest PR Test (Extra): ❌ Run #30063972097

@JustinTong0323 JustinTong0323 added the run-ci CI: run the baseline test suite on this PR label Jul 11, 2026
@github-actions github-actions Bot added the quant LLM Quantization label Jul 11, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

@JustinTong0323

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@ormandj

ormandj commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Tested commit 67a283824f in the current-main TP2 SM120 stack through server
startup, CUDA graph capture, cold 64K prefill, C1 decode, and C32 decode. Two
20-question GSM8K smoke runs scored 20/20 and 19/20. These are correctness smoke
tests only; no quality or performance claim is based on them.

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.
@JustinTong0323
JustinTong0323 force-pushed the xinyuan/quant-int64-up branch from d78b76f to 088252f Compare July 15, 2026 09:43
@JustinTong0323

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

JustinTong0323 and others added 2 commits July 16, 2026 11:43
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.
@JustinTong0323

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@ShangmingCai ShangmingCai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@Fridge003
Fridge003 merged commit a8d716c into sgl-project:main Aug 26, 2026
143 of 150 checks passed
henrylhtsang added a commit to henrylhtsang/sglang that referenced this pull request Sep 2, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel quant LLM Quantization run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants