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

New interface for sarkka_bilmes_product #539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions funsor/sum_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import OrderedDict, defaultdict
from functools import reduce
from math import gcd
from typing import Dict

import funsor.ops as ops
from funsor.cnf import Contraction
Expand Down Expand Up @@ -611,6 +612,31 @@ def naive_sarkka_bilmes_product(
return result



def modified_sequential_sum_product(
sum_op: AssociativeOp,
prod_op: AssociativeOp,
trans: Funsor,
time_var: Variable,
step: Dict[str, Dict[str, int]],
num_periods: int,
) -> Funsor:
# step example:
# {"a_t": {"a_tm1": 1, "a_tm2": 2}}
# {"b_t": {}}
global_vars = trans.input_vars - frozenset(step.keys()) - frozenset().union(*step.values())
renaming = {}
for curr, prevs in step.items():
for prev, lag in prevs.items():
renaming[prev] = lag * "_PREV_" + curr

trans = trans(**renaming)

return sarkka_bilmes_product(
sum_op, prod_op, trans, time_var, global_vars, num_periods
)


def sarkka_bilmes_product(
sum_op, prod_op, trans, time_var, global_vars=frozenset(), num_periods=1
):
Expand Down Expand Up @@ -741,6 +767,8 @@ def __init__(self, sum_op, prod_op, trans, time, step, step_names):
assert all(isinstance(k, str) for k in step_names.keys())
assert all(isinstance(v, str) for v in step_names.values())
assert set(step_names) == set(step).union(step.values())
if step_names:
breakpoint()
inputs = OrderedDict(
(step_names.get(k, k), v) for k, v in trans.inputs.items() if k != time.name
)
Expand Down Expand Up @@ -788,6 +816,7 @@ def eager_subs(self, subs):
self.sum_op, self.prod_op, self.trans, self.time, self.step, step_names
)
lazy = tuple((k, v) for k, v in subs if not isinstance(v, Variable))
breakpoint()
if lazy:
result = Subs(result, lazy)
return result
Expand Down
6 changes: 4 additions & 2 deletions test/test_sum_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@ def test_modified_partial_sum_product_17(
MarkovProduct,
partial(mixed_sequential_sum_product, num_segments=2),
partial(mixed_sequential_sum_product, num_segments=3),
],
][2:3],
)
def test_sequential_sum_product(
impl, sum_op, prod_op, batch_inputs, state_domain, num_steps
Expand All @@ -2270,7 +2270,8 @@ def test_sequential_sum_product(
trans = random_tensor(inputs)
time = Variable("time", Bint[num_steps])

actual = impl(sum_op, prod_op, trans, time, {"prev": "curr"})
with lazy:
actual = impl(sum_op, prod_op, trans, time, {"prev": "curr"})
expected_inputs = batch_inputs.copy()
expected_inputs.update(prev=state_domain, curr=state_domain)
assert dict(actual.inputs) == expected_inputs
Expand All @@ -2283,6 +2284,7 @@ def test_sequential_sum_product(
reduce_vars = frozenset("t_{}".format(t) for t in range(1, num_steps))
with reflect:
expected = sum_product(sum_op, prod_op, operands, reduce_vars)
actual = apply_optimizer(actual)
expected = apply_optimizer(expected)
expected = expected(**{"t_0": "prev", "t_{}".format(num_steps): "curr"})
expected = expected.align(tuple(actual.inputs.keys()))
Expand Down