Skip to content

PatternNodeRewriter: Allow matching variants - #2328

Draft
ricardoV94 wants to merge 1 commit into
pymc-devs:mainfrom
ricardoV94:pattern_node_rewriter_equivalent_terms
Draft

PatternNodeRewriter: Allow matching variants#2328
ricardoV94 wants to merge 1 commit into
pymc-devs:mainfrom
ricardoV94:pattern_node_rewriter_equivalent_terms

Conversation

@ricardoV94

@ricardoV94 ricardoV94 commented Aug 9, 2026

Copy link
Copy Markdown
Member

This PR extends PatternNodeRewriter to allow multiple variants of the pattern to match, and automates the get_nodes from the tracks parameter.

log1msigm_to_softplus = PatternNodeRewriter(
    [
        (log, (sub, 1, (sigmoid, "x"))),
        (log, (add, 1, (mul, -1, (sigmoid, "x")))),
        (log1p, (neg, (sigmoid, "x"))),
        (log1p, (mul, -1, (sigmoid, "x"))),
    ],
    (neg, (softplus, "x")),
    allow_multiple_clients=True,
    values_eq_approx=values_eq_approx_remove_inf,
    tracks=[sigmoid],
)

PyTensor will canonicalize different expressions at different passes, and will not uncanonicalize some. It never does log1p(x) -> log(1 + x). It coverts neg to mul(-1, x) in canonicalize, and back to neg at specialize. Instead of fighting order we match the expressions that we know are expected at different sages. This is not the same as writting down all variants.

Nothing fancy is done to try to reason jointly about the variants. The more we push for this functionality the more scope there will be.

For instance a rewrite that has log1p in it could be automatically expanded to match log(1 + x), and the like. Not done in this PR.

The motivation for this PR was pymc-devs/pymc#8394 (comment)

@ricardoV94
ricardoV94 marked this pull request as ready for review August 9, 2026 20:34
@ricardoV94
ricardoV94 force-pushed the pattern_node_rewriter_equivalent_terms branch from 82416f0 to 657cb05 Compare August 9, 2026 20:34
@ricardoV94
ricardoV94 marked this pull request as draft August 9, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant