Files
nvfp4-megamoe-kernel/tests/archive/test_reference_fmha.py
biondizzle 0ced79ab37 Clean up: archive diagnostics and superseded tests
Kept:
- example10 (CUTLASS LLM, O rescale + final normalize)
- example9 (SSA kv_coord version)
- working_softmax_maybe.py (working softmax snapshot from before the nuke)
- test_fmha_v3_stage_c.py (identity softmax baseline, n=128 cos 0.999998)
- test_fmha_v3.py (Stage A+B baseline)
- layertest.py, cudagraph_test.py (required)
- test_cutedsl.py, test_fp4_roundtrip.py (NVFP4 tests)

Archived: diag_tma_*, example8, test_diag_multitile, test_reference_fmha,
test_ref_minimal, test_tma_coord, test_fmha_v3_diag*, test_fmha_v3_12w,
test_dense_router, test_interleave*, test_fused_step1, test_router,
test_cache, test_compile_custom_op, test_custom_op, test_layer_schedule
2026-05-23 00:17:07 +00:00

81 lines
2.7 KiB
Python

"""Test the CUTLASS reference Blackwell FMHA on the B200.
Does it actually work multi-tile?"""
import sys
sys.path.insert(0, '/root/cutlass/examples/python/CuTeDSL')
import torch
import math
import cutlass
import cutlass.cute as cute
import cutlass.torch as ct
import cuda.bindings.driver as cuda
from cute.blackwell.kernel.attention.fmha.fmha import (
BlackwellFusedMultiHeadAttentionForward,
FusedMask, FusedMaskScale, FMHA_OperandMajorMode
)
from cutlass.utils import LayoutEnum
def test_reference():
HEAD_DIM = 64
for n in [128, 256, 512]:
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')
# Reference: PyTorch softmax attention
qf = q[0, 0].float()
kf = k[0, 0].float()
vf = v[0, 0].float()
scale = 1.0 / math.sqrt(HEAD_DIM)
attn = qf @ kf.T * scale
attn = torch.softmax(attn, dim=-1)
ref = attn @ 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:
result = 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()
n_tiles = n // 128
print(f'Reference FMHA n={n} ({n_tiles} 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
print(f'Reference FMHA n={n}: FAILED')
traceback.print_exc()
if __name__ == '__main__':
test_reference()