Skip to content

Commit 2378c30

Browse files
jfesereb8680
andauthored
Allow _BaseOperation subclasses to have an overrideable apply method (#414)
* stash * fixes * initial * wip * lint * ensure each subclass has a fresh operation * wip * wip * lint * wip * wip * lint * refactor class method support * move defops * fix test * remove singledispatch case and add test * move definition * cleanup * simplify * cleanup * lint * fix failing test * fix classmethod * __isabstractmethod__ * revert --------- Co-authored-by: Eli <eli@basis.ai>
1 parent 0338fcc commit 2378c30

9 files changed

Lines changed: 534 additions & 541 deletions

File tree

docs/source/lambda_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def sort_add(x: Expr[int], y: Expr[int]) -> Expr[int]:
102102
case Term(add_, (a, Term(vx, ()))), Term(vy, ()) if add_ == add and id(vx) > id(
103103
vy
104104
):
105-
return (a + vy()) + vx() # type: ignore
105+
return (a + vy()) + vx()
106106
case _:
107107
return fwd()
108108

docs/source/semi_ring.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ def vertical_fusion[S, T](e1: T, x: Operation[[], T], e2: S) -> S:
174174
return evaluate(
175175
Sum(
176176
e_sum, # type: ignore
177-
k1, # type: ignore
178-
v1, # type: ignore
177+
k1,
178+
v1,
179179
Let(
180180
e_lhs,
181-
v2, # type: ignore
181+
v2,
182182
Let(k1(), k2, Dict(k2(), Let(e_lhs, k2, e_rhs))), # type: ignore
183183
),
184184
)

effectful/handlers/numpyro.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,22 @@ def args(self):
242242
def kwargs(self):
243243
return self._kwargs
244244

245-
@defop # type: ignore
246245
@property
246+
@defop
247247
def batch_shape(self) -> tuple[int, ...]:
248248
if not (self._is_eager):
249249
raise NotHandled
250250
return self._pos_base_dist.batch_shape[len(self._indices) :]
251251

252-
@defop # type: ignore
253252
@property
253+
@defop
254254
def has_rsample(self) -> bool:
255255
if not (self._is_eager):
256256
raise NotHandled
257257
return self._pos_base_dist.has_rsample
258258

259-
@defop # type: ignore
260259
@property
260+
@defop
261261
def event_shape(self) -> tuple[int, ...]:
262262
if not (self._is_eager):
263263
raise NotHandled
@@ -312,8 +312,8 @@ def log_prob(self, value) -> jax.Array:
312312
ind_log_prob = self._reindex_sample(pos_log_prob, sample_shape)
313313
return ind_log_prob
314314

315-
@defop # type: ignore
316315
@property
316+
@defop
317317
def mean(self) -> jax.Array:
318318
if not self._is_eager:
319319
raise NotHandled
@@ -322,8 +322,8 @@ def mean(self) -> jax.Array:
322322
except NotImplementedError:
323323
raise RuntimeError(f"mean is not implemented for {type(self).__name__}")
324324

325-
@defop # type: ignore
326325
@property
326+
@defop
327327
def variance(self) -> jax.Array:
328328
if not self._is_eager:
329329
raise NotHandled

effectful/internals/runtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _get_args() -> tuple[tuple, Mapping]:
4040
def _restore_args[**P, T](fn: Callable[P, T]) -> Callable[P, T]:
4141
@functools.wraps(fn)
4242
def _cont_wrapper(*a: P.args, **k: P.kwargs) -> T:
43-
a, k = (a, k) if a or k else _get_args() # type: ignore
43+
a, k = (a, k) if a or k else _get_args()
4444
return fn(*a, **k)
4545

4646
return _cont_wrapper

effectful/ops/semantics.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717

1818

19-
@defop # type: ignore
19+
@defop
2020
def apply[**P, T](op: Operation[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2121
"""Apply ``op`` to ``args``, ``kwargs`` in interpretation ``intp``.
2222
@@ -38,23 +38,14 @@ def apply[**P, T](op: Operation[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
3838
3939
By installing an :func:`apply` handler, we capture the term instead:
4040
41-
>>> def default(*args, **kwargs):
42-
... raise NotHandled
43-
>>> with handler({apply: default }):
41+
>>> from effectful.ops.syntax import defdata
42+
>>> with handler({apply: defdata}):
4443
... term = mul(add(1, 2), 3)
4544
>>> print(str(term))
4645
mul(add(1, 2), 3)
4746
4847
"""
49-
from effectful.internals.runtime import get_interpretation
50-
51-
intp = get_interpretation()
52-
if op in intp:
53-
return intp[op](*args, **kwargs)
54-
elif apply in intp:
55-
return intp[apply](op, *args, **kwargs)
56-
else:
57-
return op.__default_rule__(*args, **kwargs) # type: ignore
48+
return op.__default_rule__(*args, **kwargs) # type: ignore
5849

5950

6051
@defop

0 commit comments

Comments
 (0)