Files
nvfp4-megamoe-kernel/tests/unit/test_384.py
2026-05-22 21:36:22 +00:00

32 lines
1.7 KiB
Python

"""Test n=384 pipeline cycling"""
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
n = 384; HEAD_DIM = 64
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)
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} (3 tiles): cos {cos:.6f} {"PASS" if cos >= 0.99 else "FAIL"}')
if cos < 0.99 and cos > 0.5:
print(f' ratio: {(out[0,:4]/ref[0,:4]).mean().item():.4f}')
except Exception as e:
print(f'n={n}: ERROR: {type(e).__name__}: {str(e)[:300]}')