mirror of
https://github.com/tile-ai/tilelang.git
synced 2026-10-21 06:40:23 +08:00
* [Refactor] Update swizzle layout functions to accept Buffer objects directly * Refactor swizzle layout functions to take Buffer parameters instead of stride and continuous values, improving usability and consistency. * Introduce helper functions to extract stride and continuous dimensions from Buffer objects. * Update related functions and documentation to reflect the new parameter types. * Enhance layout inference logic to merge swizzle layouts based on Buffer objects. * Add new reshape functionality to the Layout class for better shape management. * lint fix * use layout.expand instead of reshape to lift a swizzled layout * lint fix
138 lines
3.5 KiB
Python
138 lines
3.5 KiB
Python
from tilelang import tvm as tvm
|
|
import tilelang.testing
|
|
import tilelang.language as T
|
|
|
|
|
|
def matmul_batched(
|
|
batch,
|
|
M,
|
|
N,
|
|
K,
|
|
block_M,
|
|
block_N,
|
|
block_K,
|
|
trans_A,
|
|
trans_B,
|
|
in_dtype,
|
|
out_dtype,
|
|
accum_dtype,
|
|
num_stages,
|
|
threads,
|
|
):
|
|
A_shape = (batch, K, M) if trans_A else (batch, M, K)
|
|
B_shape = (batch, N, K) if trans_B else (batch, K, N)
|
|
A_shared_shape = (batch, block_K, block_M) if trans_A else (batch, block_M, block_K)
|
|
B_shared_shape = (batch, block_N, block_K) if trans_B else (batch, block_K, block_N)
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Tensor(A_shape, in_dtype),
|
|
B: T.Tensor(B_shape, in_dtype),
|
|
C: T.Tensor((batch, M, N), out_dtype),
|
|
):
|
|
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=threads) as (bx, by):
|
|
A_shared = T.alloc_shared(A_shared_shape, in_dtype)
|
|
B_shared = T.alloc_shared(B_shared_shape, in_dtype)
|
|
C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
|
|
T.disable_warp_group_reg_alloc()
|
|
for b in T.serial(batch):
|
|
T.clear(C_local)
|
|
for k in T.Pipelined(T.ceildiv(K, block_K), num_stages=num_stages):
|
|
if trans_A:
|
|
T.copy(A[b, k * block_K, by * block_M], A_shared[b, :, :])
|
|
else:
|
|
T.copy(A[b, by * block_M, k * block_K], A_shared[b, :, :])
|
|
if trans_B:
|
|
T.copy(B[b, bx * block_N, k * block_K], B_shared[b, :, :])
|
|
else:
|
|
T.copy(B[b, k * block_K, bx * block_N], B_shared[b, :, :])
|
|
T.gemm(A_shared[b, :, :], B_shared[b, :, :], C_local, trans_A, trans_B)
|
|
T.copy(C_local, C[b, by * block_M, bx * block_N])
|
|
|
|
return main
|
|
|
|
|
|
def run_gemm_batched(
|
|
batch,
|
|
M,
|
|
N,
|
|
K,
|
|
trans_A,
|
|
trans_B,
|
|
in_dtype,
|
|
out_dtype,
|
|
dtypeAccum,
|
|
block_M,
|
|
block_N,
|
|
block_K,
|
|
num_stages=0,
|
|
num_threads=128,
|
|
):
|
|
program = matmul_batched(
|
|
batch,
|
|
M,
|
|
N,
|
|
K,
|
|
block_M,
|
|
block_N,
|
|
block_K,
|
|
trans_A,
|
|
trans_B,
|
|
in_dtype,
|
|
out_dtype,
|
|
dtypeAccum,
|
|
num_stages,
|
|
num_threads,
|
|
)
|
|
|
|
kernel = tilelang.compile(
|
|
program,
|
|
out_idx=[2],
|
|
pass_configs={
|
|
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
|
|
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
|
|
},
|
|
)
|
|
profiler = kernel.get_profiler()
|
|
|
|
def ref_program(A, B):
|
|
import torch
|
|
|
|
if trans_A:
|
|
A = A.transpose(-1, -2)
|
|
if trans_B:
|
|
B = B.transpose(-1, -2)
|
|
if in_dtype == T.float32:
|
|
# Convert float32 to tfloat32 because tfloat32 mma cannot truncate
|
|
# float32 automatically, -0x1000 meas
|
|
A = (A.contiguous().view(torch.int32) - 0x1000).view(torch.float32)
|
|
B = (B.contiguous().view(torch.int32) - 0x1000).view(torch.float32)
|
|
C = torch.matmul(A.to(torch.float), B.to(torch.float))
|
|
C = C.to(torch.__getattribute__(out_dtype))
|
|
return C
|
|
|
|
profiler.assert_allclose(ref_program, atol=1e-2, rtol=1e-2)
|
|
|
|
|
|
@tilelang.testing.requires_cuda
|
|
def test_gemm_f16f16f16_nn():
|
|
run_gemm_batched(
|
|
2,
|
|
64,
|
|
64,
|
|
32,
|
|
False,
|
|
False,
|
|
T.float16,
|
|
T.float16,
|
|
T.float16,
|
|
64,
|
|
64,
|
|
32,
|
|
0,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
tilelang.testing.main()
|