Skip to content
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

Add Cholesky and SolveTriangular for torch #1035

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions pytensor/link/pytorch/dispatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
import pytensor.link.pytorch.dispatch.shape
import pytensor.link.pytorch.dispatch.sort
import pytensor.link.pytorch.dispatch.subtensor
import pytensor.link.pytorch.dispatch.slinalg
# isort: on
28 changes: 28 additions & 0 deletions pytensor/link/pytorch/dispatch/slinalg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import torch.linalg

from pytensor.link.pytorch.dispatch import pytorch_funcify
from pytensor.tensor.slinalg import Cholesky, SolveTriangular


@pytorch_funcify.register(Cholesky)
def pytorch_funcify_Cholesky(op, **kwargs):
lower = op.lower

def cholesky(a, lower=lower):
return torch.linalg.cholesky(a, upper=not lower)

Check warning on line 12 in pytensor/link/pytorch/dispatch/slinalg.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/slinalg.py#L12

Added line #L12 was not covered by tests

return cholesky


@pytorch_funcify.register(SolveTriangular)
def pytorch_funcify_SolveTriangular(op, **kwargs):
lower = op.lower
trans = op.trans
unit_diagonal = op.unit_diagonal

Check warning on line 21 in pytensor/link/pytorch/dispatch/slinalg.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/slinalg.py#L19-L21

Added lines #L19 - L21 were not covered by tests

def solve_triangular(A, b):
return torch.linalg.solve_triangular(

Check warning on line 24 in pytensor/link/pytorch/dispatch/slinalg.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/slinalg.py#L23-L24

Added lines #L23 - L24 were not covered by tests
A, b, upper=not lower, unit_triangle=unit_diagonal, left=trans == "T"
Copy link
Member

Choose a reason for hiding this comment

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

trans may also be 1 apparently. We should canonicalize the integers to strings so the Op only has to handle one of the encodings: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve_triangular.html

)

return solve_triangular

Check warning on line 28 in pytensor/link/pytorch/dispatch/slinalg.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/slinalg.py#L28

Added line #L28 was not covered by tests
23 changes: 23 additions & 0 deletions tests/link/pytorch/test_slinalg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np
import pytest

import pytensor
from pytensor.tensor import tensor
from pytensor.tensor.slinalg import cholesky


torch = pytest.importorskip("torch")


# @todo: We don't have blockwise yet for torch
Copy link
Member

Choose a reason for hiding this comment

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

This is a specific test, just test cholesky and solve directly with the compare_py_and_torch as usual

def test_batched_mvnormal_logp_and_dlogp():
rng = np.random.default_rng(sum(map(ord, "mvnormal")))

cov = tensor("cov", shape=(10, 10))

test_values = np.eye(cov.type.shape[-1]) * np.abs(rng.normal(size=cov.type.shape))

chol_cov = cholesky(cov, lower=True, on_error="raise")

fn = pytensor.function([cov], [chol_cov], mode="PYTORCH")
assert np.all(np.isclose(fn(test_values), np.linalg.cholesky(test_values)))
Loading