From 012cd3858b5f1a57e32cfc78ba3f787ee7de8786 Mon Sep 17 00:00:00 2001 From: "Yi (Vincent) Zhong" <207368749+vincentzed@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:04:59 -0400 Subject: [PATCH] [Fix] Make the fused KDA beta sigmoid bit-identical to torch.sigmoid (#42611) Co-authored-by: Claude Opus 5.5 --- .../sglang/kernels/ops/attention/fla/kda.py | 5 +++- .../attention/test_kda_gate_beta_cumsum.py | 27 ++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/python/sglang/kernels/ops/attention/fla/kda.py b/python/sglang/kernels/ops/attention/fla/kda.py index 3b877ec675..05dee2f2ab 100644 --- a/python/sglang/kernels/ops/attention/fla/kda.py +++ b/python/sglang/kernels/ops/attention/fla/kda.py @@ -11,6 +11,7 @@ from typing import Optional import torch import triton import triton.language as tl +from triton.language.extra import libdevice from sglang.kernels.ops.attention.fla.chunk_delta_h import chunk_gated_delta_rule_fwd_h from sglang.kernels.ops.attention.fla.chunk_intra import chunk_kda_fwd_intra @@ -1030,9 +1031,11 @@ def kda_gate_chunk_cumsum_vector_kernel( mask=offsets_t < T, other=0.0, ).to(tl.float32) + # Not tl.sigmoid: tl.exp and Triton's / are approximate on CUDA. This + # matches the unfused path's beta.float().sigmoid() bit for bit. tl.store( beta_out + (bos + offsets_t) * H + i_h, - tl.sigmoid(b_beta), + tl.div_rn(1.0, 1.0 + libdevice.exp(-b_beta)), mask=offsets_t < T, ) diff --git a/test/registered/kernels/ops/attention/test_kda_gate_beta_cumsum.py b/test/registered/kernels/ops/attention/test_kda_gate_beta_cumsum.py index 60dc2b3879..e19f1b28fa 100644 --- a/test/registered/kernels/ops/attention/test_kda_gate_beta_cumsum.py +++ b/test/registered/kernels/ops/attention/test_kda_gate_beta_cumsum.py @@ -54,10 +54,31 @@ class TestKDAGateBetaCumsum(unittest.TestCase): actual_gate, expected_gate, atol=1e-4, rtol=1e-6 ) torch.testing.assert_close( - actual_beta, beta.float().sigmoid(), atol=1.2e-7, rtol=1e-6 + actual_beta, beta.float().sigmoid(), atol=0, rtol=0 ) self.assertEqual(actual_beta.dtype, torch.float32) + @torch.inference_mode() + def test_gate_cumsum_beta_is_bit_identical_to_torch_sigmoid(self): + heads, dim = 2, 128 + bits = torch.arange(1 << 16, device="cuda").to(torch.int16) + beta = bits.view(torch.bfloat16) + beta = beta[beta.isfinite()].view(1, -1, heads) + tokens = beta.shape[1] + gate = torch.zeros(1, tokens, heads, dim, device="cuda", dtype=torch.bfloat16) + a_log = torch.zeros(heads, device="cuda") + for cu_seqlens in ( + None, + torch.tensor([0, tokens], device="cuda", dtype=torch.int32), + ): + with self.subTest(varlen=cu_seqlens is not None): + _, actual = kda_gate_chunk_cumsum( + gate, A_log=a_log, chunk_size=64, cu_seqlens=cu_seqlens, beta=beta + ) + torch.testing.assert_close( + actual, beta.float().sigmoid(), atol=0, rtol=0 + ) + @torch.inference_mode() def test_chunk_raw_beta_matches_activated_beta_and_final_state(self): torch.manual_seed(17) @@ -120,9 +141,9 @@ class TestKDAGateBetaCumsum(unittest.TestCase): initial_state=actual_state, **kwargs, ) - torch.testing.assert_close(actual, expected, atol=2e-3, rtol=2e-3) + torch.testing.assert_close(actual, expected, atol=0, rtol=0) torch.testing.assert_close( - actual_state, expected_state, atol=2e-4, rtol=2e-3 + actual_state, expected_state, atol=0, rtol=0 )