import triton import triton.language as tl from ...utils._triton.kernel_repr import make_kernel_repr _gemm_splitk_reduce_repr = make_kernel_repr( "_gemm_splitk_reduce_kernel", [ "BLOCK_SIZE_M", "BLOCK_SIZE_N", "ACTUAL_KSPLIT", "MAX_KSPLIT", "ADD_BIAS", "activation", "use_activation", ], name_key="KERNEL_NAME", ) @triton.jit(repr=_gemm_splitk_reduce_repr) def _gemm_splitk_reduce_kernel( c_in_ptr, c_out_ptr, bias_ptr, M, N, stride_c_in_k, stride_c_in_m, stride_c_in_n, stride_c_out_m, stride_c_out_n, BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, ACTUAL_KSPLIT: tl.constexpr, MAX_KSPLIT: tl.constexpr, ADD_BIAS: tl.constexpr, activation: tl.constexpr, use_activation: tl.constexpr, KERNEL_NAME: tl.constexpr = "_gemm_splitk_reduce_kernel", ): tl.assume(stride_c_in_k > 0) tl.assume(stride_c_in_m > 0) tl.assume(stride_c_in_n > 0) tl.assume(stride_c_out_m > 0) tl.assume(stride_c_out_n > 0) pid_m = tl.program_id(axis=0) pid_n = tl.program_id(axis=1) # Tell the AMD backend pid * stride stays non-negative so it can lower # the loads/stores to buffer ops instead of generic global ops. tl.assume(pid_m >= 0) tl.assume(pid_n >= 0) offs_m = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M offs_n = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N offs_k = tl.arange(0, MAX_KSPLIT) c_in_ptrs = ( c_in_ptr + (offs_k[:, None, None] * stride_c_in_k) + (offs_m[None, :, None] * stride_c_in_m) + (offs_n[None, None, :] * stride_c_in_n) ) if ACTUAL_KSPLIT == MAX_KSPLIT: c = tl.load(c_in_ptrs) else: c = tl.load(c_in_ptrs, mask=offs_k[:, None, None] < ACTUAL_KSPLIT, other=0.0) c = tl.sum(c, axis=0) if ADD_BIAS: acc_dtype = tl.float32 if c_in_ptr.type.element_ty != tl.int8 else tl.int32 bias = tl.load(bias_ptr + offs_n).to(dtype=acc_dtype) bias = tl.broadcast_to(bias[None, :], (BLOCK_SIZE_M, BLOCK_SIZE_N)) c += bias if use_activation: c = activation(c) c = c.to(c_out_ptr.type.element_ty) c_out_ptrs = ( c_out_ptr + (offs_m[:, None] * stride_c_out_m) + (offs_n[None, :] * stride_c_out_n) ) tl.store(c_out_ptrs, c)