-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[ARITH] RewriteSimplifier: min/max, logical, select #2768
Conversation
cc @sgrechanik-h @icemelon9 @merrymercy @kazum @grwlf @ZihengJiang @wweic @junrushao1994 @MarisaKirisame please help review the PR |
TVM_TRY_REWRITE(min(min(y, x), min(z, x)), min(min(y, z), x)); | ||
|
||
TVM_TRY_REWRITE(min(y + x, z + x), min(y, z) + x); | ||
TVM_TRY_REWRITE(min(y + x, x + z), min(y, z) + x); |
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 guess something similar but for -
might be needed here.
Upd: seems like these ones
|
Thanks @sgrechanik-h I have updated per your comment |
src/arithmetic/rewrite_simplify.cc
Outdated
int64_t c1val = c1.Eval()->value; | ||
int64_t c2val = c2.Eval()->value; | ||
if (c2val % c1val == 0) { | ||
if (c2val / c1val > 0) { |
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 think >
should be >=
here since c2val can be zero.
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.
updated
c1.Eval()->value + 1 == c2.Eval()->value); | ||
|
||
TVM_TRY_REWRITE(max(min(x, y), max(x, y)), max(x, y)); | ||
TVM_TRY_REWRITE(max(min(x, y), max(y, x)), max(x, y)); |
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.
Need the following ones too?
TVM_TRY_REWRITE(max(max(x, y), min(x, y)), max(x, y));
TVM_TRY_REWRITE(max(max(x, y), min(y, x)), max(x, y));
src/arithmetic/rewrite_simplify.cc
Outdated
int64_t c1val = c1.Eval()->value; | ||
int64_t c2val = c2.Eval()->value; | ||
if (c2val % c1val == 0) { | ||
if (c2val / c1val > 0) { |
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.
>
should be >=
here.
ck.analyzer.update(x, tvm.arith.ConstIntBound(0, 1000)) | ||
ck.verify(tvm.min((x + 3) / 4 * 4, tvm.max(x, 4)), tvm.max(x, 4)) | ||
ck.verify(tvm.min(x, (x + 3) / 4 * 4), x) | ||
ck.verify(tvm.min(tvm.max(x, 4), (x + 3) / 4 * 4), tvm.max(x, 4)) |
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 think the boundary of x
should be reset after the check of this rule.
TVM_TRY_REWRITE(min(x, min(x, y)), min(x, y)); | ||
TVM_TRY_REWRITE(min(y, min(x, y)), min(x, y)); | ||
|
||
TVM_TRY_REWRITE(min(min(min(x, y), z), y), min(min(x, y), z)); |
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.
Also?
TVM_TRY_REWRITE(min(min(min(x, y), z), x), min(min(x, y), z));
TVM_TRY_REWRITE(min(min(min(x, y), z), z), min(min(x, y), z));
TVM_TRY_REWRITE(min(y, min(x, y)), min(x, y)); | ||
|
||
TVM_TRY_REWRITE(min(min(min(x, y), z), y), min(min(x, y), z)); | ||
TVM_TRY_REWRITE(min(min(min(min(x, y), z), s1), y), min(min(min(x, y), z), s1)); |
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.
Add these?
TVM_TRY_REWRITE(min(min(min(min(x, y), z), s1), x), min(min(min(x, y), z), s1));
TVM_TRY_REWRITE(min(min(min(min(x, y), z), s1), z), min(min(min(x, y), z), s1));
TVM_TRY_REWRITE(min(min(min(min(x, y), z), s1), s1), min(min(min(x, y), z), s1));
|
||
TVM_TRY_REWRITE(min(min(min(x, y), z), y), min(min(x, y), z)); | ||
TVM_TRY_REWRITE(min(min(min(min(x, y), z), s1), y), min(min(min(x, y), z), s1)); | ||
TVM_TRY_REWRITE(min(min(min(min(min(x, y), z), s1), s2), y), |
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.
TVM_TRY_REWRITE(min(min(min(min(min(x, y), z), s1), s2), x), min(min(min(min(x, y), z), s1), s2));
TVM_TRY_REWRITE(min(min(min(min(min(x, y), z), s1), s2), z), min(min(min(min(x, y), z), s1), s2));
TVM_TRY_REWRITE(min(min(min(min(min(x, y), z), s1), s2), s1), min(min(min(min(x, y), z), s1), s2));
TVM_TRY_REWRITE(min(min(min(min(min(x, y), z), s1), s2), s2), min(min(min(min(x, y), z), s1), s2));
TVM_TRY_REWRITE(y < x && y <= x, cfalse); | ||
|
||
TVM_TRY_REWRITE_IF(x < c1 && c2 < x, cfalse, | ||
c2.Eval()->value + 1 >= c1.Eval()->value); |
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.
c2.Eval()->value + 1 >= c1.Eval()->value); | |
c2.Eval()->value >= c1.Eval()->value); |
?
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.
The condition is correct here
TVM_TRY_REWRITE_IF(x < c1 && c2 < x, cfalse, | ||
c2.Eval()->value + 1 >= c1.Eval()->value); | ||
TVM_TRY_REWRITE_IF(c2 < x && x < c1, cfalse, | ||
c2.Eval()->value + 1 >= c1.Eval()->value); |
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.
c2.Eval()->value + 1 >= c1.Eval()->value); | |
c2.Eval()->value >= c1.Eval()->value); |
?
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.
The condition is correct here
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.
Yes you are right.
src/arithmetic/rewrite_simplify.cc
Outdated
@@ -1127,6 +1131,8 @@ Mutate_(const Not* op, const Expr& self) { | |||
TVM_TRY_REWRITE(!(x > y), x <= y); | |||
TVM_TRY_REWRITE(!(x == y), x != y); | |||
TVM_TRY_REWRITE(!(x != y), x == y); | |||
TVM_TRY_REWRITE(!(x || y), (!x) && (!y)); | |||
TVM_TRY_REWRITE(!(x && y), (!x) || (!y)); |
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.
It would be better to use TVM_TRY_RECURSIVE_REWRITE
here, because otherwise not
s are not propagated all the way down in some expressions (like ((max(max(i, x), y) <= 9) && ((x == i) || (y == i)))
).
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.
updated
Thanks, @sgrechanik-h @kazum @wweic please take another look. I tried the best to address some of the comments. I did not add all the rules suggested though, mainly because some of them could be added later, and we do not fully rely on the rewrite based simplification. The min/max chain reduction for example, can better be done through a canonical form that recognizes the min/max chain, which we can add later as another sub-analyzer. |
Thanks, @wweic @sgrechanik-h @kazum. This is now merged |
This is followup of #2722