Skip to content

Commit 110f120

Browse files
committed
[Metal] Add tests for simdgroup register GEMM and LowerSIMDGroupCopy
Codegen tests verify correct Metal shader output (simdgroup_store to device, no C_local simdgroup_load from shared, make_filled init). Correctness tests verify numerical results against torch.matmul across square/non-square blocks and matrix sizes.
1 parent 72b4765 commit 110f120

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""Test Metal simdgroup register GEMM with direct simdgroup_store to device memory.
2+
3+
These tests verify the simdgroup register accumulation path where C is allocated
4+
in metal.simdgroup scope. This eliminates C_simd load/store round-trips through
5+
shared memory on each K iteration. The final T.copy(C_local, C[...]) is lowered
6+
to simdgroup_store directly to device memory via LowerSIMDGroupStore.
7+
"""
8+
9+
import tilelang
10+
from tilelang import tvm as tvm
11+
import tilelang.testing
12+
import tilelang.language as T
13+
import torch
14+
15+
16+
def _make_simdgroup_gemm_func(M, N, K, block_M, block_N, block_K, dtype=T.float16, accum_dtype=T.float32):
17+
@T.prim_func
18+
def gemm_kernel(
19+
A: T.Tensor((M, K), dtype),
20+
B: T.Tensor((K, N), dtype),
21+
C: T.Tensor((M, N), accum_dtype),
22+
):
23+
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=128) as (bx, by):
24+
A_shared = T.alloc_shared((block_M, block_K), dtype, scope="shared")
25+
B_shared = T.alloc_shared((block_K, block_N), dtype, scope="shared")
26+
C_local = T.alloc_simdgroup((block_M, block_N), accum_dtype)
27+
T.clear(C_local)
28+
for ko in T.Pipelined(T.ceildiv(K, block_K), num_stages=0):
29+
T.copy(A[by * block_M, ko * block_K], A_shared)
30+
T.copy(B[ko * block_K, bx * block_N], B_shared)
31+
T.gemm(A_shared, B_shared, C_local)
32+
T.copy(C_local, C[by * block_M, bx * block_N])
33+
34+
return gemm_kernel
35+
36+
37+
matmul_simdgroup = tilelang.jit(_make_simdgroup_gemm_func)
38+
39+
40+
def assert_simdgroup_store_correctness(M, N, K, block_M, block_N, block_K, dtype=T.float16, accum_dtype=T.float32, atol=1e-2):
41+
kernel = matmul_simdgroup(M, N, K, block_M, block_N, block_K, dtype=dtype, accum_dtype=accum_dtype)
42+
43+
torch_dtype = dtype.as_torch()
44+
torch_accum_dtype = accum_dtype.as_torch()
45+
a = torch.randn(M, K, dtype=torch_dtype, device="mps")
46+
b = torch.randn(K, N, dtype=torch_dtype, device="mps")
47+
c = torch.zeros(M, N, dtype=torch_accum_dtype, device="mps")
48+
49+
kernel(a, b, c)
50+
51+
ref = a.to(torch_accum_dtype) @ b.to(torch_accum_dtype)
52+
assert torch.allclose(ref, c, atol=atol), (
53+
f"Result mismatch for M={M}, N={N}, K={K}, "
54+
f"block=({block_M},{block_N},{block_K}), dtype={dtype}\n"
55+
f"max diff: {(ref - c).abs().max().item()}"
56+
)
57+
58+
59+
def assert_simdgroup_store_codegen(M, N, K, block_M, block_N, block_K, dtype=T.float16, accum_dtype=T.float32):
60+
func = _make_simdgroup_gemm_func(M, N, K, block_M, block_N, block_K, dtype=dtype, accum_dtype=accum_dtype)
61+
with tvm.transform.PassContext(), tvm.target.Target("metal"):
62+
artifact = tilelang.lower(func, target="metal")
63+
64+
src = artifact.kernel_source
65+
assert src is not None
66+
assert "kernel void" in src
67+
assert "simdgroup_multiply_accumulate" in src
68+
assert "make_filled_simdgroup_matrix" in src
69+
70+
assert "simdgroup_float8x8" in src or "simdgroup_half8x8" in src, "Expected simdgroup_float8x8 or simdgroup_half8x8 for C accumulator"
71+
72+
store_to_device = src.count("simdgroup_store(C_local")
73+
assert store_to_device > 0, "Expected simdgroup_store of C_local to device memory"
74+
75+
load_c_from_shared = [line for line in src.split("\n") if "simdgroup_load" in line and "C_local" in line]
76+
assert len(load_c_from_shared) == 0, f"C_local should not be loaded from shared memory, but found: {load_c_from_shared}"
77+
78+
79+
# --- Codegen tests (cross-platform) ---
80+
81+
82+
def test_codegen_square_small():
83+
assert_simdgroup_store_codegen(64, 64, 64, 16, 16, 16)
84+
85+
86+
def test_codegen_square_large():
87+
assert_simdgroup_store_codegen(128, 128, 128, 32, 32, 32)
88+
89+
90+
def test_codegen_non_square():
91+
assert_simdgroup_store_codegen(128, 128, 128, 32, 64, 16)
92+
93+
94+
def test_codegen_float32_accum():
95+
assert_simdgroup_store_codegen(64, 64, 64, 16, 16, 16, dtype=T.float32, accum_dtype=T.float32)
96+
97+
98+
# --- Correctness tests (require Metal hardware) ---
99+
100+
101+
@tilelang.testing.requires_metal
102+
def test_correctness_16x16x16():
103+
assert_simdgroup_store_correctness(128, 128, 128, 16, 16, 16)
104+
105+
106+
@tilelang.testing.requires_metal
107+
def test_correctness_32x32x32():
108+
assert_simdgroup_store_correctness(128, 128, 128, 32, 32, 32)
109+
110+
111+
@tilelang.testing.requires_metal
112+
def test_correctness_non_square_block():
113+
assert_simdgroup_store_correctness(128, 128, 128, 32, 64, 16)
114+
115+
116+
@tilelang.testing.requires_metal
117+
def test_correctness_64x64x32():
118+
assert_simdgroup_store_correctness(128, 128, 128, 64, 64, 32)
119+
120+
121+
@tilelang.testing.requires_metal
122+
def test_correctness_large_matrix():
123+
assert_simdgroup_store_correctness(1024, 1024, 1024, 32, 32, 32, atol=1.0)
124+
125+
126+
@tilelang.testing.requires_metal
127+
def test_correctness_non_square_matrix():
128+
assert_simdgroup_store_correctness(256, 512, 128, 32, 32, 16)
129+
130+
131+
if __name__ == "__main__":
132+
if torch.mps.is_available():
133+
tilelang.testing.main()

0 commit comments

Comments
 (0)