Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

prepare for lloyd-max (#516)

CCYeh44d8451dc993
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/b_source.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/b_source.h
index 070ccaf06..b552bac54 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/b_source.h
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/b_source.h
@@ -1,9 +1,8 @@
#pragma once
-#include "../../common/qdot.h"
-#include "../../common/quant_pack.h"
-#include "gemv_common.h"
-#include "quant_row_offsets.h"
+#include "../../../generated/gemm.h"
+#include "full_precision_b_source.h"
+#include "quantized_b_source.h"
namespace uzu {
namespace gemm {
@@ -16,6 +15,7 @@ template <
uint GROUP_SIZE,
uint BITS,
uint K_SPLIT,
+ uint RESULTS_PER_SIMDGROUP,
bool INPUT_ALIGNED>
struct BSource {
static METAL_FUNC void accumulate(
@@ -32,123 +32,29 @@ struct BSource {
uint k_slice
) {
if constexpr (B_PROLOGUE == GemmBPrologueKind::FullPrecision) {
- constexpr uint values_per_thread = 4;
- constexpr uint block_size = values_per_thread * METAL_SIMD_SIZE;
- typedef vec<BT, 4> W4;
- typedef vec<AT, 4> I4;
- const uint k_stride = K_SPLIT * block_size;
- const uint k_start = k_slice * block_size;
- const device BT* w = reinterpret_cast<const device BT*>(b);
- w += out_row * in_vec_size + simd_lane * values_per_thread + k_start;
- const device AT* in = a + batch_idx * in_vec_size + simd_lane * values_per_thread + k_start;
-
- uint k = k_start;
- for (; k + block_size <= in_vec_size; k += k_stride) {
- float4 xv = static_cast<float4>(*reinterpret_cast<const device I4*>(in));
- result[0] += dot(static_cast<float4>(*reinterpret_cast<const device W4*>(w)), xv);
- result[1] += dot(static_cast<float4>(*reinterpret_cast<const device W4*>(w + in_vec_size)), xv);
- result[2] += dot(static_cast<float4>(*reinterpret_cast<const device W4*>(w + 2 * in_vec_size)), xv);
- result[3] += dot(static_cast<float4>(*reinterpret_cast<const device W4*>(w + 3 * in_vec_size)), xv);
- w += k_stride;
- in += k_stride;
- }
-
- if constexpr (K_SPLIT == 1 && !INPUT_ALIGNED) {
- const uint thread_offset = simd_lane * values_per_thread;
- const int remaining = (k + thread_offset < in_vec_size) ? min(static_cast<int>(in_vec_size - k - thread_offset),
- static_cast<int>(values_per_thread))
- : 0;
- if (remaining > 0) {
- const device BT* w0 = w;
- const device BT* w1 = w + in_vec_size;
- const device BT* w2 = w + 2 * in_vec_size;
- const device BT* w3 = w + 3 * in_vec_size;
- for (int j = 0; j < remaining; j++) {
- U x = static_cast<U>(in[j]);
- result[0] += static_cast<U>(w0[j]) * x;
- result[1] += static_cast<U>(w1[j]) * x;
- result[2] += static_cast<U>(w2[j]) * x;
- result[3] += static_cast<U>(w3[j]) * x;
- }
- }
- }
+ FullPrecisionBSource<BT, AT, U, RESULTS_PER_SIMDGROUP, K_SPLIT, INPUT_ALIGNED>::accumulate(
+ result,
+ b,
+ a,
+ in_vec_size,
+ out_row,
+ batch_idx,
+ simd_lane,
+ k_slice
+ );
} else {
- constexpr uint packs_per_thread = BITS == 2 ? 1 : 2;
- constexpr uint pack_factor = get_pack_factor<BITS, 32>();
- constexpr uint bytes_per_pack = get_bytes_per_pack<BITS, 32>();
- constexpr uint values_per_thread = pack_factor * packs_per_thread;
- constexpr uint block_size = values_per_thread * METAL_SIMD_SIZE;
- constexpr uint scale_step_per_thread = GROUP_SIZE / values_per_thread;
- const device uint8_t* ws = (const device uint8_t*)b;
- thread U x_thread[values_per_thread];
-
- const uint in_vec_size_w = in_vec_size * bytes_per_pack / pack_factor;
- const uint in_vec_size_g = (in_vec_size + GROUP_SIZE - 1) / GROUP_SIZE;
- ws += out_row * in_vec_size_w + simd_lane * packs_per_thread * bytes_per_pack;
-
- QuantRowOffsets<BT, U, B_PROLOGUE, BITS> prep;
- prep.group_stride = in_vec_size_g;
- const uint g_offset = simd_lane / scale_step_per_thread;
- prep.scales = scales + out_row * in_vec_size_g + g_offset;
- if constexpr (B_PROLOGUE == GemmBPrologueKind::ScaleBiasDequant) {
- prep.biases = biases + out_row * in_vec_size_g + g_offset;
- } else if constexpr (B_PROLOGUE == GemmBPrologueKind::ScaleZeroPointDequant) {
- if (BITS == 4) {
- prep.zp_stride = (in_vec_size_g + 1) / 2;
- prep.zps = zero_points + out_row * prep.zp_stride + g_offset / 2;
- prep.high_nibble = (g_offset & 1);
- } else {
- prep.zp_stride = in_vec_size_g;
- prep.zps = zero_points + out_row * prep.zp_stride + g_offset;
- }
- }
-
- const device AT* in = a + batch_idx * in_vec_size + simd_lane * values_per_thread;
-
- uint k = 0;
- for (; k + block_size <= in_vec_size; k += block_size) {
- U sum = load_vector<AT, U, values_per_thread, BITS>(in, x_thread);
-
- const device uint8_t* wl0 = ws;
- const device uint8_t* wl1 = ws + in_vec_size_w;
- const device uint8_t* wl2 = ws + 2 * in_vec_size_w;
- const device uint8_t* wl3 = ws + 3 * in_vec_size_w;
-
- U scale[4];
- U offset[4];
- prep.load(scale, offset);
- result[0] += qdot<U, values_per_thread, BITS>(wl0, x_thread, scale[0], offset[0], sum);
- result[1] += qdot<U, values_per_thread, BITS>(wl1, x_thread, scale[1], offset[1], sum);
- result[2] += qdot<U, values_per_thread, BITS>(wl2, x_thread, scale[2], offset[2], sum);
- result[3] += qdot<U, values_per_thread, BITS>(wl3, x_thread, scale[3], offset[3], sum);
-
- ws += block_size * bytes_per_pack / pack_factor;
- prep.advance(block_size / GROUP_SIZE);
- in += block_size;
- }
-
- if constexpr (!INPUT_ALIGNED) {
- const uint thread_offset = simd_lane * values_per_thread;
- const int remaining = (k + thread_offset < in_vec_size) ? min(static_cast<int>(in_vec_size - k - thread_offset),
- static_cast<int>(values_per_thread))
- : 0;
- if (remaining > 0) {
- U sum = load_vector_safe<AT, U, values_per_thread>(in, x_thread, remaining);
-
- const device uint8_t* wl0 = ws;
- const device uint8_t* wl1 = ws + in_vec_size_w;
- const device uint8_t* wl2 = ws + 2 * in_vec_size_w;
- const device uint8_t* wl3 = ws + 3 * in_vec_size_w;
-
- U scale[4];
- U offset[4];
- prep.load(scale, offset);
- result[0] += qdot_safe<U, values_per_thread, BITS>(wl0, x_thread, scale[0], offset[0], sum, remaining);
- result[1] += qdot_safe<U, values_per_thread, BITS>(wl1, x_thread, scale[1], offset[1], sum, remaining);
- result[2] += qdot_safe<U, values_per_thread, BITS>(wl2, x_thread, scale[2], offset[2], sum, remaining);
- result[3] += qdot_safe<U, values_per_thread, BITS>(wl3, x_thread, scale[3], offset[3], sum, remaining);
- }
- }
+ QuantizedBSource<BT, AT, U, B_PROLOGUE, GROUP_SIZE, BITS, RESULTS_PER_SIMDGROUP, INPUT_ALIGNED>::accumulate(
+ result,
+ b,
+ scales,
+ zero_points,
+ biases,
+ a,
+ in_vec_size,
+ out_row,
+ batch_idx,
+ simd_lane
+ );
}
}
};
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/epilogue.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/epilogue.h
index 72707af54..393784ea9 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/epilogue.h
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/epilogue.h
@@ -1,12 +1,11 @@
#pragma once
#include "../../../hadamard_transform/hadamard_transform.h"
-#include "gemv_common.h"
namespace uzu {
namespace gemm {
-template <typename BT, typename DT, typename U>
+template <typename BT, typename DT, typename U, uint RESULTS_PER_SIMDGROUP>
struct Epilogue {
static METAL_FUNC void store(
thread U (&result)[RESULTS_PER_SIMDGROUP],
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/full_precision_b_source.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/full_precision_b_source.h
new file mode 100644
index 000000000..b406681cb
--- /dev/null
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/full_precision_b_source.h
@@ -0,0 +1,62 @@
+#pragma once
+
+#include "../../common/defines.h"
+
+namespace uzu {
+namespace gemm {
+
+template <typename BT, typename AT, typename U, uint RESULTS_PER_SIMDGROUP, uint K_SPLIT, bool INPUT_ALIGNED>
+struct FullPrecisionBSource {
+ static METAL_FUNC void accumulate(
+ thread U (&result)[RESULTS_PER_SIMDGROUP],
+ const device uint32_t* b,
+ const device AT* a,
+ uint in_vec_size,
+ uint out_row,
+ uint batch_idx,
+ uint simd_lane,
+ uint k_slice
+ ) {
+ constexpr uint values_per_thread = 4;
+ constexpr uint block_size = values_per_thread * METAL_SIMD_SIZE;
+ typedef vec<BT, 4> W4;
+ typedef vec<AT, 4> I4;
+
+ const uint k_stride = K_SPLIT * block_size;
+ const uint k_start = k_slice * block_size;
+ const device BT* weights = reinterpret_cast<const device BT*>(b);
+ weights += out_row * in_vec_size + simd_lane * values_per_thread + k_start;
+ const device AT* input = a + batch_idx * in_vec_size + simd_lane * values_per_thread + k_start;
+
+ uint k = k_start;
+ for (; k + block_size <= in_vec_size; k += k_stride) {
+ float4 input_values = static_cast<float4>(*reinterpret_cast<const device I4*>(input));
+ METAL_PRAGMA_UNROLL
+ for (uint row = 0; row < RESULTS_PER_SIMDGROUP; row++) {
+ const device BT* weight_row = weights + row * in_vec_size;
+ result[row] += dot(static_cast<float4>(*reinterpret_cast<const device W4*>(weight_row)), input_values);
+ }
+ weights += k_stride;
+ input += k_stride;
+ }
+
+ if constexpr (K_SPLIT == 1 && !INPUT_ALIGNED) {
+ const uint thread_offset = simd_lane * values_per_thread;
+ const int remaining = (k + thread_offset < in_vec_size) ? min(static_cast<int>(in_vec_size - k - thread_offset),
+ static_cast<int>(values_per_thread))
+ : 0;
+ if (remaining > 0) {
+ METAL_PRAGMA_UNROLL
+ for (uint row = 0; row < RESULTS_PER_SIMDGROUP; row++) {
+ const device BT* weight_row = weights + row * in_vec_size;
+ for (int index = 0; index < remaining; index++) {
+ result[row] += static_cast<U>(weight_row[index]) * static_cast<U>(input[index]);
+ }
+ }
+ }
+ }
+ }
+};
+
+} // namespace gemm
+} // namespace uzu
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/gemv_common.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/gemv_common.h
deleted file mode 100644
index f83f418d8..000000000
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/gemv_common.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#pragma once
-
-#include <metal_stdlib>
-
-#include "../../common/defines.h"
-#include "../../../generated/gemm.h"
-
-using namespace metal;
-
-namespace uzu {
-namespace gemm {
-
-METAL_CONST uint RESULTS_PER_SIMDGROUP = 4;
-
-} // namespace gemm
-} // namespace uzu
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/output_tile.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/output_tile.h
index 6fba89d29..bc25f94c3 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/output_tile.h
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/output_tile.h
@@ -1,11 +1,9 @@
#pragma once
-#include "gemv_common.h"
-
namespace uzu {
namespace gemm {
-template <uint K_SPLIT, uint NUM_SIMDGROUPS>
+template <uint K_SPLIT, uint NUM_SIMDGROUPS, uint RESULTS_PER_SIMDGROUP>
struct OutputTile {
uint out_row;
uint row_group;
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quant_row_offsets.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quant_row_offsets.h
deleted file mode 100644
index d68cd99ca..000000000
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quant_row_offsets.h
+++ /dev/null
@@ -1,64 +0,0 @@
-#pragma once
-
-#include "../../common/quant_pack.h"
-#include "gemv_common.h"
-
-namespace uzu {
-namespace gemm {
-
-template <typename BT, typename U, GemmBPrologueKind B_PROLOGUE, uint BITS>
-struct QuantRowOffsets {
- static_assert(BITS == 4 || BITS == 8, "QuantRowOffsets supports 4- and 8-bit only");
- const device BT* scales = nullptr;
- const device BT* biases = nullptr;
- const device uint8_t* zps = nullptr;
- uint group_stride = 0;
- uint zp_stride = 0;
- bool high_nibble = false;
-
- void load(thread U* scale, thread U* offset) const {
- scale[0] = static_cast<U>(scales[0]);
- scale[1] = static_cast<U>(scales[group_stride]);
- scale[2] = static_cast<U>(scales[2 * group_stride]);
- scale[3] = static_cast<U>(scales[3 * group_stride]);
-
- if constexpr (B_PROLOGUE == GemmBPrologueKind::ScaleBiasDequant) {
- offset[0] = static_cast<U>(biases[0]);
- offset[1] = static_cast<U>(biases[group_stride]);
- offset[2] = static_cast<U>(biases[2 * group_stride]);
- offset[3] = static_cast<U>(biases[3 * group_stride]);
- } else if constexpr (B_PROLOGUE == GemmBPrologueKind::ScaleZeroPointDequant) {
- uchar4 zp_bytes = uchar4(zps[0], zps[zp_stride], zps[2 * zp_stride], zps[3 * zp_stride]);
- uchar4 zp_nibbles;
- if constexpr (BITS == 4) {
- const uint8_t shift = high_nibble ? 4u : 0u;
- zp_nibbles = (zp_bytes >> shift) & uchar4(0x0F);
- } else {
- zp_nibbles = zp_bytes;
- }
- offset[0] = -scale[0] * static_cast<U>(zp_nibbles.x);
- offset[1] = -scale[1] * static_cast<U>(zp_nibbles.y);
- offset[2] = -scale[2] * static_cast<U>(zp_nibbles.z);
- offset[3] = -scale[3] * static_cast<U>(zp_nibbles.w);
- } else {
- constexpr U midpoint = U(1u << (BITS - 1));
- offset[0] = -scale[0] * midpoint;
- offset[1] = -scale[1] * midpoint;
- offset[2] = -scale[2] * midpoint;
- offset[3] = -scale[3] * midpoint;
- }
- }
-
- void advance(uint groups) {
- scales += groups;
- if constexpr (B_PROLOGUE == GemmBPrologueKind::ScaleBiasDequant) {
- biases += groups;
- } else if constexpr (B_PROLOGUE == GemmBPrologueKind::ScaleZeroPointDequant) {
- constexpr uint zps_per_byte = get_pack_factor<BITS, 8>();
- zps += groups / zps_per_byte;
- }
- }
-};
-
-} // namespace gemm
-} // namespace uzu
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quantized_b_source.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quantized_b_source.h
new file mode 100644
index 000000000..24723ff43
--- /dev/null
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quantized_b_source.h
@@ -0,0 +1,103 @@
+#pragma once
+
+#include "../../common/qdot.h"
+#include "../../common/quant_pack.h"
+#include "quantized_row_state.h"
+
+namespace uzu {
+namespace gemm {
+
+template <
+ typename BT,
+ typename AT,
+ typename U,
+ GemmBPrologueKind B_PROLOGUE,
+ uint GROUP_SIZE,
+ uint BITS,
+ uint RESULTS_PER_SIMDGROUP,
+ bool INPUT_ALIGNED>
+struct QuantizedBSource {
+ static METAL_FUNC void accumulate(
+ thread U (&result)[RESULTS_PER_SIMDGROUP],
+ const device uint32_t* b,
+ const device BT* scales,
+ const device uint8_t* zero_points,
+ const device BT* biases,
+ const device AT* a,
+ uint in_vec_size,
+ uint out_row,
+ uint batch_idx,
+ uint simd_lane
+ ) {
+ constexpr uint pack_factor = get_pack_factor<BITS, 32>();
+ constexpr uint bytes_per_pack = get_bytes_per_pack<BITS, 32>();
+ constexpr uint packs_per_thread = 2;
+ constexpr uint values_per_thread = pack_factor * packs_per_thread;
+ constexpr uint block_size = values_per_thread * METAL_SIMD_SIZE;
+ constexpr uint scale_step_per_thread = GROUP_SIZE / values_per_thread;
+ using RowState = QuantizedRowState<BT, U, B_PROLOGUE, BITS, RESULTS_PER_SIMDGROUP>;
+ using RowParams = typename RowState::Params;
+
+ const uint weights_row_stride = in_vec_size * bytes_per_pack / pack_factor;
+ const uint group_count = (in_vec_size + GROUP_SIZE - 1) / GROUP_SIZE;
+ const uint group_offset = simd_lane / scale_step_per_thread;
+ const device uint8_t* weights = reinterpret_cast<const device uint8_t*>(b);
+ weights += out_row * weights_row_stride + simd_lane * packs_per_thread * bytes_per_pack;
+
+ RowState row_state(scales, zero_points, biases, out_row, group_count, group_offset);
+
+ const device AT* input = a + batch_idx * in_vec_size + simd_lane * values_per_thread;
+ thread U input_values[values_per_thread];
+
+ uint k = 0;
+ for (; k + block_size <= in_vec_size; k += block_size) {
+ U input_sum = load_vector<AT, U, values_per_thread, BITS>(input, input_values);
+
+ RowParams row_params;
+ row_state.load(row_params);
+ METAL_PRAGMA_UNROLL
+ for (uint row = 0; row < RESULTS_PER_SIMDGROUP; row++) {
+ const device uint8_t* weight_row = weights + row * weights_row_stride;
+ result[row] += qdot<U, values_per_thread, BITS>(
+ weight_row,
+ input_values,
+ row_params.scale[row],
+ row_params.offset[row],
+ input_sum
+ );
+ }
+
+ weights += block_size * bytes_per_pack / pack_factor;
+ row_state.advance(block_size / GROUP_SIZE);
+ input += block_size;
+ }
+
+ if constexpr (!INPUT_ALIGNED) {
+ const uint thread_offset = simd_lane * values_per_thread;
+ const int remaining = (k + thread_offset < in_vec_size) ? min(static_cast<int>(in_vec_size - k - thread_offset),
+ static_cast<int>(values_per_thread))
+ : 0;
+ if (remaining > 0) {
+ U input_sum = load_vector_safe<AT, U, values_per_thread>(input, input_values, remaining);
+
+ RowParams row_params;
+ row_state.load(row_params);
+ METAL_PRAGMA_UNROLL
+ for (uint row = 0; row < RESULTS_PER_SIMDGROUP; row++) {
+ const device uint8_t* weight_row = weights + row * weights_row_stride;
+ result[row] += qdot_safe<U, values_per_thread, BITS>(
+ weight_row,
+ input_values,
+ row_params.scale[row],
+ row_params.offset[row],
+ input_sum,
+ remaining
+ );
+ }
+ }
+ }
+ }
+};
+
+} // namespace gemm
+} // namespace uzu
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quantized_row_state.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quantized_row_state.h
new file mode 100644
index 000000000..1467610fc
--- /dev/null
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/quantized_row_state.h
@@ -0,0 +1,134 @@
+#pragma once
+
+#include "../../../generated/gemm.h"
+#include "../../common/defines.h"
+#include "../../common/quant_pack.h"
+
+namespace uzu {
+namespace gemm {
+
+template <typename U, uint RESULTS_PER_SIMDGROUP>
+struct QuantizedAffineRowParams {
+ U scale[RESULTS_PER_SIMDGROUP];
+ U offset[RESULTS_PER_SIMDGROUP];
+};
+
+template <typename T, typename U>
+struct QuantizedGroupRows {
+ const device T* values;
+ uint group_stride;
+
+ QuantizedGroupRows(const device T* values_base, uint out_row, uint group_count, uint group_offset)
+ : values(values_base + out_row * group_count + group_offset), group_stride(group_count) {}
+
+ METAL_FUNC U value(uint row) const { return static_cast<U>(values[row * group_stride]); }
+
+ void advance(uint groups) { values += groups; }
+};
+
+template <typename BT, typename U, GemmBPrologueKind B_PROLOGUE, uint BITS>
+struct QuantizedOffsetState;
+
+template <typename BT, typename U, uint BITS>
+struct QuantizedOffsetState<BT, U, GemmBPrologueKind::ScaleBiasDequant, BITS> {
+ QuantizedGroupRows<BT, U> biases;
+
+ QuantizedOffsetState(
+ const device uint8_t*,
+ const device BT* biases_base,
+ uint out_row,
+ uint group_count,
+ uint group_offset
+ )
+ : biases(biases_base, out_row, group_count, group_offset) {}
+
+ METAL_FUNC U value(uint row, U) const { return biases.value(row); }
+
+ void advance(uint groups) { biases.advance(groups); }
+};
+
+template <typename BT, typename U, uint BITS>
+struct QuantizedOffsetState<BT, U, GemmBPrologueKind::ScaleZeroPointDequant, BITS> {
+ const device uint8_t* zero_points;
+ uint zero_point_stride;
+ uint zero_point_shift;
+
+ QuantizedOffsetState(
+ const device uint8_t* zero_points_base,
+ const device BT*,
+ uint out_row,
+ uint group_count,
+ uint group_offset
+ ) {
+ if constexpr (BITS == 4) {
+ zero_point_stride = (group_count + 1) / 2;
+ zero_points = zero_points_base + out_row * zero_point_stride + group_offset / 2;
+ zero_point_shift = (group_offset & 1) * 4u;
+ } else {
+ zero_point_stride = group_count;
+ zero_points = zero_points_base + out_row * zero_point_stride + group_offset;
+ }
+ }
+
+ METAL_FUNC U value(uint row, U scale) const {
+ uint8_t zero_point_value = zero_points[row * zero_point_stride];
+ if constexpr (BITS == 4) {
+ zero_point_value = (zero_point_value >> zero_point_shift) & 0x0F;
+ }
+ return -scale * static_cast<U>(zero_point_value);
+ }
+
+ void advance(uint groups) {
+ constexpr uint zero_points_per_byte = get_pack_factor<BITS, 8>();
+ zero_points += groups / zero_points_per_byte;
+ }
+};
+
+template <typename BT, typename U, uint BITS>
+struct QuantizedOffsetState<BT, U, GemmBPrologueKind::ScaleSymmetricDequant, BITS> {
+ QuantizedOffsetState(const device uint8_t*, const device BT*, uint, uint, uint) {}
+
+ METAL_FUNC U value(uint, U scale) const {
+ constexpr U midpoint = U(1u << (BITS - 1));
+ return -scale * midpoint;
+ }
+};
+
+template <typename BT, typename U, GemmBPrologueKind B_PROLOGUE, uint BITS, uint RESULTS_PER_SIMDGROUP>
+struct QuantizedRowState {
+ static_assert(BITS == 4 || BITS == 8, "QMV supports 4- and 8-bit only");
+
+ using Params = QuantizedAffineRowParams<U, RESULTS_PER_SIMDGROUP>;
+
+ QuantizedGroupRows<BT, U> scale_rows;
+ QuantizedOffsetState<BT, U, B_PROLOGUE, BITS> offset_state;
+
+ QuantizedRowState(
+ const device BT* scales_base,
+ const device uint8_t* zero_points_base,
+ const device BT* biases_base,
+ uint out_row,
+ uint group_count,
+ uint group_offset
+ )
+ : scale_rows(scales_base, out_row, group_count, group_offset),
+ offset_state(zero_points_base, biases_base, out_row, group_count, group_offset) {}
+
+ void load(thread Params& params) const {
+ METAL_PRAGMA_UNROLL
+ for (uint row = 0; row < RESULTS_PER_SIMDGROUP; row++) {
+ const U scale = scale_rows.value(row);
+ params.scale[row] = scale;
+ params.offset[row] = offset_state.value(row, scale);
+ }
+ }
+
+ void advance(uint groups) {
+ scale_rows.advance(groups);
+ if constexpr (B_PROLOGUE != GemmBPrologueKind::ScaleSymmetricDequant)
+ offset_state.advance(groups);
+ }
+};
+
+} // namespace gemm
+} // namespace uzu
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/reduce.h b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/reduce.h
index ac3947b3a..30c79fd14 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/reduce.h
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/common/reduce.h
@@ -1,11 +1,9 @@
#pragma once
-#include "gemv_common.h"
-
namespace uzu {
namespace gemm {
-template <typename U, uint K_SPLIT, uint NUM_SIMDGROUPS>
+template <typename U, uint K_SPLIT, uint NUM_SIMDGROUPS, uint RESULTS_PER_SIMDGROUP>
struct Reduce {
static METAL_FUNC void run(
thread U (&result)[RESULTS_PER_SIMDGROUP],
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/gemv.metal b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/gemv.metal
index e69aa5cc2..052fe7790 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/gemv.metal
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/gemv.metal
@@ -17,6 +17,7 @@ template <
uint BITS,
uint K_SPLIT,
bool INPUT_ALIGNED,
+ uint RESULTS_PER_SIMDGROUP,
uint NUM_SIMDGROUPS>
VARIANTS(AT, bfloat, float)
VARIANTS(BT, bfloat, float)
@@ -32,6 +33,7 @@ VARIANTS(GROUP_SIZE, 0, 16, 32, 64, 128)
VARIANTS(BITS, 0, 4, 8)
VARIANTS(K_SPLIT, 1, 2, 4, 8)
VARIANTS(INPUT_ALIGNED, false, true)
+VARIANTS(RESULTS_PER_SIMDGROUP, 4)
VARIANTS(NUM_SIMDGROUPS, 2, 8)
CONSTRAINT((B_PROLOGUE == GemmBPrologueKind::FullPrecision) == (BITS == 0))
CONSTRAINT((BITS == 0) == (GROUP_SIZE == 0))
@@ -58,7 +60,7 @@ KERNEL(Gemv)(
const constant float& ab_scale,
const constant uint& group_count_x,
const GemmDTransform output_transform SPECIALIZE,
- threadgroup float shared_results[NUM_SIMDGROUPS * 4],
+ threadgroup float shared_results[NUM_SIMDGROUPS * RESULTS_PER_SIMDGROUP],
const uint batch_idx GROUPS(batch_size),
const uint out_block_idx GROUPS(group_count_x),
const uint simd_lane THREADS(32),
@@ -67,11 +69,11 @@ KERNEL(Gemv)(
typedef float U;
thread U result[RESULTS_PER_SIMDGROUP] = {0};
- OutputTile<K_SPLIT, NUM_SIMDGROUPS> tile =
- OutputTile<K_SPLIT, NUM_SIMDGROUPS>::make(out_block_idx, simd_group, out_vec_size);
+ OutputTile<K_SPLIT, NUM_SIMDGROUPS, RESULTS_PER_SIMDGROUP> tile =
+ OutputTile<K_SPLIT, NUM_SIMDGROUPS, RESULTS_PER_SIMDGROUP>::make(out_block_idx, simd_group, out_vec_size);
d += batch_idx * out_vec_size + tile.out_row;
- BSource<BT, AT, U, B_PROLOGUE, GROUP_SIZE, BITS, K_SPLIT, INPUT_ALIGNED>::accumulate(
+ BSource<BT, AT, U, B_PROLOGUE, GROUP_SIZE, BITS, K_SPLIT, RESULTS_PER_SIMDGROUP, INPUT_ALIGNED>::accumulate(
result,
b,
scales,
@@ -85,9 +87,16 @@ KERNEL(Gemv)(
tile.k_slice
);
- Reduce<U, K_SPLIT, NUM_SIMDGROUPS>::run(result, shared_results, simd_group, simd_lane, tile.row_group, tile.k_slice);
+ Reduce<U, K_SPLIT, NUM_SIMDGROUPS, RESULTS_PER_SIMDGROUP>::run(
+ result,
+ shared_results,
+ simd_group,
+ simd_lane,
+ tile.row_group,
+ tile.k_slice
+ );
- Epilogue<BT, DT, U>::store(
+ Epilogue<BT, DT, U, RESULTS_PER_SIMDGROUP>::store(
result,
d,
output_bias,
diff --git a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/kernel.rs b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/kernel.rs
index 1643c7fde..7e581482c 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/kernel.rs
+++ b/crates/backend-uzu/src/backends/metal/kernel/matmul/gemv/kernel.rs
@@ -215,6 +215,7 @@ impl GemvDispatch {
specialization.bits,
specialization.k_split,
specialization.input_aligned,
+ QMV_RESULTS_PER_SIMDGROUP,
specialization.num_simdgroups,
specialization.output_transform,
)