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
1 change: 1 addition & 0 deletions mlx/backend/metal/kernels/reduce.metal
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ instantiate_init_min_max(max, Max)

#define instantiate_and_or(name, op) \
instantiate_reduce_functions(name, bool_, bool, bool, op) \
instantiate_reduce_functions(name, int8, int8_t, bool, op) \
instantiate_reduce_functions(name, int16, int16_t, bool, op) \
instantiate_reduce_functions(name, int32, int32_t, bool, op) \
instantiate_reduce_functions(name, int64, int64_t, bool, op) \
Expand Down
5 changes: 4 additions & 1 deletion mlx/backend/metal/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,11 @@ std::pair<Dtype, Dtype> remap_reduce_types(
default:
break;
}
if (in.dtype().size() == 1) {
if (in.dtype() == bool_) {
return {bool_, bool_};
} else if (in.dtype().size() == 1) {
// A Metal bool must hold 0 or 1, so read int8 and uint8 as int8.
return {int8, bool_};
} else if (in.dtype().size() == 2) {
return {int16, bool_};
} else if (in.dtype().size() == 4) {
Expand Down
21 changes: 21 additions & 0 deletions python/tests/test_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,27 @@ def test_and_or_negative_zero(self):
getattr(np, op)(x_np, axis=1).tolist(),
)

def test_and_or_small_ints(self):
# Even values have the low bit clear but are still nonzero
for dtype in ["int8", "uint8"]:
for shape, axis in [
((70,), None),
((4, 33), 1),
((4096, 64), 1),
((3, 7, 9), (1, 2)),
((33, 4), 0),
((8, 5000), 0),
]:
with self.subTest(dtype=dtype, shape=shape, axis=axis):
some = np.zeros(shape, dtype=dtype)
some.reshape(-1)[::5] = 2
for a_np in [some, some + 2]:
a_mx = mx.array(a_np)
for op in ["any", "all"]:
out = getattr(mx, op)(a_mx, axis=axis)
ref = getattr(np, op)(a_np, axis=axis)
self.assertTrue(np.array_equal(out, ref))

def test_large_offsets(self):
# Row r holds r % 251 and row 2**15 starts at offset 2**31, so any
# reduction that narrows offsets or sizes to 32 bits reads the wrong
Expand Down
3 changes: 0 additions & 3 deletions python/tests/xp-tests/xfail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,6 @@ array_api_tests/test_searching_functions.py::test_argmin
## Misisng arguments
### Missing argument `append` `prepend`
array_api_tests/test_utility_functions.py::test_diff_append_prepend
## Other failures
### AssertionError: out=False, but should be True [any()]
array_api_tests/test_utility_functions.py::test_any

# test_creation_functions.py
## Other failures
Expand Down
Loading