Fix Nx.LinAlg.pinv for batched input - #1816
Merged
Merged
Conversation
pinv's final contraction used plain Nx.dot/2, which for rank-3+ inputs
is a full tensor contraction rather than a batched matmul — producing a
doubled-batch intermediate ({b, n, b, m}). The defn cond that guards
the all-zeros case then either failed to unify branch shapes (crash for
n >= 3: "cannot broadcast {2, 3, 2, 3} to {2, 3, 3}") or, worse,
silently broadcast the zero branch INTO the rank-4 shape (n = 2
returned {2, 2, 2, 2} with no error).
Use the batched form via the file's existing batch_axes/1 helper:
Nx.dot(v, [-1], batch_axes, sut, [-2], batch_axes)
For rank-2 input batch_axes is [] and behavior is unchanged.
Adds a batched test: square/non-square/double-batch shapes, asserting
the batched result equals the stacked per-matrix results and the
Moore-Penrose identity A P A = A per batch (f64; the identity is
conditioning-sensitive at f32 with unconditioned random draws).
Not covered here: Nx.LinAlg.svd itself crashes on any *batched* matrix
with a size-1 dimension ({2, 1, 1}, {2, 1, 2}, {2, 2, 1}), which also
blocks pinv for those shapes — a separate pre-existing bug.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
polvalente
approved these changes
Aug 17, 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.
Problem
Nx.LinAlg.pinvis broken for every batched (3-D+) input, with three size-dependent failure modes:{2, 2, 2}{2, 2, 2, 2}(rank 4), no error{2, n, n}, n ≥ 3ArgumentError: cannot broadcast tensor of dimensions {2, n, 2, n} to {2, n, n}Found by fuzzing; every other
Nx.LinAlgdecomposition supports leading batch dimensions.Cause
The final contraction in
pinv_non_zeroused plainNx.dot/2:For rank-3+ inputs
Nx.dot/2is a full tensor contraction, not a batched matmul — it produces a doubled-batch{b, n, b, m}. The defncondguarding the all-zeros case then tries to unify the two branch shapes: for n ≥ 3 they can't broadcast (the crash), but for n = 2 the correct{2, 2, 2}zero-branch happens to broadcast into{2, 2, 2, 2}— which is why that case fails silently with a wrong-shape result.Fix
One line, using the
batch_axes/1helper that already exists inlin_alg.exfor exactly this purpose:Rank-2 input yields
batch_axes == []and identical behavior to before.Tests
New "supports batched input" test in the pinv describe:
{2,3,3},{2,3,4},{2,4,3}, and double-batch{2,2,3,3}, asserting (1) output shape, (2) the batched result equals the stacked per-matrixpinvresults, and (3) the Moore–Penrose identityA·P·A ≈ Aper batch. f64 inputs — the MP identity is conditioning-sensitive at f32 with unconditioned random draws.Full nx suite green: 1384 doctests + 1370 tests.
Explicitly out of scope
Nx.LinAlg.svditself crashes on any batched matrix with a size-1 dimension ({2,1,1},{2,1,2},{2,2,1}— unbatched{1,1}works), which independently blockspinvfor those shapes. That's a separate pre-existing bug and is not addressed here.🤖 Generated with Claude Code