-
Notifications
You must be signed in to change notification settings - Fork 135
Rewrite scalar solve to division #1453
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -400,6 +400,39 @@ | |
return [exp(x)] | ||
|
||
|
||
@register_canonicalize | ||
@register_specialize | ||
@node_rewriter([sqrt, sqr]) | ||
def local_sqrt_sqr(fgraph, node): | ||
x = node.inputs[0] | ||
|
||
if not (x.owner and isinstance(x.owner.op, Elemwise)): | ||
return | ||
|
||
prev_op = x.owner.op.scalar_op | ||
node_op = node.op.scalar_op | ||
|
||
# Case for sqrt(sqr(x)) -> |x| | ||
if isinstance(prev_op, ps.Sqrt) and isinstance(node_op, ps.Sqr): | ||
new_out = pt_abs(x.owner.inputs[0]) | ||
old_out = node.outputs[0] | ||
|
||
# Handle potential integer to float cast by sqr | ||
if new_out.dtype != old_out.dtype: | ||
new_out = cast(new_out, old_out.dtype) | ||
return [new_out] | ||
|
||
# Case for sqr(sqrt(x)) -> x | ||
if isinstance(prev_op, ps.Sqr) and isinstance(node_op, ps.Sqrt): | ||
new_out = x.owner.inputs[0] | ||
jessegrabowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
old_out = node.outputs[0] | ||
|
||
# Handle potential integer to float cast by sqrt | ||
if x.dtype != old_out.dtype: | ||
new_out = cast(new_out, old_out.dtype) | ||
return [new_out] | ||
Comment on lines
+425
to
+433
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should introduce a nan switch like we do for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we register the nan rewrite only in stabilize/specialize so if you have something like |
||
|
||
|
||
@register_specialize | ||
@node_rewriter([exp, expm1]) | ||
def local_exp_log_nan_switch(fgraph, node): | ||
|
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.
WHAT AN OPTIMIZATION!!!!!!!!!!!!!!!!!!!