diff --git a/mlx/backend/metal/hadamard.cpp b/mlx/backend/metal/hadamard.cpp index c696e4d0ff..ef2f030b93 100644 --- a/mlx/backend/metal/hadamard.cpp +++ b/mlx/backend/metal/hadamard.cpp @@ -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); @@ -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( @@ -115,7 +112,6 @@ void hadamard_mn_contiguous( get_type_string(x.dtype()), n1, max_radix_1, - read_width_n1, n2); } if (m > 1) { diff --git a/mlx/backend/metal/kernels/hadamard.h b/mlx/backend/metal/kernels/hadamard.h index 9f2311c101..99712a4b38 100644 --- a/mlx/backend/metal/kernels/hadamard.h +++ b/mlx/backend/metal/kernels/hadamard.h @@ -26,7 +26,7 @@ METAL_FUNC void radix_func(thread float* x) { } } -template +template [[kernel]] void hadamard_n( const device T* in [[buffer(0)]], device T* out [[buffer(1)]], @@ -49,26 +49,7 @@ template 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; @@ -78,23 +59,32 @@ template 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(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) { @@ -113,28 +103,9 @@ template 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]; - } } } diff --git a/python/tests/test_ops.py b/python/tests/test_ops.py index eb2f2cb3f1..2d414209e3 100644 --- a/python/tests/test_ops.py +++ b/python/tests/test_ops.py @@ -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)