Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Vectorized Hadamard (#910)

CCYehc92723bdc04e
diff --git a/crates/uzu-engine/src/backends/metal/kernel/activation_transform/activation_transform.metal b/crates/uzu-engine/src/backends/metal/kernel/activation_transform/activation_transform.metal
index b4df7bf5d..45d8d9ee5 100644
--- a/crates/uzu-engine/src/backends/metal/kernel/activation_transform/activation_transform.metal
+++ b/crates/uzu-engine/src/backends/metal/kernel/activation_transform/activation_transform.metal
@@ -1,8 +1,8 @@
#include <metal_stdlib>
+#include "../common/activation_quantization.h"
#include "../common/defines.h"
#include "../common/dsl.h"
#include "../common/thread_context.h"
-#include "../common/activation_quantization.h"
#include "../generated/activation_transform.h"
#include "../hadamard_transform/hadamard_transform.h"
@@ -11,6 +11,7 @@ using namespace uzu::activation_transform;
#define NUM_SIMDGROUPS 4
#define NUM_THREADS NUM_SIMDGROUPS* METAL_SIMD_SIZE
+#define TILE_ELEMENTS NUM_THREADS* HADAMARD_VECTOR_SIZE
#define QUANTIZED (ops == ActivationTransformOp::Quantize || ops == ActivationTransformOp::QuantizeWithGroupSums)
#define EMITS_GROUP_SUMS (ops == ActivationTransformOp::QuantizeWithGroupSums)
@@ -34,85 +35,58 @@ PUBLIC KERNEL(ActivationTransform)(
const uint activation_scale_group_size SPECIALIZE,
const uint sum_group_size SPECIALIZE,
const bool has_bias SPECIALIZE,
- threadgroup float partial_max OPTIONAL(QUANTIZED && activation_scale_group_size > METAL_SIMD_SIZE)[NUM_SIMDGROUPS],
- threadgroup int partial_sums OPTIONAL(EMITS_GROUP_SUMS && sum_group_size > METAL_SIMD_SIZE)[NUM_SIMDGROUPS],
- uint activation_tile_index GROUPS(element_count.div_ceil(NUM_THREADS)),
+ uint activation_tile_index GROUPS(element_count.div_ceil(TILE_ELEMENTS)),
uint batch_index GROUPS(batch_size),
uint thread_index THREADS(NUM_THREADS),
const ThreadContext thread_context
) {
- (void)thread_index;
if (in_place) {
input = reinterpret_cast<const device T*>(fp_out);
}
const bool input_rht = ops != ActivationTransformOp::OutputRht;
const ushort lane_index = thread_context.simd_lane_id;
- const ushort simdgroup_index = thread_context.simdgroup_index;
- const uint tile_offset = activation_tile_index * ACTIVATION_QUANT_TILE_SIZE;
- const uint simdgroup_offset = tile_offset + simdgroup_index * METAL_SIMD_SIZE;
- const bool in_bounds = simdgroup_offset < element_count;
- const uint factor_index = simdgroup_offset + lane_index;
- const uint element_index = batch_index * element_count + factor_index;
+ const uint first_index = activation_tile_index * TILE_ELEMENTS + thread_index * HADAMARD_VECTOR_SIZE;
+ const bool in_bounds = first_index < element_count;
+ const uint element_index = batch_index * element_count + first_index;
- float value = 0.0f;
+ float4 values = 0.0f;
+ int4 factors = 0;
if (in_bounds) {
- value = static_cast<float>(input[element_index]);
- value = input_rht ? simdgroup_input_random_hadamard_transform(lane_index, value, rht_factors[factor_index])
- : simdgroup_output_random_hadamard_transform(lane_index, value, rht_factors[factor_index]);
+ values = float4(load_hadamard_vector(input + element_index));
+ factors = load_hadamard_vector(rht_factors + first_index);
+ if (input_rht) {
+ values *= float4(factors);
+ }
+ }
+ values = simdgroup_hadamard_transform_vector(lane_index, values);
+ if (!input_rht) {
+ values *= float4(factors);
}
if (!QUANTIZED) {
if (in_bounds) {
if (has_bias) {
- value = static_cast<float>(static_cast<T>(value)) + static_cast<float>(bias[factor_index]);
+ values = float4(vec<T, HADAMARD_VECTOR_SIZE>(values)) + float4(load_hadamard_vector(bias + first_index));
}
- fp_out[element_index] = static_cast<T>(value);
+ store_hadamard_vector(fp_out + element_index, values);
}
return;
}
- const float maximum = reduce_activation_quantization_group(
- fabs(value),
- activation_scale_group_size,
- partial_max,
- thread_context,
- [](float x) { return simd_max(x); },
- [](float x, float y) { return max(x, y); }
- );
- const float scale = isfinite(maximum) && maximum > 0.0f ? maximum / ACTIVATION_QUANT_INT8_MAX : 1.0f;
- const int8_t code = quantize_activation_int8(value, scale);
- if (in_bounds) {
- const uint output_factor_index = grouped_by_weight_nibble ? nibble_grouped_index(factor_index) : factor_index;
- q_out[batch_index * element_count + output_factor_index] = code;
- }
- write_activation_quantization_group(
- scales_out,
- scale,
- activation_scale_group_size,
+ store_quantized_activation_vector(
+ values,
+ lane_index,
+ in_bounds,
+ first_index,
element_count,
- activation_tile_index,
batch_index,
- thread_context
+ activation_scale_group_size,
+ grouped_by_weight_nibble,
+ EMITS_GROUP_SUMS,
+ sum_group_size,
+ q_out,
+ scales_out,
+ group_sums_out
);
-
- if (EMITS_GROUP_SUMS) {
- const int sum = reduce_activation_quantization_group(
- int(code),
- sum_group_size,
- partial_sums,
- thread_context,
- [](int x) { return simd_sum(x); },
- [](int x, int y) { return x + y; }
- );
- write_activation_quantization_group(
- group_sums_out,
- sum,
- sum_group_size,
- element_count,
- activation_tile_index,
- batch_index,
- thread_context
- );
- }
}
diff --git a/crates/uzu-engine/src/backends/metal/kernel/common/activation_quantization.h b/crates/uzu-engine/src/backends/metal/kernel/common/activation_quantization.h
index b32304fff..c82322b75 100644
--- a/crates/uzu-engine/src/backends/metal/kernel/common/activation_quantization.h
+++ b/crates/uzu-engine/src/backends/metal/kernel/common/activation_quantization.h
@@ -6,88 +6,102 @@
using namespace metal;
-UZU_CONST uint ACTIVATION_QUANT_TILE_SIZE = 128;
UZU_CONST float ACTIVATION_QUANT_INT8_MAX = 127.0f;
-#define ACTIVATION_QUANT_SIMDGROUPS 4
static METAL_FUNC int8_t quantize_activation_int8(const float value, const float scale) {
return static_cast<int8_t>(clamp(round(value / scale), -ACTIVATION_QUANT_INT8_MAX, ACTIVATION_QUANT_INT8_MAX));
}
-static METAL_FUNC uint nibble_grouped_index(const uint index) {
- constexpr uint NIBBLES_PER_BYTE = 2;
- constexpr uint CODES_PER_WORD = sizeof(uint) * NIBBLES_PER_BYTE;
- constexpr uint NIBBLE_GROUP_SIZE = CODES_PER_WORD / NIBBLES_PER_BYTE;
- // [0, 1, 2, 3, 4, 5, 6, 7] -> [0, 4, 1, 5, 2, 6, 3, 7]
- const uint offset = index % CODES_PER_WORD;
- return index - offset + (offset % NIBBLES_PER_BYTE) * NIBBLE_GROUP_SIZE + offset / NIBBLES_PER_BYTE;
-}
-
-template <typename T, typename SimdReduce, typename Combine>
-METAL_FUNC T reduce_activation_quantization_group(
- const T value,
- const uint group_size,
- threadgroup T* partials,
- const thread ThreadContext& thread_context,
- SimdReduce simd_reduce,
- Combine combine
-) {
- const ushort lane_index = thread_context.simd_lane_id;
- const ushort simdgroup_index = thread_context.simdgroup_index;
- const T simdgroup_value = simd_reduce(value);
- if (group_size == METAL_SIMD_SIZE) {
- return simdgroup_value;
- }
- if (lane_index == 0) {
- partials[simdgroup_index] = simdgroup_value;
- }
- threadgroup_barrier(mem_flags::mem_threadgroup);
-
- const uint simdgroups_per_group = group_size / METAL_SIMD_SIZE;
- const uint simdgroup_base = simdgroup_index / simdgroups_per_group * simdgroups_per_group;
- T result = partials[simdgroup_base];
- METAL_PRAGMA_UNROLL
- for (uint index = 1; index < simdgroups_per_group; ++index) {
- result = combine(result, partials[simdgroup_base + index]);
- }
- return result;
-}
-
template <uint SIMDGROUPS>
METAL_FUNC float reduce_activation_quantization_row_maximum(
- const float magnitude,
+ const float largest_abs_value,
threadgroup float* partials,
const thread ThreadContext& thread_context
) {
static_assert(SIMDGROUPS <= METAL_SIMD_SIZE, "one lane per simdgroup partial");
const ushort lane_index = thread_context.simd_lane_id;
- const float simdgroup_maximum = simd_max(magnitude);
+ const float simdgroup_maximum = simd_max(largest_abs_value);
if (lane_index == 0) {
partials[thread_context.simdgroup_index] = simdgroup_maximum;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
- // magnitudes are non-negative, so 0 is the identity of the maximum
+ // magnitudes are non-negative, so 0 is the identity of the group_max_abs
return simd_max(lane_index < SIMDGROUPS ? partials[lane_index] : 0.0f);
}
-template <typename T>
-METAL_FUNC void write_activation_quantization_group(
- device T* output,
- const T value,
- const uint group_size,
- const uint element_count,
- const uint tile_index,
+UZU_CONST uint ACTIVATION_QUANT_VECTOR_SIZE = 4;
+
+template <typename T, typename Combine>
+static METAL_FUNC T reduce_adjacent_lanes(T value, const uint lane_count, Combine combine) {
+ for (ushort lane_stride = 1; lane_stride < lane_count; lane_stride <<= 1) {
+ value = combine(value, simd_shuffle_xor(value, lane_stride));
+ }
+ return value;
+}
+
+static METAL_FUNC void store_quantized_activation_vector(
+ const float4 values,
+ const ushort lane_index,
+ const bool in_bounds,
+ const uint first_index,
+ const uint row_width,
const uint batch_index,
- const thread ThreadContext& thread_context
+ const uint scale_group_size,
+ const bool grouped_by_weight_nibble,
+ const bool emit_group_sums,
+ const uint sum_group_size,
+ device int8_t* q_out,
+ device float* scales_out,
+ device int32_t* group_sums_out
) {
- const ushort simdgroup_index = thread_context.simdgroup_index;
- const uint simdgroups_per_group = group_size / METAL_SIMD_SIZE;
- const uint simdgroup_offset = tile_index * ACTIVATION_QUANT_TILE_SIZE + simdgroup_index * METAL_SIMD_SIZE;
- if (thread_context.simd_lane_id == 0 && simdgroup_offset < element_count &&
- (simdgroup_index + 1) % simdgroups_per_group == 0) {
- const uint groups_per_row = element_count / group_size;
- const uint groups_per_tile = ACTIVATION_QUANT_TILE_SIZE / group_size;
- const uint group_index = tile_index * groups_per_tile + simdgroup_index / simdgroups_per_group;
- output[batch_index * groups_per_row + group_index] = value;
+ const float largest_abs_value = max(max(fabs(values[0]), fabs(values[1])), max(fabs(values[2]), fabs(values[3])));
+ const uint lanes_per_scale_group = scale_group_size / ACTIVATION_QUANT_VECTOR_SIZE;
+ const float group_max_abs =
+ reduce_adjacent_lanes(largest_abs_value, lanes_per_scale_group, [](float x, float y) { return max(x, y); });
+ const float scale =
+ isfinite(group_max_abs) && group_max_abs > 0.0f ? group_max_abs / ACTIVATION_QUANT_INT8_MAX : 1.0f;
+ int8_t codes[ACTIVATION_QUANT_VECTOR_SIZE];
+ int thread_code_sum = 0;
+ uint packed_codes = 0;
+ METAL_PRAGMA_UNROLL
+ for (uint index = 0; index < ACTIVATION_QUANT_VECTOR_SIZE; ++index) {
+ codes[index] = quantize_activation_int8(values[index], scale);
+ thread_code_sum += static_cast<int>(codes[index]);
+ packed_codes |= static_cast<uint>(static_cast<uchar>(codes[index])) << (8 * index);
+ }
+
+ const uint row_start = batch_index * row_width;
+ if (grouped_by_weight_nibble) {
+ // Word order [0..7] -> [0, 4, 1, 5, 2, 6, 3, 7]: the even lane writes codes (0, 2, 4, 6), the odd lane (1, 3, 5,
+ // 7).
+ const uint neighbor_codes = simd_shuffle_xor(packed_codes, 1);
+ const uint is_odd_lane = lane_index & 1;
+ const uint even_codes = is_odd_lane ? neighbor_codes : packed_codes;
+ const uint odd_codes = is_odd_lane ? packed_codes : neighbor_codes;
+ if (in_bounds) {
+ const uint shift = 8 * is_odd_lane;
+ const packed_char4 word = packed_char4(
+ static_cast<char>(even_codes >> shift),
+ static_cast<char>(even_codes >> (shift + 16)),
+ static_cast<char>(odd_codes >> shift),
+ static_cast<char>(odd_codes >> (shift + 16))
+ );
+ *reinterpret_cast<device packed_char4*>(q_out + row_start + (first_index & ~7u) + 4 * is_odd_lane) = word;
+ }
+ } else if (in_bounds) {
+ *reinterpret_cast<device packed_char4*>(q_out + row_start + first_index) =
+ packed_char4(codes[0], codes[1], codes[2], codes[3]);
+ }
+
+ if (in_bounds && lane_index % lanes_per_scale_group == 0) {
+ scales_out[batch_index * (row_width / scale_group_size) + first_index / scale_group_size] = scale;
+ }
+
+ if (emit_group_sums) {
+ const uint lanes_per_sum_group = sum_group_size / ACTIVATION_QUANT_VECTOR_SIZE;
+ const int sum = reduce_adjacent_lanes(thread_code_sum, lanes_per_sum_group, [](int x, int y) { return x + y; });
+ if (in_bounds && lane_index % lanes_per_sum_group == 0) {
+ group_sums_out[batch_index * (row_width / sum_group_size) + first_index / sum_group_size] = sum;
+ }
}
}
diff --git a/crates/uzu-engine/src/backends/metal/kernel/gated_act_mul/gated_act_mul.metal b/crates/uzu-engine/src/backends/metal/kernel/gated_act_mul/gated_act_mul.metal
index 19ab11278..85b32e5f4 100644
--- a/crates/uzu-engine/src/backends/metal/kernel/gated_act_mul/gated_act_mul.metal
+++ b/crates/uzu-engine/src/backends/metal/kernel/gated_act_mul/gated_act_mul.metal
@@ -12,6 +12,7 @@ using namespace uzu::gated_act_mul;
#define NUM_SIMDGROUPS 4
#define NUM_THREADS NUM_SIMDGROUPS* METAL_SIMD_SIZE
+#define TILE_ELEMENTS NUM_THREADS* HADAMARD_VECTOR_SIZE
#define QUANTIZED (ops == GatedActMulOp::Quantize || ops == GatedActMulOp::QuantizeWithGroupSums)
#define EMITS_GROUP_SUMS (ops == GatedActMulOp::QuantizeWithGroupSums)
@@ -45,120 +46,94 @@ PUBLIC KERNEL(GatedActMul) (
const bool custom_activation_alpha SPECIALIZE,
const bool clip_gate SPECIALIZE,
const bool clip_value SPECIALIZE,
- threadgroup float partial_max OPTIONAL(QUANTIZED && activation_scale_group_size > METAL_SIMD_SIZE)[NUM_SIMDGROUPS],
- threadgroup int partial_sums OPTIONAL(EMITS_GROUP_SUMS && sum_group_size > METAL_SIMD_SIZE)[NUM_SIMDGROUPS],
- uint activation_tile_index GROUPS(gated_dim.div_ceil(NUM_THREADS)),
+ uint activation_tile_index GROUPS(gated_dim.div_ceil(TILE_ELEMENTS)),
uint batch_idx GROUPS(batch_dim),
uint thread_index THREADS(NUM_THREADS),
const ThreadContext thread_context
) {
- const uint gated_idx = activation_tile_index * ACTIVATION_QUANT_TILE_SIZE + thread_index;
- const uint simdgroup_offset =
- activation_tile_index * ACTIVATION_QUANT_TILE_SIZE + (thread_index / METAL_SIMD_SIZE) * METAL_SIMD_SIZE;
- const bool element_in_bounds = gated_idx < gated_dim;
- const bool simdgroup_in_bounds = simdgroup_offset + METAL_SIMD_SIZE <= gated_dim;
- T value = static_cast<T>(0);
- T gate = static_cast<T>(0);
- if (element_in_bounds) {
+ const uint first_index = activation_tile_index * TILE_ELEMENTS + thread_index * HADAMARD_VECTOR_SIZE;
+ const ushort lane_index = thread_context.simd_lane_id;
+ const uint valid_count =
+ first_index < gated_dim ? min(static_cast<uint>(HADAMARD_VECTOR_SIZE), gated_dim - first_index) : 0u;
+ const bool use_vector_io = valid_count == HADAMARD_VECTOR_SIZE;
+
+ vec<T, HADAMARD_VECTOR_SIZE> value = static_cast<T>(0);
+ vec<T, HADAMARD_VECTOR_SIZE> gate = static_cast<T>(0);
+ {
+ const device T* value_row;
+ const device T* gate_row;
if (interleaved) {
- const uint base = batch_idx * (2 * gated_dim);
- value = act_operand[base + gated_idx];
- gate = act_operand[base + gated_dim + gated_idx];
+ value_row = act_operand + batch_idx * (2 * gated_dim);
+ gate_row = value_row + gated_dim;
+ } else {
+ value_row = value_operand + batch_idx * value_row_stride + value_offset;
+ gate_row = act_operand + batch_idx * gated_dim;
+ }
+ if (use_vector_io) {
+ value = load_hadamard_vector(value_row + first_index);
+ gate = load_hadamard_vector(gate_row + first_index);
} else {
- value = value_operand[batch_idx * value_row_stride + value_offset + gated_idx];
- gate = act_operand[batch_idx * gated_dim + gated_idx];
+ for (uint index = 0; index < valid_count; ++index) {
+ value[index] = value_row[first_index + index];
+ gate[index] = gate_row[first_index + index];
+ }
}
}
if (clip_gate) {
- gate = static_cast<T>(clamp(float(gate), gate_clip_min, gate_clip_max));
+ gate = vec<T, HADAMARD_VECTOR_SIZE>(clamp(float4(gate), gate_clip_min, gate_clip_max));
}
if (clip_value) {
- value = static_cast<T>(clamp(float(value), value_clip_min, value_clip_max));
+ value = vec<T, HADAMARD_VECTOR_SIZE>(clamp(float4(value), value_clip_min, value_clip_max));
}
- if (!QUANTIZED) {
- T result = static_cast<T>(0);
- if (element_in_bounds && (!use_hadamard || simdgroup_in_bounds)) {
- float transformed;
+
+ const bool hadamard = QUANTIZED || use_hadamard;
+ const bool block_in_bounds =
+ (first_index / HADAMARD_TRANSFORM_BLOCK_SIZE + 1) * HADAMARD_TRANSFORM_BLOCK_SIZE <= gated_dim;
+ float4 results = 0.0f;
+ METAL_PRAGMA_UNROLL
+ for (uint index = 0; index < HADAMARD_VECTOR_SIZE; ++index) {
+ if (index < valid_count && (!hadamard || block_in_bounds)) {
if (custom_activation_alpha && act_type == ActivationType::SILU) {
- const T activated = activate_silu_alpha(gate, activation_alpha);
- const T gated = value * activated;
- transformed = static_cast<float>(gated);
+ const T activated = activate_silu_alpha(gate[index], activation_alpha);
+ const T gated = value[index] * activated;
+ results[index] = static_cast<float>(gated);
} else {
- transformed = gated_act_mul(value, gate, act_type);
+ results[index] = gated_act_mul(value[index], gate[index], act_type);
}
- if (use_hadamard) {
- transformed = simdgroup_input_random_hadamard_transform(
- static_cast<ushort>(gated_idx % METAL_SIMD_SIZE),
- transformed,
- hadamard_factors[gated_idx]
- );
- }
- result = static_cast<T>(transformed);
}
- if (element_in_bounds) {
- fp_out[batch_idx * gated_dim + gated_idx] = result;
+ }
+ if (hadamard) {
+ if (block_in_bounds) {
+ results *= float4(load_hadamard_vector(hadamard_factors + first_index));
}
- return;
+ results = simdgroup_hadamard_transform_vector(lane_index, results);
}
- float result = 0.0f;
- if (simdgroup_in_bounds) {
- if (custom_activation_alpha && act_type == ActivationType::SILU) {
- const T activated = activate_silu_alpha(gate, activation_alpha);
- const T gated = value * activated;
- result = static_cast<float>(gated);
+ if (!QUANTIZED) {
+ device T* out_row = fp_out + batch_idx * gated_dim;
+ if (use_vector_io) {
+ store_hadamard_vector(out_row + first_index, results);
} else {
- result = gated_act_mul(value, gate, act_type);
+ for (uint index = 0; index < valid_count; ++index) {
+ out_row[first_index + index] = static_cast<T>(results[index]);
+ }
}
- result = simdgroup_input_random_hadamard_transform(
- static_cast<ushort>(gated_idx % METAL_SIMD_SIZE),
- result,
- hadamard_factors[gated_idx]
- );
- }
-
- const float maximum = reduce_activation_quantization_group(
- fabs(result),
- activation_scale_group_size,
- partial_max,
- thread_context,
- [](float x) { return simd_max(x); },
- [](float x, float y) { return max(x, y); }
- );
- const float scale = isfinite(maximum) && maximum > 0.0f ? maximum / ACTIVATION_QUANT_INT8_MAX : 1.0f;
- const int8_t code = quantize_activation_int8(result, scale);
- if (element_in_bounds) {
- const uint output_gated_index = grouped_by_weight_nibble ? nibble_grouped_index(gated_idx) : gated_idx;
- q_out[batch_idx * gated_dim + output_gated_index] = code;
+ return;
}
- write_activation_quantization_group(
- scales_out,
- scale,
- activation_scale_group_size,
+ store_quantized_activation_vector(
+ results,
+ lane_index,
+ valid_count > 0,
+ first_index,
gated_dim,
- activation_tile_index,
batch_idx,
- thread_context
+ activation_scale_group_size,
+ grouped_by_weight_nibble,
+ EMITS_GROUP_SUMS,
+ sum_group_size,
+ q_out,
+ scales_out,
+ group_sums_out
);
-
- if (EMITS_GROUP_SUMS) {
- const int sum = reduce_activation_quantization_group(
- static_cast<int>(code),
- sum_group_size,
- partial_sums,
- thread_context,
- [](int x) { return simd_sum(x); },
- [](int x, int y) { return x + y; }
- );
- write_activation_quantization_group(
- group_sums_out,
- sum,
- sum_group_size,
- gated_dim,
- activation_tile_index,
- batch_idx,
- thread_context
- );
- }
}
diff --git a/crates/uzu-engine/src/backends/metal/kernel/hadamard_transform/hadamard_transform.h b/crates/uzu-engine/src/backends/metal/kernel/hadamard_transform/hadamard_transform.h
index 655772529..7777464e8 100644
--- a/crates/uzu-engine/src/backends/metal/kernel/hadamard_transform/hadamard_transform.h
+++ b/crates/uzu-engine/src/backends/metal/kernel/hadamard_transform/hadamard_transform.h
@@ -31,3 +31,36 @@ template <typename T>
static METAL_FUNC T simdgroup_output_random_hadamard_transform(ushort lane_index, T lane_value, int32_t lane_factor) {
return T(simdgroup_hadamard_transform(lane_index, float(lane_value)) * float(lane_factor));
}
+
+// Each thread owns HADAMARD_VECTOR_SIZE consecutive elements;
+#define HADAMARD_VECTOR_SIZE 4
+UZU_CONST ushort HADAMARD_LANES_PER_BLOCK = HADAMARD_TRANSFORM_BLOCK_SIZE / HADAMARD_VECTOR_SIZE;
+
+static METAL_FUNC float4 simdgroup_hadamard_transform_vector(ushort lane_index, float4 lane_values) {
+ for (ushort stride = 1; stride < HADAMARD_VECTOR_SIZE; stride <<= 1) {
+ METAL_PRAGMA_UNROLL
+ for (ushort index = 0; index < HADAMARD_VECTOR_SIZE; ++index) {
+ if (!(index & stride)) {
+ const float value = lane_values[index];
+ const float partner_value = lane_values[index + stride];
+ lane_values[index] = partner_value + value;
+ lane_values[index + stride] = value - partner_value;
+ }
+ }
+ }
+ for (ushort lane_stride = 1; lane_stride < HADAMARD_LANES_PER_BLOCK; lane_stride <<= 1) {
+ const float4 partner_values = simd_shuffle_xor(lane_values, lane_stride);
+ lane_values = (lane_index & lane_stride) ? (partner_values - lane_values) : (partner_values + lane_values);
+ }
+ return lane_values / sqrt(static_cast<float>(HADAMARD_TRANSFORM_BLOCK_SIZE));
+}
+
+template <typename T>
+static METAL_FUNC vec<T, HADAMARD_VECTOR_SIZE> load_hadamard_vector(const device T* source) {
+ return *reinterpret_cast<const device packed_vec<T, HADAMARD_VECTOR_SIZE>*>(source);
+}
+
+template <typename T, typename SourceT>
+static METAL_FUNC void store_hadamard_vector(device T* destination, const vec<SourceT, HADAMARD_VECTOR_SIZE> source) {
+ *reinterpret_cast<device packed_vec<T, HADAMARD_VECTOR_SIZE>*>(destination) = vec<T, HADAMARD_VECTOR_SIZE>(source);
+}