-
Notifications
You must be signed in to change notification settings - Fork 148
Use LAPACK functions for cho_solve, lu_factor, solve_triangular
#1605
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
jessegrabowski
merged 6 commits into
pymc-devs:main
from
Fyrebright:1468-chosolve-lufact-triangularsolve
Oct 12, 2025
+140
−21
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
63e2ccc
Use lapack instead of `scipy_linalg.cho_solve`
Fyrebright 3a8a9a4
Use lapack instead of `scipy_linalg.lu_factor`
Fyrebright 0197de5
Use lapack instead of `scipy_linalg.solve_triangular`
Fyrebright 334a44e
Add empty test for lu_factor
Fyrebright 8061159
Tidy imports
Fyrebright 1d3e180
remove ndim check
Fyrebright File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import numpy as np | ||
| import scipy.linalg as scipy_linalg | ||
| from numpy.exceptions import ComplexWarning | ||
| from scipy.linalg import get_lapack_funcs | ||
| from scipy.linalg import LinAlgError, LinAlgWarning, get_lapack_funcs | ||
|
|
||
| import pytensor | ||
| from pytensor import ifelse | ||
|
|
@@ -384,15 +384,28 @@ def make_node(self, *inputs): | |
| return Apply(self, [A, b], [out]) | ||
|
|
||
| def perform(self, node, inputs, output_storage): | ||
| C, b = inputs | ||
| rval = scipy_linalg.cho_solve( | ||
| (C, self.lower), | ||
| b, | ||
| check_finite=self.check_finite, | ||
| overwrite_b=self.overwrite_b, | ||
| ) | ||
| c, b = inputs | ||
|
|
||
| (potrs,) = get_lapack_funcs(("potrs",), (c, b)) | ||
|
|
||
| output_storage[0][0] = rval | ||
| if self.check_finite and not (np.isfinite(c).all() and np.isfinite(b).all()): | ||
| raise ValueError("array must not contain infs or NaNs") | ||
|
|
||
| if c.shape[0] != c.shape[1]: | ||
| raise ValueError("The factored matrix c is not square.") | ||
| if c.shape[1] != b.shape[0]: | ||
| raise ValueError(f"incompatible dimensions ({c.shape} and {b.shape})") | ||
|
|
||
| # Quick return for empty arrays | ||
| if b.size == 0: | ||
| output_storage[0][0] = np.empty_like(b, dtype=potrs.dtype) | ||
| return | ||
|
|
||
| x, info = potrs(c, b, lower=self.lower, overwrite_b=self.overwrite_b) | ||
| if info != 0: | ||
| raise ValueError(f"illegal value in {-info}th argument of internal potrs") | ||
|
|
||
| output_storage[0][0] = x | ||
|
|
||
| def L_op(self, *args, **kwargs): | ||
| # TODO: Base impl should work, let's try it | ||
|
|
@@ -696,9 +709,27 @@ def inplace_on_inputs(self, allowed_inplace_inputs: list[int]) -> "Op": | |
| def perform(self, node, inputs, outputs): | ||
| A = inputs[0] | ||
|
|
||
| LU, p = scipy_linalg.lu_factor( | ||
| A, overwrite_a=self.overwrite_a, check_finite=self.check_finite | ||
| ) | ||
| # Quick return for empty arrays | ||
| if A.size == 0: | ||
| outputs[0][0] = np.empty_like(A) | ||
| outputs[1][0] = np.array([], dtype=np.int32) | ||
| return | ||
|
|
||
| if self.check_finite and not np.isfinite(A).all(): | ||
| raise ValueError("array must not contain infs or NaNs") | ||
|
|
||
| (getrf,) = get_lapack_funcs(("getrf",), (A,)) | ||
| LU, p, info = getrf(A, overwrite_a=self.overwrite_a) | ||
| if info < 0: | ||
| raise ValueError( | ||
| f"illegal value in {-info}th argument of internal getrf (lu_factor)" | ||
| ) | ||
| if info > 0: | ||
| warnings.warn( | ||
| f"Diagonal number {info} is exactly zero. Singular matrix.", | ||
| LinAlgWarning, | ||
| stacklevel=2, | ||
| ) | ||
|
Comment on lines
+723
to
+732
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
|
|
||
| outputs[0][0] = LU | ||
| outputs[1][0] = p | ||
|
|
@@ -865,15 +896,51 @@ def __init__(self, *, unit_diagonal=False, **kwargs): | |
|
|
||
| def perform(self, node, inputs, outputs): | ||
| A, b = inputs | ||
| outputs[0][0] = scipy_linalg.solve_triangular( | ||
| A, | ||
| b, | ||
| lower=self.lower, | ||
| trans=0, | ||
| unit_diagonal=self.unit_diagonal, | ||
| check_finite=self.check_finite, | ||
| overwrite_b=self.overwrite_b, | ||
| ) | ||
|
|
||
| if self.check_finite and not (np.isfinite(A).all() and np.isfinite(b).all()): | ||
| raise ValueError("array must not contain infs or NaNs") | ||
|
|
||
| if len(A.shape) != 2 or A.shape[0] != A.shape[1]: | ||
| raise ValueError("expected square matrix") | ||
|
|
||
| if A.shape[0] != b.shape[0]: | ||
| raise ValueError(f"shapes of a {A.shape} and b {b.shape} are incompatible") | ||
|
|
||
| (trtrs,) = get_lapack_funcs(("trtrs",), (A, b)) | ||
|
|
||
| # Quick return for empty arrays | ||
| if b.size == 0: | ||
| outputs[0][0] = np.empty_like(b, dtype=trtrs.dtype) | ||
| return | ||
|
|
||
| if A.flags["F_CONTIGUOUS"]: | ||
| x, info = trtrs( | ||
| A, | ||
| b, | ||
| overwrite_b=self.overwrite_b, | ||
| lower=self.lower, | ||
| trans=0, | ||
| unitdiag=self.unit_diagonal, | ||
| ) | ||
| else: | ||
| # transposed system is solved since trtrs expects Fortran ordering | ||
| x, info = trtrs( | ||
| A.T, | ||
| b, | ||
| overwrite_b=self.overwrite_b, | ||
| lower=not self.lower, | ||
| trans=1, | ||
| unitdiag=self.unit_diagonal, | ||
| ) | ||
|
|
||
| if info > 0: | ||
| raise LinAlgError( | ||
| f"singular matrix: resolution failed at diagonal {info-1}" | ||
| ) | ||
| elif info < 0: | ||
| raise ValueError(f"illegal value in {-info}-th argument of internal trtrs") | ||
|
Comment on lines
+936
to
+941
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
|
|
||
| outputs[0][0] = x | ||
|
|
||
| def L_op(self, inputs, outputs, output_gradients): | ||
| res = super().L_op(inputs, outputs, output_gradients) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if we returned a matrix of
np.nanifinfo !=0rather than erroring out. This is what jax does, and it makes it a lot more ergonomic to work with in iterative algorithms.This might be out of scope for this PR; asking @ricardoV94 for a 2nd opinion