Repository navigation
Reject negative dimensions in broadcast_to and random shapes - #4046
Merged
Merged
Conversation
Contributor
Author
|
Rebased on main and added >>> mx.as_strided(mx.arange(6.0), (-2, 3), (3, 1), 0).size
18446744073709551610Only the shape is checked, negative strides are still allowed since |
ayaangazali
force-pushed
the
reject-negative-dims
branch
from
August 7, 2026 17:09
60caca2 to
a7265bd
Compare
zcbenz
approved these changes
Aug 8, 2026
1 task done
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
mx.broadcast_toand everymx.random.*sampler accept negative dimensions and hand back an array whose shape is negative. The size is then computed as an unsigned product, so it underflows:Reachable without writing a negative literal, since scale factors flow into shapes:
Everything else in the library already rejects this.
mx.zeros,mx.onesandmx.fullraise[full] Negative dimensions not allowed.,mx.reshaperaises, and numpy raisesall elements of broadcast shape must be non-negativefor the samebroadcast_tocall. These two paths are the ones that missed the check.Added the same guard
fullalready uses, at the two places a user supplied shape enters:broadcast_toinmlx/ops.cpprandom::bitsinmlx/random.cpp, which every sampler (uniform,normal,randint,gumbel,laplace,truncated_normal) routes throughZero sized dimensions are untouched:
mx.broadcast_to(mx.ones((3,)), (0, 3))andmx.random.uniform(shape=(0,))still work.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesTested on a CPU only build (
MLX_BUILD_METAL=OFF).test_ops.py,test_random.py,test_array.py,test_nn.pyandtest_autograd.pyall pass; the two new assertions fail on main.freshman contributor, i use Claude Code alongside my own digging. found this by feeding deliberately bad shapes to the array constructors and comparing which ones complained, then traced the two that did not back to the missing check.