Skip to content
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

Avoid trivial subs #545

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions test/test_adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,32 @@ def test_sequential_sum_product_adjoint(
)
expected_bwd = expected_bwds[operand]
assert (actual_bwd_t - expected_bwd).abs().data.max() < 5e-3 * num_steps


@pytest.mark.parametrize(
"test",
[
None,
xfail_param("same"),
xfail_param("empty"),
xfail_param("other"),
xfail_param("reduce"),
],
)
def test_identity_adjoint(test):
x = random_tensor(OrderedDict(i=Bint[3]))

with AdjointTape() as tape:
y = 2 * x
if test == "same":
y = y(i="i")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This substitution shouldn't change adjoint value.

elif test == "empty":
y = y()
elif test == "other":
y = y(j=0)
elif test == "reduce":
y = funsor.terms.Reduce(ops.add, y, frozenset())
Copy link
Member Author

@ordabayevy ordabayevy Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it seems that any (unary) atomic op that doesn't change the arg doubles the adjoint value.


# these identity tests return Number(4.0)
actual = tape.adjoint(ops.add, ops.mul, y, (x,))[x]
assert actual is funsor.Number(2.0)