Fix Metal row reductions on negative-stride views - #4267
Merged
zcbenz merged 2 commits intoAug 16, 2026
Conversation
FU-max-boop
marked this pull request as ready for review
August 15, 2026 09:46
zcbenz
reviewed
Aug 16, 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.
Proposed changes
Metal row reductions return incorrect values when a non-reduced dimension has a negative stride and the reduced row contains more than 64 elements. For example:
NumPy and the MLX CPU backend return
[6370, 2145]; current Metal returns[6370, 0]. The same pointer error affectsmax,min,mean, andvar.row_reduce_loopedcombines the signed offset returned byelem_to_loc<IdxT>with the unsigned expressionlid.x * N_READS. For the ordinaryIdxT=intspecialization, a negative outer-dimension offset is therefore promoted touintand wraps before pointer arithmetic. Castinglid.xtoIdxTkeeps this addition signed. The large precompiledIdxT=int64_tspecialization is unchanged semantically.This adds an explicit Metal regression test for five row reductions on a 65-element negative-stride view. This is adjacent to the signed-indexing issue addressed for sort kernels in #4252, but affects the separate row-reduction kernel.
Validation
main(140faa8a) / development Metal wheel: the new regression test fails for all five operations.python -m unittest test_reduce— 13 passed; the Metal-only regression is skipped as intended.pre-commit run --all-files— passed.git diff --check— passed.This machine has the Command Line Tools but not the standalone Xcode Metal compiler, so the patched metallib has not been rebuilt locally. The repository's macOS CI must compile and validate the Metal change before merge.
Disclosure: Codex assisted with differential testing and review. I verified the reproduction, root cause, focused diff, and local checks before submission.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes