Skip to content
Open
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
2 changes: 1 addition & 1 deletion mlx/backend/metal/kernels/reduction/reduce_col.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ template <typename T, typename U, typename Op, typename IdxT, int NDIMS>

IdxT out_idx = gid.x + gsize.x * IdxT(gid.y);
IdxT in_idx = elem_to_loc<IdxT>(out_idx, shape, strides, ndim);
in += in_idx + lid.x;
in += in_idx + IdxT(lid.x);

U total = Op::init;
IdxT total_rows = IdxT(non_col_reductions) * IdxT(reduction_size);
Expand Down
28 changes: 28 additions & 0 deletions python/tests/test_reduce.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright © 2023 Apple Inc.

import math
from itertools import combinations, permutations

import mlx.core as mx
Expand Down Expand Up @@ -56,6 +57,33 @@ def test_row_reduce_negative_stride(self):
actual = getattr(mx, op)(x_mlx, axis=-1)
self.assertTrue(np.allclose(expected, actual))

def test_col_reduce_negative_stride(self):
# Exercises each Metal column reduction kernel on a negative-stride view.
cases = [
((2, 1024, 16), 1),
((2, 2, 1024, 16), 2),
((2, 512, 2, 2, 16), (1, 3)),
((2, 512, 64), 1),
((2, 64, 512), 1),
((2, 16, 16), 1),
]
for shape, axis in cases:
size = math.prod(shape)
x_npy = np.arange(1, size + 1).reshape(shape)[::-1]
x_mlx = mx.arange(1, size + 1).reshape(shape)[::-1]
for op in ["sum", "max", "min", "mean", "var"]:
with self.subTest(shape=shape, axis=axis, op=op):
expected = getattr(np, op)(x_npy, axis=axis)
actual = getattr(mx, op)(x_mlx, axis=axis)
self.assertTrue(np.allclose(expected, actual))
x_ones = mx.ones(shape, dtype=mx.float32)[::-1]
with self.subTest(shape=shape, axis=axis, op="prod"):
self.assertTrue(mx.all(mx.prod(x_ones, axis=axis) == 1).item())
x_bool = mx.ones(shape, dtype=mx.bool_)[::-1]
for op in ["all", "any"]:
with self.subTest(shape=shape, axis=axis, op=op):
self.assertTrue(mx.all(getattr(mx, op)(x_bool, axis=axis)).item())

def test_dtypes(self):
int_dtypes = [
"int8",
Expand Down