From 49766a74f9de2b5cdd3c11269671329f3d716ac2 Mon Sep 17 00:00:00 2001 From: Jiuqiang Tang <3359144+jiuqiant@users.noreply.github.com> Date: Mon, 19 Oct 2026 22:12:12 +0000 Subject: [PATCH] [Fix] attn_residual: int64 program ids in the score/combine kernels (int32 overflow at 37,451 tokens) (#43438) --- .../kernels/ops/attention/attn_res_hip.py | 2 +- python/sglang/srt/layers/attn_residual.py | 4 +- .../test_attn_res_mix_large_tokens.py | 46 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 test/registered/kernels/ops/attention/test_attn_res_mix_large_tokens.py diff --git a/python/sglang/kernels/ops/attention/attn_res_hip.py b/python/sglang/kernels/ops/attention/attn_res_hip.py index 7b6c8d470c..82b52c4fc1 100644 --- a/python/sglang/kernels/ops/attention/attn_res_hip.py +++ b/python/sglang/kernels/ops/attention/attn_res_hip.py @@ -83,7 +83,7 @@ def _agg_kernel( Taking the global max before exponentiating makes this bit-comparable to the 2-kernel pipeline rather than to the SM100 kernel. """ - t = tl.program_id(0) + t = tl.program_id(0).to(tl.int64) # t * stride_bm overflows int32 offs = tl.arange(0, BLOCK_H) mask = offs < H diff --git a/python/sglang/srt/layers/attn_residual.py b/python/sglang/srt/layers/attn_residual.py index afad3e58b6..1a14c20505 100644 --- a/python/sglang/srt/layers/attn_residual.py +++ b/python/sglang/srt/layers/attn_residual.py @@ -133,7 +133,7 @@ def _score_kernel( BLOCK_H: tl.constexpr, ): """One CTA per (token, row): scan H, output one scalar score.""" - pid_t = tl.program_id(0) + pid_t = tl.program_id(0).to(tl.int64) # pid_t * stride_bm overflows int32 j = tl.program_id(1) if j > NVB: return @@ -174,7 +174,7 @@ def _combine_kernel( Softmax is redundantly computed by each H-chunk CTA (≤16 elements, trivial). This gives full H-parallelism: 7 CTAs for H=7168/1024. """ - pid_t = tl.program_id(0) + pid_t = tl.program_id(0).to(tl.int64) # pid_t * stride_bm overflows int32 pid_h = tl.program_id(1) h0 = pid_h * BLOCK_H diff --git a/test/registered/kernels/ops/attention/test_attn_res_mix_large_tokens.py b/test/registered/kernels/ops/attention/test_attn_res_mix_large_tokens.py new file mode 100644 index 0000000000..46f92316b7 --- /dev/null +++ b/test/registered/kernels/ops/attention/test_attn_res_mix_large_tokens.py @@ -0,0 +1,46 @@ +"""The attention-residual score/combine kernels index the [T, 8, 7168] bank with +pid_t * stride_bm, which overflows int32 at 37,451 rows.""" + +import unittest +from types import SimpleNamespace + +import torch + +from sglang.srt.layers.attn_residual import _mix_fused +from sglang.test.ci.ci_register import register_cuda_ci +from sglang.test.test_utils import CustomTestCase + +register_cuda_ci(est_time=30, stage="base-b-kernel-unit", runner_config="1-gpu-large") + +_H, _NB, _NVB = 7168, 8, 7 +_T = 2**31 // (_NB * _H) + 2 # 37,451: the first row count whose offset overflows + + +class TestAttnResMixLargeTokens(CustomTestCase): + @classmethod + def setUpClass(cls): + if not torch.cuda.is_available(): + raise unittest.SkipTest("CUDA is not available") + + def test_rows_past_int32_offset(self): + g = torch.Generator(device="cuda").manual_seed(0) + prefix = torch.randn(_T, _H, generator=g, device="cuda").bfloat16() + bank = torch.randn(_T, _NB, _H, generator=g, device="cuda").bfloat16() + proj = SimpleNamespace(weight=torch.randn(1, _H, device="cuda") * _H**-0.5) + norm = SimpleNamespace( + weight=1 + 0.1 * torch.randn(_H, device="cuda"), variance_epsilon=1e-6 + ) + + out = _mix_fused(prefix, bank, _NVB, proj, norm) + torch.cuda.synchronize() + + # Rows are independent, so the tail must match a run on the tail alone, + # whose offsets fit in int32. + n = 4096 + tail = _mix_fused(prefix[-n:], bank[-n:], _NVB, proj, norm) + self.assertTrue(torch.isfinite(out).all()) + self.assertTrue(torch.equal(out[-n:], tail)) + + +if __name__ == "__main__": + unittest.main()