Reduce algebra canonicalization footprint#2290
Open
ricardoV94 wants to merge 3 commits into
Open
Conversation
ricardoV94
commented
Jul 11, 2026
Member
- Handle the absorbing element in AlgebraicCanonizer
- Emit canonical forms directly from local_neg_to_mul
- Speedup AlgebraicCanonizer
`AlgebraicCanonizer` already drops the *neutral* element of `main` (`mul_calculate` returns [] for 1, `add_calculate` for 0). But `mul` also has an *absorbing* element that `add` does not, and the canonizer did not know about it: given `mul(0, x, y)`, `simplify_constants` folds the constant subset to `0` and re-emits `mul(0, x, y)` with the non-constant factors intact. `local_mul_zero` existed only to perform the collapse the canonizer skipped, and it does so less thoroughly -- it scans direct inputs, so it misses `mul(2, x, 0.5, 0)`, where the zero only emerges once the constants are folded together. Teach the canonizer the absorbing element instead: - new `absorbing_element` argument (default None; `add` keeps None) - `transform` folds a `main` node with a constant absorbing input directly, skipping the `get_num_denum` walk - `simplify_constants` drops the surviving factors when the constants fold to the absorbing element -- ahead of the single-constant shortcut, which would otherwise hand back the original num/denum untouched `local_mul_zero` is therefore no longer registered in canonicalize, which was its only registration. The rewrite itself is kept for rewrite databases that run without the canonizer.
`local_neg_to_mul` rewrote every `neg(x)` to `mul(-1, x)`, which by
construction is a fresh `Mul` node for `local_mul_canonizer` to flatten and
re-fold on the next canonicalize iteration. Emit the already-canonical form
instead:
neg(neg(x)) -> x
neg(mul(c, *xs)) -> mul(-c, *xs) (c constant; no extra factor)
neg(mul(*xs)) -> mul(-1, *xs) (flat, not mul(-1, mul(*xs)))
neg(c) -> -c
With the double-negation case handled here, the `useless_neg_of_neg` pattern
rewriter is redundant in canonicalize, so it is no longer registered there. It
stays registered in specialize, where `local_neg_to_mul` does not run.
Measured over the PyMC model catalogue (98 model/axis compiles): final graphs
are unchanged, while `local_mul_canonizer` loses 31 fires, MergeOptimizer 82,
and `constant_folding` 56.
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.