From dd7356afc6889ba96a43f2624d84d2b8ad3b71ac Mon Sep 17 00:00:00 2001 From: biondizzle Date: Sun, 24 May 2026 03:29:14 +0000 Subject: [PATCH] D1: Simplified debug test --- tests/unit/test_d1_hd128_debug.py | 42 +++++++++++-------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/tests/unit/test_d1_hd128_debug.py b/tests/unit/test_d1_hd128_debug.py index 37e91870..f3ae008a 100644 --- a/tests/unit/test_d1_hd128_debug.py +++ b/tests/unit/test_d1_hd128_debug.py @@ -1,4 +1,4 @@ -"""D1: Debug hd=128 — check if the QK output is correct.""" +"""D1: Debug hd=128 — check if the pipeline works with TMEM-P.""" import torch, math import cutlass.cute as cute import cutlass.torch as ct @@ -6,16 +6,13 @@ import cuda.bindings.driver as cuda from dsv4.kernels.attention.fmha import FmhaKernel -def test_hd128_debug(): - hd = 128 - n_kv = 128 +def test_hd(hd, n_kv=128, use_smem_p=False): m = 128 torch.manual_seed(42) q = torch.randn(m, hd, 1, dtype=torch.bfloat16, device='cuda') k = torch.randn(n_kv, hd, 1, dtype=torch.bfloat16, device='cuda') v = torch.randn(n_kv, hd, dtype=torch.bfloat16, device='cuda') - # Reference: just the QK @ V attention (un-normalized) qf = q[:, :, 0].float() kf = k[:, :, 0].float() scale = 1.0 / math.sqrt(hd) @@ -26,17 +23,11 @@ def test_hd128_debug(): ref_unnorm = attn_exp @ v.float() ref_lse = (torch.log(attn_sum.squeeze(-1)) + attn_max.squeeze(-1))[0].item() - # Run kernel with TMEM-P (force) lse_tensor = torch.zeros(m, 1, 1, dtype=torch.float32, device='cuda') - kernel = FmhaKernel(head_dim=hd, s_k=n_kv, use_smem_p=False) + kernel = FmhaKernel(head_dim=hd, s_k=n_kv, use_smem_p=use_smem_p) pv_n_tile = kernel.pv_n_tile - print(f'pv_n_tile={pv_n_tile}, n_pv_tiles={kernel.n_pv_tiles}') - print(f'tmem_o0_offset={kernel.tmem_o0_offset}, tmem_p0_offset={kernel.tmem_p0_offset}') - print(f'tOrP0_offset={kernel.tOrP0_offset}') - print(f'num_tmem_alloc_cols={kernel.num_tmem_alloc_cols}') - print(f'scale_softmax={kernel.scale_softmax}, scale_softmax_log2={kernel.scale_softmax_log2}') - stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream) + v_tile = v[:, 0:pv_n_tile].contiguous().unsqueeze(-1) c_tile = torch.zeros(m, pv_n_tile, 1, dtype=torch.bfloat16, device='cuda') mQ = ct.from_dlpack(q).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q)) @@ -45,6 +36,8 @@ def test_hd128_debug(): mC = ct.from_dlpack(c_tile).mark_layout_dynamic(leading_dim=ct.get_leading_dim(c_tile)) mLSE = ct.from_dlpack(lse_tensor).mark_layout_dynamic(leading_dim=ct.get_leading_dim(lse_tensor)) + mode = "SMEM-P" if use_smem_p else "TMEM-P" + print(f'hd={hd} {mode}: Compiling...', flush=True) compiled = cute.compile(kernel, mQ, mK, mV, mC, stream, mLSE) compiled(mQ, mK, mV, mC, stream, mLSE) torch.cuda.synchronize() @@ -52,22 +45,17 @@ def test_hd128_debug(): out = c_tile[:, :, 0].float() kernel_lse = lse_tensor[0, 0, 0].item() cos = torch.nn.functional.cosine_similarity(out.flatten().unsqueeze(0), ref_unnorm.flatten().unsqueeze(0)).item() + lse_err = abs(kernel_lse - ref_lse) - print(f'\nResults:') - print(f' cos_unnorm={cos:.6f}') - print(f' kernel_lse={kernel_lse:.6f} ref_lse={ref_lse:.6f} err={abs(kernel_lse - ref_lse):.6f}') - print(f' out[0,:4]={out[0,:4].tolist()}') - print(f' ref[0,:4]={ref_unnorm[0,:4].tolist()}') - - # Check: is the output roughly the right magnitude? - print(f' out.abs().max()={out.abs().max().item():.4f}') - print(f' ref.abs().max()={ref_unnorm.abs().max().item():.4f}') - - # Check row-by-row: is O[0] proportional to ref[0]? + print(f'hd={hd} {mode}: cos={cos:.6f} lse_err={lse_err:.6f} {"PASS" if cos >= 0.99 else "FAIL"}') if cos < 0.99: - row_cos = torch.nn.functional.cosine_similarity(out[0].unsqueeze(0), ref_unnorm[0].unsqueeze(0)).item() - print(f' row0_cos={row_cos:.6f}') + print(f' out[0,:4]={out[0,:4].tolist()}') + print(f' ref[0,:4]={ref_unnorm[0,:4].tolist()}') + return cos if __name__ == '__main__': - test_hd128_debug() + print("=== D1 Debug: TMEM-P at various hd ===\n") + test_hd(64, use_smem_p=False) + test_hd(128, use_smem_p=False) + test_hd(256, use_smem_p=False)