Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions mlx/backend/metal/hadamard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ void hadamard_mn_contiguous(
metal::Device& d,
const Stream& s) {
int n = n1 * n2;
int read_width_n1 = n1 == 2 ? 2 : 4;
int read_width_n2 = n2 == 2 ? 2 : 4;
// The m stage strides by n / read_width_m, so cap the read width at n.
int read_width_m = (n == 1) ? 1 : ((n == 2 || m == 28) ? 2 : 4);
int max_radix_1 = std::min(n1, 16);
Expand Down Expand Up @@ -105,8 +103,7 @@ void hadamard_mn_contiguous(
"hadamard_n",
get_type_string(x.dtype()),
n2,
max_radix_2,
read_width_n2);
max_radix_2);
}
if (n1 > 1) {
kernel += get_template_definition(
Expand All @@ -115,7 +112,6 @@ void hadamard_mn_contiguous(
get_type_string(x.dtype()),
n1,
max_radix_1,
read_width_n1,
n2);
}
if (m > 1) {
Expand Down
69 changes: 20 additions & 49 deletions mlx/backend/metal/kernels/hadamard.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ METAL_FUNC void radix_func(thread float* x) {
}
}

template <typename T, int N, int max_radix, int read_width, int stride = 1>
template <typename T, int N, int max_radix, int stride = 1>
[[kernel]] void hadamard_n(
const device T* in [[buffer(0)]],
device T* out [[buffer(1)]],
Expand All @@ -49,26 +49,7 @@ template <typename T, int N, int max_radix, int read_width, int stride = 1>
int batch_idx = elem.y * N * stride + elem.z;
short i = elem.x;

threadgroup T buf[N];

// Read values from device
if (stride == 1) {
STEEL_PRAGMA_UNROLL
for (short j = 0; j < max_radix / read_width; j++) {
short index = j * read_width * num_threads + i * read_width;
STEEL_PRAGMA_UNROLL
for (short r = 0; r < read_width; r++) {
buf[index + r] = in[batch_idx + index + r];
}
}
} else {
STEEL_PRAGMA_UNROLL
for (short j = 0; j < max_radix; j++) {
buf[j * num_threads + i] = in[batch_idx + (j * num_threads + i) * stride];
}
}

threadgroup_barrier(mem_flags::mem_threadgroup);
threadgroup float buf[N];

float x[max_radix];
short h = 1;
Expand All @@ -78,23 +59,32 @@ template <typename T, int N, int max_radix, int read_width, int stride = 1>
short k = i & (h - 1);
short j = ((i - k) << logR) + k;

// The first step reads from device memory
STEEL_PRAGMA_UNROLL
for (short r = 0; r < max_radix; r++) {
x[r] = buf[j + h * r];
x[r] =
(s == 0) ? float(in[batch_idx + (j + r) * stride]) : buf[j + h * r];
}

radix_func<max_radix>(x);

STEEL_PRAGMA_UNROLL
for (short r = 0; r < max_radix; r++) {
buf[j + h * r] = T(x[r]);
// Without a final radix, the last step writes to device memory
if (final_radix == 1 && s == num_steps - 1) {
STEEL_PRAGMA_UNROLL
for (short r = 0; r < max_radix; r++) {
out[batch_idx + (j + h * r) * stride] = T(x[r] * scale);
}
} else {
STEEL_PRAGMA_UNROLL
for (short r = 0; r < max_radix; r++) {
buf[j + h * r] = x[r];
}
h <<= logR;
threadgroup_barrier(mem_flags::mem_threadgroup);
}

h <<= logR;
threadgroup_barrier(mem_flags::mem_threadgroup);
}

// Do the final radix
// Do the final radix and write to device memory
// e.g. max_radix = 16
// N = 1024 = 16 * 16 * 4
if (final_radix > 1) {
Expand All @@ -113,28 +103,9 @@ template <typename T, int N, int max_radix, int read_width, int stride = 1>

STEEL_PRAGMA_UNROLL
for (short r = 0; r < final_radix; r++) {
buf[j + h * r] = T(x[r]);
}
}
threadgroup_barrier(mem_flags::mem_threadgroup);
}

// Write values to device
if (stride == 1) {
STEEL_PRAGMA_UNROLL
for (short j = 0; j < max_radix / read_width; j++) {
short index = j * read_width * num_threads + i * read_width;
STEEL_PRAGMA_UNROLL
for (short r = 0; r < read_width; r++) {
out[batch_idx + index + r] = T(buf[index + r] * scale);
out[batch_idx + (j + h * r) * stride] = T(x[r] * scale);
}
}
} else {
STEEL_PRAGMA_UNROLL
for (short j = 0; j < max_radix; j++) {
out[batch_idx + (j * num_threads + i) * stride] =
buf[j * num_threads + i];
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions python/tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4571,6 +4571,21 @@ def test_hadamard_m_only(self):
mx.eval(y_nd)
self.assertLess(mx.abs(y_cpu - y_nd).max().item(), 1e-5)

@unittest.skipIf(not mx.metal.is_available(), "Metal only")
def test_hadamard_half_precision(self):
if mx.default_device() == mx.cpu:
self.skipTest("requires GPU")

# A transform in one kernel must round only once, as float32 does
sizes = [2**k for k in range(1, 14)] + [12, 20, 28]
tests = product(sizes, (mx.float16, mx.bfloat16), (None, 0.25))
for n, dtype, scale in tests:
with self.subTest(n=n, dtype=dtype, scale=scale):
x = mx.random.normal((3, 5, n), key=mx.random.key(n)).astype(dtype)
y = mx.hadamard_transform(x, scale=scale)
y_f32 = mx.hadamard_transform(x.astype(mx.float32), scale=scale)
self.assertTrue(mx.array_equal(y, y_f32.astype(dtype)))

def test_hadamard_grad_vmap(self):
np.random.seed(4)

Expand Down
Loading