-
Notifications
You must be signed in to change notification settings - Fork 146
Add rewrite for 1 ** x = 1
#1179
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
Changes from 4 commits
97bd1c4
d873bff
daf9286
6df788c
836eb45
c4f662a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1905,12 +1905,27 @@ def local_reciprocal_canon(fgraph, node): | |
@register_canonicalize | ||
@node_rewriter([pt_pow]) | ||
def local_pow_canonicalize(fgraph, node): | ||
cst = get_underlying_scalar_constant_value( | ||
""" | ||
Rewrites for exponential functions with straight-forward simplifications: | ||
1. x ** 0 -> 1 | ||
2. x ** 1 -> x | ||
3. 1 ** x -> 1 | ||
|
||
In all cases, the shape of the output is the result of broadcasting the shapes of the inputs. | ||
""" | ||
|
||
cst_base = get_underlying_scalar_constant_value( | ||
node.inputs[0], only_process_constants=True, raise_not_constant=False | ||
) | ||
if cst_base == 1: | ||
return [broadcast_arrays(*node.inputs)[0].astype(node.outputs[0].dtype)] | ||
|
||
cst_exponent = get_underlying_scalar_constant_value( | ||
node.inputs[1], only_process_constants=True, raise_not_constant=False | ||
) | ||
if cst == 0: | ||
if cst_exponent == 0: | ||
return [alloc_like(1, node.outputs[0], fgraph)] | ||
if cst == 1: | ||
if cst_exponent == 1: | ||
return [alloc_like(node.inputs[0], node.outputs[0], fgraph)] | ||
|
||
|
||
|
||
|
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.
Can clean up a bit by storing
inp_idx
in the 3 branches and doing something like:?
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.
my concern about alloc_like applies to the old code as well
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.
I don't think it can be as clean as this, because in the x ** 0 case we're not using either input value in the output. But I tried to make it look more like this.