Fix diag for zero-size input - #4165
Merged
Merged
Conversation
diag builds an n x n zero matrix and scatters the input onto its diagonal. When the input is empty and k is zero, n is zero too, and scattering the empty updates into the 0x0 output raised [scatter] Updates with shape (0,1,1) are too large for array with shape (0,0) Skip the scatter when there is nothing to place. Every non-zero k already worked, since n is then at least |k|.
zcbenz
approved these changes
Aug 12, 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
What's broken.
mx.diagraises an internal scatter error on a zero-size 1-D input:NumPy returns a
(0, 0)array. The message comes fromscatter, so this is an internal failure leaking out rather than a deliberate rejection — comparetril, which rejects a 1-D input with a clear[tril] array must be at least 2-D.Only
k = 0is affected, which is the default. Every other offset already worked, becausenis then at least|k|:mx.diag(mx.zeros((0,)), k)np.diag(np.zeros((0,)), k)k=-2(2, 2)(2, 2)k=-1(1, 1)(1, 1)k=0(0, 0)k=1(1, 1)(1, 1)k=2(2, 2)(2, 2)The neighbouring zero-size cases are all fine too, so this is an isolated hole:
mx.diagon(0, 0),(0, 3)and(3, 0), plusmx.eye(0),mx.tri(0),mx.identity(0)andmx.tril/mx.triuon(0, 0)all match NumPy.Why.
diagbuilds ann x nzero matrix and scatters the input onto its diagonal. With an empty input andk = 0,nis0, and scattering the empty updates into the0x0output is rejected.The fix. Return the zero matrix directly when there is nothing to place on the diagonal.
The test.
test_diag_zero_sizeinpython/tests/test_ops.py, coveringkin-2..2acrossfloat32,int32andcomplex64(shape, dtype and values against NumPy), plus the zero-size 2-D inputs that already worked, so they stay covered.Fails before, passes after:
python/tests/test_ops.pyis green (151 passed, 309 subtests). The rest of the suite matches the baseline on this machine; the only failures are 18 pre-existingMetal DLPack import is not availableerrors from my CPU-only build, which are unrelated to this change.No benchmark: the change is an early return on a zero-size input, so it cannot affect any non-empty path.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes