Skip to content

Fix incorrect static output shape in MatrixPinv for non-square inputs#1885

Merged
ricardoV94 merged 2 commits intopymc-devs:mainfrom
WHOIM1205:fix/pinv-static-shape
Feb 11, 2026
Merged

Fix incorrect static output shape in MatrixPinv for non-square inputs#1885
ricardoV94 merged 2 commits intopymc-devs:mainfrom
WHOIM1205:fix/pinv-static-shape

Conversation

@WHOIM1205
Copy link
Contributor

Fix static output shape in MatrixPinv for non-square inputs

Summary

MatrixPinv.make_node was incorrectly assigning the output static shape to match the input (m, n), even though the pseudoinverse returns (n, m).

Although gufunc_signature and infer_shape were correct, the static TensorType.shape created in make_node was not transposed. This caused incorrect shape reporting when input dimensions were known.

Fix

Updated pytensor/tensor/nlinalg.py:

python

Before

matrix(shape=x.type.shape, dtype=out_dtype)

After

matrix(shape=(x.type.shape[1], x.type.shape[0]), dtype=out_dtype)

  • all the test cases are passed locally
image

Signed-off-by: WHOIM1205 <rathourprateek8@gmail.com>
@WHOIM1205
Copy link
Contributor Author

WHOIM1205 commented Feb 11, 2026

hey @ricardoV94
This fixes a static shape mismatch in MatrixPinv.make_node for non-square inputs. Runtime behavior was correct, but the symbolic shape wasn’t transposed, which could break gradients. The change is minimal and all related tests pass.

@WHOIM1205
Copy link
Contributor Author

pre-commit.ci autofix

Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

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

Interesting, I didn't know that

@ricardoV94 ricardoV94 merged commit d212017 into pymc-devs:main Feb 11, 2026
64 checks passed
@ricardoV94
Copy link
Member

Thanks @WHOIM1205

@ricardoV94 ricardoV94 added bug Something isn't working shape inference linalg Linear algebra labels Feb 11, 2026
@ricardoV94
Copy link
Member

@WHOIM1205 Do you mind me asking how did you find this issue?

@WHOIM1205
Copy link
Contributor Author

@WHOIM1205 Do you mind me asking how did you find this issue?

Thanks 🙂

I was experimenting with pinv on non square matrices and noticed that the reported type.shape didn’t match the expected transposed output.
That felt odd, so I followed it through the Op definition and saw that make_node was using the input shape directly.
From there it was pretty clear that the static shape just needed to be swapped to stay consistent with the signature and infer_shape.

@jessegrabowski
Copy link
Member

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working linalg Linear algebra shape inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants