mirror of
https://github.com/tile-ai/tilelang.git
synced 2026-10-20 06:30:07 +08:00
* remove llvm build * [Refactor] Update kernel compilation and profiling in examples - Replaced `tilelang.lower` with `tilelang.compile` in multiple example scripts to streamline kernel compilation. - Updated profiling calls to utilize the new `get_profiler` method, enhancing performance measurement consistency. - Adjusted assertions and benchmarking methods to align with the new profiling structure across various examples, ensuring correctness and clarity in performance evaluations. * lint fix * License Update * [Refactor] Improve code formatting and documentation in CUDA header and HIP runtime files - Adjusted formatting in `cuda.h` for better readability, including alignment of comments and struct fields. - Cleaned up whitespace and improved comment clarity in `rt_mod_hip.cc` to enhance code maintainability. * [Refactor] Enhance formatting and clarity in CUDA header and HIP runtime files - Improved comment alignment and readability in `cuda.h`. - Cleaned up whitespace and formatting in `rt_mod_hip.cc` to enhance maintainability. * lint fix * lint fix * lint fix * lint fix * fix * License update * [Enhancement] Update JITKernel to use artifact for kernel source - Assigned the generated artifact to `self.artifact` for better management. - Updated kernel source references to use `artifact.kernel_source` for consistency in execution backend handling. * lint fix * Add @tilelang.testing.requires_llvm decorator to vectorization tests * Enhance setup.py and env.py for library management - Added functionality to remove original files after copying in CMakeBuild. - Updated TVM_LIBRARY_PATH in env.py to include the PyPI build library path for better integration. * Refactor TVM_LIBRARY_PATH assignment for improved readability in env.py * Refactor CMakeBuild file handling in setup.py - Added a check to ensure the target library directory exists before copying .so files. - Improved the logic for creating the target directory and copying files to enhance robustness. * bugfix * Rename BuildTLDebug to BuildTileLangCUDAWithoutCompile and update registration. Add @tilelang.testing.requires_llvm decorator to multiple tests for LLVM requirement. * lint fix * Enhance TileLang code generation by adding support for device code generation without compilation. Updated `host_codegen` and `device_codegen` functions to include new transformations and registration for `tilelang_hip_without_compile`. Refactored JIT kernel adapters to accommodate host and device modules, improving overall integration and flexibility. * lint fix * Add support for C target in device code generation - Updated `device_codegen_without_compile` to include handling for the C target by registering the `tilelang_cpp` function. * [Enhancement] Implement auto-clear cache feature based on environment variable * Added TILELANG_CLEAR_CACHE environment variable to control cache clearing. * Updated CI workflow to set TILELANG_CLEAR_CACHE during testing. * Modified cache initialization to clear cache if TILELANG_CLEAR_CACHE is set to true. * [Refactor] Update kernel invocation and import paths in tests and cache * Changed kernel invocation in `test_tilelang_kernel_dequantize_gemm.py` to return the result. * Updated import statements in `test_tilelang_kernel_int4_gemm_mma.py` to use `bitblas` instead of `tilelang`. * Refactored paths for artifact and parameters in `kernel_cache.py` for better maintainability. * [Refactor] Clean up whitespace and improve code formatting in kernel_cache.py * Removed unnecessary blank lines and adjusted spacing for better readability in the KernelCache class. * Enhanced overall code formatting to align with project standards. * [Enhancement] Add bfloat16 test case and improve kernel caching logic * Introduced a new test case for bfloat16 matrix multiplication in `test_tilelang_kernel_gemm_mma_intrinsic.py`. * Updated `KernelCache` to handle multiple kernel source files and improve error handling during saving and loading. * Refactored `JITKernel` to support instantiation from a database, enhancing flexibility in kernel management. * Adjusted `CtypesKernelAdapter` and `CythonKernelAdapter` to utilize the new kernel loading mechanism from the database. * Improved code formatting and readability across several files. * lint fix * Update bfloat16 matrix multiplication test case to use larger dimensions for improved coverage
182 lines
6.0 KiB
Python
182 lines
6.0 KiB
Python
import torch
|
|
import torch.backends
|
|
import tilelang.testing
|
|
from tilelang import tvm as tvm
|
|
from tvm import DataType
|
|
import tilelang.language as T
|
|
from tilelang import JITKernel
|
|
from tilelang.transform.simplify import apply_simplify
|
|
from tilelang.utils.tensor import map_torch_type
|
|
from typing import Optional
|
|
|
|
tilelang.testing.set_random_seed(0)
|
|
|
|
|
|
def gemv_simt(
|
|
M: int,
|
|
N: int,
|
|
K: int,
|
|
in_dtype: str,
|
|
out_dtype: str,
|
|
accum_dtype: str,
|
|
trans_A: bool,
|
|
trans_B: bool,
|
|
with_bias: bool = False,
|
|
n_partition: Optional[int] = 4,
|
|
reduce_thread: Optional[int] = 32,
|
|
):
|
|
assert n_partition is not None, "n_partition must be provided"
|
|
assert reduce_thread is not None, (
|
|
"reduce_thread must be provided currently, as related bitblas.gpu.gemv.GEMV"
|
|
"sch_outer_reduction_with_config is not implemented")
|
|
|
|
assert isinstance(N, int) and isinstance(K, int), "Do not support dynamic N and K Currently"
|
|
|
|
assert trans_A is False, "Dequantize only implement for trans_A=False currently"
|
|
assert trans_B is True, "Dequantize only implement for trans_B=TRue currently"
|
|
|
|
MAX_TRANSACTION_SIZE_IN_BITS = 128
|
|
micro_size_k = MAX_TRANSACTION_SIZE_IN_BITS // DataType(in_dtype).bits
|
|
|
|
block_K = reduce_thread * micro_size_k
|
|
|
|
A_shape = (M, K)
|
|
B_shape = (N, K)
|
|
Bias_shape = (N,)
|
|
C_shape = (M, N)
|
|
|
|
dp4a_size = 4
|
|
use_dp4a = in_dtype == "int8" and accum_dtype == "int32"
|
|
|
|
@T.prim_func
|
|
def main(
|
|
A: T.Buffer(A_shape, in_dtype),
|
|
B: T.Buffer(B_shape, in_dtype),
|
|
Bias: T.Buffer(Bias_shape, out_dtype),
|
|
C: T.Buffer(C_shape, out_dtype),
|
|
):
|
|
with T.Kernel(
|
|
T.ceildiv(N, n_partition), M, threads=(reduce_thread, n_partition)) as (
|
|
bx,
|
|
by,
|
|
):
|
|
A_local = T.alloc_local((micro_size_k,), in_dtype)
|
|
B_local = T.alloc_local((micro_size_k,), in_dtype)
|
|
accum_res = T.alloc_local((1,), accum_dtype)
|
|
reduced_accum_res = T.alloc_local((1,), accum_dtype)
|
|
|
|
kr = T.get_thread_binding(0)
|
|
ni = T.get_thread_binding(1)
|
|
|
|
T.clear(accum_res)
|
|
for ko in T.serial(T.ceildiv(K, block_K)):
|
|
for v in T.vectorized(micro_size_k):
|
|
A_local[v] = A[by, ko * block_K + kr * micro_size_k + v]
|
|
|
|
for v in T.vectorized(micro_size_k):
|
|
B_local[v] = B[
|
|
bx * n_partition + ni,
|
|
ko * block_K + kr * micro_size_k + v,
|
|
]
|
|
|
|
if use_dp4a:
|
|
for ki in T.serial(micro_size_k // dp4a_size):
|
|
T.dp4a(
|
|
A_local[ki * dp4a_size],
|
|
B_local[ki * dp4a_size],
|
|
accum_res[0],
|
|
)
|
|
else:
|
|
for ki in T.serial(micro_size_k):
|
|
accum_res[0] += A_local[ki].astype(accum_dtype) * B_local[ki].astype(
|
|
accum_dtype)
|
|
|
|
with T.attr(
|
|
T.comm_reducer(lambda x, y: x + y, [T.Cast(accum_dtype, 0)]),
|
|
"reduce_scope",
|
|
T.reinterpret(T.uint64(0), dtype="handle"),
|
|
):
|
|
T.evaluate(
|
|
T.tvm_thread_allreduce(
|
|
T.uint32(1),
|
|
accum_res[0],
|
|
True,
|
|
reduced_accum_res[0],
|
|
kr,
|
|
dtype="handle",
|
|
))
|
|
if kr == 0:
|
|
if with_bias:
|
|
C[by,
|
|
bx * n_partition + ni] = reduced_accum_res[0] + Bias[bx * n_partition + ni]
|
|
else:
|
|
C[by, bx * n_partition + ni] = reduced_accum_res[0]
|
|
|
|
return apply_simplify(main)
|
|
|
|
|
|
def evaluate_gemv_simt(
|
|
M: int,
|
|
N: int,
|
|
K: int,
|
|
in_dtype: str,
|
|
out_dtype: str,
|
|
accum_dtype: str,
|
|
trans_A: bool = False,
|
|
trans_B: bool = True,
|
|
with_bias: bool = False,
|
|
):
|
|
program = gemv_simt(M, N, K, in_dtype, out_dtype, accum_dtype, trans_A, trans_B, with_bias)
|
|
|
|
kernel = JITKernel(program, target="cuda")
|
|
|
|
in_dtype = map_torch_type(in_dtype)
|
|
out_dtype = map_torch_type(out_dtype)
|
|
accum_dtype = map_torch_type(accum_dtype)
|
|
|
|
if in_dtype in {torch.int8, torch.int32}:
|
|
A = torch.randint(-128, 128, (M, K), dtype=torch.int8).to(in_dtype).cuda()
|
|
B = torch.randint(-128, 128, (N, K), dtype=torch.int8).to(in_dtype).cuda()
|
|
Bias = torch.randint(-128, 128, (N,), dtype=torch.int32).to(accum_dtype).cuda()
|
|
elif in_dtype in {torch.float8_e4m3fn, torch.float8_e5m2}:
|
|
A = torch.randn(M, K).to(in_dtype).cuda()
|
|
B = torch.randn(N, K).to(in_dtype).cuda()
|
|
Bias = torch.randn(N).to(accum_dtype).cuda()
|
|
else:
|
|
A = torch.randn(M, K).to(in_dtype).cuda() - 0.5
|
|
B = torch.randn(N, K).to(in_dtype).cuda() - 0.5
|
|
Bias = torch.randn(N).to(accum_dtype).cuda() - 0.5
|
|
|
|
C = torch.zeros(M, N).to(out_dtype).cuda()
|
|
|
|
if with_bias:
|
|
kernel(A, B, Bias, C)
|
|
else:
|
|
kernel(A, B, C)
|
|
|
|
ref_c = torch.mm(A.to(torch.float32), B.T.to(torch.float32))
|
|
if with_bias:
|
|
ref_c += Bias.to(torch.float32)
|
|
ref_c = ref_c.to(out_dtype)
|
|
print(C)
|
|
print(ref_c)
|
|
tilelang.testing.torch_assert_close(C, ref_c, rtol=1e-2, atol=1e-2)
|
|
|
|
|
|
@tilelang.testing.requires_cuda
|
|
@tilelang.testing.requires_cuda_compute_version(8, 0)
|
|
def test_gemv_simt():
|
|
evaluate_gemv_simt(1, 1024, 1024, "float16", "float16", "float16", with_bias=False)
|
|
evaluate_gemv_simt(1, 1024, 1024, "int8", "int32", "int32", with_bias=False)
|
|
|
|
|
|
@tilelang.testing.requires_cuda
|
|
@tilelang.testing.requires_cuda_compute_version(8, 9)
|
|
def test_gemv_simt_fp8():
|
|
evaluate_gemv_simt(1, 1024, 1024, "e4m3_float8", "float32", "float32", with_bias=False)
|
|
evaluate_gemv_simt(1, 1024, 1024, "e5m2_float8", "float32", "float32", with_bias=False)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
tilelang.testing.main()
|