Repository navigation
Fix Nx.LinAlg.pinv zero-branch shape for multiple batch dimensions - #1854
Merged
polvalente merged 1 commit intoOct 7, 2026
Merged
Conversation
pinv_zero_shape transposes the last two dimensions by reversing the
dimension list, taking the first two, and rebuilding, but it never
re-reverses the remaining batch prefix. With a single batch dimension
this is invisible, since reversing one element is a no-op, and equal
batch dimensions also come out right. With two or more unequal batch
dimensions the zero branch gets a batch-transposed shape and defn's
cond raises when it unifies the two branch shapes:
Nx.LinAlg.pinv(Nx.iota({3, 2, 2, 2}, type: :f64) |> Nx.add(Nx.eye(2)))
** (ArgumentError) cannot broadcast tensor of dimensions
{3, 2, 2, 2} to {2, 3, 2, 2}
Transpose the trailing two dimensions in place with put_elem, leaving
the batch prefix untouched.
Tests cover unequal batch dimensions ({3,2,2,2} and non-square
{5,4,2,3}) asserting the output shape and the Moore-Penrose identity
A·P·A == A per batch, plus the all-zeros branch on a double batch.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Contributor
Author
|
this one leads to a pretty straightforward argument error, but I couldn't say why the fix works as this is some arcane math to me, pinv. so if there's a better solution to this failing test, probably should go with that |
blasphemetheus
marked this pull request as ready for review
October 7, 2026 06:23
polvalente
reviewed
Oct 7, 2026
Comment on lines
-1263
to
-1264
| tl | ||
| |> List.to_tuple() |
Member
There was a problem hiding this comment.
I think we just missed an Enum.reverse here to undo the reverse above! Code below is cleaner anyway :)
polvalente
approved these changes
Oct 7, 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.
Nx.LinAlg.pinvraises for inputs with two or more batch dimensions of different sizes:pinv_zero_shapebuilds the shape of the all-zeros branch by reversing the dimension list, taking the first two, and rebuilding, but it never re-reverses the remaining batch prefix. With one batch dimension, or with equal batch dimensions, reversing is a no-op and nothing shows. With unequal ones the zero branch gets a batch-transposed shape, andcondraises when it unifies the two branch shapes.The fix transposes the trailing two dimensions in place with
put_elemand leaves the batch prefix alone.The tests cover
{3, 2, 2, 2}and the non-square{5, 4, 2, 3}, asserting both the output shape and the Moore-Penrose identity A·P·A = A per batch, plus the all-zeros branch on a double batch. They fail onmainwith the error above and pass with the fix. The fullnxsuite passes.🤖 Generated with Claude Code