From c47d229e6ae55eec55d20d9e9bcd73414f51041c Mon Sep 17 00:00:00 2001 From: biondizzle Date: Fri, 22 May 2026 21:32:18 +0000 Subject: [PATCH] Sweep test --- tests/unit/test_sweep.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/unit/test_sweep.py diff --git a/tests/unit/test_sweep.py b/tests/unit/test_sweep.py new file mode 100644 index 00000000..fcf0b510 --- /dev/null +++ b/tests/unit/test_sweep.py @@ -0,0 +1,35 @@ +"""Sweep: n=128,256,512,1024 stage C test""" +import torch, math, cutlass, cutlass.cute as cute, cutlass.torch as ct, cuda.bindings.driver as cuda +from cutlass import Float32, BFloat16, Int32, Boolean +from test_fmha_v3_stage_c import FmhaV3StageCMulti + +HEAD_DIM = 64 +for n in [128, 256, 512, 1024]: + torch.manual_seed(42) + q = torch.randn(128, HEAD_DIM, 1, dtype=torch.bfloat16, device='cuda') + k = torch.randn(n, HEAD_DIM, 1, dtype=torch.bfloat16, device='cuda') + v = torch.randn(n, HEAD_DIM, dtype=torch.bfloat16, device='cuda') + v_kernel = v.unsqueeze(-1) + c = torch.zeros(128, HEAD_DIM, 1, dtype=torch.bfloat16, device='cuda') + qf = q[:,:,0].float(); kf = k[:,:,0].float() + ref = torch.softmax(qf @ kf.T * (1.0/math.sqrt(HEAD_DIM)), dim=-1) @ v.float() + mQ = ct.from_dlpack(q).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q)) + mK = ct.from_dlpack(k).mark_layout_dynamic(leading_dim=ct.get_leading_dim(k)) + mV = ct.from_dlpack(v_kernel).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v_kernel)) + mC = ct.from_dlpack(c).mark_layout_dynamic(leading_dim=ct.get_leading_dim(c)) + stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream) + kernel = FmhaV3StageCMulti(s_k=n) + print(f'n={n}: Compiling...', flush=True) + try: + compiled = cute.compile(kernel, mQ, mK, mV, mC, stream) + compiled(mQ, mK, mV, mC, stream) + torch.cuda.synchronize() + out = c[:,:,0].float() + cos = torch.nn.functional.cosine_similarity(out.flatten().unsqueeze(0), ref.flatten().unsqueeze(0)).item() + print(f'n={n} ({n//128} tiles): cos {cos:.6f} {"PASS" if cos >= 0.99 else "FAIL"}') + if cos < 0.99 and cos > 0.5: + ratio = (out[0,:4] / ref[0,:4]).mean().item() + print(f' out/ref ratio: {ratio:.4f} (expect ~1.0)') + except Exception as e: + print(f'n={n}: ERROR: {type(e).__name__}: {str(e)[:200]}') + torch.cuda.empty_cache()