Repository navigation
Fix empty unsigned sum/prod failing on Metal - #4546
Merged
Merged
Conversation
zcbenz
marked this pull request as ready for review
October 1, 2026 03:02
zcbenz
force-pushed
the
fix-init-reduce-unsigned
branch
from
October 1, 2026 03:05
7af0a79 to
c5adcb8
Compare
zcbenz
approved these changes
Oct 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
mlx runs operations like
sum,prod,maxon GPUs. The problem in the existing code is related to the init kernel. It's that we don't have unsigned types defined forsumandprod:instantiate_init_min_max, right below it in the same file, lists all 13 types including uint8 to uint64, somin/maxalready work on empty unsigned arrays.Most of the time it goes unnoticed, but when the array is empty, there's nothing to add up or multiply, so the answer is only that starting value.
init_reduceis reached only there:mlx goes looking for the init kernel that was never built, and we get:
Any ordinary way of ending up with an empty unsigned array does the same:
This hits uint8, uint16, uint32 and uint64, for both
sumandprod. Signed, float and bool types are fine.On the released 0.32.2 version it can be worse than an error:
mx.sum(...).item()raises a catchableRuntimeError, butnp.array(mx.sum(...))aborts the process (libc++abi: terminating) even inside atry/except.The fix
We add
uint32anduint64to the sum/prod list. These are the two types missing, because summing any unsigned type produces uint32 (from uint8, uint16, uint32) or uint64 (from uint64). CUDA is unaffected:init_reduce.cuusesdispatch_all_types, so it instantiates every type already.How I found this
I ran mlx against numpy across many shapes, types and edge cases, comparing answers. The empty-unsigned case crashed the test script, and narrowing it down gave the exact types and the missing kernels.
How I proved it
I built mlx from source on my MacBook, confirmed this failure exists on unmodified code, applied the fix, rebuilt it and confirmed all 4 unsigned types now return 0 for
sumand 1 forprod:The added test
test_zero_size_sum_prod_all_dtypescovers 11 dtypes and both empty shapes; it fails on main and passes with the fix.Built at commit
59d600b5ewith Xcode 27 on an M4 Pro.