-
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] Simplifications for floormod(x, 2) #13936
Conversation
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
Because `floormod(x,2)` has only two possible values, it can be simplified more aggressively than most FloorMod expressions. The additional simplifications are derived from `floormod(x,2) + floormod(x+1,2) == 1`, which is true for denominator `2`, along with the usual `floordiv(x,2)*2 + floormod(x,2) == x`, which is true for all denominators. This initially arose from an index expression `floormod(x + 1, 2) * 8192`, for `x ∈ [0, 2)`. This commit allows the expression to be re-written as `x * (-8192) + 8192` and recognized as a strided access.
f5c5912
to
31803c7
Compare
@@ -898,6 +898,11 @@ class IterMapRewriter : public ExprMutator { | |||
PrimExpr SplitFloorModConst(IterSplitExpr lhs, PrimExpr base, PrimExpr rhs); | |||
|
|||
static void AddToLhs(IterSumExprNode* lhs, IterSplitExpr rhs, int sign) { | |||
if (sign < 0 && is_const_int(rhs->extent, 2)) { |
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.
Could we add a cover case for the codepath?
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 do, and added! This was a case that was caught by other unit tests, since the additional RewriteSimplifier
rules prevented DetectIterMap
from recognizing some patterns after they had been simplfied, but a specific unit test for this case is better than needing to track it down later.
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.
NOTE: this actually is a bug, see #14571
This makes me think that if other rules leads to the regression. Please do check.
BTW for cancellation like For my curiosity about our tradeoff in simplication, may in the future it allows introduce O(n^2) level rewriting for summation terms to fully utilize rewrite rules? cc @tqchen @Lunderberg |
Thank you for the review, @wrongtest-intellif , and everything should be updated now. Regarding cases where it can fail to cancel, that definitely can occur. I have some work on a local branch where I've been trying to make the rewrite rules more general (and to reduce duplication as a benefit), but at the moment it slows down the RewriteSimplifier by about 10x. One thing that I think could help would be to enforce an ordering between terms for any commutative and associative operator. For example, right now |
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.
LGTM!
some expected tir in ut seems outdated |
@wrongtest-intellif Thank you for pointing that out, and merged main into the dev branch to resolve. |
Could you please fix the CI and we can get it in @Lunderberg |
@Hzfengsy , thank you on the reminder, and it would be good to get this PR finished and merged in. @junrushao Could I get your assistance on the last CI error? The failing unit tests are in It looks like this simplification allowed the search space to go down an entirely different route than the unit test was expecting, as the generated sketch outputs the transpose ( |
hey sorry for the delayed response - on pto right now :-) 🤯 wow, i mean any schedule is not supposed to make a conv become transposed conv...do you think it might be a parser issue that it kinda capture the wrong source code? if needed, i can help with debugging 🐛 once i'm back |
Thank you, and sorry for pinging you on PTO, and this is much lower priority than PTO rest/relaxation. I wouldn't guess that it was a parsing issue, as the parsed TIR hasn't changed as a result of the additional simplifications. So, maybe it would be more likely to be a change in the output of |
I think I got it. The first issue was that the failure was in After that, the only difference was resolved by replacing |
Note that this PR introduces a bug in #14571 And the bug was intended to fix a regression, which now comes back. This makes me think that we should revisit other rules here. Since there is a possibility of other issues. Likely we should remove some of the simplification in favor of iter_map because that is more important. @Lunderberg @junrushao let us quickly figure out the situation here. |
@wrongtest-intellif sorry for following up late, for term cancelations, we have canonical simplifier which should be able to do such cancelation, so likely we don't need to enhance rewrite simplifier too much here. |
added more discussions in #14571 |
Because
floormod(x,2)
has only two possible values, it can be simplified more aggressively than most FloorMod expressions. The additional simplifications are derived fromfloormod(x,2) + floormod(x+1,2) == 1
, which is true for denominator2
, along with the usualfloordiv(x,2)*2 + floormod(x,2) == x
, which is true for all denominators.This initially arose from an index expression
floormod(x + 1, 2) * 8192
, forx ∈ [0, 2)
. This commit allows the expression to be re-written asx * (-8192) + 8192
and recognized as a strided access.