-
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
[Tracking Issue] TIR-level CSE follow up #10211
Comments
Thanks @masahi for the summary. Kind regards, |
Hello, For semantic extension, I listed some items:
We may call tvm/arith library to do the job. bool EquivalentTerms(const PrimExpr& a, const PrimExpr& b) {
// For now, we just check the syntactic equality, but that could later become a semantic test,
// for instance identifying computations modulo commutativity (like x+y and y+x), or modulo
// associativity (like (x+y)+z and x+(y+z)), etc.
arith::Analyzer analyser;
PrimExpr a_simplified = analyser.Simplify(a);
PrimExpr b_simplified = analyser.Simplify(b);
return EqualTerms(a_simplified, b_simplified);
}
An example test :
And I got:
However I found that the arith system may not be able to do the commutativity (x+y != y+x and x*y != y*x). |
Hi @yuanfz98 Thanks for your interest in the pass! When I worked on the CSE pass, I designed it with the idea that it should be easy to extend it for working with terms modulo some equations (by just replacing the EquivalentTerms() function as you did), but I though that we would have to write the normalization procedures on terms for doing that (which would simplify terms within an algebraic structure). And writing such normalization functions is quite a bit annoying to do (it implies deciding on some order terms, sorting them, etc). So I left it for a potential future work. I wasn't even sure that the efforts to implement such normalization procedures would be worth it, because I didn't know how often semantically equivalent computations appear in a TIR program. But as we already have this Simplify() method from arith::Analyzer that does simplification work, we can indeed reuse it without even thinking about it. That's great! Too bad that arith::Analyzer can't deal with commutativity. I had a quick look at how RewriteSimplifier (which is used by Simplify()) is implemented, and it works by stating rewriting rules that could be applied. They are tried in the order in which they appear with the TVM_TRY_REWRITE() directive. The reason why they can't define a commutativity rule is therefore likely because it would create infinite loops of rewriting: In short, for rewriting rules that can be applied again and which lead back to the original term (i.e, A --> B --> A), this strategy of just stating rewrite rules by priority won't work unfortunately. Simplify() will do the amount of work that it can do and it will be better than nothing. |
Would you like to make a PR for this @yuanfz98 ? Many thanks again for your interest in the pass! |
Sure, I will make a PR this week ! |
@masahi is this still in progress? the related PRs have merged so I'm closing this tracking issue but please re-open if it's not totally done |
Yeah none of the items I raised have been done actually |
Follow up items after #9482
@FranckQC @jroesch, also cc @mbs-octoml @mikepapadim @electriclilies
cc @Hzfengsy @junrushao
The text was updated successfully, but these errors were encountered: