diff --git a/mlx/backend/common/reduce.cpp b/mlx/backend/common/reduce.cpp index ceef464007..322c2225f0 100644 --- a/mlx/backend/common/reduce.cpp +++ b/mlx/backend/common/reduce.cpp @@ -39,7 +39,8 @@ ReductionPlan get_reduction_plan(const array& x, const std::vector& axes) { Shape shape = {x.shape(axes[0])}; Strides strides = {x.strides()[axes[0]]}; for (int i = 1; i < axes.size(); i++) { - if (axes[i] - 1 == axes[i - 1] && x.shape(axes[i]) > 1) { + if (axes[i] - 1 == axes[i - 1] && x.shape(axes[i]) > 1 && + static_cast(shape.back()) * x.shape(axes[i]) <= INT32_MAX) { shape.back() *= x.shape(axes[i]); strides.back() = x.strides()[axes[i]]; } else { @@ -94,7 +95,8 @@ ReductionPlan get_reduction_plan(const array& x, const std::vector& axes) { auto b = reductions[i - 1]; // b.stride = a.shape * a.stride then a and b are contiguous - if (b.second == a.first * a.second) { + if (b.second == a.first * a.second && + static_cast(a.first) * b.first <= INT32_MAX) { reductions.erase(reductions.begin() + i); reductions[i - 1] = std::make_pair(a.first * b.first, a.second); } diff --git a/mlx/backend/cpu/reduce.cpp b/mlx/backend/cpu/reduce.cpp index cd4911e898..edd7efa4bf 100644 --- a/mlx/backend/cpu/reduce.cpp +++ b/mlx/backend/cpu/reduce.cpp @@ -158,6 +158,12 @@ void reduction_op( array& out, const std::vector& axes, U init) { + // Nothing to reduce just initialize the output + if (x.size() == 0) { + std::fill_n(out.data(), out.size(), init); + return; + } + ReductionPlan plan = get_reduction_plan(x, axes); auto in_ptr = x.data(); diff --git a/mlx/backend/metal/reduce.cpp b/mlx/backend/metal/reduce.cpp index aa1b07fcd4..8a2ea95b32 100644 --- a/mlx/backend/metal/reduce.cpp +++ b/mlx/backend/metal/reduce.cpp @@ -568,8 +568,8 @@ void row_reduce_general_dispatch( // Prepare the arguments for the kernel RowReduceArgs args(in, plan, axes); - // Case 1: The row is small - if (args.row_size <= 64) { + // Case 1: The row is small and the kernel's 32-bit counter cannot wrap + if (args.row_size <= 64 && args.non_row_reductions <= UINT32_MAX - 31) { return row_reduce_small(in, out, op_name, args, compute_encoder, d, s); } diff --git a/python/tests/test_reduce.py b/python/tests/test_reduce.py index 22a77bdabc..a1446a2549 100644 --- a/python/tests/test_reduce.py +++ b/python/tests/test_reduce.py @@ -1,6 +1,7 @@ # Copyright © 2023 Apple Inc. import math +import os import unittest from itertools import combinations, permutations @@ -217,6 +218,10 @@ def test_zero_size(self): mx.eval(out) self.assertEqual(out.shape, (0,)) + # An empty array with large dimensions + a = mx.zeros((1, 2**16, 2**16, 0)) + self.assertEqual(mx.sum(a, axis=(1, 2, 3)).item(), 0) + def test_sum_bool(self): x = np.random.uniform(0, 1, size=(10, 10, 10)) > 0.5 y = mx.array(x) @@ -385,6 +390,11 @@ def test_large_offsets(self): self.assertTrue(x.any().item()) self.assertFalse(x.all().item()) + # Reducing two axes that together hold 2**31 + 2**16 elements + if not (mx.cuda.is_available() and "CI" in os.environ): + self.assertEqual(x[None].max(axis=(1, 2)).item(), 250) + self.assertEqual(x.T[None].max(axis=(1, 2)).item(), 250) + @unittest.skipIf(not mx.metal.is_available(), "requires Metal") def test_init_reduce_large_output(self): out = mx.all(mx.zeros((0, 4, 2**30), dtype=mx.bool_), axis=0)