-
Notifications
You must be signed in to change notification settings - Fork 134
feat(useless-rewrite): Useless rewrite from log1mexp(log1mexp(x))
to x
#535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…follow important parts
I think to finish this I need to add a test here pytensor/tests/tensor/rewriting/test_math.py Lines 1945 to 1971 in c10c376
|
@@ -319,7 +319,7 @@ def local_exp_log(fgraph, node): | |||
@register_specialize | |||
@node_rewriter([Elemwise]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a bit better, as it will only call the rewrite on nodes with these Ops
@node_rewriter([Elemwise]) | |
@node_rewriter([exp, expm1, log1mexp]) |
@@ -319,7 +319,7 @@ def local_exp_log(fgraph, node): | |||
@register_specialize | |||
@node_rewriter([Elemwise]) | |||
def local_exp_log_nan_switch(fgraph, node): | |||
# Rewrites of the kind exp(log...(x)) that require a `nan` switch | |||
"""Rewrites of the kind exp(log...(x)) that require a `nan` switch.""" | |||
x = node.inputs[0] | |||
|
|||
if not isinstance(node.op, Elemwise): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed (can't select the return None below for the git suggestion)
if not isinstance(node.op, Elemwise): |
return [new_out] | ||
x = x.owner.inputs[0] | ||
|
||
op_map = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neat but I find the old form more readable tbh? Also if I ever have to debug this rewrite I would rather have the unrolled if/elses.
I am happy with the nan_switch_helper
and nesting the if/else based on the outer Ops.
Sounds about right |
The valid condition is that |
Motivation for these changes
Picking up this ticket on the PyData Global 2023 OSS sprint 🏃
log1mexp(log1mexp(x)) -> x
#471The motivating issue concerned an issue using PyMC's
Censored
functionality which was traced back to the need for a "useless rewrite":Implementation details
x
andnode
from the function body in its scope, meaning we don't need to pass them as parameters, so the body of each case becomes simplerlog1mexp
for this caseChecklist
Major / Breaking Changes
New features
Bugfixes
log1mexp(log1mexp(x)) -> x
#471Documentation
Maintenance