D1: test raw unnormalized output via epilogue_tma_store
This commit is contained in:
52
tests/unit/test_d1_raw.py
Normal file
52
tests/unit/test_d1_raw.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""D1: Test raw unnormalized PV output (epilogue_tma_store without normalize)."""
|
||||
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
|
||||
|
||||
for hd in [64, 128, 256]:
|
||||
torch.manual_seed(42)
|
||||
n = 128; m = 128
|
||||
q = torch.randn(m, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
k = torch.randn(n, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(n, hd, dtype=torch.bfloat16, device='cuda')
|
||||
c = torch.zeros(m, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
# Reference: unnormalized PV = (softmax(QK^T) * scale) @ V (without sum normalization)
|
||||
qf = q[:,:,0].float(); kf = k[:,:,0].float()
|
||||
scale = 1.0 / math.sqrt(hd)
|
||||
attn = qf @ kf.T * scale
|
||||
attn_unnorm = torch.exp(attn - attn.max(dim=-1, keepdim=True).values) # unnormalized softmax
|
||||
ref_unnorm = attn_unnorm @ v.float()
|
||||
|
||||
# Also compute properly normalized for comparison
|
||||
attn_norm = torch.softmax(attn, dim=-1)
|
||||
ref_norm = attn_norm @ v.float()
|
||||
|
||||
v_kernel = v.unsqueeze(-1)
|
||||
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_kernel).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v_kernel))
|
||||
mC = ct.from_dlpack(c).mark_layout_dynamic(leading_dim=ct.get_leading_dim(c))
|
||||
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
||||
|
||||
kernel = FmhaKernel(head_dim=hd, s_k=n)
|
||||
print(f'hd={hd}: Compiling...', flush=True)
|
||||
compiled = cute.compile(kernel, mQ, mK, mV, mC, stream)
|
||||
compiled(mQ, mK, mV, mC, stream)
|
||||
torch.cuda.synchronize()
|
||||
|
||||
out = c[:,:,0].float()
|
||||
|
||||
# Check against unnormalized reference
|
||||
cos_unnorm = torch.nn.functional.cosine_similarity(
|
||||
out.flatten().unsqueeze(0), ref_unnorm.flatten().unsqueeze(0)
|
||||
).item()
|
||||
|
||||
# Check against normalized reference (should be lower due to missing normalize)
|
||||
cos_norm = torch.nn.functional.cosine_similarity(
|
||||
out.flatten().unsqueeze(0), ref_norm.flatten().unsqueeze(0)
|
||||
).item()
|
||||
|
||||
print(f'hd={hd}: cos_unnorm={cos_unnorm:.6f} cos_norm={cos_norm:.6f}')
|
||||
Reference in New Issue
Block a user