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

Fix a scatter eager_subs issue #517

Merged
merged 3 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions funsor/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,16 @@ def _alpha_convert(self, alpha_subs):
reduced_vars = frozenset(alpha_subs.get(var.name, var) for var in reduced_vars)
return op, subs, source, reduced_vars

def eager_subs(self, subs):
subs = OrderedDict(subs)
new_subs = []
for name, sub in self.subs:
if name in subs and isinstance(subs[name], Variable):
new_subs.append((subs[name].name, sub))
else:
new_subs.append((name, sub))
return Scatter(self.op, tuple(new_subs), self.source, self.reduced_vars)


class Approximate(Funsor):
"""
Expand Down
43 changes: 43 additions & 0 deletions test/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import funsor
import funsor.ops as ops
from funsor.cnf import Contraction
from funsor.delta import Delta
from funsor.domains import Array, Bint, Product, Real, Reals, find_domain
from funsor.interpretations import eager, lazy
from funsor.tensor import (
Expand Down Expand Up @@ -1468,3 +1470,44 @@ def raw_reduction(x, dim=None, keepdims=False, batch_ndims=len(batch_shape)):
actual = op(Tensor(data, inputs), dim, keepdims=keepdims)
expected = Tensor(raw_reduction(data, dim, keepdims), inputs, dtype)
assert_close(actual, expected, rtol=rtol)


def test_scatter_substitute():
expr = Scatter(
ops.logaddexp,
(
(
"_time_states_38",
Number(0, 1),
),
),
Contraction(
ops.null,
ops.add,
frozenset(),
(
Delta(
(
(
"states",
(
Tensor(np.array(5, dtype=np.int32), (), 10),
Number(0.0),
),
),
(
"_PREV_states",
(
Tensor(np.array(4, dtype=np.int32), (), 10),
Number(0.0),
),
),
)
),
Tensor(np.array(0.3386716842651367, dtype=np.float32), (), "real"),
),
),
frozenset(),
)

expr(_time_states_38="_time_states")