-
Notifications
You must be signed in to change notification settings - Fork 132
Add rewrite to merge multiple SVD Ops with different settings #769
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
Changes from 12 commits
23ad3c4
d4a89ad
d4a2f2f
03c8e6f
839f479
4a1be4b
b999d68
14f89f8
55ad931
0337e9d
ecc62ae
8ba5119
1c30ee9
551a350
27ff606
67e1f06
9e21635
3ba3ba4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,4 +55,4 @@ pytensor-venv/ | |
.vscode/ | ||
testing-report.html | ||
coverage.xml | ||
.coverage.* | ||
.coverage.* |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,25 @@ | |
|
||
from pytensor import Variable | ||
from pytensor.graph import Apply, FunctionGraph | ||
from pytensor.graph.rewriting.basic import copy_stack_trace, node_rewriter | ||
from pytensor.graph.rewriting.basic import ( | ||
copy_stack_trace, | ||
node_rewriter, | ||
) | ||
from pytensor.tensor.basic import TensorVariable, diagonal | ||
from pytensor.tensor.blas import Dot22 | ||
from pytensor.tensor.blockwise import Blockwise | ||
from pytensor.tensor.elemwise import DimShuffle | ||
from pytensor.tensor.math import Dot, Prod, _matrix_matrix_matmul, log, prod | ||
from pytensor.tensor.nlinalg import ( | ||
SVD, | ||
KroneckerProduct, | ||
MatrixInverse, | ||
MatrixPinv, | ||
det, | ||
inv, | ||
kron, | ||
pinv, | ||
svd, | ||
) | ||
from pytensor.tensor.rewriting.basic import ( | ||
register_canonicalize, | ||
|
@@ -377,3 +382,50 @@ | |
return [block_diag(*inner_matrices)] | ||
else: | ||
raise NotImplementedError # pragma: no cover | ||
|
||
|
||
@register_canonicalize | ||
@register_stabilize | ||
@register_specialize | ||
@node_rewriter([SVD]) | ||
def local_svd_uv_simplify(fgraph, node): | ||
"""If we have more than one `SVD` `Op`s and at least one has keyword argument | ||
`compute_uv=True`, then we can change `compute_uv = False` to `True` everywhere | ||
and allow `pytensor` to re-use the decomposition outputs instead of recomputing. | ||
""" | ||
(x,) = node.inputs | ||
|
||
if node.compute_uv: | ||
HangenYuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# compute_uv=True returns [u, s, v]. | ||
# if at least u or v is used, no need to rewrite this node. | ||
if ( | ||
fgraph.clients[node.outputs[0]] is not None | ||
or fgraph.clients[node.outputs[2]] is not None | ||
HangenYuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
return | ||
|
||
# Else, has to replace the s of this node with s of an SVD Op that compute_uv=False. | ||
# First, iterate to see if there is an SVD Op that can be reused. | ||
for cl, _ in fgraph.clients[x]: | ||
if cl == "output": | ||
continue | ||
if isinstance(cl.op, Blockwise) and isinstance(cl.op.core_op, SVD): | ||
if not cl.op.core_op.compute_uv: | ||
return {fgraph.clients[node.outputs[1]]: cl.outputs[0]} | ||
|
||
# If no SVD reusable, return a new one. | ||
return { | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"remove": [node.outputs[0], node.ouputs[2]], | ||
node.outputs[1]: svd(x, full_matrices=node.full_matrices, compute_uv=False), | ||
} | ||
|
||
else: | ||
# compute_uv=False returns [s]. | ||
# We want rewrite if there is another one with compute_uv=True. | ||
# For this case, just reuse the `s` from the one with compute_uv=True. | ||
for cl, _ in fgraph.clients[x]: | ||
if cl == "output": | ||
continue | ||
if isinstance(cl.op, Blockwise) and isinstance(cl.op.core_op, SVD): | ||
if cl.op.core_op.compute_uv: | ||
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. We only want to do this if that other node is actually using the UV. If not we would actually want to replace that node by this one 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. That would be taken care by the first half at that node turn. As this is a local rewrite applied to all SVD node, each node will have its turn. 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. Even if you don't want to handle that other node there's no reason to rewrite this node into it. In general it's better to do as few rewrites as possible as every time a rewrite succeeds all other candidate rewrites are rerun (until an Equilibrium is achieved and nothing changes anymore). 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. On second thought I like your eager approach better, it's not readable. Since SVDs are rare we don't need to over optimize |
||
return [cl.outputs[1]] | ||
Uh oh!
There was an error while loading. Please reload this page.