Add ReduceGroundCartesianProduct - #731
Open
eb8680 wants to merge 16 commits into
Open
Conversation
eb8680
force-pushed
the
eb-ground-cartesian-product
branch
from
July 28, 2026 16:07
c5efdf2 to
93a5895
Compare
eb8680
marked this pull request as ready for review
July 29, 2026 00:08
Contributor
Author
|
The slop in the PR description is from disaggregating #724. The code in this PR was largely written and checked by me. |
jfeser
requested changes
Jul 29, 2026
jfeser
left a comment
Contributor
There was a problem hiding this comment.
I pushed some new tests that currently fail. We should either fix those cases or forward. The diagonal stream consumption issue also affects ReduceDistributeCartesianProduct (there it causes a performance problem, not a correctness one).
* add monoid module * clean up * fix doctest * fix * wip * remove incorrect rule * add disjoint set tests and fix bug * lint * drop jax monoid defs * drop incorrect comment * add assert * reduce nondeterminism and add assertions * fix inconsistent stream numbering and missing constant factors
* Add monoid module (#653) * add monoid module * clean up * fix doctest * fix * wip * remove incorrect rule * add disjoint set tests and fix bug * lint * drop jax monoid defs * drop incorrect comment * add assert * reduce nondeterminism and add assertions * fix inconsistent stream numbering and missing constant factors * wip * cleanup * fix rule * wip * fix bug * cleanup * lin
* Add monoid module (#653) * add monoid module * clean up * fix doctest * fix * wip * remove incorrect rule * add disjoint set tests and fix bug * lint * drop jax monoid defs * drop incorrect comment * add assert * reduce nondeterminism and add assertions * fix inconsistent stream numbering and missing constant factors * wip * cleanup * fix rule * wip * fix bug * cleanup * lin * wip * fix tests * format * lint * wip
* Add monoid module (#653) * add monoid module * clean up * fix doctest * fix * wip * remove incorrect rule * add disjoint set tests and fix bug * lint * drop jax monoid defs * drop incorrect comment * add assert * reduce nondeterminism and add assertions * fix inconsistent stream numbering and missing constant factors * wip * cleanup * wip * fix rule * wip * fix bug * cleanup * lin * wip * fix tests * format * lint * wip * wip * wip * wip * wip * wip * wip * wip * drop runtime typed dict lifting * wip * format * reorganize * stop using string dicts to avoid unification issue * wip * wip * wip * wip * wip * use check_rewrite in jax tests * lint * fix bugs
* Add monoid module (#653) * add monoid module * clean up * fix doctest * fix * wip * remove incorrect rule * add disjoint set tests and fix bug * lint * drop jax monoid defs * drop incorrect comment * add assert * reduce nondeterminism and add assertions * fix inconsistent stream numbering and missing constant factors * wip * cleanup * wip * wip * fix rule * wip * fix bug * cleanup * lin * wip * fix tests * format * lint * wip * wip * wip * wip * wip * wip * wip * wip * drop runtime typed dict lifting * wip * format * reorganize * stop using string dicts to avoid unification issue * wip * wip * wip * wip * wip * use check_rewrite in jax tests * lint * wip * fix bugs * comment on not implemented cases * format * simplify * lint * add matmul test
* more precise stream type * add tests for weighted rules * add reduction rule for weighted streams and tests * add test to demo expectation * add numpyro monoid module * add quadrature * add tests * wip * refactor tests * wip * test composition of lifting and weighting * drop numpyro changes * drop unused ops * lint * make weighted a Monoid method * fix typing of jax arrays * change weighted typing to take callable * fix test * fix test * resolve type aliases before dispatching * wip * wip * remove typeof_full * wip * wip * wip * format * refactor test harness * drop unused test
* more agressive factorization that hoists shared streams * reduce nesting * comment * replace with simpler push-based rule * format * drop unused disjoint set * remove unused * push multiple streams instead of one at a time
* more precise stream type * add tests for weighted rules * add reduction rule for weighted streams and tests * add test to demo expectation * add numpyro monoid module * add quadrature * add tests * wip * refactor tests * wip * test composition of lifting and weighting * drop numpyro changes * drop unused ops * lint * make weighted a Monoid method * fix typing of jax arrays * change weighted typing to take callable * fix test * fix test * resolve type aliases before dispatching * wip * wip * remove typeof_full * wip * wip * wip * format * refactor test harness * fix behavior of delta terms * add baseline einsum * rework einsum to work on shapes instead of concrete tensors * add einsum benchmark * wip * wip * finish sum/product contraction * allow bind_dims to bind nonexistent named dimensions * wip * add custom partial eval for reductions * working benchmarks * fix infinite loop * eliminate identity indexing when possible * wip * handle getitem where dimensions are created * treat any index with bare ops and slice(None) as canonical * simplify range op and add reduction rules * wip * remove old benchmark code * another try at removing identity gathers * refactor * fix test * lint * clean up comment * fix some test failures * drop sketchy bind_dims rule * drop more type-incompatible plus rules * format * fix reduction issue * drop dimension creating behavior from bind_dims * lint * simplify comment * drop partition * fix docstring * handle negative dimension indexing * fix creation of empty tensors * fully restore previous behavior for missing named dims * reduce any arraylike or named tensor * require at least one jax array to reduce * fix typing test * drop typing test * drop einsum parser in favor of opt_einsum * more agressive factorization that hoists shared streams * reduce nesting * comment * replace with simpler push-based rule * format * drop unused disjoint set * remove unused * push multiple streams instead of one at a time * drop contraction ordering handler * fold BindDimsBindDims into default behavior * handle Sum.reduce instead of Monoid.reduce * wip * wip * hacks * extract contraction heuristic * lint * fix test * use a named dimension einsum for contractions * lint * drop custom arange op * wip * simplify by targetting delta rules * wip * fixes * fixes * lint * drop unused * pick up constants but not rest of module * lint
* wip * wip * drop syntactic tests * wip * wip * wip * wip * wip * add cartesian product tests * wip * wip * revise reducesplit to leave shared streams * wip * allow ReduceEqualityMaskRange to look through plus * add mask hoisting * wip * wip * wip * unsupplied parameters stay bound in deffn * wip * wip * wip * allow factorization over masks * don't do leave-one-out for factors with no output dims * plated einsum tests pass * format * fix some tests * fix tests * more fixes and documentation * passing plated einsum tests * format * simplify and generalize plusdistr * normalize both order and duplicates in plus * drop unused test * push masks instead of hoisting * stop emitting extra masks * wip * add missing code * wip * revert to simple ReduceSplit, handle plus bodies in cprod elim * replace ReduceFactorization with new combined Factor * wip * give delta mapping semantics * fix tests * fix tests * add ReduceEqualityMaskRange tests * wip * wip * refactor einsum term generation * wip * wip * use where as a hoistable conditional primitive * drop binddimswhere * fix tests and clean up * avoid expensive traversal when looking up signatures * fix tests * remove ReduceCartesianWeightedStream cartesian products changed type, making these rules invalid * wip * wip * fix tests remove outdated * lint * drop test * fix notebook * drop 3.14 in CI * drop 3.14 * wip * restrict litellm * reset ci scripts * reset * drop _ArrayTerm * introduce ite op and move where handlers to ops/monoid.py * wip * remove SplitDisjointProduct * wip * move ReduceDependentRangeMask to ops/monoid.py * move ContractLongestArrayStream to ops/monoid.py * start generalizing ReduceDistributeCartesianProduct * wip * drop unused SumOfProductsIntp * wip * drop unused * simplify * more work * lint * update comment * add tests to ReduceUnfactor * drop unnecessary check * fix bug * fix tests * format * lint * drop unused code * fix tests * replace Union.delta with a dict building op * drop comment * replace Union.delta with as_dict * lint * revert * fix flipped mask * require simple ranges in scan rule * clean up collection types * enforce simple range requirement in dependent range elim * simplify typing in _EinsumBuilder * lint * add helper function _conjuncts * extract jax-independent behavior * fix test
`_jax_args` admitted `jax.typing.ArrayLike`, a union that includes `bool`, `int`, `float` and `complex`, so the jax `Monoid.plus` handlers claimed pure-Python scalar arithmetic. They extend `EvaluateIntp` after the scalar implementations and so take precedence, silently narrowing a Python float to a `float32` array and leaving downstream rules treating a scalar body as array-valued. Require at least one genuine array. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Don't route all-scalar monoid ops through jax `_jax_args` admitted `jax.typing.ArrayLike`, a union that includes `bool`, `int`, `float` and `complex`, so the jax `Monoid.plus` handlers claimed pure-Python scalar arithmetic. They extend `EvaluateIntp` after the scalar implementations and so take precedence, silently narrowing a Python float to a `float32` array and leaving downstream rules treating a scalar body as array-valued. Require at least one genuine array. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Add a generator-expression bytecode disassembler `effectful/internals/disassembly.py` symbolically interprets the bytecode of a generator expression (and of lambdas and comprehensions nested inside it) back into an `ast` node, so a comprehension's source syntax can be recovered from the code object at runtime. Supports CPython 3.12 and 3.13. Standalone: imports nothing from `effectful` and touches no existing code. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Address review comments on the generator-expression disassembler Six fixes, each with tests that fail without them: - `handle_build_map` read the key/value pairs of a dict display from the top of the stack down, reversing source order: a later duplicate key lost to an earlier one, and side effects ran backwards. - `_ensure_ast_tuple` treated any tuple whose first element was the string "dict_item" as an internal marker and dropped that element. Nothing produced such a marker; user data holding that string was silently corrupted. The special case is gone. - A free variable was reconstructed as a bare `ast.Name`, so evaluating the result resolved it against the evaluating namespace instead of the captured cell. The captured value is now written into the tree, for the generator itself, for lambdas reached as live objects, and for lambdas and comprehensions nested inside. A cell the comprehension creates -- a target captured by a nested lambda -- still stands as a name, since the reconstruction binds it too. A capture with no AST spelling, including an iterator, raises `TypeError` rather than reconstructing to a name that would answer differently. - `_ensure_ast_iterator_adaptor` ignored the strictness a `zip` pickles as reduction state, so a strict zip silently truncated ragged input where the original raised. - A lambda reached as a live object lost its default values, which live on the function rather than in its code object, leaving parameters with no way to be filled. - `disassemble` asserted on its input; it now raises `ValueError`, and checks the generator has not been started rather than leaving that to an assert further in. Also documents what reconstruction does and does not recover: evaluating the result re-runs every expression in it, so a stateful filter answers against state as it then stands. 663 passed, 2 xfailed on 3.12, 3.13 and 3.14. --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Replaces a cartesian-product stream with one stream per plate assignment the body actually subscripts. Where the body folds over the plates uniformly, ReduceDistributeCartesianProduct inverts the reduction instead, which is cheaper and leaves the plate fold intact. This rule is the fallback for bodies that admit no per-plate factorization: a chain `X[t], X[t+1]` couples adjacent plate indices, so inversion does not apply. Grounding costs one variable per assignment instead of enumerating the |D|^|P| rows, after which the result is an ordinary variable-elimination problem that `Factor` solves. `ReducePartial` gains an `unrolled` filter so grounding can expand only the plate folds it is grounding over. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
jfeser
force-pushed
the
eb-ground-cartesian-product
branch
from
July 30, 2026 18:58
7acbe8c to
7be9b05
Compare
jfeser
force-pushed
the
staging-weighted
branch
from
August 10, 2026 16:49
42a4771 to
19e211f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
ReduceGroundCartesianProduct, the fallback rule for a reduction over acartesian-product row stream whose body admits no per-plate factorization.
The rewrite
where
aranges over the plate assignments the body actually subscripts._GroundRowis aMappingthat stands in for the row variable. Unrolling thebody's plate folds hands it concrete keys, and it answers each with a variable
minted on first sight (the same variable for a repeated key, which is what ties
the factors of a chain together). A key that is still symbolic never reaches it:
the subscript stays a term until the fold supplying the index is expanded.
_GroundRow.residualreturns whatever plate values no key consumed, which stayin the expression as a smaller cartesian product;
sealed+fvsofreject ahalf-ground result, in which case the rule declines.
When this applies vs.
ReduceDistributeCartesianProductWhere the body folds over the plates uniformly,
ReduceDistributeCartesianProductinverts the reduction instead — cheaper, and it leaves the plate fold intact.
Grounding is for bodies where inversion does not apply, e.g. a chain
X[t], X[t+1]couples adjacent plate indices. Grounding costs one variable perassignment (
|P|variables overD) instead of enumerating the|D|^|P|rows,after which what is left is an ordinary variable-elimination problem that
Factorsolves.ReduceGroundCartesianProduct()is registered inNormalizeIntpimmediately beforeReduceDistributeCartesianProduct().ReduceDistributeCartesianProductitself is untouched here.ReducePartial.unrolledReducePartialgains an optionalunrolledset of stream variables and skipsany stream not in it. Grounding installs
ReducePartial(plate_vars)so thatexpanding the body expands only the plate folds it is grounding over, and not,
say, the value domains.
Test
tests/test_ops_monoid.py::test_ground_cartesian_product_chainreduces a chainSum_ixs Prod_t phi[t][ixs[t]][ixs[t+1]]and checks the result against theforward algorithm computed in plain Python.
It is written longhand with explicit
defopvariables. In #724 it is writtenas
using the generator-comprehension syntax that lands in a later PR; the body
below is exactly what
desugar_comprehensionproduces for it.Tis 10 rather than the 20 used in #724: normalization is superlinear in thechain length until the separate
evaluateperformance fix lands, and thisbranch is deliberately not stacked on it. At
T = 10the test takes ~6s; atT = 20it takes over an hour.K = 3as in #724.The test does discriminate: unregistering the rule makes normalization a no-op
and the added structural assertion fails immediately. (Without that assertion
the test would still pass at
T = 10, just 11x slower, because evaluationfalls back to enumerating all
K ** Trows — which is why the assertion isthere.)
Prerequisite: #729
This PR is stacked on #729 (
eb-jax-scalar-plus), which fixes_jax_argsineffectful/handlers/jax/monoid.py. That fix is a genuine prerequisite here, nota cosmetic one:
jax.typing.ArrayLikeincludesbool/int/float/complex,so the jax handlers claimed pure-Python scalar arithmetic. Without it the new
test does not merely produce a different type — it raises
TypeError: Cannot unify type <class 'int'> with <class 'jax.Array'>, becausetests/test_ops_monoid.pyimportseffectful.handlers.jax.monoidunconditionally.
This branch's own diff is just
effectful/ops/monoid.pyandtests/test_ops_monoid.py.The
ReduceDisequalityMaskor->is Nonefix that also lives in #724 waschecked and is not needed here: the full suite passes without it.
Testing
tests/test_ops_monoid.py+tests/test_handlers_jax_monoid.py: 531 passed.tests/sweep excludingtests/test_handlers_llm_*.py: 18318 passed,2 skipped, 2078 xfailed — versus 18317/2/2078 on pristine
staging-weighted,the difference being the new test. No regressions.
ruff check,ruff format --diffclean.mypyclean apart from the onepre-existing
effectful/handlers/jax/monoid.pyerror already onstaging-weighted.Split out of #724 for review. Stacked on #729. Independent of the sibling
ReduceDistributeCartesianProductrefactor (#730), also split out of #724.🤖 Generated with Claude Code