"""D1 test: HEAD_DIM=512 only (faster iteration on compilation issues).""" import torch, math, sys 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(): torch.manual_seed(42) hd, n = 512, 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') 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_lse = (torch.log(attn_sum.squeeze(-1)) + attn_max.squeeze(-1))[0].item() lse_tensor = torch.zeros(m, 1, 1, dtype=torch.float32, device='cuda') kernel = FmhaKernel(head_dim=hd, s_k=n, use_smem_p=False) pv_n_tile = kernel.pv_n_tile n_pv_tiles = kernel.n_pv_tiles stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream) print(f'hd={hd}, pv_n_tile={pv_n_tile}, n_pv_tiles={n_pv_tiles}, n_k_sub_tiles={kernel.n_k_sub_tiles}, k_tile={kernel.k_tile}', flush=True) print(f'Compiling first PV tile...', flush=True) # Only compile the first PV tile to isolate compilation issues 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)) import time t0 = time.time() from cutlass.base_dsl.compiler import PtxasOptions, OptLevel compiled = cute.compile(kernel, mQ, mK, mV, mC, stream, mLSE) t1 = time.time() print(f'Compilation took {t1-t0:.1f}s', flush=True) # Run all PV tiles lse_val = None c = torch.zeros(m, hd, 1, dtype=torch.bfloat16, device='cuda') 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() print(f' PV tile {nt}: done', flush=True) c[:, v_start:v_end, :] = c_tile if nt == 0: lse_val = lse_tensor[0, 0, 0].item() out_unnorm = c[:, :, 0].float() cos_unnorm = torch.nn.functional.cosine_similarity( out_unnorm.flatten().unsqueeze(0), ref_unnorm.flatten().unsqueeze(0) ).item() lse_err = abs(lse_val - ref_lse) if lse_val is not None else float('inf') status = "PASS" if cos_unnorm >= 0.99 else "FAIL" print(f'hd={hd}: cos_unnorm {cos_unnorm:.6f} lse_err {lse_err:.6f} {status}') if __name__ == '__main__': test()