Skip to content

Fix lu_solve with batch inputs #1394

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
May 9, 2025
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
76 changes: 50 additions & 26 deletions pytensor/tensor/slinalg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import warnings
from collections.abc import Sequence
from functools import reduce
from functools import partial, reduce
from typing import Literal, cast

import numpy as np
Expand Down Expand Up @@ -589,6 +589,7 @@ def lu(


class PivotToPermutations(Op):
gufunc_signature = "(x)->(x)"
__props__ = ("inverse",)

def __init__(self, inverse=True):
Expand Down Expand Up @@ -723,40 +724,22 @@ def lu_factor(
)


def lu_solve(
LU_and_pivots: tuple[TensorLike, TensorLike],
def _lu_solve(
LU: TensorLike,
pivots: TensorLike,
b: TensorLike,
trans: bool = False,
b_ndim: int | None = None,
check_finite: bool = True,
overwrite_b: bool = False,
):
"""
Solve a system of linear equations given the LU decomposition of the matrix.

Parameters
----------
LU_and_pivots: tuple[TensorLike, TensorLike]
LU decomposition of the matrix, as returned by `lu_factor`
b: TensorLike
Right-hand side of the equation
trans: bool
If True, solve A^T x = b, instead of Ax = b. Default is False
b_ndim: int, optional
The number of core dimensions in b. Used to distinguish between a batch of vectors (b_ndim=1) and a matrix
of vectors (b_ndim=2). Default is None, which will infer the number of core dimensions from the input.
check_finite: bool
If True, check that the input matrices contain only finite numbers. Default is True.
overwrite_b: bool
Ignored by Pytensor. Pytensor will always compute inplace when possible.
"""
b_ndim = _default_b_ndim(b, b_ndim)
LU, pivots = LU_and_pivots

LU, pivots, b = map(pt.as_tensor_variable, [LU, pivots, b])
inv_permutation = pivot_to_permutation(pivots, inverse=True)

inv_permutation = pivot_to_permutation(pivots, inverse=True)
x = b[inv_permutation] if not trans else b
# TODO: Use PermuteRows on b
# x = permute_rows(b, pivots) if not trans else b

x = solve_triangular(
LU,
Expand All @@ -777,11 +760,52 @@ def lu_solve(
b_ndim=b_ndim,
check_finite=check_finite,
)
x = x[pt.argsort(inv_permutation)] if trans else x

# TODO: Use PermuteRows(inverse=True) on x
# if trans:
# x = permute_rows(x, pivots, inverse=True)
x = x[pt.argsort(inv_permutation)] if trans else x
return x


def lu_solve(
LU_and_pivots: tuple[TensorLike, TensorLike],
b: TensorLike,
trans: bool = False,
b_ndim: int | None = None,
check_finite: bool = True,
overwrite_b: bool = False,
):
"""
Solve a system of linear equations given the LU decomposition of the matrix.

Parameters
----------
LU_and_pivots: tuple[TensorLike, TensorLike]
LU decomposition of the matrix, as returned by `lu_factor`
b: TensorLike
Right-hand side of the equation
trans: bool
If True, solve A^T x = b, instead of Ax = b. Default is False
b_ndim: int, optional
The number of core dimensions in b. Used to distinguish between a batch of vectors (b_ndim=1) and a matrix
of vectors (b_ndim=2). Default is None, which will infer the number of core dimensions from the input.
check_finite: bool
If True, check that the input matrices contain only finite numbers. Default is True.
overwrite_b: bool
Ignored by Pytensor. Pytensor will always compute inplace when possible.
"""
b_ndim = _default_b_ndim(b, b_ndim)
if b_ndim == 1:
signature = "(m,m),(m),(m)->(m)"
else:
signature = "(m,m),(m),(m,n)->(m,n)"
partialled_func = partial(
_lu_solve, trans=trans, b_ndim=b_ndim, check_finite=check_finite
)
return pt.vectorize(partialled_func, signature=signature)(*LU_and_pivots, b)


class SolveTriangular(SolveBase):
"""Solve a system of linear equations."""

Expand Down
43 changes: 29 additions & 14 deletions tests/tensor/test_slinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pytest
import scipy

import pytensor
from pytensor import function, grad
from pytensor import tensor as pt
from pytensor.configdefaults import config
Expand Down Expand Up @@ -130,7 +129,7 @@ def test_cholesky_grad_indef():

def test_cholesky_infer_shape():
x = matrix()
f_chol = pytensor.function([x], [cholesky(x).shape, cholesky(x, lower=False).shape])
f_chol = function([x], [cholesky(x).shape, cholesky(x, lower=False).shape])
if config.mode != "FAST_COMPILE":
topo_chol = f_chol.maker.fgraph.toposort()
f_chol.dprint()
Expand Down Expand Up @@ -313,7 +312,7 @@ def test_solve_correctness(
b_ndim=len(b_size),
)

solve_func = pytensor.function([A, b], y)
solve_func = function([A, b], y)
X_np = solve_func(A_val.copy(), b_val.copy())

ATOL = 1e-8 if config.floatX.endswith("64") else 1e-4
Expand Down Expand Up @@ -444,7 +443,7 @@ def test_correctness(self, b_shape: tuple[int], lower, trans, unit_diagonal):
b_ndim=len(b_shape),
)

f = pytensor.function([A, b], x)
f = function([A, b], x)

x_pt = f(A_val, b_val)
x_sp = scipy.linalg.solve_triangular(
Expand Down Expand Up @@ -508,8 +507,8 @@ def test_infer_shape(self):
A = matrix()
b = matrix()
self._compile_and_check(
[A, b], # pytensor.function inputs
[self.op_class(b_ndim=2)(A, b)], # pytensor.function outputs
[A, b], # function inputs
Copy link
Member

Choose a reason for hiding this comment

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

These comments are silly anyway, just use a keyword argument?

Copy link
Member Author

Choose a reason for hiding this comment

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

That was a find replace spill over to comments

Copy link
Member

Choose a reason for hiding this comment

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

The comments are still stupid >:(

Copy link
Member Author

Choose a reason for hiding this comment

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

But I didn't add them :D

[self.op_class(b_ndim=2)(A, b)], # function outputs
# A must be square
[
np.asarray(rng.random((5, 5)), dtype=config.floatX),
Expand All @@ -522,8 +521,8 @@ def test_infer_shape(self):
A = matrix()
b = vector()
self._compile_and_check(
[A, b], # pytensor.function inputs
[self.op_class(b_ndim=1)(A, b)], # pytensor.function outputs
[A, b], # function inputs
[self.op_class(b_ndim=1)(A, b)], # function outputs
# A must be square
[
np.asarray(rng.random((5, 5)), dtype=config.floatX),
Expand All @@ -538,10 +537,10 @@ def test_solve_correctness(self):
A = matrix()
b = matrix()
y = self.op_class(lower=True, b_ndim=2)(A, b)
cho_solve_lower_func = pytensor.function([A, b], y)
cho_solve_lower_func = function([A, b], y)

y = self.op_class(lower=False, b_ndim=2)(A, b)
cho_solve_upper_func = pytensor.function([A, b], y)
cho_solve_upper_func = function([A, b], y)

b_val = np.asarray(rng.random((5, 1)), dtype=config.floatX)

Expand Down Expand Up @@ -603,7 +602,7 @@ def test_lu_decomposition(
A = tensor("A", shape=shape, dtype=dtype)
out = lu(A, permute_l=permute_l, p_indices=p_indices)

f = pytensor.function([A], out)
f = function([A], out)

rng = np.random.default_rng(utt.fetch_seed())
x = rng.normal(size=shape).astype(config.floatX)
Expand Down Expand Up @@ -706,7 +705,7 @@ def test_lu_solve(self, b_shape: tuple[int], trans):

x = self.factor_and_solve(A, b, trans=trans, sum=False)

f = pytensor.function([A, b], x)
f = function([A, b], x)
x_pt = f(A_val.copy(), b_val.copy())
x_sp = scipy.linalg.lu_solve(
scipy.linalg.lu_factor(A_val.copy()), b_val.copy(), trans=trans
Expand Down Expand Up @@ -738,13 +737,29 @@ def test_lu_solve_gradient(self, b_shape: tuple[int], trans: bool):
test_fn = functools.partial(self.factor_and_solve, sum=True, trans=trans)
utt.verify_grad(test_fn, [A_val, b_val], 3, rng)

def test_lu_solve_batch_dims(self):
A = pt.tensor("A", shape=(3, 1, 5, 5))
b = pt.tensor("b", shape=(1, 4, 5))
lu_and_pivots = lu_factor(A)
x = lu_solve(lu_and_pivots, b, b_ndim=1)
assert x.type.shape in {(3, 4, None), (3, 4, 5)}
Copy link
Preview

Copilot AI May 8, 2025

Choose a reason for hiding this comment

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

[nitpick] The assertion on x.type.shape uses a set containing None, which may be brittle or ambiguous. Consider clarifying the expected shape explicitly to improve test robustness.

Suggested change
assert x.type.shape in {(3, 4, None), (3, 4, 5)}
assert x.type.shape == (3, 4, 5) or (x.type.shape[:2] == (3, 4) and x.type.shape[2] is None)

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

This was on purpose, right now it returns None, but if we improve static type it should return 5 and won't make the test fail


rng = np.random.default_rng(748)
A_test = rng.random(A.type.shape).astype(A.type.dtype)
b_test = rng.random(b.type.shape).astype(b.type.dtype)
np.testing.assert_allclose(
x.eval({A: A_test, b: b_test}),
solve(A, b, b_ndim=1).eval({A: A_test, b: b_test}),
rtol=1e-9 if config.floatX == "float64" else 1e-5,
)


def test_lu_factor():
rng = np.random.default_rng(utt.fetch_seed())
A = matrix()
A_val = rng.normal(size=(5, 5)).astype(config.floatX)

f = pytensor.function([A], lu_factor(A))
f = function([A], lu_factor(A))

LU, pt_p_idx = f(A_val)
sp_LU, sp_p_idx = scipy.linalg.lu_factor(A_val)
Expand All @@ -764,7 +779,7 @@ def test_cho_solve():
A = matrix()
b = matrix()
y = cho_solve((A, True), b)
cho_solve_lower_func = pytensor.function([A, b], y)
cho_solve_lower_func = function([A, b], y)

b_val = np.asarray(rng.random((5, 1)), dtype=config.floatX)

Expand Down