Skip to content

Add jax implementation of pt.linalg.pinv #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pytensor/link/jax/dispatch/nlinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
from pytensor.link.jax.dispatch import jax_funcify
from pytensor.tensor.blas import BatchedDot
from pytensor.tensor.math import Dot, MaxAndArgmax
from pytensor.tensor.nlinalg import SVD, Det, Eig, Eigh, MatrixInverse, QRFull, SLogDet
from pytensor.tensor.nlinalg import (
SVD,
Det,
Eig,
Eigh,
MatrixInverse,
MatrixPinv,
QRFull,
SLogDet,
)


@jax_funcify.register(SVD)
Expand Down Expand Up @@ -77,6 +86,14 @@ def dot(x, y):
return dot


@jax_funcify.register(MatrixPinv)
def jax_funcify_Pinv(op, **kwargs):
def pinv(x):
return jnp.linalg.pinv(x)

return pinv


@jax_funcify.register(BatchedDot)
def jax_funcify_BatchedDot(op, **kwargs):
def batched_dot(a, b):
Expand Down
9 changes: 9 additions & 0 deletions tests/link/jax/test_nlinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,12 @@ def test_tensor_basics():
out = at_max(y)
fgraph = FunctionGraph([y], [out])
compare_jax_and_py(fgraph, [get_test_value(i) for i in fgraph.inputs])


def test_pinv():
x = matrix("x")
x_inv = at_nlinalg.pinv(x)

fgraph = FunctionGraph([x], [x_inv])
x_np = np.array([[1.0, 2.0], [3.0, 4.0]])
compare_jax_and_py(fgraph, [x_np])