mirror of
https://github.com/tile-ai/tilelang.git
synced 2026-10-22 06:50:25 +08:00
* [Refactor] Update JIT kernel functions and streamline GEMM tests * Renamed and refactored matmul and run_gemm functions to matmul_kernel_jit and run_gemm_kernel_jit for clarity. * Removed redundant JIT decorator from the matmul function, ensuring it is applied only to the kernel function. * Updated test function names to reflect changes in the kernel functions, enhancing consistency and readability. * Cleaned up commented-out code and unnecessary imports to improve overall code quality. * Update main function call in GEMM test to use tilelang testing framework * Update README and example scripts to include JIT decorator comments * Added comments in README.md and various example scripts to indicate the use of the @tilelang.jit decorator for returning torch functions. * Removed redundant comments that previously instructed to add the decorator, streamlining the documentation and improving clarity. * Update GEMM test parameters for improved performance * Set num_stages to 0 and adjusted matrix dimensions in test functions to enhance performance and consistency across GEMM tests in test_tilelang_kernel_gemm.py.
182 lines
5.7 KiB
Python
182 lines
5.7 KiB
Python
import tilelang
|
|
import tilelang.language as T
|
|
import torch
|
|
|
|
|
|
# add decorator @tilelang.jit if you want to return a torch function
|
|
# @tilelang.jit
|
|
def tilelang_copy_mask_parallel(M, N, block_M, block_N, dtype="float16"):
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((M, N), dtype),
|
|
B: T.Tensor((M, N), dtype),
|
|
):
|
|
# Initialize Kernel Context
|
|
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=256) as (bx, by):
|
|
A_shared = T.alloc_shared((block_M, block_N), dtype)
|
|
|
|
tx = T.get_thread_binding(0)
|
|
|
|
if tx < 128:
|
|
for i, k in T.Parallel(block_M, block_N):
|
|
A_shared[i, k] = A[by * block_M + i, bx * block_N + k]
|
|
|
|
T.copy(A_shared, B[by * block_M, bx * block_N])
|
|
|
|
return main
|
|
|
|
|
|
def run_tilelang_copy_mask_parallel(M=1024, N=1024, block_M=128, block_N=128, dtype="float16"):
|
|
program = tilelang_copy_mask_parallel(M, N, block_M, block_N, dtype)
|
|
kernel = tilelang.compile(
|
|
program,
|
|
out_idx=[1],
|
|
target="cuda",
|
|
pass_configs={
|
|
"tl.disable_warp_specialized": True,
|
|
"tl.disable_tma_lower": True
|
|
})
|
|
a = torch.randn(M, N, device="cuda", dtype=getattr(torch, dtype))
|
|
b = kernel(a)
|
|
torch.testing.assert_close(b, a, rtol=1e-2, atol=1e-2)
|
|
|
|
|
|
def test_tilelang_copy_mask_parallel():
|
|
run_tilelang_copy_mask_parallel(M=1024, N=1024, block_M=128, block_N=128)
|
|
|
|
|
|
# add decorator @tilelang.jit if you want to return a torch function
|
|
# @tilelang.jit
|
|
def tilelang_copy_mask_copy(M, N, block_M, block_N, dtype="float16"):
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((M, N), dtype),
|
|
B: T.Tensor((M, N), dtype),
|
|
):
|
|
# Initialize Kernel Context
|
|
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=256) as (bx, by):
|
|
A_shared = T.alloc_shared((block_M, block_N), dtype)
|
|
|
|
tx = T.get_thread_binding(0)
|
|
|
|
if tx < 128:
|
|
T.copy(A[by * block_M, bx * block_N], A_shared)
|
|
|
|
T.copy(A_shared, B[by * block_M, bx * block_N])
|
|
|
|
return main
|
|
|
|
|
|
def run_tilelang_copy_mask_copy(M=1024, N=1024, block_M=128, block_N=128, dtype="float16"):
|
|
program = tilelang_copy_mask_copy(M, N, block_M, block_N, dtype)
|
|
kernel = tilelang.compile(
|
|
program,
|
|
out_idx=[1],
|
|
target="cuda",
|
|
pass_configs={
|
|
"tl.disable_warp_specialized": True,
|
|
"tl.disable_tma_lower": True
|
|
})
|
|
a = torch.randn(M, N, device="cuda", dtype=getattr(torch, dtype))
|
|
b = kernel(a)
|
|
torch.testing.assert_close(b, a, rtol=1e-2, atol=1e-2)
|
|
|
|
|
|
def test_tilelang_copy_mask_copy():
|
|
run_tilelang_copy_mask_copy(M=1024, N=1024, block_M=128, block_N=128)
|
|
|
|
|
|
# add decorator @tilelang.jit if you want to return a torch function
|
|
# @tilelang.jit
|
|
def tilelang_copy_mask_parallel_range(M, N, block_M, block_N, dtype="float16"):
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((M, N), dtype),
|
|
B: T.Tensor((M, N), dtype),
|
|
):
|
|
# Initialize Kernel Context
|
|
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=256) as (bx, by):
|
|
A_shared = T.alloc_shared((block_M, block_N), dtype)
|
|
|
|
tx = T.get_thread_binding(0)
|
|
|
|
if tx >= 128 and tx < 256:
|
|
for i, k in T.Parallel(block_M, block_N):
|
|
A_shared[i, k] = A[by * block_M + i, bx * block_N + k]
|
|
|
|
T.copy(A_shared, B[by * block_M, bx * block_N])
|
|
|
|
return main
|
|
|
|
|
|
def run_tilelang_copy_mask_parallel_range(M=1024,
|
|
N=1024,
|
|
block_M=128,
|
|
block_N=128,
|
|
dtype="float16"):
|
|
program = tilelang_copy_mask_parallel_range(M, N, block_M, block_N, dtype)
|
|
kernel = tilelang.compile(
|
|
program,
|
|
out_idx=[1],
|
|
target="cuda",
|
|
pass_configs={
|
|
"tl.disable_warp_specialized": True,
|
|
"tl.disable_tma_lower": True
|
|
})
|
|
a = torch.randn(M, N, device="cuda", dtype=getattr(torch, dtype))
|
|
b = kernel(a)
|
|
torch.testing.assert_close(b, a, rtol=1e-2, atol=1e-2)
|
|
|
|
|
|
def test_tilelang_copy_mask_parallel_range():
|
|
run_tilelang_copy_mask_parallel_range(M=1024, N=1024, block_M=128, block_N=128)
|
|
|
|
|
|
# add decorator @tilelang.jit if you want to return a torch function
|
|
# @tilelang.jit
|
|
def tilelang_copy_mask_copy_range(M, N, block_M, block_N, dtype="float16"):
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor((M, N), dtype),
|
|
B: T.Tensor((M, N), dtype),
|
|
):
|
|
# Initialize Kernel Context
|
|
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=256) as (bx, by):
|
|
A_shared = T.alloc_shared((block_M, block_N), dtype)
|
|
|
|
tx = T.get_thread_binding(0)
|
|
|
|
if tx >= 128 and tx < 256:
|
|
T.copy(A[by * block_M, bx * block_N], A_shared)
|
|
|
|
T.copy(A_shared, B[by * block_M, bx * block_N])
|
|
|
|
return main
|
|
|
|
|
|
def run_tilelang_copy_mask_copy_range(M=1024, N=1024, block_M=128, block_N=128, dtype="float16"):
|
|
program = tilelang_copy_mask_copy_range(M, N, block_M, block_N, dtype)
|
|
kernel = tilelang.compile(
|
|
program,
|
|
out_idx=[1],
|
|
target="cuda",
|
|
pass_configs={
|
|
"tl.disable_warp_specialized": True,
|
|
"tl.disable_tma_lower": True
|
|
})
|
|
a = torch.randn(M, N, device="cuda", dtype=getattr(torch, dtype))
|
|
b = kernel(a)
|
|
torch.testing.assert_close(b, a, rtol=1e-2, atol=1e-2)
|
|
|
|
|
|
def test_tilelang_copy_mask_copy_range():
|
|
run_tilelang_copy_mask_copy_range(M=1024, N=1024, block_M=128, block_N=128)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_tilelang_copy_mask_copy_range()
|