Minimal reference FMHA test: n=256 only
This commit is contained in:
41
tests/test_ref_minimal.py
Normal file
41
tests/test_ref_minimal.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""Minimal test: CUTLASS reference FMHA, n=256 only."""
|
||||
import sys
|
||||
sys.path.insert(0, '/root/cutlass/examples/python/CuTeDSL')
|
||||
|
||||
import torch, math, cutlass, cutlass.cute as cute, cuda.bindings.driver as cuda
|
||||
from cute.blackwell.kernel.attention.fmha.fmha import BlackwellFusedMultiHeadAttentionForward, FMHA_OperandMajorMode
|
||||
|
||||
HEAD_DIM = 64
|
||||
n = 256
|
||||
torch.manual_seed(42)
|
||||
m = 128; batch = 1
|
||||
q = torch.randn(batch, 1, m, HEAD_DIM, dtype=torch.bfloat16, device='cuda')
|
||||
k = torch.randn(batch, 1, n, HEAD_DIM, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(batch, 1, n, HEAD_DIM, dtype=torch.bfloat16, device='cuda')
|
||||
c = torch.zeros(batch, 1, m, HEAD_DIM, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
qf = q[0,0].float(); kf = k[0,0].float(); vf = v[0,0].float()
|
||||
scale = 1.0/math.sqrt(HEAD_DIM)
|
||||
ref = torch.softmax(qf @ kf.T * scale, dim=-1) @ vf
|
||||
|
||||
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
||||
kernel = BlackwellFusedMultiHeadAttentionForward(
|
||||
q_major_mode=FMHA_OperandMajorMode.K, k_major_mode=FMHA_OperandMajorMode.K,
|
||||
v_major_mode=FMHA_OperandMajorMode.MN, o_major_mode=FMHA_OperandMajorMode.K,
|
||||
q_head_dim=HEAD_DIM, kv_head_dim=HEAD_DIM, num_q_heads=1, num_kv_heads=1,
|
||||
q_dtype=cutlass.BFloat16, k_dtype=cutlass.BFloat16, v_dtype=cutlass.BFloat16,
|
||||
o_dtype=cutlass.BFloat16, acc_dtype=cutlass.Float32, epilogue_dtype=cutlass.Float32,
|
||||
use_2cta_instrs=False,
|
||||
)
|
||||
print(f'n={n}: Compiling reference FMHA...', flush=True)
|
||||
try:
|
||||
kernel.run(q, k, v, c, stream)
|
||||
torch.cuda.synchronize()
|
||||
out = c[0,0].float()
|
||||
cos = torch.nn.functional.cosine_similarity(out.flatten().unsqueeze(0), ref.flatten().unsqueeze(0)).item()
|
||||
print(f'Reference FMHA n={n} (2 tiles): cos {cos:.6f} {"PASS" if cos >= 0.99 else "FAIL"}')
|
||||
if cos < 0.99:
|
||||
print(f' out[0,:4]={out[0,:4].tolist()}')
|
||||
print(f' ref[0,:4]={ref[0,:4].tolist()}')
|
||||
except Exception as e:
|
||||
import traceback; traceback.print_exc()
|
||||
Reference in New Issue
Block a user