Skip to content

Implemented JAX backend for Eigvalsh #867

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 2 commits into from
Jun 28, 2024
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
25 changes: 24 additions & 1 deletion pytensor/link/jax/dispatch/slinalg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import jax

from pytensor.link.jax.dispatch.basic import jax_funcify
from pytensor.tensor.slinalg import BlockDiagonal, Cholesky, Solve, SolveTriangular
from pytensor.tensor.slinalg import (
BlockDiagonal,
Cholesky,
Eigvalsh,
Solve,
SolveTriangular,
)


@jax_funcify.register(Eigvalsh)
def jax_funcify_Eigvalsh(op, **kwargs):
if op.lower:
UPLO = "L"
else:
UPLO = "U"

def eigvalsh(a, b):
if b is not None:
raise NotImplementedError(
"jax.numpy.linalg.eigvalsh does not support generalized eigenvector problems (b != None)"
)
return jax.numpy.linalg.eigvalsh(a, UPLO=UPLO)

return eigvalsh


@jax_funcify.register(Cholesky)
Expand Down
31 changes: 31 additions & 0 deletions tests/link/jax/test_slinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,34 @@ def test_jax_block_diag_blockwise():
np.random.normal(size=(5, 3, 3)).astype(config.floatX),
],
)


@pytest.mark.parametrize("lower", [False, True])
def test_jax_eigvalsh(lower):
A = matrix("A")
B = matrix("B")

out = pt_slinalg.eigvalsh(A, B, lower=lower)
out_fg = FunctionGraph([A, B], [out])

with pytest.raises(NotImplementedError):
compare_jax_and_py(
out_fg,
[
np.array(
[[6, 3, 1, 5], [3, 0, 5, 1], [1, 5, 6, 2], [5, 1, 2, 2]]
).astype(config.floatX),
np.array(
[[10, 0, 1, 3], [0, 12, 7, 8], [1, 7, 14, 2], [3, 8, 2, 16]]
).astype(config.floatX),
],
)
compare_jax_and_py(
out_fg,
[
np.array([[6, 3, 1, 5], [3, 0, 5, 1], [1, 5, 6, 2], [5, 1, 2, 2]]).astype(
config.floatX
),
None,
],
)
Loading