diff --git a/tests/unit/test_quick.py b/tests/unit/test_quick.py new file mode 100644 index 00000000..240c919e --- /dev/null +++ b/tests/unit/test_quick.py @@ -0,0 +1,30 @@ +"""Quick test: n=128, 256 only""" +import torch, math, cutlass, cutlass.cute as cute, cutlass.torch as ct, cuda.bindings.driver as cuda +from cutlass import Float32, BFloat16, Int32 +from test_fmha_v3_stage_c import FmhaV3StageCMulti + +HEAD_DIM = 64 +for n in [128, 256]: + 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') + 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.unsqueeze(-1)).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v.unsqueeze(-1))) + 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) + 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}')