mirror of
https://github.com/tile-ai/tilelang.git
synced 2026-10-23 07:00:29 +08:00
* [Refactor] Move backend-specific GEMM implementations and transforms into backend directories Restructure the codebase so that each backend (cpu, cuda, rocm) owns its GEMM implementations, sparse GEMM implementations, and transform passes under a consistent op/ subdirectory layout. The shared GEMM registry and base classes remain in tileop/ as the platform-agnostic dispatch layer. - Move gemm/gemm_sp registries from backend/ into tileop/ as registry.py - Move CUDA GEMM impls (mma, mma_sm70, wgmma, tcgen05) into backend/cuda/op/gemm/ - Move CUDA sparse GEMM impl into backend/cuda/op/gemm_sp/ - Move CPU GEMM impl (scalar) into backend/cpu/op/gemm/ - Move ROCm GEMM impls (mfma, wmma) into backend/rocm/op/gemm/ - Move CUDA-specific transform passes from src/transform/ into src/backend/cuda/transform/ - Move CUDA runtime sources from src/runtime/ into src/backend/cuda/ - Remove dead backend-importing wrappers from transform/__init__.py - Update phase.py and tests to import CUDA transforms from their canonical location Each backend now has a symmetric op/ directory structure. Adding a new backend no longer requires modifying shared transform or tileop modules. * Refactor Python backend package layout * Remove deprecated intrinsic implementations and related tests for matrix multiplication and element-wise addition. This includes the deletion of files for `benchmark_matmul_intrinsic.py`, `example_tilelang_gemm_fp8_intrinsic.py`, and associated test files, streamlining the codebase by eliminating unused components. * Move CUDA transform passes back to common transform
291 lines
7.9 KiB
Python
291 lines
7.9 KiB
Python
import tilelang.testing
|
|
import tilelang as tl
|
|
from tilelang import language as T
|
|
import torch
|
|
import pytest
|
|
|
|
|
|
def reshape_test(N, M, dtype):
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((N,), dtype),
|
|
B: T.Tensor((N // M, M), dtype),
|
|
):
|
|
with T.Kernel(1) as _:
|
|
A_reshaped = T.reshape(A, [N // M, M])
|
|
T.copy(A_reshaped, B)
|
|
|
|
return main
|
|
|
|
|
|
def run_reshape(N, M, dtype):
|
|
program = reshape_test(N, M, dtype)
|
|
# TODO(lei): reshape cannot apply shared memory
|
|
# layout transform propagation
|
|
jit_kernel = tl.compile(
|
|
program,
|
|
out_idx=-1,
|
|
pass_configs={tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True},
|
|
)
|
|
profiler = jit_kernel.get_profiler()
|
|
|
|
def ref_program(A):
|
|
return A.reshape(N // M, M)
|
|
|
|
profiler.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)
|
|
|
|
|
|
def test_reshape_smem():
|
|
# Test reshape
|
|
run_reshape(1024, 32, T.float32)
|
|
run_reshape(2048, 64, T.float16)
|
|
|
|
|
|
def reshape_test_smem_1d_2_2d(N, M, dtype):
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((N,), dtype),
|
|
B: T.Tensor((N // M, M), dtype),
|
|
):
|
|
with T.Kernel(1) as _:
|
|
A_shared = T.alloc_shared((N,), dtype)
|
|
for i in T.Parallel(N):
|
|
A_shared[i] = A[i]
|
|
|
|
A_smem_reshaped = T.reshape(A_shared, [N // M, M])
|
|
T.copy(A_smem_reshaped, B)
|
|
|
|
return main
|
|
|
|
|
|
def run_reshape_smem_1d_2_2d(N, M, dtype):
|
|
program = reshape_test_smem_1d_2_2d(N, M, dtype)
|
|
# TODO(lei): reshape cannot apply shared memory
|
|
# layout transform propagation
|
|
jit_kernel = tl.compile(
|
|
program,
|
|
out_idx=-1,
|
|
pass_configs={tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True},
|
|
)
|
|
profiler = jit_kernel.get_profiler()
|
|
|
|
def ref_program(A):
|
|
return A.reshape(N // M, M)
|
|
|
|
profiler.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)
|
|
|
|
|
|
def test_reshape_smem_1d_2_2d():
|
|
run_reshape_smem_1d_2_2d(1024, 32, T.float32)
|
|
run_reshape_smem_1d_2_2d(2048, 64, T.float16)
|
|
|
|
|
|
def reshape_test_smem_2d_2_1d(N, M, dtype):
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((N // M, M), dtype),
|
|
B: T.Tensor((N,), dtype),
|
|
):
|
|
with T.Kernel(1) as _:
|
|
A_shared = T.alloc_shared((N // M, M), dtype)
|
|
for i, j in T.Parallel(N // M, M):
|
|
A_shared[i, j] = A[i, j]
|
|
|
|
A_smem_reshaped = T.reshape(A_shared, [N])
|
|
T.copy(A_smem_reshaped, B)
|
|
|
|
return main
|
|
|
|
|
|
def run_reshape_smem_2d_2_1d(N, M, dtype):
|
|
program = reshape_test_smem_2d_2_1d(N, M, dtype)
|
|
# TODO(lei): reshape cannot apply shared memory
|
|
# layout transform propagation
|
|
jit_kernel = tl.compile(
|
|
program,
|
|
out_idx=-1,
|
|
pass_configs={tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True},
|
|
)
|
|
profiler = jit_kernel.get_profiler()
|
|
|
|
def ref_program(A):
|
|
return A.reshape(N)
|
|
|
|
profiler.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)
|
|
|
|
|
|
def test_reshape_smem_2d_2_1d():
|
|
run_reshape_smem_2d_2_1d(1024, 32, T.float32)
|
|
run_reshape_smem_2d_2_1d(2048, 64, T.float16)
|
|
|
|
|
|
def reshape_fragment_test(N, M, dtype):
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((N // M, M), dtype),
|
|
B: T.Tensor((N,), dtype),
|
|
):
|
|
with T.Kernel(1, threads=32) as _:
|
|
A_shared = T.alloc_shared((N // M, M), dtype, scope="shared")
|
|
A_local = T.alloc_fragment((N // M, M), dtype)
|
|
B_shared = T.alloc_shared((N,), dtype, scope="shared")
|
|
|
|
T.copy(A, A_shared)
|
|
T.copy(A_shared, A_local)
|
|
A_local_reshape = T.reshape(A_local, [N])
|
|
T.copy(A_local_reshape, B_shared)
|
|
T.copy(B_shared, B)
|
|
|
|
return main
|
|
|
|
|
|
def run_reshape_fragment(N, M, dtype):
|
|
program = reshape_fragment_test(N, M, dtype)
|
|
jit_kernel = tl.compile(
|
|
program,
|
|
out_idx=-1,
|
|
pass_configs={tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True},
|
|
)
|
|
profiler = jit_kernel.get_profiler()
|
|
|
|
def ref_program(A):
|
|
return A.reshape(N)
|
|
|
|
profiler.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)
|
|
|
|
|
|
def test_reshape_fragment():
|
|
run_reshape_fragment(1024, 32, T.float32)
|
|
run_reshape_fragment(2048, 64, T.float16)
|
|
|
|
|
|
def reshape_layout_transform_shared(N, M, dtype):
|
|
from tilelang.cuda.intrinsics.layout.mma_layout import make_mma_swizzle_layout
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((N // M, M), dtype),
|
|
B: T.Tensor((N,), dtype),
|
|
):
|
|
with T.Kernel(1, threads=32) as _:
|
|
A_shared = T.alloc_shared((N // M, M), dtype, scope="shared")
|
|
|
|
T.annotate_layout(
|
|
{
|
|
A_shared: make_mma_swizzle_layout(A_shared),
|
|
}
|
|
)
|
|
T.copy(A, A_shared)
|
|
A_shared_reshape = T.reshape(A_shared, [N])
|
|
T.copy(A_shared_reshape, B)
|
|
|
|
return main
|
|
|
|
|
|
def run_reshape_layout_transform_shared(N, M, dtype):
|
|
program = reshape_layout_transform_shared(N, M, dtype)
|
|
jit_kernel = tl.compile(
|
|
program,
|
|
out_idx=-1,
|
|
pass_configs={tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True},
|
|
)
|
|
profiler = jit_kernel.get_profiler()
|
|
|
|
def ref_program(A):
|
|
return A.reshape(N)
|
|
|
|
profiler.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)
|
|
|
|
|
|
def test_reshape_layout_transform_shared():
|
|
run_reshape_layout_transform_shared(1024, 32, T.float32)
|
|
run_reshape_layout_transform_shared(2048, 64, T.float16)
|
|
|
|
|
|
def reduce_after_reshape_test(N, M, dtype):
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((N,), dtype),
|
|
B: T.Tensor((N // M,), dtype),
|
|
):
|
|
with T.Kernel(1, threads=32) as _:
|
|
A_shared = T.alloc_shared((N,), dtype, scope="shared")
|
|
A_local = T.alloc_fragment((N,), dtype)
|
|
B_local = T.alloc_fragment((N // M,), dtype)
|
|
|
|
T.copy(A, A_shared)
|
|
T.copy(A_shared, A_local)
|
|
A_local_reshape = T.reshape(A_local, [N // M, M])
|
|
T.reduce_max(A_local_reshape, B_local, dim=1)
|
|
T.copy(B_local, B)
|
|
|
|
return main
|
|
|
|
|
|
def run_reduce_after_reshape(N, M, dtype):
|
|
program = reduce_after_reshape_test(N, M, dtype)
|
|
jit_kernel = tl.compile(
|
|
program,
|
|
out_idx=-1,
|
|
pass_configs={tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True},
|
|
)
|
|
profiler = jit_kernel.get_profiler()
|
|
|
|
def ref_program(A):
|
|
return torch.max(A.reshape(N // M, M), dim=1).values
|
|
|
|
profiler.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)
|
|
|
|
|
|
def test_reduce_after_reshape():
|
|
run_reduce_after_reshape(1024, 32, T.float32)
|
|
run_reduce_after_reshape(2048, 64, T.float16)
|
|
|
|
|
|
def reshape_shape_mismatch_test(N, M, dtype):
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((N,), dtype),
|
|
B: T.Tensor((N // M, M), dtype),
|
|
):
|
|
with T.Kernel(1) as _:
|
|
A_reshaped = T.reshape(A, [N // M, M + 1])
|
|
T.copy(A_reshaped, B)
|
|
|
|
return main
|
|
|
|
|
|
def test_reshape_shape_mismatch():
|
|
with pytest.raises(AssertionError):
|
|
reshape_shape_mismatch_test(1024, 32, T.float32)
|
|
|
|
|
|
def test_reduce_absmax_after_reshape_3d():
|
|
M, N, num_groups, num_per_channels = 2, 384, 3, 128
|
|
threads = 128
|
|
dtype = "int64"
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((M, N), dtype),
|
|
B: T.Tensor((M, num_groups), dtype),
|
|
):
|
|
with T.Kernel(1, threads=threads) as _:
|
|
A_local = T.alloc_fragment((M, N), dtype)
|
|
A_reshaped = T.reshape(A_local, [M, num_groups, num_per_channels])
|
|
B_local = T.alloc_fragment((M, num_groups), dtype)
|
|
T.copy(A, A_local)
|
|
T.reduce_absmax(A_reshaped, B_local, dim=2)
|
|
T.copy(B_local, B)
|
|
|
|
jit_kernel = tl.compile(main, out_idx=-1)
|
|
A_torch = torch.randint(-100, 100, (M, N), dtype=getattr(torch, dtype)).cuda()
|
|
B_torch = jit_kernel(A_torch)
|
|
|
|
ref = A_torch.abs().reshape(M, num_groups, num_per_channels).max(dim=2).values
|
|
torch.testing.assert_close(B_torch, ref)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
tilelang.testing.main()
|