mirror of
https://github.com/tile-ai/tilelang.git
synced 2026-10-26 07:22:54 +08:00
* [Example] Migrate eligible examples to eager style * fix wrong shape annotation in gemm_split_k examples * fix incorrect dtype in gemm_streamk example * fix * revert some changes * Refactor attention functions to use new parameter structures and improve readability. Updated `example_amd_flash_attn_bwd.py` and `example_amd_flash_attn_fwd.py` to define input parameters more clearly. Enhanced shared memory usage in `flashattn_fwd` and `flashattn_sink` functions across various examples, ensuring compatibility with new configurations. Adjusted kernel definitions and added necessary checks for GPU capabilities. * lint fix * FIX --------- Co-authored-by: LeiWang1999 <leiwang1999@outlook.com>
73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
import tilelang
|
|
import tilelang.language as T
|
|
|
|
|
|
@tilelang.jit
|
|
def matmul(A, B, C, block_M, block_N, block_K, split_k, dtype=T.float16, accum_dtype=T.float32, out_dtype=T.float32):
|
|
M, N, K = T.const("M, N, K")
|
|
splitK = K // split_k
|
|
|
|
A: T.Tensor((M, K), dtype)
|
|
B: T.Tensor((K, N), dtype)
|
|
C: T.Tensor((M, N), out_dtype)
|
|
|
|
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), split_k, threads=128) as (bx, by, bz):
|
|
A_shared = T.alloc_shared((block_M, block_K), dtype)
|
|
B_shared = T.alloc_shared((block_K, block_N), dtype)
|
|
C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
|
|
|
|
T.clear(C_local)
|
|
for ko in T.Pipelined(T.ceildiv(splitK, block_K), num_stages=0):
|
|
T.copy(A[by * block_M, bz * splitK + ko * block_K], A_shared)
|
|
T.copy(B[bz * splitK + ko * block_K, bx * block_N], B_shared)
|
|
T.gemm(A_shared, B_shared, C_local)
|
|
|
|
for i, j in T.Parallel(block_M, block_N):
|
|
T.atomic_add(C[by * block_M + i, bx * block_N + j], C_local[i, j])
|
|
|
|
|
|
def main():
|
|
M = 1024
|
|
N = 1024
|
|
K = 1024
|
|
block_M = 128
|
|
block_N = 128
|
|
block_K = 32
|
|
split_k = 4
|
|
|
|
import torch
|
|
|
|
torch.random.manual_seed(42)
|
|
a = torch.randn(M, K).cuda().half()
|
|
b = torch.randn(K, N).cuda().half()
|
|
c = torch.zeros(M, N).cuda().float()
|
|
matmul(a, b, c, block_M, block_N, block_K, split_k)
|
|
|
|
ref_c = a @ b
|
|
|
|
torch.testing.assert_close(c, ref_c.to(c.dtype), rtol=1e-2, atol=1e-2)
|
|
|
|
|
|
def run_regression_perf():
|
|
M = 4096
|
|
N = 4096
|
|
K = 4096
|
|
block_M = 128
|
|
block_N = 128
|
|
block_K = 32
|
|
split_k = 4
|
|
|
|
import torch
|
|
|
|
torch.random.manual_seed(42)
|
|
a = torch.randn(M, K).cuda().half()
|
|
b = torch.randn(K, N).cuda().half()
|
|
c = torch.zeros(M, N).cuda().float()
|
|
from tilelang.profiler import do_bench
|
|
|
|
return do_bench(lambda: matmul(a, b, c, block_M, block_N, block_K, split_k), backend="cupti")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|