You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This proposes a way to implement a LazyTensor-like feature in Funsor, should we decide we want it.
As explained in the LazyTensor paper, this interface improves debuggability by allowing code to run in different ways depending on whether or not attributes are accessed (e.g. via an assert statement or debug print statement), i.e. the interpretation is controlled not by an outer context manager but by inner attribute access. This might be a good interface for Funsor use in Pyro, where interpretations are outside of model-author control. E.g.
x = pyro.sample("x", dist.Normal(0, 1))
+ print(x) # <--- this could trigger eager evaluation
pyro.sample("y", dist.Normal(x, 1), obs=data)
The idea of LazyFunsor would be to create a mutable object with a Funsor-like interface, to allow construction of reflected funsor terms, and then to trigger some other interpretation as soon as any attribute of that LazyFunsor is accessed, e.g. .data or maybe even .inputs. Here's a rough sketch
classLazyFunsor:
def__init__(self, term: Funsor):
self._term=termself._done=False# Any sort of access triggers interpretation.def__str__(self):
returnstr(self._eval())
def_eval(self):
ifnotself._done:
self._term=reinterpret(self._term)
self._done=Truereturnself._term# Operations are evaluated under reflect.def__add__(self, other):
ifisinstance(other, LazyFunsor):
other=other._termwithreflect:
term=self._term+otherreturnLazyFunsor(term, self._interpretation)
# We'd want to convert to_funsor before leaving the enclosing interpretation context.@to_funsor.register(LazyFunsor)deflazy_funsor_to_funsor(lazy_term):
returnlazy_term._eval()
The text was updated successfully, but these errors were encountered:
This proposes a way to implement a LazyTensor-like feature in Funsor, should we decide we want it.
As explained in the LazyTensor paper, this interface improves debuggability by allowing code to run in different ways depending on whether or not attributes are accessed (e.g. via an assert statement or debug print statement), i.e. the interpretation is controlled not by an outer context manager but by inner attribute access. This might be a good interface for Funsor use in Pyro, where interpretations are outside of model-author control. E.g.
x = pyro.sample("x", dist.Normal(0, 1)) + print(x) # <--- this could trigger eager evaluation pyro.sample("y", dist.Normal(x, 1), obs=data)
The idea of
LazyFunsor
would be to create a mutable object with a Funsor-like interface, to allow construction of reflected funsor terms, and then to trigger some other interpretation as soon as any attribute of thatLazyFunsor
is accessed, e.g..data
or maybe even.inputs
. Here's a rough sketchThe text was updated successfully, but these errors were encountered: