Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

gemv updates

Eugene Bokhana709db18d8a4
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/gemv.metal b/crates/uzu/src/backends/metal/kernel/matmul/gemv.metal
index c93b3510a..e2984b527 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/gemv.metal
+++ b/crates/uzu/src/backends/metal/kernel/matmul/gemv.metal
@@ -1,600 +1,276 @@
// SPDX-License-Identifier: MIT
+#include <metal_simdgroup>
#include <metal_stdlib>
using namespace metal;
template <
- typename T,
- typename AccT,
- int ROWS_PER_SIMDGROUP,
- int ELEMS_PER_THREAD>
-METAL_FUNC void gemv_multirow(
- const device T* __restrict weights,
- const device T* __restrict input,
- device T* __restrict output,
- const int input_dim,
- const int output_dim,
- const int weight_stride,
- const int batch_idx,
- const int input_batch_stride,
- const int output_batch_stride,
- uint row_base,
- uint simd_lid
-) {
-
- constexpr int BLOCK_K = 32 * ELEMS_PER_THREAD;
-
- input += batch_idx * input_batch_stride;
- output += batch_idx * output_batch_stride;
-
- if (row_base >= static_cast<uint>(output_dim)) {
- return;
- }
-
- const int rows_to_compute =
- min(ROWS_PER_SIMDGROUP, output_dim - static_cast<int>(row_base));
-
- AccT accumulators[ROWS_PER_SIMDGROUP];
-#pragma unroll(ROWS_PER_SIMDGROUP)
- for (int r = 0; r < ROWS_PER_SIMDGROUP; ++r) {
- accumulators[r] = AccT(0);
- }
-
- AccT input_cache[ELEMS_PER_THREAD];
-
- const device T* weight_row_ptrs[ROWS_PER_SIMDGROUP];
-#pragma unroll(ROWS_PER_SIMDGROUP)
- for (int r = 0; r < ROWS_PER_SIMDGROUP; ++r) {
- weight_row_ptrs[r] = weights + (row_base + r) * weight_stride;
- }
-
- int k = 0;
- for (; k + BLOCK_K <= input_dim; k += BLOCK_K) {
- const int thread_k = k + simd_lid * ELEMS_PER_THREAD;
-
-#pragma unroll(ELEMS_PER_THREAD)
- for (int e = 0; e < ELEMS_PER_THREAD; ++e) {
- input_cache[e] = static_cast<AccT>(input[thread_k + e]);
- }
-
-#pragma unroll(ROWS_PER_SIMDGROUP)
- for (int r = 0; r < ROWS_PER_SIMDGROUP; ++r) {
- AccT dot = 0;
-#pragma unroll(ELEMS_PER_THREAD)
- for (int e = 0; e < ELEMS_PER_THREAD; ++e) {
- dot += input_cache[e] *
- static_cast<AccT>(weight_row_ptrs[r][thread_k + e]);
- }
- accumulators[r] += dot;
- }
- }
-
- if (k < input_dim) {
- const int thread_k = k + simd_lid * ELEMS_PER_THREAD;
- const int remaining = input_dim - k;
-
-#pragma unroll(ELEMS_PER_THREAD)
- for (int e = 0; e < ELEMS_PER_THREAD; ++e) {
- const int idx = simd_lid * ELEMS_PER_THREAD + e;
- input_cache[e] =
- (idx < remaining) ? static_cast<AccT>(input[thread_k + e]) : AccT(0);
+ typename DataType,
+ typename AccumulatorType,
+ ushort SIMDGROUPS_PER_ROW,
+ ushort SIMDGROUPS_PER_REDUCTION,
+ ushort THREADS_PER_SIMDGROUP_ROW,
+ ushort THREADS_PER_SIMDGROUP_COL,
+ ushort ELEMENTS_PER_THREAD_ROW,
+ ushort ELEMENTS_PER_THREAD_COL,
+ bool HAS_BIAS>
+struct GemvKernel {
+
+ static constant constexpr ushort TOTAL_THREADS_ROW = SIMDGROUPS_PER_ROW * THREADS_PER_SIMDGROUP_ROW;
+ static constant constexpr ushort TOTAL_THREADS_COL = SIMDGROUPS_PER_REDUCTION * THREADS_PER_SIMDGROUP_COL;
+ static constant constexpr ushort OUTPUT_ELEMENTS_PER_THREADGROUP = TOTAL_THREADS_ROW * ELEMENTS_PER_THREAD_ROW;
+ static constant constexpr ushort INPUT_ELEMENTS_PER_ITERATION = TOTAL_THREADS_COL * ELEMENTS_PER_THREAD_COL;
+ static constant constexpr bool NEEDS_THREADGROUP_REDUCTION = SIMDGROUPS_PER_REDUCTION > 1;
+ static constant constexpr ushort THREADGROUP_MEMORY_SIZE = NEEDS_THREADGROUP_REDUCTION
+ ? SIMDGROUPS_PER_REDUCTION * (OUTPUT_ELEMENTS_PER_THREADGROUP + ELEMENTS_PER_THREAD_ROW)
+ : 0;
+
+ static_assert(THREADS_PER_SIMDGROUP_ROW * THREADS_PER_SIMDGROUP_COL == 32,
+ "simdgroup must have exactly 32 threads");
+
+ template <typename DestinationType>
+ static METAL_FUNC void load_input_elements_unchecked(
+ const device DataType* source,
+ thread DestinationType destination[ELEMENTS_PER_THREAD_COL],
+ int source_offset
+ ) {
+ #pragma unroll(ELEMENTS_PER_THREAD_COL)
+ for (ushort element_index = 0; element_index < ELEMENTS_PER_THREAD_COL; ++element_index) {
+ destination[element_index] = static_cast<DestinationType>(source[source_offset + element_index]);
+ }
}
-#pragma unroll(ROWS_PER_SIMDGROUP)
- for (int r = 0; r < ROWS_PER_SIMDGROUP; ++r) {
- AccT dot = 0;
-#pragma unroll(ELEMS_PER_THREAD)
- for (int e = 0; e < ELEMS_PER_THREAD; ++e) {
- const int idx = simd_lid * ELEMS_PER_THREAD + e;
- if (idx < remaining) {
- dot += input_cache[e] *
- static_cast<AccT>(weight_row_ptrs[r][thread_k + e]);
+ template <typename DestinationType>
+ static METAL_FUNC void load_input_elements_checked(
+ const device DataType* source,
+ thread DestinationType destination[ELEMENTS_PER_THREAD_COL],
+ int source_offset,
+ int source_size
+ ) {
+ #pragma unroll(ELEMENTS_PER_THREAD_COL)
+ for (ushort element_index = 0; element_index < ELEMENTS_PER_THREAD_COL; ++element_index) {
+ int global_index = source_offset + element_index;
+ destination[element_index] = (global_index < source_size)
+ ? static_cast<DestinationType>(source[global_index])
+ : DestinationType(0);
}
- }
- accumulators[r] += dot;
}
- }
-
-#pragma unroll(ROWS_PER_SIMDGROUP)
- for (int r = 0; r < ROWS_PER_SIMDGROUP; ++r) {
- accumulators[r] = simd_sum(accumulators[r]);
- }
- if (simd_lid == 0) {
-#pragma unroll(ROWS_PER_SIMDGROUP)
- for (int r = 0; r < rows_to_compute; ++r) {
- output[row_base + r] = static_cast<T>(accumulators[r]);
- }
- }
-}
-
-#define DECL_GEMV_FAST(NAME, TYPE, ACC) \
- [[kernel]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- device TYPE* output [[buffer(2)]], \
- constant int& input_dim [[buffer(3)]], \
- constant int& output_dim [[buffer(4)]], \
- constant int& weight_stride [[buffer(5)]], \
- constant int& input_batch_stride [[buffer(6)]], \
- constant int& output_batch_stride [[buffer(7)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_lid [[thread_index_in_simdgroup]] \
- ) { \
- constexpr int ROWS_PER_SIMDGROUP = 4; \
- constexpr int ELEMS_PER_THREAD = 4; \
- const uint row_base = tgid.x * ROWS_PER_SIMDGROUP; \
- gemv_multirow<TYPE, ACC, ROWS_PER_SIMDGROUP, ELEMS_PER_THREAD>( \
- weights, \
- input, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- tgid.z, \
- input_batch_stride, \
- output_batch_stride, \
- row_base, \
- simd_lid \
- ); \
- }
-
-DECL_GEMV_FAST(gemv_fast_f16, half, float)
-DECL_GEMV_FAST(gemv_fast_bf16, bfloat, float)
-DECL_GEMV_FAST(gemv_fast_f32, float, float)
-
-#define DECL_GEMV_FAST_BIAS(NAME, TYPE, ACC) \
- [[kernel]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- const device TYPE* bias [[buffer(2)]], \
- device TYPE* output [[buffer(3)]], \
- constant int& input_dim [[buffer(4)]], \
- constant int& output_dim [[buffer(5)]], \
- constant int& weight_stride [[buffer(6)]], \
- constant int& input_batch_stride [[buffer(7)]], \
- constant int& output_batch_stride [[buffer(8)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_lid [[thread_index_in_simdgroup]] \
- ) { \
- constexpr int ROWS_PER_SIMDGROUP = 4; \
- constexpr int ELEMS_PER_THREAD = 4; \
- const uint row_base = tgid.x * ROWS_PER_SIMDGROUP; \
- gemv_multirow<TYPE, ACC, ROWS_PER_SIMDGROUP, ELEMS_PER_THREAD>( \
- weights, \
- input, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- tgid.z, \
- input_batch_stride, \
- output_batch_stride, \
- row_base, \
- simd_lid \
- ); \
- if (simd_lid == 0 && row_base < static_cast<uint>(output_dim)) { \
- const int rows_to_compute = \
- min(ROWS_PER_SIMDGROUP, output_dim - static_cast<int>(row_base)); \
- device TYPE* out_ptr = output + tgid.z * output_batch_stride; \
- for (int r = 0; r < rows_to_compute; ++r) { \
- const uint row = row_base + static_cast<uint>(r); \
- out_ptr[row] = out_ptr[row] + bias[row]; \
- } \
- } \
- }
-
-DECL_GEMV_FAST_BIAS(gemv_fast_f16_bias, half, float)
-DECL_GEMV_FAST_BIAS(gemv_fast_bf16_bias, bfloat, float)
-DECL_GEMV_FAST_BIAS(gemv_fast_f32_bias, float, float)
-
-template <typename T, typename AccT, ushort ROWS_PER_THREADGROUP, ushort TILE_K>
-METAL_FUNC void gemv_tiled(
- const device T* __restrict weights,
- const device T* __restrict input,
- device T* __restrict output,
- constant uint& input_dim,
- constant uint& output_dim,
- constant uint& weight_stride,
- constant uint& input_stride,
- constant uint& output_stride,
- uint3 tgid,
- uint simd_gid,
- uint simd_lid,
- uint tid,
- threadgroup AccT* input_tile
-) {
-
- const ushort THREADS_PER_THREADGROUP = ROWS_PER_THREADGROUP * 32;
- if (simd_gid >= ROWS_PER_THREADGROUP) {
+ static METAL_FUNC void execute(
+ const device DataType* weight_matrix,
+ const device DataType* input_vector,
+ const device DataType* bias_vector,
+ device DataType* output_vector,
+ constant int& input_dimension,
+ constant int& output_dimension,
+ constant int& weight_row_stride,
+ constant int& input_batch_stride,
+ constant int& output_batch_stride,
+ threadgroup AccumulatorType* shared_memory,
+ uint3 threadgroup_position,
+ uint simdgroup_index,
+ uint thread_index_in_simdgroup
+ ) {
+ thread AccumulatorType accumulated_results[ELEMENTS_PER_THREAD_ROW] = {0};
+ thread DataType weight_elements[ELEMENTS_PER_THREAD_COL];
+ thread AccumulatorType input_elements[ELEMENTS_PER_THREAD_COL];
+
+ int thread_row_in_simdgroup = (THREADS_PER_SIMDGROUP_COL != 32)
+ ? (thread_index_in_simdgroup / THREADS_PER_SIMDGROUP_COL)
+ : 0;
+ int thread_col_in_simdgroup = (THREADS_PER_SIMDGROUP_COL != 32)
+ ? (thread_index_in_simdgroup % THREADS_PER_SIMDGROUP_COL)
+ : int(thread_index_in_simdgroup);
+
+ int simdgroup_reduction_index = NEEDS_THREADGROUP_REDUCTION
+ ? (simdgroup_index % SIMDGROUPS_PER_REDUCTION)
+ : 0;
+
+ int simdgroup_row_offset = NEEDS_THREADGROUP_REDUCTION
+ ? THREADS_PER_SIMDGROUP_ROW * (simdgroup_index / SIMDGROUPS_PER_REDUCTION)
+ : int(THREADS_PER_SIMDGROUP_ROW * simdgroup_index);
+
+ int simdgroup_col_offset = NEEDS_THREADGROUP_REDUCTION
+ ? THREADS_PER_SIMDGROUP_COL * simdgroup_reduction_index
+ : 0;
+
+ int thread_output_offset = (simdgroup_row_offset + thread_row_in_simdgroup) * ELEMENTS_PER_THREAD_ROW;
+ int thread_input_offset = (simdgroup_col_offset + thread_col_in_simdgroup) * ELEMENTS_PER_THREAD_COL;
+
+ int batch_index = threadgroup_position.z;
+ int output_row_start = threadgroup_position.x * OUTPUT_ELEMENTS_PER_THREADGROUP + thread_output_offset;
+
+ if (output_row_start >= output_dimension) {
return;
}
- const uint row = tgid.x * ROWS_PER_THREADGROUP + simd_gid;
- if (row >= output_dim) {
- return;
- }
+ output_row_start = (output_row_start + ELEMENTS_PER_THREAD_ROW <= output_dimension)
+ ? output_row_start
+ : (output_dimension - ELEMENTS_PER_THREAD_ROW);
- const uint batch = tgid.z;
- const device T* weight_row = weights + row * weight_stride;
- const device T* input_ptr = input + batch * input_stride;
- device T* output_ptr = output + batch * output_stride;
+ const device DataType* batch_input = input_vector + batch_index * input_batch_stride;
+ device DataType* batch_output = output_vector + batch_index * output_batch_stride;
+ const device DataType* weight_rows = weight_matrix + output_row_start * weight_row_stride;
- AccT accumulator = AccT(0);
+ int full_iterations = input_dimension / INPUT_ELEMENTS_PER_ITERATION;
+ int remaining_elements = input_dimension - (full_iterations * INPUT_ELEMENTS_PER_ITERATION);
- for (uint k_block = 0; k_block < input_dim; k_block += TILE_K) {
- const uint remaining = input_dim - k_block;
- const uint tile_size = remaining < TILE_K ? remaining : TILE_K;
+ int current_input_offset = thread_input_offset;
- if (tid < tile_size && tid < THREADS_PER_THREADGROUP) {
- input_tile[tid] = static_cast<AccT>(input_ptr[k_block + tid]);
- }
- threadgroup_barrier(mem_flags::mem_threadgroup);
-
- for (uint idx = simd_lid; idx < tile_size; idx += 32) {
- accumulator +=
- static_cast<AccT>(weight_row[k_block + idx]) * input_tile[idx];
- }
- threadgroup_barrier(mem_flags::mem_threadgroup);
- }
+ for (int iteration = 0; iteration < full_iterations; ++iteration) {
+ load_input_elements_unchecked<AccumulatorType>(batch_input, input_elements, current_input_offset);
- for (ushort offset = 16; offset > 0; offset >>= 1) {
- accumulator += simd_shuffle_down(accumulator, offset);
- }
+ int weight_offset = 0;
+ #pragma unroll(ELEMENTS_PER_THREAD_ROW)
+ for (ushort row = 0; row < ELEMENTS_PER_THREAD_ROW; ++row) {
+ load_input_elements_unchecked<DataType>(weight_rows, weight_elements, weight_offset + current_input_offset);
- if (simd_lid == 0) {
- output_ptr[row] = static_cast<T>(accumulator);
- }
-}
+ #pragma unroll(ELEMENTS_PER_THREAD_COL)
+ for (ushort col = 0; col < ELEMENTS_PER_THREAD_COL; ++col) {
+ accumulated_results[row] += static_cast<AccumulatorType>(weight_elements[col]) * input_elements[col];
+ }
-#define DECL_GEMV_TILED(NAME, TYPE, ACC, ROWS, TILE_K) \
- [[kernel, max_total_threads_per_threadgroup(ROWS * 32)]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- device TYPE* output [[buffer(2)]], \
- constant uint& input_dim [[buffer(3)]], \
- constant uint& output_dim [[buffer(4)]], \
- constant uint& weight_stride [[buffer(5)]], \
- constant uint& input_stride [[buffer(6)]], \
- constant uint& output_stride [[buffer(7)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_gid [[simdgroup_index_in_threadgroup]], \
- uint simd_lid [[thread_index_in_simdgroup]], \
- uint tid [[thread_index_in_threadgroup]] \
- ) { \
- threadgroup ACC input_tile[TILE_K]; \
- gemv_tiled<TYPE, ACC, ROWS, TILE_K>( \
- weights, \
- input, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- input_stride, \
- output_stride, \
- tgid, \
- simd_gid, \
- simd_lid, \
- tid, \
- input_tile \
- ); \
- }
+ weight_offset += weight_row_stride;
+ }
-#define DECL_GEMV_TILED_BIAS(NAME, TYPE, ACC, ROWS, TILE_K) \
- [[kernel, max_total_threads_per_threadgroup(ROWS * 32)]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- const device TYPE* bias [[buffer(2)]], \
- device TYPE* output [[buffer(3)]], \
- constant uint& input_dim [[buffer(4)]], \
- constant uint& output_dim [[buffer(5)]], \
- constant uint& weight_stride [[buffer(6)]], \
- constant uint& input_stride [[buffer(7)]], \
- constant uint& output_stride [[buffer(8)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_gid [[simdgroup_index_in_threadgroup]], \
- uint simd_lid [[thread_index_in_simdgroup]], \
- uint tid [[thread_index_in_threadgroup]] \
- ) { \
- threadgroup ACC input_tile[TILE_K]; \
- gemv_tiled<TYPE, ACC, ROWS, TILE_K>( \
- weights, \
- input, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- input_stride, \
- output_stride, \
- tgid, \
- simd_gid, \
- simd_lid, \
- tid, \
- input_tile \
- ); \
- const uint row = tgid.x * ROWS + simd_gid; \
- if (simd_lid == 0 && simd_gid < ROWS && row < output_dim) { \
- device TYPE* out_ptr = output + tgid.z * output_stride; \
- out_ptr[row] = out_ptr[row] + bias[row]; \
- } \
- }
-
-DECL_GEMV_TILED_BIAS(gemv_f16_rows2_bias, half, float, 2, 128)
-DECL_GEMV_TILED_BIAS(gemv_f16_rows4_bias, half, float, 4, 128)
-DECL_GEMV_TILED_BIAS(gemv_f16_rows8_bias, half, float, 8, 128)
-DECL_GEMV_TILED_BIAS(gemv_f16_rows16_bias, half, float, 16, 128)
-
-DECL_GEMV_TILED_BIAS(gemv_bf16_rows2_bias, bfloat, float, 2, 128)
-DECL_GEMV_TILED_BIAS(gemv_bf16_rows4_bias, bfloat, float, 4, 128)
-DECL_GEMV_TILED_BIAS(gemv_bf16_rows8_bias, bfloat, float, 8, 128)
-DECL_GEMV_TILED_BIAS(gemv_bf16_rows16_bias, bfloat, float, 16, 128)
-
-DECL_GEMV_TILED_BIAS(gemv_f32_rows2_bias, float, float, 2, 128)
-DECL_GEMV_TILED_BIAS(gemv_f32_rows4_bias, float, float, 4, 128)
-DECL_GEMV_TILED_BIAS(gemv_f32_rows8_bias, float, float, 8, 128)
-DECL_GEMV_TILED_BIAS(gemv_f32_rows16_bias, float, float, 16, 128)
-
-DECL_GEMV_TILED(gemv_f16_rows2, half, float, 2, 128)
-DECL_GEMV_TILED(gemv_f16_rows4, half, float, 4, 128)
-DECL_GEMV_TILED(gemv_f16_rows8, half, float, 8, 128)
-DECL_GEMV_TILED(gemv_f16_rows16, half, float, 16, 128)
-
-DECL_GEMV_TILED(gemv_bf16_rows2, bfloat, float, 2, 128)
-DECL_GEMV_TILED(gemv_bf16_rows4, bfloat, float, 4, 128)
-DECL_GEMV_TILED(gemv_bf16_rows8, bfloat, float, 8, 128)
-DECL_GEMV_TILED(gemv_bf16_rows16, bfloat, float, 16, 128)
-
-DECL_GEMV_TILED(gemv_f32_rows2, float, float, 2, 128)
-DECL_GEMV_TILED(gemv_f32_rows4, float, float, 4, 128)
-DECL_GEMV_TILED(gemv_f32_rows8, float, float, 8, 128)
-DECL_GEMV_TILED(gemv_f32_rows16, float, float, 16, 128)
-
-template <typename T, typename AccT, ushort KPARTS>
-METAL_FUNC void gemv_split_k_rows16_impl(
- const device T* __restrict weights,
- const device T* __restrict input,
- const device T* __restrict bias,
- device T* __restrict output,
- const int input_dim,
- const int output_dim,
- const int weight_stride,
- const int input_batch_stride,
- const int output_batch_stride,
- uint3 tgid,
- uint simd_gid,
- uint simd_lid,
- threadgroup AccT* partial_sums
-) {
- constexpr ushort rows_per_threadgroup = 16;
- constexpr ushort rows_per_rowgroup = 2;
- constexpr ushort rowgroups = rows_per_threadgroup / rows_per_rowgroup;
- constexpr ushort elems_per_thread = 4;
- constexpr int block_k = 32 * elems_per_thread;
-
- const uint rowgroup_idx = simd_gid / KPARTS;
- const uint kpart_idx = simd_gid - rowgroup_idx * KPARTS;
-
- const uint batch = tgid.z;
- const uint row_base =
- tgid.x * rows_per_threadgroup + rowgroup_idx * rows_per_rowgroup;
-
- const bool valid_rowgroup = row_base < static_cast<uint>(output_dim);
- const uint rows_to_compute = valid_rowgroup
- ? min(static_cast<uint>(rows_per_rowgroup),
- static_cast<uint>(output_dim) - row_base)
- : 0;
-
- const device T* input_ptr = input + batch * input_batch_stride;
- device T* output_ptr = output + batch * output_batch_stride;
-
- AccT accumulators[rows_per_rowgroup] = {AccT(0), AccT(0)};
-
- if (valid_rowgroup) {
- const device T* weight_row_ptrs[rows_per_rowgroup];
- weight_row_ptrs[0] = weights + (row_base + 0) * weight_stride;
- weight_row_ptrs[1] = weights + (row_base + 1) * weight_stride;
-
- for (int k_block = static_cast<int>(kpart_idx) * block_k;
- k_block < input_dim;
- k_block += block_k * static_cast<int>(KPARTS))
- {
- AccT input_cache[elems_per_thread];
-
-#pragma unroll(elems_per_thread)
- for (int e = 0; e < elems_per_thread; ++e) {
- const int col = k_block + static_cast<int>(simd_lid) * elems_per_thread + e;
- input_cache[e] =
- (col < input_dim) ? static_cast<AccT>(input_ptr[col]) : AccT(0);
- }
-
-#pragma unroll(rows_per_rowgroup)
- for (int r = 0; r < rows_per_rowgroup; ++r) {
- AccT dot = 0;
-#pragma unroll(elems_per_thread)
- for (int e = 0; e < elems_per_thread; ++e) {
- const int col =
- k_block + static_cast<int>(simd_lid) * elems_per_thread + e;
- if (col < input_dim) {
- dot += input_cache[e] * static_cast<AccT>(weight_row_ptrs[r][col]);
- }
+ current_input_offset += INPUT_ELEMENTS_PER_ITERATION;
}
- accumulators[r] += dot;
- }
- }
- }
-#pragma unroll(rows_per_rowgroup)
- for (int r = 0; r < rows_per_rowgroup; ++r) {
- accumulators[r] = simd_sum(accumulators[r]);
- }
+ if (remaining_elements > 0) {
+ load_input_elements_checked<AccumulatorType>(batch_input, input_elements, current_input_offset, input_dimension);
+
+ #pragma unroll(ELEMENTS_PER_THREAD_ROW)
+ for (ushort row = 0; row < ELEMENTS_PER_THREAD_ROW; ++row) {
+ load_input_elements_checked<DataType>(
+ weight_rows + row * weight_row_stride,
+ weight_elements,
+ current_input_offset,
+ input_dimension
+ );
+
+ #pragma unroll(ELEMENTS_PER_THREAD_COL)
+ for (ushort col = 0; col < ELEMENTS_PER_THREAD_COL; ++col) {
+ accumulated_results[row] += static_cast<AccumulatorType>(weight_elements[col]) * input_elements[col];
+ }
+ }
+ }
- const uint partial_base =
- rowgroup_idx * (KPARTS * rows_per_rowgroup) + kpart_idx * rows_per_rowgroup;
+ #pragma unroll(ELEMENTS_PER_THREAD_ROW)
+ for (ushort row = 0; row < ELEMENTS_PER_THREAD_ROW; ++row) {
+ #pragma unroll
+ for (ushort shuffle_offset = (THREADS_PER_SIMDGROUP_COL / 2); shuffle_offset >= 1; shuffle_offset >>= 1) {
+ accumulated_results[row] += simd_shuffle_down(accumulated_results[row], shuffle_offset);
+ }
+ }
- if (simd_lid == 0) {
- partial_sums[partial_base + 0] = accumulators[0];
- partial_sums[partial_base + 1] = accumulators[1];
- }
+ if (NEEDS_THREADGROUP_REDUCTION) {
+ threadgroup AccumulatorType* reduction_memory =
+ shared_memory + simdgroup_reduction_index * (OUTPUT_ELEMENTS_PER_THREADGROUP + ELEMENTS_PER_THREAD_ROW)
+ + thread_output_offset;
+
+ if (thread_col_in_simdgroup == 0) {
+ #pragma unroll(ELEMENTS_PER_THREAD_ROW)
+ for (ushort row = 0; row < ELEMENTS_PER_THREAD_ROW; ++row) {
+ reduction_memory[row] = accumulated_results[row];
+ }
+
+ threadgroup_barrier(mem_flags::mem_none);
+
+ if (simdgroup_reduction_index == 0) {
+ #pragma unroll
+ for (ushort other_simdgroup = 1; other_simdgroup < SIMDGROUPS_PER_REDUCTION; ++other_simdgroup) {
+ #pragma unroll(ELEMENTS_PER_THREAD_ROW)
+ for (ushort row = 0; row < ELEMENTS_PER_THREAD_ROW; ++row) {
+ accumulated_results[row] +=
+ reduction_memory[other_simdgroup * (OUTPUT_ELEMENTS_PER_THREADGROUP + ELEMENTS_PER_THREAD_ROW) + row];
+ }
+ }
+ }
+ }
+ }
- threadgroup_barrier(mem_flags::mem_threadgroup);
-
- if (kpart_idx == 0 && simd_lid == 0 && rows_to_compute > 0) {
- for (uint r = 0; r < rows_to_compute; ++r) {
- AccT sum = 0;
- for (uint kp = 0; kp < KPARTS; ++kp) {
- sum += partial_sums[rowgroup_idx * (KPARTS * rows_per_rowgroup) +
- kp * rows_per_rowgroup + r];
- }
- const uint row = row_base + r;
- if (bias != nullptr) {
- sum += static_cast<AccT>(bias[row]);
- }
- output_ptr[row] = static_cast<T>(sum);
+ bool is_first_thread_in_reduction = (simdgroup_col_offset == 0) && (thread_col_in_simdgroup == 0);
+
+ if (is_first_thread_in_reduction) {
+ #pragma unroll(ELEMENTS_PER_THREAD_ROW)
+ for (ushort row = 0; row < ELEMENTS_PER_THREAD_ROW; ++row) {
+ int output_index = output_row_start + row;
+ if (HAS_BIAS) {
+ batch_output[output_index] =
+ static_cast<DataType>(accumulated_results[row]) + bias_vector[output_index];
+ } else {
+ batch_output[output_index] = static_cast<DataType>(accumulated_results[row]);
+ }
+ }
+ }
}
- }
+};
+
+#define INSTANTIATE_GEMV_KERNEL( \
+ kernel_name, data_type, accumulator_type, \
+ simdgroups_per_row, simdgroups_per_reduction, \
+ threads_per_simdgroup_row, threads_per_simdgroup_col, \
+ elements_per_thread_row, elements_per_thread_col, has_bias) \
+ \
+[[kernel, max_total_threads_per_threadgroup( \
+ simdgroups_per_row * simdgroups_per_reduction * 32)]] \
+void kernel_name( \
+ const device data_type* weight_matrix [[buffer(0)]], \
+ const device data_type* input_vector [[buffer(1)]], \
+ const device data_type* bias_vector [[buffer(2)]], \
+ device data_type* output_vector [[buffer(3)]], \
+ constant int& input_dimension [[buffer(4)]], \
+ constant int& output_dimension [[buffer(5)]], \
+ constant int& weight_row_stride [[buffer(6)]], \
+ constant int& input_batch_stride [[buffer(7)]], \
+ constant int& output_batch_stride [[buffer(8)]], \
+ uint3 threadgroup_position [[threadgroup_position_in_grid]], \
+ uint simdgroup_index [[simdgroup_index_in_threadgroup]], \
+ uint thread_index_in_simdgroup [[thread_index_in_simdgroup]] \
+) { \
+ using Kernel = GemvKernel< \
+ data_type, accumulator_type, \
+ simdgroups_per_row, simdgroups_per_reduction, \
+ threads_per_simdgroup_row, threads_per_simdgroup_col, \
+ elements_per_thread_row, elements_per_thread_col, has_bias>; \
+ \
+ threadgroup accumulator_type shared_memory[ \
+ Kernel::THREADGROUP_MEMORY_SIZE == 0 ? 1 : Kernel::THREADGROUP_MEMORY_SIZE]; \
+ \
+ Kernel::execute( \
+ weight_matrix, \
+ input_vector, \
+ bias_vector, \
+ output_vector, \
+ input_dimension, \
+ output_dimension, \
+ weight_row_stride, \
+ input_batch_stride, \
+ output_batch_stride, \
+ shared_memory, \
+ threadgroup_position, \
+ simdgroup_index, \
+ thread_index_in_simdgroup \
+ ); \
}
-#define DECL_GEMV_SPLIT_K_KPARTS2(NAME, TYPE, ACC) \
- [[kernel, max_total_threads_per_threadgroup(512)]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- device TYPE* output [[buffer(2)]], \
- constant int& input_dim [[buffer(3)]], \
- constant int& output_dim [[buffer(4)]], \
- constant int& weight_stride [[buffer(5)]], \
- constant int& input_batch_stride [[buffer(6)]], \
- constant int& output_batch_stride [[buffer(7)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_gid [[simdgroup_index_in_threadgroup]], \
- uint simd_lid [[thread_index_in_simdgroup]] \
- ) { \
- threadgroup ACC partial_sums[8 * 2 * 2]; \
- gemv_split_k_rows16_impl<TYPE, ACC, 2>( \
- weights, \
- input, \
- nullptr, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- input_batch_stride, \
- output_batch_stride, \
- tgid, \
- simd_gid, \
- simd_lid, \
- partial_sums \
- ); \
- }
-
-#define DECL_GEMV_SPLIT_K_KPARTS4(NAME, TYPE, ACC) \
- [[kernel, max_total_threads_per_threadgroup(1024)]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- device TYPE* output [[buffer(2)]], \
- constant int& input_dim [[buffer(3)]], \
- constant int& output_dim [[buffer(4)]], \
- constant int& weight_stride [[buffer(5)]], \
- constant int& input_batch_stride [[buffer(6)]], \
- constant int& output_batch_stride [[buffer(7)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_gid [[simdgroup_index_in_threadgroup]], \
- uint simd_lid [[thread_index_in_simdgroup]] \
- ) { \
- threadgroup ACC partial_sums[8 * 4 * 2]; \
- gemv_split_k_rows16_impl<TYPE, ACC, 4>( \
- weights, \
- input, \
- nullptr, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- input_batch_stride, \
- output_batch_stride, \
- tgid, \
- simd_gid, \
- simd_lid, \
- partial_sums \
- ); \
- }
-
-#define DECL_GEMV_SPLIT_K_KPARTS2_BIAS(NAME, TYPE, ACC) \
- [[kernel, max_total_threads_per_threadgroup(512)]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- const device TYPE* bias [[buffer(2)]], \
- device TYPE* output [[buffer(3)]], \
- constant int& input_dim [[buffer(4)]], \
- constant int& output_dim [[buffer(5)]], \
- constant int& weight_stride [[buffer(6)]], \
- constant int& input_batch_stride [[buffer(7)]], \
- constant int& output_batch_stride [[buffer(8)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_gid [[simdgroup_index_in_threadgroup]], \
- uint simd_lid [[thread_index_in_simdgroup]] \
- ) { \
- threadgroup ACC partial_sums[8 * 2 * 2]; \
- gemv_split_k_rows16_impl<TYPE, ACC, 2>( \
- weights, \
- input, \
- bias, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- input_batch_stride, \
- output_batch_stride, \
- tgid, \
- simd_gid, \
- simd_lid, \
- partial_sums \
- ); \
- }
-
-#define DECL_GEMV_SPLIT_K_KPARTS4_BIAS(NAME, TYPE, ACC) \
- [[kernel, max_total_threads_per_threadgroup(1024)]] void NAME( \
- const device TYPE* weights [[buffer(0)]], \
- const device TYPE* input [[buffer(1)]], \
- const device TYPE* bias [[buffer(2)]], \
- device TYPE* output [[buffer(3)]], \
- constant int& input_dim [[buffer(4)]], \
- constant int& output_dim [[buffer(5)]], \
- constant int& weight_stride [[buffer(6)]], \
- constant int& input_batch_stride [[buffer(7)]], \
- constant int& output_batch_stride [[buffer(8)]], \
- uint3 tgid [[threadgroup_position_in_grid]], \
- uint simd_gid [[simdgroup_index_in_threadgroup]], \
- uint simd_lid [[thread_index_in_simdgroup]] \
- ) { \
- threadgroup ACC partial_sums[8 * 4 * 2]; \
- gemv_split_k_rows16_impl<TYPE, ACC, 4>( \
- weights, \
- input, \
- bias, \
- output, \
- input_dim, \
- output_dim, \
- weight_stride, \
- input_batch_stride, \
- output_batch_stride, \
- tgid, \
- simd_gid, \
- simd_lid, \
- partial_sums \
- ); \
- }
-
-DECL_GEMV_SPLIT_K_KPARTS2(gemv_split_k_f16_rows16_kparts2, half, float)
-DECL_GEMV_SPLIT_K_KPARTS4(gemv_split_k_f16_rows16_kparts4, half, float)
-DECL_GEMV_SPLIT_K_KPARTS2_BIAS(gemv_split_k_f16_rows16_kparts2_bias, half, float)
-DECL_GEMV_SPLIT_K_KPARTS4_BIAS(gemv_split_k_f16_rows16_kparts4_bias, half, float)
-
-DECL_GEMV_SPLIT_K_KPARTS2(gemv_split_k_bf16_rows16_kparts2, bfloat, float)
-DECL_GEMV_SPLIT_K_KPARTS4(gemv_split_k_bf16_rows16_kparts4, bfloat, float)
-DECL_GEMV_SPLIT_K_KPARTS2_BIAS(gemv_split_k_bf16_rows16_kparts2_bias, bfloat, float)
-DECL_GEMV_SPLIT_K_KPARTS4_BIAS(gemv_split_k_bf16_rows16_kparts4_bias, bfloat, float)
-
-DECL_GEMV_SPLIT_K_KPARTS2(gemv_split_k_f32_rows16_kparts2, float, float)
-DECL_GEMV_SPLIT_K_KPARTS4(gemv_split_k_f32_rows16_kparts4, float, float)
-DECL_GEMV_SPLIT_K_KPARTS2_BIAS(gemv_split_k_f32_rows16_kparts2_bias, float, float)
-DECL_GEMV_SPLIT_K_KPARTS4_BIAS(gemv_split_k_f32_rows16_kparts4_bias, float, float)
\ No newline at end of file
+#define INSTANTIATE_GEMV_CONFIG(dtype, acc_type, rows, reduction, thr_row, thr_col, elem_row, elem_col) \
+ INSTANTIATE_GEMV_KERNEL( \
+ gemv_##dtype##_rows##rows##_reduction##reduction##_elements##elem_row, \
+ dtype, acc_type, rows, reduction, thr_row, thr_col, elem_row, elem_col, false) \
+ INSTANTIATE_GEMV_KERNEL( \
+ gemv_##dtype##_rows##rows##_reduction##reduction##_elements##elem_row##_bias, \
+ dtype, acc_type, rows, reduction, thr_row, thr_col, elem_row, elem_col, true)
+
+#define INSTANTIATE_ALL_CONFIGS_FOR_DTYPE(dtype, acc_type) \
+ INSTANTIATE_GEMV_CONFIG(dtype, acc_type, 1, 8, 1, 32, 4, 4) \
+ INSTANTIATE_GEMV_CONFIG(dtype, acc_type, 1, 8, 1, 32, 1, 4) \
+ INSTANTIATE_GEMV_CONFIG(dtype, acc_type, 1, 1, 8, 4, 4, 4) \
+ INSTANTIATE_GEMV_CONFIG(dtype, acc_type, 1, 1, 8, 4, 1, 4) \
+ INSTANTIATE_GEMV_CONFIG(dtype, acc_type, 4, 1, 1, 32, 1, 4) \
+ INSTANTIATE_GEMV_CONFIG(dtype, acc_type, 4, 1, 1, 32, 4, 4) \
+ INSTANTIATE_GEMV_CONFIG(dtype, acc_type, 8, 1, 1, 32, 4, 4)
+
+INSTANTIATE_ALL_CONFIGS_FOR_DTYPE(half, float)
+INSTANTIATE_ALL_CONFIGS_FOR_DTYPE(bfloat, float)
+INSTANTIATE_ALL_CONFIGS_FOR_DTYPE(float, float)
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/gemv.rs b/crates/uzu/src/backends/metal/kernel/matmul/gemv.rs
index e7a0f28f7..bb1e473ff 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/gemv.rs
+++ b/crates/uzu/src/backends/metal/kernel/matmul/gemv.rs
@@ -1,10 +1,6 @@
use std::collections::HashMap;
-use metal::{
- Buffer as MTLBuffer,
- ComputeCommandEncoderRef, ComputePipelineState as MTLComputePipelineState,
- MTLSize,
-};
+use metal::{Buffer as MTLBuffer, ComputeCommandEncoderRef, ComputePipelineState, MTLSize};
use super::arguments::MatmulArguments;
use crate::{
@@ -13,470 +9,210 @@ use crate::{
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-enum PipelineVariant {
- Fast {
- has_bias: bool,
- },
- Tiled {
- rows_per_threadgroup: u32,
- has_bias: bool,
- },
- SplitK {
- rows_per_threadgroup: u32,
- kparts: u32,
- has_bias: bool,
- },
+struct GemvConfiguration {
+ simdgroups_per_row: u32,
+ simdgroups_per_reduction: u32,
+ threads_per_simdgroup_row: u32,
+ threads_per_simdgroup_col: u32,
+ elements_per_thread_row: u32,
+ elements_per_thread_col: u32,
+ has_bias: bool,
}
-pub struct GemvKernel {
- data_type: DataType,
- pipelines: HashMap<PipelineVariant, MTLComputePipelineState>,
-}
-
-impl GemvKernel {
- pub fn new(data_type: DataType) -> Self {
- Self {
- data_type,
- pipelines: HashMap::new(),
- }
- }
-
- fn fast_kernel_name(
- &self,
- has_bias: bool,
- ) -> Result<String, MTLError> {
- let base = match self.data_type {
- DataType::F16 => "gemv_fast_f16",
- DataType::BF16 => "gemv_fast_bf16",
- DataType::F32 => "gemv_fast_f32",
- _ => {
- return Err(MTLError::Generic(format!(
- "Unsupported data type for fast GEMV: {:?}",
- self.data_type
- )));
- },
- };
- Ok(if has_bias {
- format!("{base}_bias")
- } else {
- base.to_string()
- })
- }
-
- fn tiled_kernel_name(
- &self,
- rows_per_threadgroup: u32,
- has_bias: bool,
- ) -> Result<String, MTLError> {
- let prefix = match self.data_type {
- DataType::F16 => "gemv_f16",
- DataType::BF16 => "gemv_bf16",
- DataType::F32 => "gemv_f32",
- _ => {
- return Err(MTLError::Generic(format!(
- "Unsupported data type for tiled GEMV: {:?}",
- self.data_type
- )));
- },
- };
- Ok(if has_bias {
- format!("{prefix}_rows{rows_per_threadgroup}_bias")
- } else {
- format!("{prefix}_rows{rows_per_threadgroup}")
- })
+impl GemvConfiguration {
+ fn output_elements_per_threadgroup(&self) -> u32 {
+ let total_threads_row = self.simdgroups_per_row * self.threads_per_simdgroup_row;
+ total_threads_row * self.elements_per_thread_row
}
- fn split_k_kernel_name(
- &self,
- kparts: u32,
- has_bias: bool,
- ) -> Result<String, MTLError> {
- let dtype = match self.data_type {
- DataType::F16 => "f16",
- DataType::BF16 => "bf16",
- DataType::F32 => "f32",
- _ => {
- return Err(MTLError::Generic(format!(
- "Unsupported data type for split-k GEMV: {:?}",
- self.data_type
- )));
- },
- };
- if !matches!(kparts, 2 | 4) {
- return Err(MTLError::Generic(format!(
- "Unsupported split-k kparts={kparts} (expected 2 or 4)"
- )));
- }
- let base = format!("gemv_split_k_{dtype}_rows16_kparts{kparts}");
- Ok(if has_bias {
- format!("{base}_bias")
- } else {
- base
- })
+ fn total_threads_per_threadgroup(&self) -> u32 {
+ self.simdgroups_per_row * self.simdgroups_per_reduction * 32
}
+}
- fn get_fast_pipeline(
- &mut self,
- context: &MTLContext,
- has_bias: bool,
- ) -> Result<&MTLComputePipelineState, MTLError> {
- let variant = PipelineVariant::Fast {
+fn select_gemv_configuration(input_dimension: i32, output_dimension: i32, has_bias: bool) -> GemvConfiguration {
+ let is_small_input_dimension = input_dimension <= 64;
+ let is_large_input_relative_to_output = input_dimension >= 16 * output_dimension;
+ let is_large_output_dimension = output_dimension >= 4096;
+ let is_very_small_output_dimension = output_dimension < 4;
+
+ if is_small_input_dimension {
+ GemvConfiguration {
+ simdgroups_per_row: 1,
+ simdgroups_per_reduction: 1,
+ threads_per_simdgroup_row: 8,
+ threads_per_simdgroup_col: 4,
+ elements_per_thread_row: if is_very_small_output_dimension { 1 } else { 4 },
+ elements_per_thread_col: 4,
has_bias,
- };
- if !self.pipelines.contains_key(&variant) {
- let kernel_name = self.fast_kernel_name(has_bias)?;
- let pipeline =
- context.compute_pipeline_state(&kernel_name, None)?;
- self.pipelines.insert(variant, pipeline);
}
- Ok(self.pipelines.get(&variant).unwrap())
- }
-
- fn get_tiled_pipeline(
- &mut self,
- context: &MTLContext,
- rows_per_threadgroup: u32,
- has_bias: bool,
- ) -> Result<&MTLComputePipelineState, MTLError> {
- let variant = PipelineVariant::Tiled {
- rows_per_threadgroup,
+ } else if is_large_input_relative_to_output {
+ GemvConfiguration {
+ simdgroups_per_row: 1,
+ simdgroups_per_reduction: 8,
+ threads_per_simdgroup_row: 1,
+ threads_per_simdgroup_col: 32,
+ elements_per_thread_row: if is_very_small_output_dimension { 1 } else { 4 },
+ elements_per_thread_col: 4,
has_bias,
- };
- if !self.pipelines.contains_key(&variant) {
- let kernel_name =
- self.tiled_kernel_name(rows_per_threadgroup, has_bias)?;
- let pipeline =
- context.compute_pipeline_state(&kernel_name, None)?;
- self.pipelines.insert(variant, pipeline);
}
- Ok(self.pipelines.get(&variant).unwrap())
- }
-
- fn get_split_k_pipeline(
- &mut self,
- context: &MTLContext,
- kparts: u32,
- has_bias: bool,
- ) -> Result<&MTLComputePipelineState, MTLError> {
- let variant = PipelineVariant::SplitK {
- rows_per_threadgroup: 16,
- kparts,
+ } else {
+ GemvConfiguration {
+ simdgroups_per_row: if is_large_output_dimension { 8 } else { 4 },
+ simdgroups_per_reduction: 1,
+ threads_per_simdgroup_row: 1,
+ threads_per_simdgroup_col: 32,
+ elements_per_thread_row: if is_very_small_output_dimension { 1 } else { 4 },
+ elements_per_thread_col: 4,
has_bias,
- };
- if !self.pipelines.contains_key(&variant) {
- let kernel_name = self.split_k_kernel_name(kparts, has_bias)?;
- let pipeline =
- context.compute_pipeline_state(&kernel_name, None)?;
- self.pipelines.insert(variant, pipeline);
}
- Ok(self.pipelines.get(&variant).unwrap())
- }
-
- fn can_use_fast_kernel(
- &self,
- args: &MatmulArguments,
- ) -> bool {
- args.input_dim >= 128 && args.input_dim % 4 == 0
}
+}
- fn split_k_kparts(
- &self,
- args: &MatmulArguments,
- ) -> Option<u32> {
- let k = args.input_dim;
- let n = args.output_dim;
- if k >= 8192 && n >= 2048 {
- return Some(4);
- }
- if k >= 4096 && n >= 2048 {
- return Some(2);
+fn kernel_name_for_configuration(data_type: DataType, configuration: &GemvConfiguration) -> Result<String, MTLError> {
+ let type_name = match data_type {
+ DataType::F16 => "half",
+ DataType::BF16 => "bfloat",
+ DataType::F32 => "float",
+ _ => {
+ return Err(MTLError::Generic(format!(
+ "Unsupported data type for GEMV: {:?}",
+ data_type
+ )));
}
- None
- }
-
- fn is_logits_like(
- &self,
- args: &MatmulArguments,
- ) -> bool {
- let k = args.input_dim;
- let n = args.output_dim;
- n >= 50_000 || (n >= 16_384 && n >= k * 7)
- }
+ };
+
+ let base_name = format!(
+ "gemv_{}_rows{}_reduction{}_elements{}",
+ type_name,
+ configuration.simdgroups_per_row,
+ configuration.simdgroups_per_reduction,
+ configuration.elements_per_thread_row
+ );
+
+ Ok(if configuration.has_bias {
+ format!("{}_bias", base_name)
+ } else {
+ base_name
+ })
+}
- fn encode_fast(
- &mut self,
- context: &MTLContext,
- encoder: &ComputeCommandEncoderRef,
- args: MatmulArguments,
- bias: Option<&MTLBuffer>,
- ) -> Result<(), MTLError> {
- let has_bias = bias.is_some();
- let pipeline = self.get_fast_pipeline(context, has_bias)?;
- encoder.set_compute_pipeline_state(pipeline);
+pub struct GemvKernel {
+ data_type: DataType,
+ pipelines: HashMap<GemvConfiguration, ComputePipelineState>,
+}
- encoder.set_buffer(0, Some(args.b), 0);
- encoder.set_buffer(1, Some(args.a), 0);
- if let Some(bias) = bias {
- encoder.set_buffer(2, Some(bias), 0);
- encoder.set_buffer(3, Some(args.d), 0);
- } else {
- encoder.set_buffer(2, Some(args.d), 0);
+impl GemvKernel {
+ pub fn new(data_type: DataType) -> Self {
+ Self {
+ data_type,
+ pipelines: HashMap::new(),
}
-
- let input_dim = args.input_dim;
- let output_dim = args.output_dim;
- let weight_stride = args.ldb;
- let input_batch_stride = args.lda;
- let output_batch_stride = args.ldd;
-
- let base = if has_bias { 4 } else { 3 };
- encoder.set_bytes(
- base,
- std::mem::size_of::<i32>() as u64,
- &input_dim as *const i32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 1,
- std::mem::size_of::<i32>() as u64,
- &output_dim as *const i32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 2,
- std::mem::size_of::<i32>() as u64,
- &weight_stride as *const i32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 3,
- std::mem::size_of::<i32>() as u64,
- &input_batch_stride as *const i32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 4,
- std::mem::size_of::<i32>() as u64,
- &output_batch_stride as *const i32 as *const std::ffi::c_void,
- );
-
- const ROWS_PER_SIMDGROUP: i32 = 4;
- let threadgroup_count_x =
- ((output_dim + ROWS_PER_SIMDGROUP - 1) / ROWS_PER_SIMDGROUP) as u64;
- let threadgroup_count_z = args.batch_count as u64;
-
- let threadgroup_count =
- MTLSize::new(threadgroup_count_x, 1, threadgroup_count_z);
- let threads_per_threadgroup = MTLSize::new(32, 1, 1);
-
- encoder
- .dispatch_thread_groups(threadgroup_count, threads_per_threadgroup);
- Ok(())
}
- fn encode_tiled(
+ fn get_pipeline(
&mut self,
context: &MTLContext,
- encoder: &ComputeCommandEncoderRef,
- args: MatmulArguments,
- rows_per_threadgroup: u32,
- bias: Option<&MTLBuffer>,
- ) -> Result<(), MTLError> {
- let rows_per_threadgroup = rows_per_threadgroup.max(1);
- let has_bias = bias.is_some();
- let pipeline = self.get_tiled_pipeline(
- context,
- rows_per_threadgroup,
- has_bias,
- )?;
- encoder.set_compute_pipeline_state(pipeline);
-
- encoder.set_buffer(0, Some(args.b), 0);
- encoder.set_buffer(1, Some(args.a), 0);
- if let Some(bias) = bias {
- encoder.set_buffer(2, Some(bias), 0);
- encoder.set_buffer(3, Some(args.d), 0);
- } else {
- encoder.set_buffer(2, Some(args.d), 0);
+ configuration: GemvConfiguration,
+ ) -> Result<&ComputePipelineState, MTLError> {
+ if !self.pipelines.contains_key(&configuration) {
+ let kernel_name = kernel_name_for_configuration(self.data_type, &configuration)?;
+ let pipeline = context.compute_pipeline_state(&kernel_name, None)?;
+ self.pipelines.insert(configuration, pipeline);
}
-
- let input_dim = args.input_dim as u32;
- let output_dim = args.output_dim as u32;
- let weight_stride = args.ldb as u32;
- let input_stride = args.lda as u32;
- let output_stride = args.ldd as u32;
-
- let base = if has_bias { 4 } else { 3 };
- encoder.set_bytes(
- base,
- std::mem::size_of::<u32>() as u64,
- &input_dim as *const u32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 1,
- std::mem::size_of::<u32>() as u64,
- &output_dim as *const u32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 2,
- std::mem::size_of::<u32>() as u64,
- &weight_stride as *const u32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 3,
- std::mem::size_of::<u32>() as u64,
- &input_stride as *const u32 as *const std::ffi::c_void,
- );
- encoder.set_bytes(
- base + 4,
- std::mem::size_of::<u32>() as u64,
- &output_stride as *const u32 as *const std::ffi::c_void,
- );
-
- let threads_per_threadgroup =
- MTLSize::new((rows_per_threadgroup * 32) as u64, 1, 1);
- let threadgroup_count_x = ((output_dim + rows_per_threadgroup - 1)
- / rows_per_threadgroup) as u64;
- let threadgroup_count_z = args.batch_count as u64;
- let threadgroup_count =
- MTLSize::new(threadgroup_count_x, 1, threadgroup_count_z);
-
- encoder
- .dispatch_thread_groups(threadgroup_count, threads_per_threadgroup);
- Ok(())
+ Ok(self.pipelines.get(&configuration).unwrap())
}
- fn encode_split_k(
+ fn encode_with_configuration(
&mut self,
context: &MTLContext,
encoder: &ComputeCommandEncoderRef,
- args: MatmulArguments,
- kparts: u32,
+ arguments: MatmulArguments,
+ configuration: GemvConfiguration,
bias: Option<&MTLBuffer>,
) -> Result<(), MTLError> {
- let has_bias = bias.is_some();
- let pipeline = self.get_split_k_pipeline(context, kparts, has_bias)?;
+ let pipeline = self.get_pipeline(context, configuration)?;
encoder.set_compute_pipeline_state(pipeline);
- encoder.set_buffer(0, Some(args.b), 0);
- encoder.set_buffer(1, Some(args.a), 0);
- if let Some(bias) = bias {
- encoder.set_buffer(2, Some(bias), 0);
- encoder.set_buffer(3, Some(args.d), 0);
- } else {
- encoder.set_buffer(2, Some(args.d), 0);
+ encoder.set_buffer(0, Some(arguments.b), 0);
+ encoder.set_buffer(1, Some(arguments.a), 0);
+ if let Some(bias_buffer) = bias {
+ encoder.set_buffer(2, Some(bias_buffer), 0);
}
+ encoder.set_buffer(3, Some(arguments.d), 0);
- let input_dim = args.input_dim;
- let output_dim = args.output_dim;
- let weight_stride = args.ldb;
- let input_batch_stride = args.lda;
- let output_batch_stride = args.ldd;
+ let input_dimension = arguments.input_dim;
+ let output_dimension = arguments.output_dim;
+ let weight_row_stride = arguments.ldb;
+ let input_batch_stride = arguments.lda;
+ let output_batch_stride = arguments.ldd;
- let base = if has_bias { 4 } else { 3 };
encoder.set_bytes(
- base,
+ 4,
std::mem::size_of::<i32>() as u64,
- &input_dim as *const i32 as *const std::ffi::c_void,
+ &input_dimension as *const i32 as *const std::ffi::c_void,
);
encoder.set_bytes(
- base + 1,
+ 5,
std::mem::size_of::<i32>() as u64,
- &output_dim as *const i32 as *const std::ffi::c_void,
+ &output_dimension as *const i32 as *const std::ffi::c_void,
);
encoder.set_bytes(
- base + 2,
+ 6,
std::mem::size_of::<i32>() as u64,
- &weight_stride as *const i32 as *const std::ffi::c_void,
+ &weight_row_stride as *const i32 as *const std::ffi::c_void,
);
encoder.set_bytes(
- base + 3,
+ 7,
std::mem::size_of::<i32>() as u64,
&input_batch_stride as *const i32 as *const std::ffi::c_void,
);
encoder.set_bytes(
- base + 4,
+ 8,
std::mem::size_of::<i32>() as u64,
&output_batch_stride as *const i32 as *const std::ffi::c_void,
);
- const ROWS_PER_THREADGROUP: i32 = 16;
- let threadgroup_count_x = ((output_dim + ROWS_PER_THREADGROUP - 1)
- / ROWS_PER_THREADGROUP) as u64;
- let threadgroup_count_z = args.batch_count as u64;
-
- let threads_per_threadgroup_x = match kparts {
- 2 => 512,
- 4 => 1024,
- _ => {
- return Err(MTLError::Generic(format!(
- "Unsupported split-k kparts={kparts} (expected 2 or 4)"
- )));
- },
- };
+ let output_elements_per_threadgroup = configuration.output_elements_per_threadgroup();
+ let threadgroup_count_x =
+ ((output_dimension as u32 + output_elements_per_threadgroup - 1) / output_elements_per_threadgroup) as u64;
+ let threadgroup_count_z = arguments.batch_count as u64;
- let threadgroup_count =
- MTLSize::new(threadgroup_count_x, 1, threadgroup_count_z);
- let threads_per_threadgroup =
- MTLSize::new(threads_per_threadgroup_x, 1, 1);
+ let threadgroup_count = MTLSize::new(threadgroup_count_x, 1, threadgroup_count_z);
+ let threads_per_threadgroup = MTLSize::new(configuration.total_threads_per_threadgroup() as u64, 1, 1);
- encoder
- .dispatch_thread_groups(threadgroup_count, threads_per_threadgroup);
+ encoder.dispatch_thread_groups(threadgroup_count, threads_per_threadgroup);
Ok(())
}
- fn encode_inner(
- &mut self,
- context: &MTLContext,
- encoder: &ComputeCommandEncoderRef,
- args: MatmulArguments,
- rows_per_threadgroup: u32,
- bias: Option<&MTLBuffer>,
- ) -> Result<(), MTLError> {
- if self.is_logits_like(&args) {
- if args.input_dim >= 8192 {
- if let Some(kparts) = self.split_k_kparts(&args) {
- return self.encode_split_k(
- context,
- encoder,
- args,
- kparts,
- bias,
- );
- }
- }
- return self.encode_tiled(context, encoder, args, 16, bias);
- }
- if let Some(kparts) = self.split_k_kparts(&args) {
- return self.encode_split_k(context, encoder, args, kparts, bias);
- }
- if self.can_use_fast_kernel(&args) {
- self.encode_fast(context, encoder, args, bias)
- } else {
- self.encode_tiled(context, encoder, args, rows_per_threadgroup, bias)
- }
- }
-
pub fn encode(
&mut self,
context: &MTLContext,
encoder: &ComputeCommandEncoderRef,
- args: MatmulArguments,
- rows_per_threadgroup: u32,
+ arguments: MatmulArguments,
+ _rows_per_threadgroup: u32,
) -> Result<(), MTLError> {
- self.encode_inner(context, encoder, args, rows_per_threadgroup, None)
+ let configuration = select_gemv_configuration(
+ arguments.input_dim,
+ arguments.output_dim,
+ false,
+ );
+ self.encode_with_configuration(context, encoder, arguments, configuration, None)
}
pub fn encode_with_bias(
&mut self,
context: &MTLContext,
encoder: &ComputeCommandEncoderRef,
- args: MatmulArguments,
- rows_per_threadgroup: u32,
+ arguments: MatmulArguments,
+ _rows_per_threadgroup: u32,
bias: &MTLBuffer,
) -> Result<(), MTLError> {
- self.encode_inner(
- context,
- encoder,
- args,
- rows_per_threadgroup,
- Some(bias),
- )
+ let configuration = select_gemv_configuration(
+ arguments.input_dim,
+ arguments.output_dim,
+ true,
+ );
+ self.encode_with_configuration(context, encoder, arguments, configuration, Some(bias))
}
}
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/kernel.rs b/crates/uzu/src/backends/metal/kernel/matmul/kernel.rs
index 81e82a79e..e9913698e 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/kernel.rs
+++ b/crates/uzu/src/backends/metal/kernel/matmul/kernel.rs
@@ -77,19 +77,6 @@ impl MatmulKernel {
})
}
- fn select_gemv_rows(
- &self,
- output_dim: i32,
- ) -> u32 {
- if output_dim >= 2048 {
- 8
- } else if output_dim >= 512 {
- 4
- } else {
- 2
- }
- }
-
fn maybe_use_gemv_impl(
&mut self,
mtl: &MTLContext,
@@ -112,7 +99,6 @@ impl MatmulKernel {
let use_batched_gemv =
m > 1 && m <= 8 && n > 1 && args.batch_count == 1;
if use_batched_gemv {
- // Reshape: treat M rows as M batches of 1-row GEMV
let batched_args = MatmulArguments {
a: args.a,
b: args.b,
@@ -120,32 +106,29 @@ impl MatmulKernel {
batch: 1,
input_dim: args.input_dim,
output_dim: args.output_dim,
- lda: args.lda, // stride between input vectors
+ lda: args.lda,
ldb: args.ldb,
- ldd: args.ldd, // stride between output vectors
- batch_count: m, // M becomes batch count
+ ldd: args.ldd,
+ batch_count: m,
};
- let rows = self.select_gemv_rows(n);
let gemv =
self.gemv.get_or_insert_with(|| GemvKernel::new(self.dt));
if let Some(bias) = bias {
- gemv.encode_with_bias(mtl, enc, batched_args, rows, bias)?;
+ gemv.encode_with_bias(mtl, enc, batched_args, 0, bias)?;
} else {
- gemv.encode(mtl, enc, batched_args, rows)?;
+ gemv.encode(mtl, enc, batched_args, 0)?;
}
return Ok(true);
}
- // Standard GEMV for M=1 or N=1
if m != 1 && n != 1 {
return Ok(false);
}
- let rows = self.select_gemv_rows(n);
let gemv = self.gemv.get_or_insert_with(|| GemvKernel::new(self.dt));
if let Some(bias) = bias {
- gemv.encode_with_bias(mtl, enc, args, rows, bias)?;
+ gemv.encode_with_bias(mtl, enc, args, 0, bias)?;
} else {
- gemv.encode(mtl, enc, args, rows)?;
+ gemv.encode(mtl, enc, args, 0)?;
}
Ok(true)
}