-
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 17 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 |
---|---|---|
|
@@ -15,11 +15,13 @@ | |
from pytensor.tensor.elemwise import DimShuffle | ||
from pytensor.tensor.math import _allclose, dot, matmul | ||
from pytensor.tensor.nlinalg import ( | ||
SVD, | ||
Det, | ||
KroneckerProduct, | ||
MatrixInverse, | ||
MatrixPinv, | ||
matrix_inverse, | ||
svd, | ||
) | ||
from pytensor.tensor.rewriting.linalg import inv_as_solve | ||
from pytensor.tensor.slinalg import ( | ||
|
@@ -390,3 +392,67 @@ def test_local_lift_through_linalg(constructor, f_op, f, g_op, g): | |
test_vals = [x @ np.swapaxes(x, -1, -2) for x in test_vals] | ||
|
||
np.testing.assert_allclose(f1(*test_vals), f2(*test_vals), atol=1e-8) | ||
|
||
|
||
def test_svd_uv_merge(): | ||
a = matrix("a") | ||
s_1 = svd(a, full_matrices=False, compute_uv=False) | ||
_, s_2, _ = svd(a, full_matrices=False, compute_uv=True) | ||
_, s_3, _ = svd(a, full_matrices=True, compute_uv=True) | ||
u_4, s_4, v_4 = svd(a, full_matrices=True, compute_uv=True) | ||
# `grad` will introduces an SVD Op with compute_uv=True | ||
# full_matrices = True is not supported for grad of svd | ||
gs = pt.grad(pt.sum(s_1), a) | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# 1. compute_uv=False needs rewriting with compute_uv=True | ||
f_1 = pytensor.function([a], gs) | ||
nodes = f_1.maker.fgraph.apply_nodes | ||
svd_counter = 0 | ||
for node in nodes: | ||
if isinstance(node.op, SVD): | ||
assert node.op.compute_uv | ||
svd_counter += 1 | ||
assert svd_counter == 1 | ||
|
||
# 2. compute_uv=True needs rewriting with compute=False, reuse node | ||
f_2 = pytensor.function([a], [s_1, s_2]) | ||
nodes = f_2.maker.fgraph.apply_nodes | ||
svd_counter = 0 | ||
for node in nodes: | ||
if isinstance(node.op, SVD): | ||
assert not node.op.compute_uv | ||
svd_counter += 1 | ||
assert svd_counter == 1 | ||
|
||
# 3. compute_uv=True needs rewriting with compute=False, create new node | ||
# full_matrices needs to retain the value | ||
f_3 = pytensor.function([a], [s_2]) | ||
HangenYuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nodes = f_3.maker.fgraph.apply_nodes | ||
svd_counter = 0 | ||
for node in nodes: | ||
if isinstance(node.op, SVD): | ||
assert not node.op.compute_uv | ||
svd_counter += 1 | ||
assert svd_counter == 1 | ||
|
||
# Case 2 of 3. for a different full_matrices | ||
f_4 = pytensor.function([a], [s_3]) | ||
nodes = f_4.maker.fgraph.apply_nodes | ||
svd_counter = 0 | ||
for node in nodes: | ||
if isinstance(node.op, SVD): | ||
assert not node.op.compute_uv | ||
assert node.op.full_matrices | ||
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. Same here, there's no point in worrying about whether we keep the same full_matrices or not, since they play no role when we don't compute_uv (right?) 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 check for |
||
svd_counter += 1 | ||
assert svd_counter == 1 | ||
|
||
# 4. No rewrite should happen | ||
f_5 = pytensor.function([a], [u_4]) | ||
nodes = f_5.maker.fgraph.apply_nodes | ||
svd_counter = 0 | ||
for node in nodes: | ||
if isinstance(node.op, SVD): | ||
assert node.op.full_matrices | ||
assert node.op.compute_uv | ||
HangenYuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
svd_counter += 1 | ||
assert svd_counter == 1 |
Uh oh!
There was an error while loading. Please reload this page.