mirror of
https://github.com/tile-ai/tilelang.git
synced 2026-10-26 07:22:54 +08:00
* Fix indentation in JIT adapter wrapper to ensure consistent formatting of return statement in generated C code. * Enhance Fill Operation in TileLang - Updated the Fill constructor to support BufferLoad instances, adding checks for ramp indices and ensuring only stride 1 ramps are processed. - Introduced a region array to manage the bounds of the fill operation, improving error checking for static regions. - Modified the MakeSIMTLoop method to utilize the new region array for loop variable bounds, enhancing flexibility in kernel generation. - Updated the fill and clear functions in fill.py to accept both tir.Buffer and tir.BufferRegion types, improving usability and type handling. * Refactor Fill Operation and Improve Readability - Simplified the Fill constructor by enhancing the handling of BufferLoad instances and ensuring proper checks for ramp indices. - Improved error messages for region size checks to enhance clarity. - Cleaned up formatting in the Fill method for better readability. - Added a blank line in the matmul function test to improve code organization. - Introduced a blank line in the fill function to enhance readability in fill.py. * Add matrix multiplication functionality and test in TileLang - Introduced a new test file `test_tilelang_language_clear.py` that implements a matrix multiplication function using TileLang's primitives. - The `matmul` function defines a kernel for performing tile-level GEMM operations with customizable block sizes and data types. - Added a `run_matmul` function to compile and execute the kernel, along with a test function to validate the implementation. - Updated the `__init__.py` in the utils module to include `map_torch_type`, enhancing type handling for tensor operations. * lint fix
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import tilelang
|
|
import tilelang.language as T
|
|
|
|
|
|
def matmul(M, N, K, block_M, block_N, block_K, dtype="float16", accum_dtype="float"):
|
|
# add decorator @tilelang.jit if you want to return a torch function
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Buffer((M, K), dtype),
|
|
B: T.Buffer((N, K), dtype),
|
|
C: T.Buffer((M, N), dtype),
|
|
):
|
|
# Initialize Kernel Context
|
|
with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=128) as (bx, by):
|
|
A_shared = T.alloc_shared((block_M, block_K), dtype)
|
|
B_shared = T.alloc_shared((block_N, block_K), dtype)
|
|
C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
|
|
|
|
T.clear(C_local)
|
|
|
|
X_shared = A_shared[:block_M, :block_K]
|
|
X_local = C_local[:block_M, :block_K]
|
|
T.clear(X_local)
|
|
|
|
for ko in T.Pipelined(T.ceildiv(K, block_K), num_stages=0):
|
|
# Copy tile of A
|
|
# This is a sugar syntax for parallelized copy
|
|
T.copy(A[by * block_M, ko * block_K], X_shared)
|
|
|
|
# Demonstrate parallelized copy from global to shared for B
|
|
T.copy(B[bx * block_N, ko * block_K], B_shared[:block_N, :block_K])
|
|
|
|
# Perform a tile-level GEMM on the shared buffers
|
|
# Currently we dispatch to the cute/hip on Nvidia/AMD GPUs
|
|
T.gemm(X_shared, B_shared, C_local, transpose_B=True)
|
|
|
|
# Copy result back to global memory
|
|
T.copy(C_local, C[by * block_M, bx * block_N])
|
|
|
|
return main
|
|
|
|
|
|
def run_matmul(M, N, K, block_M, block_N, block_K, dtype="float16", accum_dtype="float"):
|
|
program = matmul(M, N, K, block_M, block_N, block_K, dtype, accum_dtype)
|
|
kernel = tilelang.compile(program, out_idx=[2], target="cuda")
|
|
kernel.run_once()
|
|
|
|
|
|
def test_matmul():
|
|
run_matmul(1024, 1024, 1024, 128, 128, 32)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_matmul()
|