Files
nvfp4-megamoe-kernel/tests/archive/test_ref_minimal.py
biondizzle 524f0bdfb4 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

42 lines
1.9 KiB
Python

"""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()