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

[ARITH] RewriteSimplifier: min/max, logical, select #2768

Merged
merged 3 commits into from
Mar 14, 2019

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented Mar 10, 2019

This is followup of #2722

  • Added support for min/max, logical, select
  • Added test cases to cover the rules

@tqchen
Copy link
Member Author

tqchen commented Mar 10, 2019

cc @sgrechanik-h @icemelon9 @merrymercy @kazum @grwlf @ZihengJiang @wweic @junrushao1994 @MarisaKirisame please help review the PR

src/arithmetic/rewrite_simplify.cc Outdated Show resolved Hide resolved
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);
Copy link
Contributor

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.

src/arithmetic/rewrite_simplify.cc Show resolved Hide resolved
@sgrechanik-h
Copy link
Contributor

sgrechanik-h commented Mar 11, 2019

(((i0 % 2) != 1) || ((i0 % 2) == 0)) gets rewritten into ((i0 % 2) != 0), don't know what rule causes it. By the way, it would be great to have some rule debugging tool, may be something like printing out every transformation step if some compilation flag is on.

Upd: seems like these ones

  TVM_TRY_REWRITE(x == c1 || x != c2, x != c1 || c1 == c2);
  TVM_TRY_REWRITE(x != c2 || x == c1, x != c1 || c1 == c2);

@tqchen
Copy link
Member Author

tqchen commented Mar 12, 2019

Thanks @sgrechanik-h I have updated per your comment

src/arithmetic/rewrite_simplify.cc Show resolved Hide resolved
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
if (c2val % c1val == 0) {
if (c2val / c1val > 0) {
Copy link
Contributor

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.

Copy link
Member Author

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));
Copy link
Contributor

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));

int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
if (c2val % c1val == 0) {
if (c2val / c1val > 0) {
Copy link
Contributor

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))
Copy link
Contributor

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));
Copy link
Contributor

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));
Copy link
Contributor

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),
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c2.Eval()->value + 1 >= c1.Eval()->value);
c2.Eval()->value >= c1.Eval()->value);

?

Copy link
Member Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c2.Eval()->value + 1 >= c1.Eval()->value);
c2.Eval()->value >= c1.Eval()->value);

?

Copy link
Member Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right.

@@ -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));
Copy link
Contributor

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 nots are not propagated all the way down in some expressions (like ((max(max(i, x), y) <= 9) && ((x == i) || (y == i)))).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@tqchen
Copy link
Member Author

tqchen commented Mar 14, 2019

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.

@tqchen tqchen merged commit 046e4ff into apache:master Mar 14, 2019
@tqchen
Copy link
Member Author

tqchen commented Mar 14, 2019

Thanks, @wweic @sgrechanik-h @kazum. This is now merged

@tqchen tqchen deleted the rewrite2 branch March 15, 2019 01:39
wweic pushed a commit to wweic/tvm that referenced this pull request Mar 20, 2019
wweic pushed a commit to neo-ai/tvm that referenced this pull request Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants