fix(linalg): keep the input rank in norm with keepdims and no axis - #4166
Open
devteamaegis wants to merge 1 commit into
Open
fix(linalg): keep the input rank in norm with keepdims and no axis#4166devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
With no axis and no ord, norm flattens the input before reducing, so keepdims restored the rank of the flattened array instead of the original one. A (2, 3, 4) input returned shape (1,) instead of (1, 1, 1). Reshape the result back to the input rank. Every other path was already correct: an explicit ord, an explicit axis, and the other reductions (sum, mean, max, var) all keep the rank. The existing test already covered this case but compared with np.allclose, which broadcasts (1,) against (1, 1) and passes. Assert the shape alongside the values so the blind spot is closed.
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.linalg.norm(x, keepdims=True)collapses the result to a single dimension instead of keeping the input rank, when bothaxisandordare left at their defaults:(4,)(1,)(1,)(2, 3)(1,)(1, 1)(2, 3, 4)(1,)(1, 1, 1)(2, 3, 4, 5)(1,)(1, 1, 1, 1)A 0-d input is wrong in the other direction: mlx gives
(1,)where NumPy gives().Every neighbouring path is already correct, so this is specifically the default-argument path:
ordworks —ord=2,1,inf,"fro","nuc"all give(1, 1)for a(2, 3)inputaxisworks —axis=0,1,(0, 1)all match NumPysum,mean,max,varwithkeepdims=Trueall give(1, 1)Why. With no axis,
normflattens the input and reduces axis 0, sokeepdimspreserves the rank of the flattened array (always 1) rather than the original. The two sibling overloads that take anorddon't flatten — they build the axis list over the original array — which is why they were unaffected.The fix. Reshape the result back to the input rank when
keepdimsis set. The computed value is untouched; only the shape changes. This also fixes the 0-d case, since a rank-0 input reshapes back to a scalar.The test. The existing
test_normalready exercised this exact case, but compared withnp.allclose, which broadcasts(1,)against(1, 1)and passes — so the shape bug was invisible. I addedassertEqual(out_mx.shape, out_np.shape)alongside the value check in all threetest_normloops, which closes the blind spot generally rather than only for this bug.Fails before, passes after:
Only those three subtests failed beforehand, so the added shape assertions do not flag anything else.
Benchmark. The added
reshapeis metadata-only and runs only whenkeepdimsis true. CPU, M4, best of 3, usingbenchmarks/python/time_utils.py:norm((1_000_000,))keepdims=Falsenorm((1_000_000,))keepdims=Truenorm((1024, 1024))keepdims=Falsenorm((1024, 1024))keepdims=Truenorm((128, 128, 64))keepdims=Falsenorm((128, 128, 64))keepdims=Truenorm((1024, 1024), ord="fro", keepdims=True)(control)All within ~2% of each other and of the untouched control, i.e. indistinguishable from noise.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes