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: 4 additions & 2 deletions mlx/backend/common/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ ReductionPlan get_reduction_plan(const array& x, const std::vector<int>& 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<int64_t>(shape.back()) * x.shape(axes[i]) <= INT32_MAX) {
shape.back() *= x.shape(axes[i]);
strides.back() = x.strides()[axes[i]];
} else {
Expand Down Expand Up @@ -94,7 +95,8 @@ ReductionPlan get_reduction_plan(const array& x, const std::vector<int>& 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<int64_t>(a.first) * b.first <= INT32_MAX) {
reductions.erase(reductions.begin() + i);
reductions[i - 1] = std::make_pair(a.first * b.first, a.second);
}
Expand Down
6 changes: 6 additions & 0 deletions mlx/backend/cpu/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ void reduction_op(
array& out,
const std::vector<int>& axes,
U init) {
// Nothing to reduce just initialize the output
if (x.size() == 0) {
std::fill_n(out.data<U>(), out.size(), init);
return;
}

ReductionPlan plan = get_reduction_plan(x, axes);

auto in_ptr = x.data<T>();
Expand Down
4 changes: 2 additions & 2 deletions mlx/backend/metal/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 10 additions & 0 deletions python/tests/test_reduce.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright © 2023 Apple Inc.

import math
import os
import unittest
from itertools import combinations, permutations

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading