-
Notifications
You must be signed in to change notification settings - Fork 129
Add specialization rewrite for solve with batched b #482
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
Changes from all commits
Commits
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import logging | ||
from typing import cast | ||
|
||
from pytensor.graph.rewriting.basic import node_rewriter | ||
from pytensor.graph.rewriting.basic import copy_stack_trace, node_rewriter | ||
from pytensor.tensor.basic import TensorVariable, diagonal, swapaxes | ||
from pytensor.tensor.blas import Dot22 | ||
from pytensor.tensor.blockwise import Blockwise | ||
|
@@ -13,7 +13,14 @@ | |
register_specialize, | ||
register_stabilize, | ||
) | ||
from pytensor.tensor.slinalg import Cholesky, Solve, cholesky, solve, solve_triangular | ||
from pytensor.tensor.slinalg import ( | ||
Cholesky, | ||
Solve, | ||
SolveBase, | ||
cholesky, | ||
solve, | ||
solve_triangular, | ||
) | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -131,6 +138,52 @@ def generic_solve_to_solve_triangular(fgraph, node): | |
] | ||
|
||
|
||
@register_stabilize | ||
@register_specialize | ||
@node_rewriter([Blockwise]) | ||
def batched_vector_b_solve_to_matrix_b_solve(fgraph, node): | ||
"""Replace a batched Solve(a, b, b_ndim=1) by Solve(a, b.T, b_ndim=2).T | ||
|
||
`a` must have no batched dimensions, while `b` can have arbitrary batched dimensions. | ||
Only the last two dimensions of `b` and the output are swapped. | ||
""" | ||
core_op = node.op.core_op | ||
|
||
if not isinstance(core_op, SolveBase): | ||
return None | ||
|
||
if node.op.core_op.b_ndim != 1: | ||
return None | ||
|
||
[a, b] = node.inputs | ||
|
||
# Check `b` is actually batched | ||
if b.type.ndim == 1: | ||
return None | ||
|
||
# Check `a` is a matrix (possibly with degenerate dims on the left) | ||
a_bcast_batch_dims = a.type.broadcastable[:-2] | ||
if not all(a_bcast_batch_dims): | ||
return None | ||
# We squeeze degenerate dims, any that are still needed will be introduced by the new_solve | ||
elif len(a_bcast_batch_dims): | ||
a = a.squeeze(axis=tuple(range(len(a_bcast_batch_dims)))) | ||
|
||
# Recreate solve Op with b_ndim=2 | ||
props = core_op._props_dict() | ||
props["b_ndim"] = 2 | ||
new_core_op = type(core_op)(**props) | ||
matrix_b_solve = Blockwise(new_core_op) | ||
|
||
# Apply the rewrite | ||
new_solve = _T(matrix_b_solve(a, _T(b))) | ||
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. Actually we could put any batched b dimension here when there are multiple of them. We may choose the larger one to reduce outer looping |
||
|
||
old_solve = node.outputs[0] | ||
copy_stack_trace(old_solve, new_solve) | ||
|
||
return [new_solve] | ||
|
||
|
||
@register_canonicalize | ||
@register_stabilize | ||
@register_specialize | ||
|
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
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 want to make this a specialize only rewrite but for now it's in stabilize because pymc includes those before calling grad, and otherwise we still end up with messy unoptimized blockwise graphs