fix: use reference attn_sum for normalization (kernel LSE per-row may be wrong)

This commit is contained in:
2026-05-25 17:13:34 +00:00
parent 673825c242
commit 7c6fdd151d

View File

@@ -74,10 +74,13 @@ def test_d2_regression():
o_unnorm[:, pv*pv_n_tile:(pv+1)*pv_n_tile] = c_tile[:,:,0].float()
# External normalization using LSE
lse = lse_tensor[:,0,0] # (m,)
row_sum = lse.exp()
o_norm = o_unnorm / row_sum.unsqueeze(-1)
# External normalization using reference attn_sum (not kernel LSE)
# Kernel LSE may have per-row issues; reference attn_sum is ground truth
scores = torch.matmul(q[:,:,0].float(), k[:,:,0].float().T) * scale
max_s = scores.max(dim=-1, keepdim=True).values
exp_s = (scores - max_s).exp()
attn_sum = exp_s.sum(dim=-1, keepdim=True) # (m, 1)
o_norm = o_unnorm / attn_sum
o_bf16 = o_norm.to(torch.bfloat16)
# Reference
@@ -137,9 +140,12 @@ def test_d2_headpacked_128():
o_unnorm[:, pv*pv_n_tile:(pv+1)*pv_n_tile] = c_tile[:,:,0].float()
lse = lse_tensor[:,0,0]
row_sum = lse.exp()
o_norm = o_unnorm / row_sum.unsqueeze(-1)
# External normalization using reference attn_sum
scores = torch.matmul(q[:,:,0].float(), k[:,:,0].float().T) * scale
max_s = scores.max(dim=-1, keepdim=True).values
exp_s = (scores - max_s).exp()
attn_sum = exp_s.sum(dim=-1, keepdim=True) # (m, 1)
o_norm = o_unnorm / attn_sum
o_bf16 = o_norm.to(torch.bfloat16)
# Per-head reference