""" D1: Minimal O rescale test with just s_k=256 at hd=64. Tests the exact same thing as test_d1_multi_kv but simpler. """ 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(): hd = 64 s_k = 256 m = 128 n_kv_tiles = s_k // 128 torch.manual_seed(42) q = torch.randn(m, hd, 1, dtype=torch.bfloat16, device='cuda') k = torch.randn(s_k, hd, 1, dtype=torch.bfloat16, device='cuda') v = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda') c = torch.zeros(m, hd, 1, dtype=torch.bfloat16, device='cuda') # FP32 reference (full attention) qf = q[:, :, 0].float() kf = k[:, :, 0].float() scale = 1.0 / math.sqrt(hd) attn_max = (qf @ kf.T * scale).max(dim=-1, keepdim=True)[0] attn_exp = torch.exp(qf @ kf.T * scale - attn_max) attn_sum = attn_exp.sum(dim=-1, keepdim=True) ref_unnorm = attn_exp @ v.float() ref_norm = (attn_exp / attn_sum) @ v.float() # Per-tile references for debugging # Tile 0 only kf0 = k[:128, :, 0].float() attn0 = qf @ kf0.T * scale attn_max0 = attn0.max(dim=-1, keepdim=True)[0] attn_exp0 = torch.exp(attn0 - attn_max0) ref0 = attn_exp0 @ v[:128].float() # Tile 1 only (with rescale from tile 0's max) kf1 = k[128:, :, 0].float() attn1 = qf @ kf1.T * scale new_max = torch.max(attn_max0, (qf @ kf1.T * scale).max(dim=-1, keepdim=True)[0]) acc_scale = torch.exp(attn_max0 - new_max) attn_exp1 = torch.exp(attn1 - new_max) ref_rescaled = acc_scale * ref0 + attn_exp1 @ v[128:].float() print(f" Tile-0 only O[0,:4] = {ref0[0,:4].tolist()}") print(f" Rescaled O[0,:4] = {ref_rescaled[0,:4].tolist()}") print(f" Full ref O[0,:4] = {ref_unnorm[0,:4].tolist()}") print(f" acc_scale range = [{acc_scale.min().item():.4f}, {acc_scale.max().item():.4f}]") lse_tensor = torch.zeros(m, 1, 1, dtype=torch.float32, device='cuda') kernel = FmhaKernel(head_dim=hd, s_k=s_k, use_smem_p=False, normalize=False) pv_n_tile = kernel.pv_n_tile n_pv_tiles = kernel.n_pv_tiles stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream) v_tile = v[:, 0:pv_n_tile].contiguous() v_kernel = v_tile.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_kernel).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v_kernel)) 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' Compiling (n_kv_tiles={n_kv_tiles})...', flush=True) compiled = cute.compile(kernel, mQ, mK, mV, mC, stream, mLSE) lse_val = None for nt in range(n_pv_tiles): v_start = nt * pv_n_tile v_end = v_start + pv_n_tile v_tile = v[:, v_start:v_end].contiguous() v_kernel = v_tile.unsqueeze(-1) c_tile = torch.zeros(m, pv_n_tile, 1, dtype=torch.bfloat16, device='cuda') lse_tensor.zero_() 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_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(mQ, mK, mV, mC, stream, mLSE) torch.cuda.synchronize() c[:, v_start:v_end, :] = c_tile if nt == 0: lse_val = lse_tensor[0, 0, 0].item() out_unnorm = c[:, :, 0].float() out_norm = out_unnorm / attn_sum cos_unnorm = torch.nn.functional.cosine_similarity( out_unnorm.flatten().unsqueeze(0), ref_unnorm.flatten().unsqueeze(0) ).item() cos_norm = torch.nn.functional.cosine_similarity( out_norm.flatten().unsqueeze(0), ref_norm.flatten().unsqueeze(0) ).item() print(f" cos_unnorm={cos_unnorm:.6f} cos_norm={cos_norm:.6f}") print(f" out[0,:4]={out_unnorm[0,:4].tolist()}") print(f" lse_val={lse_val}") print(f" {'PASS' if cos_unnorm >= 0.99 else 'FAIL'}") if __name__ == '__main__': test()