-
Notifications
You must be signed in to change notification settings - Fork 201
Record assume_a as an assumption about the solved matrix #2374
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
Open
jessegrabowski
wants to merge
4
commits into
pymc-devs:main
Choose a base branch
from
jessegrabowski:extract-assumptions-from-assume-a
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d707149
Drain SpecifyAssumptions markers wherever they appear
jessegrabowski 12be4cb
Extract assume_a normalization from Solve.__init__
jessegrabowski bbd3802
Record assume_a as an assumption about the solved matrix
jessegrabowski c7a12cf
Mypy :D
jessegrabowski 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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import numpy as np | ||
|
|
||
| from pytensor import tensor as pt | ||
| from pytensor.assumptions.specify import assume | ||
| from pytensor.graph.op import Op | ||
| from pytensor.tensor.basic import diagonal | ||
| from pytensor.tensor.blockwise import Blockwise | ||
|
|
@@ -14,6 +15,21 @@ | |
| from pytensor.tensor.variable import TensorVariable | ||
|
|
||
|
|
||
| # ``Solve`` uses the short spellings, as the various backend dispatches are more | ||
| # likely to recognize them. | ||
| _ASSUME_A_LONG_TO_SHORT = { | ||
| "general": "gen", | ||
| "symmetric": "sym", | ||
| "hermitian": "her", | ||
| "positive definite": "pos", | ||
| } | ||
|
|
||
|
|
||
| def _normalize_assume_a(assume_a: str) -> str: | ||
| assume_a = assume_a.lower() | ||
| return _ASSUME_A_LONG_TO_SHORT.get(assume_a, assume_a) | ||
|
|
||
|
|
||
| class Solve(SolveBase): | ||
| """ | ||
| Solve a system of linear equations. | ||
|
|
@@ -31,19 +47,11 @@ def __init__(self, *, assume_a="gen", **kwargs): | |
| # Triangular and diagonal are handled outside of Solve | ||
| valid_options = ["gen", "sym", "her", "pos", "tridiagonal", "banded"] | ||
|
|
||
| assume_a = assume_a.lower() | ||
| # We use the old names as the different dispatches are more likely to support them | ||
| long_to_short = { | ||
| "general": "gen", | ||
| "symmetric": "sym", | ||
| "hermitian": "her", | ||
| "positive definite": "pos", | ||
| } | ||
| assume_a = long_to_short.get(assume_a, assume_a) | ||
| assume_a = _normalize_assume_a(assume_a) | ||
|
|
||
| if assume_a not in valid_options: | ||
| raise ValueError( | ||
| f"Invalid assume_a: {assume_a}. It must be one of {valid_options} or {list(long_to_short.keys())}" | ||
| f"Invalid assume_a: {assume_a}. It must be one of {valid_options} or {list(_ASSUME_A_LONG_TO_SHORT)}" | ||
| ) | ||
|
|
||
| if assume_a in ("tridiagonal", "banded"): | ||
|
|
@@ -90,6 +98,31 @@ def inplace_on_inputs(self, allowed_inplace_inputs: list[int]) -> "Op": | |
| return type(self)(**new_props) | ||
|
|
||
|
|
||
| def _record_assume_a(a, assume_a: str): | ||
| """Restate the structure ``assume_a`` promises as an assumption about ``a``. | ||
|
|
||
| ``assume_a`` and :func:`assume` are the same promise in two spellings, but only the | ||
| solve can act on the first. Recording it lets every other consumer of ``a`` use it. | ||
| """ | ||
| match assume_a: | ||
| case "diagonal": | ||
| return assume(a, diagonal=True) | ||
| case "lower triangular": | ||
| return assume(a, lower_triangular=True) | ||
| case "upper triangular": | ||
| return assume(a, upper_triangular=True) | ||
| case "pos": | ||
| return assume(a, positive_definite=True) | ||
| case "sym": | ||
| return assume(a, symmetric=True) | ||
| case "her" if not a.type.dtype.startswith("complex"): | ||
| # A real Hermitian matrix is symmetric; a complex one is not. | ||
| return assume(a, symmetric=True) | ||
| case _: | ||
| # "gen" promises nothing, and "tridiagonal" and "banded" have no key. | ||
| return a | ||
|
|
||
|
|
||
| def solve( | ||
| a, | ||
| b, | ||
|
|
@@ -154,7 +187,10 @@ def solve( | |
| This will influence how batched dimensions are interpreted. | ||
| By default, we assume b_ndim = b.ndim is 2 if b.ndim > 1, else 1. | ||
| """ | ||
| assume_a = assume_a.lower() | ||
| # _record_assume_a reads the dtype off ``a``, so it has to be a variable first. | ||
| a = pt.as_tensor_variable(a) | ||
| assume_a = _normalize_assume_a(assume_a) | ||
| a = _record_assume_a(a, assume_a) | ||
|
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. inline it... |
||
|
|
||
| if assume_a in ("lower triangular", "upper triangular"): | ||
| lower = "lower" in assume_a | ||
|
|
||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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 hate this, why lift stuff out of the Op? attributes and methods are a thing