Fix Metal sort of a view with a negative stride - #4252
Open
robertomeroni wants to merge 1 commit into
Open
Conversation
The non-contiguous sort kernels compute each row's base offset with elem_to_loc(tid.y, ...). IdxT is deduced from the argument, so tid.y makes it uint and a negative stride wraps to ~4.29e9 instead of stepping backwards, so every row but the first reads out of bounds and Metal returns zeros. Ask for int64_t, matching what the CUDA kernels already do with int64_t(blockIdx.y).
4 tasks
zcbenz
requested changes
Aug 15, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
The added tests pass without the change.
Contributor
Author
|
@zcbenz they fail on the Metal stream. On the CPU stream they pass either way, because the CPU path was |
zcbenz
approved these changes
Aug 15, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Ah sorry didn't realize I was running a cpu only build, this looks good to me!
Contributor
Author
|
no worries :) |
4 tasks
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
Fixes #4225. #4226 addresses the same bug by copying negative-stride inputs to a contiguous buffer
before the sort; the review there asks for the root cause instead, which is what this does. On Metal,
mx.sort/mx.argsort/mx.partition/mx.topkreturn zeros for every segment but the firstwhen the input is a view with a negative stride on an axis that is not being sorted:
block_sort_ncandmb_block_sortget each segment's base offset fromelem_to_loc(tid.y, ...).elem_to_locdeducesIdxTfrom its first argument, sotid.ymakes ituint: a negative stridewraps to ~4.29e9 instead of stepping backwards, the segment reads out of bounds, and Metal returns
zeros. Asking for
int64_tmatches what the CUDA kernels already do withint64_t(blockIdx.y).block_sorthas the same signedness issue atsort.h:283-284, left unpatched here: slicing andas_stridedclearflags().contiguousfor a negative stride, so the only producer that reaches it isan independent
Splitmis-flagging bug whose CPU path is wrong for the same input. Happy to add thetwo lines if you would rather have all three sites consistent.
Added to
test_sort, coveringint32andfloat32at both the single-block and multi-block sizes;all four subtests fail on the unpatched kernel. On an M3 Pro
python/testsis 810 passed / 21 skippedand the C++ suite 266 cases / 3600 assertions.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI assistance was used in developing this change. I reviewed and tested every line.