D1: LSE diagnostic at various hd
This commit is contained in:
60
tests/unit/test_d1_lse.py
Normal file
60
tests/unit/test_d1_lse.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""Quick LSE diagnostic: is the softmax correct at hd>64?"""
|
||||
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_lse(hd, 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')
|
||||
c = torch.zeros(m, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
# Reference LSE
|
||||
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_lse = torch.log(attn_sum.squeeze(-1)) + attn_max.squeeze(-1)
|
||||
|
||||
lse_tensor = torch.zeros(m, 1, 1, dtype=torch.float32, device='cuda')
|
||||
kernel = FmhaKernel(head_dim=hd, s_k=n_kv)
|
||||
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))
|
||||
|
||||
print(f'hd={hd}: Compiling...', flush=True)
|
||||
compiled = cute.compile(kernel, mQ, mK, mV, mC, stream, mLSE)
|
||||
compiled(mQ, mK, mV, mC, stream, mLSE)
|
||||
torch.cuda.synchronize()
|
||||
|
||||
kernel_lse = lse_tensor[0, 0, 0].item()
|
||||
ref_lse_val = ref_lse[0].item()
|
||||
lse_err = abs(kernel_lse - ref_lse_val)
|
||||
print(f'hd={hd}: kernel_lse={kernel_lse:.6f} ref_lse={ref_lse_val:.6f} err={lse_err:.6f} {"PASS" if lse_err < 0.01 else "FAIL"}')
|
||||
|
||||
# Also check if P store to TMEM is correct by comparing O directly
|
||||
# Output the raw O (un-normalized) from the kernel
|
||||
out = c[:, :, 0].float()
|
||||
ref_unnorm = attn_exp @ v.float()
|
||||
cos = torch.nn.functional.cosine_similarity(out.flatten().unsqueeze(0), ref_unnorm.flatten().unsqueeze(0)).item()
|
||||
print(f'hd={hd}: cos_unnorm={cos:.6f}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for hd in [64, 128, 256]:
|
||||
test_lse(hd)
|
||||
Reference in New Issue
Block a user