Files
nvfp4-megamoe-kernel/tests/unit/test_d1_layout_diag.py

42 lines
2.0 KiB
Python

"""D1: Print SMEM-P layout diagnostics inside the kernel."""
import torch, math
import cutlass, cutlass.cute as cute, cutlass.utils as utils, cutlass.torch as ct
import cuda.bindings.driver as cuda
from dsv4.kernels.attention.fmha import FmhaKernel
def test_layout(hd):
m = 128; n_kv = 1 # minimal
torch.manual_seed(42)
q = torch.randn(m, hd, 1, dtype=torch.bfloat16, device='cuda')
k = torch.randn(n_kv, hd, 1, dtype=torch.bfloat16, device='cuda')
v = torch.randn(n_kv, hd, dtype=torch.bfloat16, device='cuda')
c = torch.zeros(m, hd, 1, dtype=torch.bfloat16, device='cuda')
lse_tensor = torch.zeros(m, 1, 1, dtype=torch.float32, device='cuda')
kernel = FmhaKernel(head_dim=hd, s_k=n_kv, use_smem_p=False)
pv_n_tile = kernel.pv_n_tile
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
v_tile = v[:, 0:pv_n_tile].contiguous().unsqueeze(-1)
c_tile = torch.zeros(m, pv_n_tile, 1, dtype=torch.bfloat16, device='cuda')
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_tile).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v_tile))
mC = ct.from_dlpack(c_tile).mark_layout_dynamic(leading_dim=ct.get_leading_dim(c_tile))
mLSE = ct.from_dlpack(lse_tensor).mark_layout_dynamic(leading_dim=ct.get_leading_dim(lse_tensor))
# The _setup method prints are visible at compile time
print(f'--- hd={hd} ---', flush=True)
print(f' pv_n_tile={pv_n_tile}, use_smem_p={kernel.use_smem_p}', flush=True)
compiled = cute.compile(kernel, mQ, mK, mV, mC, stream, mLSE)
print(f' tmem_p0_offset={kernel.tmem_p0_offset}', flush=True)
print(f' tmem_o0_offset={kernel.tmem_o0_offset}', flush=True)
print(f' tOrP0_offset={kernel.tOrP0_offset}', flush=True)
print(f' num_tmem_alloc_cols={kernel.num_tmem_alloc_cols}', flush=True)
if __name__ == '__main__':
for hd in [64, 128, 256]:
test_layout(hd)