fix tests: use 3D tensors (M, hd, 1) matching kernel local_tile expectations
This commit is contained in:
@@ -1,21 +1,16 @@
|
||||
"""
|
||||
FMHA D2: Head-packed multi-head attention.
|
||||
|
||||
Strategy A: Fold the head dimension into M. Each CTA processes
|
||||
all heads' queries for its M tile. At decode T=1, n_h=128, M=128
|
||||
fills exactly one MMA tile. The kernel doesn't need to know about
|
||||
heads — it just processes M rows with per-row softmax.
|
||||
|
||||
Q is reshaped from (n_h, T, hd) to (n_h * T, hd) in Python.
|
||||
K/V are shared (MQA) with shape (s_k, hd).
|
||||
Strategy A: Fold the head dimension into M. Q is reshaped from (n_h, T, hd)
|
||||
to (n_h*T, hd, 1). K/V are (s_k, hd, 1) — shared MQA.
|
||||
At decode T=1, n_h=128: M=128, exactly one MMA tile.
|
||||
The kernel treats each row as independent attention (per-row softmax).
|
||||
|
||||
Run: ~/.openclaw/workspace/fire_b200_test tests/unit/test_d2_headpacked.py
|
||||
"""
|
||||
import torch
|
||||
import math
|
||||
import cutlass
|
||||
import cutlass.cute as cute
|
||||
from cutlass import Float32, BFloat16
|
||||
import cuda.bindings.driver as cuda
|
||||
import cutlass.torch as ct
|
||||
|
||||
@@ -33,107 +28,93 @@ def reference_fmha(q, k, v, scale):
|
||||
return o.to(torch.bfloat16)
|
||||
|
||||
|
||||
def _run_fmha(fmha, q_3d, k_3d, v_3d, o_3d):
|
||||
"""Run FmhaKernel with CuTe tensors."""
|
||||
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
||||
q_c = ct.from_dlpack(q_3d).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q_3d))
|
||||
k_c = ct.from_dlpack(k_3d).mark_layout_dynamic(leading_dim=ct.get_leading_dim(k_3d))
|
||||
v_c = ct.from_dlpack(v_3d).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v_3d))
|
||||
o_c = ct.from_dlpack(o_3d).mark_layout_dynamic(leading_dim=ct.get_leading_dim(o_3d))
|
||||
fmha(q_c, k_c, v_c, o_c, stream)
|
||||
|
||||
|
||||
def test_d2_headpacked_n1():
|
||||
"""Regression: n_h=1 (same as single-head, backward compatible)."""
|
||||
"""Regression: n_h=1 (same as single-head)."""
|
||||
print("\n=== Test 1: n_h=1 regression (hd=64) ===")
|
||||
torch.manual_seed(42)
|
||||
T, s_k, hd = 1, 128, 64
|
||||
M, s_k, hd = 128, 128, 64
|
||||
scale = 1.0 / math.sqrt(hd)
|
||||
|
||||
q = torch.randn(T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
k = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
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, 1, dtype=torch.bfloat16, device='cuda')
|
||||
o = torch.zeros(M, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
fmha = FmhaKernel(head_dim=hd, s_k=s_k, normalize=True)
|
||||
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
||||
_run_fmha(fmha, q, k, v, o)
|
||||
|
||||
# Use the existing test pattern: pad Q to 128 rows
|
||||
q_128 = torch.randn(128, hd, dtype=torch.bfloat16, device='cuda')
|
||||
q_128[0] = q[0] # Put real data in row 0
|
||||
o_128 = torch.zeros(128, hd, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
q_c = ct.from_dlpack(q_128).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q_128))
|
||||
k_c = ct.from_dlpack(k).mark_layout_dynamic(leading_dim=ct.get_leading_dim(k))
|
||||
v_c = ct.from_dlpack(v).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v))
|
||||
o_c = ct.from_dlpack(o_128).mark_layout_dynamic(leading_dim=ct.get_leading_dim(o_128))
|
||||
fmha(q_c, k_c, v_c, o_c, stream)
|
||||
|
||||
o = o_128[:T] # Take only the real rows
|
||||
ref = reference_fmha(q, k, v, scale)
|
||||
ref = reference_fmha(q[:,:,0], k[:,:,0], v[:,:,0], scale)
|
||||
cos = torch.nn.functional.cosine_similarity(
|
||||
o.flatten().float().unsqueeze(0), ref.flatten().float().unsqueeze(0)
|
||||
o[:,:,0].flatten().float().unsqueeze(0), ref.flatten().float().unsqueeze(0)
|
||||
).item()
|
||||
print(f" cos = {cos:.6f}")
|
||||
assert cos >= 0.99, f"cosine too low: {cos}"
|
||||
print(" ✅ PASS")
|
||||
|
||||
|
||||
def test_d2_headpacked_basic():
|
||||
"""n_h=128, T=1 (Pro decode): M=128, exactly one M tile."""
|
||||
def test_d2_headpacked_128():
|
||||
"""n_h=128, T=1 (Pro decode): M=128, one M tile, all heads packed."""
|
||||
print("\n=== Test 2: n_h=128, T=1 (Pro decode, hd=64) ===")
|
||||
torch.manual_seed(42)
|
||||
n_h, T, s_k, hd = 128, 1, 128, 64
|
||||
scale = 1.0 / math.sqrt(hd)
|
||||
|
||||
# Q: (n_h, T, hd) → (n_h*T, hd) = (128, 64)
|
||||
q_heads = torch.randn(n_h, T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
q = q_heads.reshape(n_h * T, hd)
|
||||
k = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
# Pack heads into M: (n_h*T, hd) → (128, 64, 1)
|
||||
q = q_heads.reshape(n_h * T, hd).unsqueeze(-1)
|
||||
k = torch.randn(s_k, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(s_k, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
o = torch.zeros(n_h * T, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
fmha = FmhaKernel(head_dim=hd, s_k=s_k, normalize=True, num_query_heads=n_h)
|
||||
o = torch.zeros(n_h * T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
||||
|
||||
q_c = ct.from_dlpack(q).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q))
|
||||
k_c = ct.from_dlpack(k).mark_layout_dynamic(leading_dim=ct.get_leading_dim(k))
|
||||
v_c = ct.from_dlpack(v).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v))
|
||||
o_c = ct.from_dlpack(o).mark_layout_dynamic(leading_dim=ct.get_leading_dim(o))
|
||||
fmha(q_c, k_c, v_c, o_c, stream)
|
||||
_run_fmha(fmha, q, k, v, o)
|
||||
|
||||
# Reference: per-head attention
|
||||
o_ref = torch.zeros(n_h, T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
for h in range(n_h):
|
||||
o_ref[h, 0] = reference_fmha(q_heads[h], k, v, scale)[0]
|
||||
o_ref[h, 0] = reference_fmha(q_heads[h], k[:,:,0], v[:,:,0], scale)[0]
|
||||
o_ref_flat = o_ref.reshape(n_h * T, hd)
|
||||
|
||||
cos = torch.nn.functional.cosine_similarity(
|
||||
o.flatten().float().unsqueeze(0), o_ref_flat.flatten().float().unsqueeze(0)
|
||||
o[:,:,0].flatten().float().unsqueeze(0), o_ref_flat.flatten().float().unsqueeze(0)
|
||||
).item()
|
||||
print(f" cos = {cos:.6f}")
|
||||
assert cos >= 0.99, f"cosine too low: {cos}"
|
||||
print(" ✅ PASS")
|
||||
|
||||
|
||||
def test_d2_headpacked_flash():
|
||||
"""n_h=64, T=1 (Flash decode): M=64, underutilized (1 CTA, 64 rows)."""
|
||||
def test_d2_headpacked_64():
|
||||
"""n_h=64, T=1 (Flash decode): M=64, pad to 128."""
|
||||
print("\n=== Test 3: n_h=64, T=1 (Flash decode, hd=64) ===")
|
||||
torch.manual_seed(42)
|
||||
n_h, T, s_k, hd = 64, 1, 128, 64
|
||||
scale = 1.0 / math.sqrt(hd)
|
||||
|
||||
q_heads = torch.randn(n_h, T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
q = q_heads.reshape(n_h * T, hd)
|
||||
# Pad to 128 rows (M tile size) — kernel expects M >= 128
|
||||
q_padded = torch.nn.functional.pad(q, (0, 0, 0, 128 - n_h * T))
|
||||
k = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
q_flat = q_heads.reshape(n_h * T, hd) # (64, 64)
|
||||
# Pad to 128 rows
|
||||
q = torch.nn.functional.pad(q_flat, (0, 0, 0, 128 - n_h * T)).unsqueeze(-1) # (128, 64, 1)
|
||||
k = torch.randn(s_k, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(s_k, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
o_padded = torch.zeros(128, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
fmha = FmhaKernel(head_dim=hd, s_k=s_k, normalize=True, num_query_heads=n_h)
|
||||
o_padded = torch.zeros(128, hd, dtype=torch.bfloat16, device='cuda')
|
||||
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
||||
|
||||
q_c = ct.from_dlpack(q_padded).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q_padded))
|
||||
k_c = ct.from_dlpack(k).mark_layout_dynamic(leading_dim=ct.get_leading_dim(k))
|
||||
v_c = ct.from_dlpack(v).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v))
|
||||
o_c = ct.from_dlpack(o_padded).mark_layout_dynamic(leading_dim=ct.get_leading_dim(o_padded))
|
||||
fmha(q_c, k_c, v_c, o_c, stream)
|
||||
|
||||
o = o_padded[:n_h * T] # Trim padding
|
||||
_run_fmha(fmha, q, k, v, o_padded)
|
||||
|
||||
o = o_padded[:n_h * T, :, 0] # Trim padding
|
||||
o_ref = torch.zeros(n_h, T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
for h in range(n_h):
|
||||
o_ref[h, 0] = reference_fmha(q_heads[h], k, v, scale)[0]
|
||||
o_ref[h, 0] = reference_fmha(q_heads[h], k[:,:,0], v[:,:,0], scale)[0]
|
||||
o_ref_flat = o_ref.reshape(n_h * T, hd)
|
||||
|
||||
cos = torch.nn.functional.cosine_similarity(
|
||||
@@ -145,33 +126,26 @@ def test_d2_headpacked_flash():
|
||||
|
||||
|
||||
def test_d2_headpacked_hd128():
|
||||
"""n_h=8, T=1, hd=128 (multi-head with larger head dim)."""
|
||||
"""n_h=8, T=1, hd=128: pad to 128 rows, larger head dim."""
|
||||
print("\n=== Test 4: n_h=8, T=1, hd=128 ===")
|
||||
torch.manual_seed(42)
|
||||
n_h, T, s_k, hd = 8, 1, 128, 128
|
||||
scale = 1.0 / math.sqrt(hd)
|
||||
|
||||
q_heads = torch.randn(n_h, T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
q = q_heads.reshape(n_h * T, hd) # (8, 128)
|
||||
# Pad to 128 rows (M tile)
|
||||
q_padded = torch.nn.functional.pad(q, (0, 0, 0, 128 - n_h * T))
|
||||
k = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(s_k, hd, dtype=torch.bfloat16, device='cuda')
|
||||
q_flat = q_heads.reshape(n_h * T, hd) # (8, 128)
|
||||
q = torch.nn.functional.pad(q_flat, (0, 0, 0, 128 - n_h * T)).unsqueeze(-1) # (128, 128, 1)
|
||||
k = torch.randn(s_k, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
v = torch.randn(s_k, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
o_padded = torch.zeros(128, hd, 1, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
fmha = FmhaKernel(head_dim=hd, s_k=s_k, normalize=True, num_query_heads=n_h)
|
||||
o_padded = torch.zeros(128, hd, dtype=torch.bfloat16, device='cuda')
|
||||
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
||||
_run_fmha(fmha, q, k, v, o_padded)
|
||||
|
||||
q_c = ct.from_dlpack(q_padded).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q_padded))
|
||||
k_c = ct.from_dlpack(k).mark_layout_dynamic(leading_dim=ct.get_leading_dim(k))
|
||||
v_c = ct.from_dlpack(v).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v))
|
||||
o_c = ct.from_dlpack(o_padded).mark_layout_dynamic(leading_dim=ct.get_leading_dim(o_padded))
|
||||
fmha(q_c, k_c, v_c, o_c, stream)
|
||||
|
||||
o = o_padded[:n_h * T] # Trim padding
|
||||
o = o_padded[:n_h * T, :, 0]
|
||||
o_ref = torch.zeros(n_h, T, hd, dtype=torch.bfloat16, device='cuda')
|
||||
for h in range(n_h):
|
||||
o_ref[h, 0] = reference_fmha(q_heads[h], k, v, scale)[0]
|
||||
o_ref[h, 0] = reference_fmha(q_heads[h], k[:,:,0], v[:,:,0], scale)[0]
|
||||
o_ref_flat = o_ref.reshape(n_h * T, hd)
|
||||
|
||||
cos = torch.nn.functional.cosine_similarity(
|
||||
@@ -185,8 +159,8 @@ def test_d2_headpacked_hd128():
|
||||
def test():
|
||||
print("=== D2: Head-Packed Multi-Head FMHA ===")
|
||||
test_d2_headpacked_n1()
|
||||
test_d2_headpacked_basic()
|
||||
test_d2_headpacked_flash()
|
||||
test_d2_headpacked_128()
|
||||
test_d2_headpacked_64()
|
||||
test_d2_headpacked_hd128()
|
||||
print("\n=== ALL TESTS PASSED ===")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user