Fix scan and sort for a zero-size axis - #4340
Open
eyupcanakman wants to merge 1 commit into
Open
Conversation
eyupcanakman
force-pushed
the
fix/empty-axis-scan-sort
branch
from
August 19, 2026 15:08
beffa50 to
f07f722
Compare
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
ScanandSortwork out the row count on the host within.size() / in.shape(axis).When the scanned or sorted axis has length zero, both operands are zero.
On x86-64 the
DIVinstruction raises#DE, somx.cumsum(mx.array([]), axis=0)kills the process withSIGFPE.Apple silicon never sees it.
AArch64 integer division by zero returns 0 and raises nothing, so the loop bound comes out zero and the empty result is right by accident.
I built
mainfor linux/amd64 in a container and rancumsumon a shape(0,)array.It dies with a floating point exception, exit 136.
The same build with the guard exits 0 and prints the empty result.
A
USE_UBSANbuild on macOS reports the division atmlx/backend/cpu/scan.cpp:170andmlx/backend/cpu/sort.cpp:124before the change, and nothing after it.The fix returns early when the output is empty, in the scan and sort entry points of all three backends.
It is the same
out.size() == 0checkSearchSortedalready carries.Eight files is one guard landing in three backends across two ops, plus the two test files.
PartitionandArgPartitiondivide the same way on CPU and are left alone.mlx/ops.cpprejects everykthon a zero-length axis, so those primitives are never built.On Metal and CUDA they route through the shared sort helper and pick up the guard anyway.
I have no NVIDIA hardware here.
Both
.cufiles compile clean undernvcc12.9 for sm_80, but running them needs a real GPU, so that part rides on CI.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes