-
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 8 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 |
---|---|---|
|
@@ -56,3 +56,5 @@ pytensor-venv/ | |
testing-report.html | ||
coverage.xml | ||
.coverage.* | ||
pics | ||
*.ipynb | ||
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,13 +4,17 @@ | |||||
|
||||||
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, | ||||||
|
@@ -377,3 +381,26 @@ | |||||
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): | ||||||
HangenYuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
"""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 | ||||||
compute_uv = False | ||||||
|
||||||
for cl, _ in fgraph.clients[x]: | ||||||
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. You have to be careful because if the output of the SVD is an output of the function one of the clients will be a string |
||||||
if isinstance(cl.op, Blockwise) and isinstance(cl.op.core_op, SVD): | ||||||
if (not compute_uv) and 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. I don't think you need that first check?
Suggested change
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. You should check if the uv outputs of this node are actually used (i.e., they have clients of their own). If not, they are useless and the rewrite shouldn't happen. In fact, this or another rewrite should change the flag from True to False for those nodes |
||||||
compute_uv = True | ||||||
break | ||||||
|
||||||
if compute_uv and not node.op.compute_uv: | ||||||
full_matrices = node.op.full_matrices | ||||||
return [SVD(full_matrices=full_matrices, compute_uv=compute_uv)] | ||||||
Uh oh!
There was an error while loading. Please reload this page.