Skip to content

Fix Nx.LinAlg.pinv zero-branch shape for multiple batch dimensions - #1854

Merged
polvalente merged 1 commit into
elixir-nx:mainfrom
blasphemetheus:fix/pinv-zero-shape-batch
Oct 7, 2026
Merged

polvalente merged 1 commit into
elixir-nx:mainfrom
blasphemetheus:fix/pinv-zero-shape-batch

Conversation

@blasphemetheus

Copy link
Copy Markdown
Contributor

Nx.LinAlg.pinv raises for inputs with two or more batch dimensions of different sizes:

iex> 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}

pinv_zero_shape builds 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, and cond raises when it unifies the two branch shapes.

The fix transposes the trailing two dimensions in place with put_elem and 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 on main with the error above and pass with the fix. The full nx suite passes.

🤖 Generated with Claude Code

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>
@blasphemetheus

Copy link
Copy Markdown
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
blasphemetheus marked this pull request as ready for review October 7, 2026 06:23
Comment thread nx/lib/nx/lin_alg.ex
Comment on lines -1263 to -1264
tl
|> List.to_tuple()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just missed an Enum.reverse here to undo the reverse above! Code below is cleaner anyway :)

@polvalente
polvalente merged commit 8033979 into elixir-nx:main Oct 7, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants