D1: Add hd=128 debug test
This commit is contained in:
73
tests/unit/test_d1_hd128_debug.py
Normal file
73
tests/unit/test_d1_hd128_debug.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""D1: Debug hd=128 — check if the QK output is correct."""
|
||||
import torch, math
|
||||
import cutlass.cute as cute
|
||||
import cutlass.torch as ct
|
||||
import cuda.bindings.driver as cuda
|
||||
from dsv4.kernels.attention.fmha import FmhaKernel
|
||||
|
||||
|
||||
def test_hd128_debug():
|
||||
hd = 128
|
||||
n_kv = 128
|
||||
m = 128
|
||||
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')
|
||||
|
||||
# Reference: just the QK @ V attention (un-normalized)
|
||||
qf = q[:, :, 0].float()
|
||||
kf = k[:, :, 0].float()
|
||||
scale = 1.0 / math.sqrt(hd)
|
||||
attn = qf @ kf.T * scale
|
||||
attn_max = attn.max(dim=-1, keepdim=True)[0]
|
||||
attn_exp = torch.exp(attn - attn_max)
|
||||
attn_sum = attn_exp.sum(dim=-1, keepdim=True)
|
||||
ref_unnorm = attn_exp @ v.float()
|
||||
ref_lse = (torch.log(attn_sum.squeeze(-1)) + attn_max.squeeze(-1))[0].item()
|
||||
|
||||
# Run kernel with TMEM-P (force)
|
||||
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
|
||||
print(f'pv_n_tile={pv_n_tile}, n_pv_tiles={kernel.n_pv_tiles}')
|
||||
print(f'tmem_o0_offset={kernel.tmem_o0_offset}, tmem_p0_offset={kernel.tmem_p0_offset}')
|
||||
print(f'tOrP0_offset={kernel.tOrP0_offset}')
|
||||
print(f'num_tmem_alloc_cols={kernel.num_tmem_alloc_cols}')
|
||||
print(f'scale_softmax={kernel.scale_softmax}, scale_softmax_log2={kernel.scale_softmax_log2}')
|
||||
|
||||
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))
|
||||
|
||||
compiled = cute.compile(kernel, mQ, mK, mV, mC, stream, mLSE)
|
||||
compiled(mQ, mK, mV, mC, stream, mLSE)
|
||||
torch.cuda.synchronize()
|
||||
|
||||
out = c_tile[:, :, 0].float()
|
||||
kernel_lse = lse_tensor[0, 0, 0].item()
|
||||
cos = torch.nn.functional.cosine_similarity(out.flatten().unsqueeze(0), ref_unnorm.flatten().unsqueeze(0)).item()
|
||||
|
||||
print(f'\nResults:')
|
||||
print(f' cos_unnorm={cos:.6f}')
|
||||
print(f' kernel_lse={kernel_lse:.6f} ref_lse={ref_lse:.6f} err={abs(kernel_lse - ref_lse):.6f}')
|
||||
print(f' out[0,:4]={out[0,:4].tolist()}')
|
||||
print(f' ref[0,:4]={ref_unnorm[0,:4].tolist()}')
|
||||
|
||||
# Check: is the output roughly the right magnitude?
|
||||
print(f' out.abs().max()={out.abs().max().item():.4f}')
|
||||
print(f' ref.abs().max()={ref_unnorm.abs().max().item():.4f}')
|
||||
|
||||
# Check row-by-row: is O[0] proportional to ref[0]?
|
||||
if cos < 0.99:
|
||||
row_cos = torch.nn.functional.cosine_similarity(out[0].unsqueeze(0), ref_unnorm[0].unsqueeze(0)).item()
|
||||
print(f' row0_cos={row_cos:.6f}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_hd128_debug()
|
||||
Reference in New Issue
Block a user