Skip to content
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

Create canonicalization for sub(x, neg(y)) #546

Closed
ricardoV94 opened this issue Aug 3, 2021 · 1 comment · Fixed by #549
Closed

Create canonicalization for sub(x, neg(y)) #546

ricardoV94 opened this issue Aug 3, 2021 · 1 comment · Fixed by #549
Assignees
Labels
enhancement New feature or request graph rewriting

Comments

@ricardoV94
Copy link
Contributor

ricardoV94 commented Aug 3, 2021

Seems like we are missing a canonicalization for sub(x, neg(y)) -> add(x, y) :

import aesara
import aesara.tensor as at

x = at.scalar("x")
y = at.scalar("y")
z = x - (-y)

f = aesara.function([x, y], z)
aesara.dprint(f)
Elemwise{Composite{(i0 - (-i1))}} [id A] ''   0
 |x [id B]
 |y [id C]

It does work with constants

z = 5 - (-x)

f = aesara.function([x], z)
aesara.dprint(f)
Elemwise{add,no_inplace} [id A] ''   0
 |TensorConstant{5.0} [id B]
 |x [id C]

And we have one for double negation:

z = - (-x)

f = aesara.function([x], z)
aesara.dprint(f)
DeepCopyOp [id A] 'x'   0
 |x [id B]

Originally posted by @ricardoV94 in #473 (comment)

@ricardoV94 ricardoV94 changed the title Create canonicalization for sub(x, neg(y)): Create canonicalization for sub(x, neg(y)) Aug 3, 2021
@ricardoV94
Copy link
Contributor Author

ricardoV94 commented Aug 3, 2021

We might also want to canonicalize -x + y -> y - x.

Not sure if it is a faster operation, but some lazy benchmarks on Colab suggest so. In any case it might be worth it just as a canonicalization.

@brandonwillard brandonwillard added enhancement New feature or request help wanted Extra attention is needed graph rewriting labels Aug 3, 2021
@ricardoV94 ricardoV94 removed the help wanted Extra attention is needed label Aug 6, 2021
@ricardoV94 ricardoV94 self-assigned this Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request graph rewriting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants