From 133c5f68081876ef117a902a86e7774d406cf407 Mon Sep 17 00:00:00 2001 From: "shuyan.ycf" Date: Fri, 13 Nov 2026 15:55:57 +0800 Subject: [PATCH] fix(kda): guard recompute beta tail loads Signed-off-by: shuyan.ycf --- cula/ops/kda/sm100/recompute_wu.py | 9 +++---- docs/kda_sm100_cutedsl_fwd_status.md | 1 + tests/test_kda_sm100_recompute_wu_cutedsl.py | 27 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/cula/ops/kda/sm100/recompute_wu.py b/cula/ops/kda/sm100/recompute_wu.py index a0c5c433..84005c08 100644 --- a/cula/ops/kda/sm100/recompute_wu.py +++ b/cula/ops/kda/sm100/recompute_wu.py @@ -776,11 +776,10 @@ def kernel( ), cute.make_layout((self.BT,), stride=(H_v,)), ) - sBeta[(aux_tidx, beta_h.index)] = cutlass.select_( - aux_tidx < remaining, - beta_gmem[aux_tidx].to(self.acc_dtype), - self.acc_dtype(0.0), - ) + value = self.acc_dtype(0.0) + if aux_tidx < remaining: + value = beta_gmem[aux_tidx].to(self.acc_dtype) + sBeta[(aux_tidx, beta_h.index)] = value cute.arch.fence_proxy("async.shared", space="cta") beta_h.commit() diff --git a/docs/kda_sm100_cutedsl_fwd_status.md b/docs/kda_sm100_cutedsl_fwd_status.md index 4326c178..0386653e 100644 --- a/docs/kda_sm100_cutedsl_fwd_status.md +++ b/docs/kda_sm100_cutedsl_fwd_status.md @@ -118,6 +118,7 @@ Final pure-path results: - frozen former-csrc bitwise vectors; - FP64 mathematical oracle bounds; - repeated-call and CUDA Graph determinism; +- Compute Sanitizer coverage for guarded beta loads in final uneven sequence tails; - default-stream and concurrent-stream isolation; - output lifetime, pool reuse, and bounded retained memory; - fixed, GVA, QG, packed-varlen, and tail routing; diff --git a/tests/test_kda_sm100_recompute_wu_cutedsl.py b/tests/test_kda_sm100_recompute_wu_cutedsl.py index eac5302b..1ebe5ea7 100644 --- a/tests/test_kda_sm100_recompute_wu_cutedsl.py +++ b/tests/test_kda_sm100_recompute_wu_cutedsl.py @@ -1,6 +1,8 @@ # Copyright 2025-2026 Ant Group Co., Ltd. # SPDX-License-Identifier: Apache-2.0 +import os + import pytest import torch @@ -32,6 +34,31 @@ def test_recompute_wu_repeated_calls_are_bitwise_deterministic(beta_dtype): assert torch.equal(expected, actual) +@pytest.mark.sanitizer +@pytest.mark.sm100_only +@pytest.mark.skipif( + os.environ.get("CULA_RUN_SANITIZER_TESTS") != "1", + reason="set CULA_RUN_SANITIZER_TESTS=1 and run under compute-sanitizer", +) +@pytest.mark.parametrize("beta_dtype", [torch.float32, torch.bfloat16]) +def test_recompute_wu_uneven_tail_beta_load_is_in_bounds(beta_dtype): + _requires_sm100() + torch.manual_seed(7) + batch, seqlen, heads, dim, chunk_size = 1, 65, 1, 128, 64 + k = torch.randn(batch, seqlen, heads, dim, device="cuda", dtype=torch.bfloat16) * 0.1 + v = torch.randn_like(k) * 0.1 + beta = torch.rand(batch, seqlen, heads, device="cuda", dtype=beta_dtype) + gk = torch.randn(batch, seqlen, heads, dim, device="cuda", dtype=torch.float32) * 0.02 + A = torch.randn(batch, seqlen, heads, chunk_size, device="cuda", dtype=torch.bfloat16) * 0.02 + + w, u, qg, kg = recompute_w_u_fwd(k, v, beta, A, gk) + torch.cuda.synchronize() + + assert qg is None + for output in (w, u, kg): + assert torch.isfinite(output).all() + + @pytest.mark.cula_fast def test_preprocessed_recompute_wu_matches_torch(): _requires_sm100()