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

[TIR][Bugfix] Improved massive build times caused by tir.floormod and tir.floordiv. Fixed Topi testcase. #5666

Merged
merged 9 commits into from Jul 28, 2020

Conversation

dprankratz
Copy link
Contributor

Build times

I experienced hangs when attempting to build a compute statement that depended on deep TIR expressions involving the tir.floormod and tir.floordiv operaors. The build times can be reproduced with the script here. This is due to the very complicated TIR expression generated when FloorMod or FloorDiv are lowered.

I was able to improve upon this by changing how these operators were lowered to use the tir.floor intrinsic which also has the benefit of matching the definitions of the operators.

Topi testcase

There are existing tests for tir.floormod and tir.floordiv through the topi test_topi_broadcast.py file. However, I noticed that this files used np.fmod and np.floor_divide as points of reference which are not equivalent to floormod and floordiv.

I fixed the testcase to use a correct version of floormod and floordiv.

@ghost
Copy link

ghost commented May 24, 2020

Reviewed.

@tqchen
Copy link
Member

tqchen commented May 24, 2020

Thanks @dpankratz , in this particular case, maybe we could try to go and fix the integer lowering path. How about wrap the temp value creation with a LetNode?

@dprankratz
Copy link
Contributor Author

dprankratz commented May 25, 2020

@tqchen I'm not sure how to gracefully add a LetNode since I'm running into trouble creating a variable with a unique name. For example, using a simple counter to create variables of the form temp0, temp1 will run into problems when multiple expressions exist concurrently and the names conflict causing SSA form to be broken.

I've tried searching for other places where creating variables with unique names is handled and haven't seen anything which makes me uneasy. Any suggestions?

@tqchen
Copy link
Member

tqchen commented May 25, 2020

the name hint of the variable is not required to be unique, as the address of the variable is used to uniquely identify the var

@tqchen
Copy link
Member

tqchen commented Jun 11, 2020

cc @dprankratz can we followup

@dprankratz
Copy link
Contributor Author

@tqchen I did some more digging into using let nodes. Consider the following expression:

floormod(floormod(a,b),floormod(c,d))

floormod(a,b) becomes let rmod1 = (a % b) in (expression with multiple rmod1)
floormod(c,d) becomes let rmod2 = (c % d) in (expression with multiple rmod2)

Then composing those with another floormod ends up creating multiple copies of the let statements using rmod1 and rmod2.

floormod(floormod(a,b),floormod(c,d)) becomes

let rmod3 = (let rmod1 = .... in ....) % (let rmod2 = ... in ....) in
   (expression with multiple rmod3)

The net effect is that multiple rmod3 copies are materialized which contain LetNodes for rmod1 and rmod2 which have the same address. This ends up failing the VarUseDefAnalysis pass since it believes the redefinitions of rmod1 and rmod2 break SSA.

Is there an easy solution to this problem?

@tqchen
Copy link
Member

tqchen commented Jun 28, 2020

@dpankratz sorry for the delayed reply. #5949 relaxes the constraint of the Let to allow a single var to be binded multiple times as long as the binding values are the same. Can you please try again?

*/

// floor(a / b)
auto fdtype = DataType::Float(dtype.bits() * 2, dtype.lanes());
Copy link
Member

@tqchen tqchen Jun 28, 2020

Choose a reason for hiding this comment

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

Let us also use the rules in the else branch instead (in case the target does not support floating pts but support integer arithmetics)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the logic in the most recent commit. Thanks for the suggestion!


// a - floor(a / b) * b
auto fdtype = DataType::Float(dtype.bits() * 2, dtype.lanes());
auto div = tir::Div(tir::Cast(fdtype, op->a), tir::Cast(fdtype, op->b));
Copy link
Member

@tqchen tqchen Jun 28, 2020

Choose a reason for hiding this comment

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

Let us also use the rules in the else branch instead (in case the target does not support floating pts but support integer arithmetics)

@tqchen tqchen merged commit a02d377 into apache:master Jul 28, 2020
@tqchen
Copy link
Member

tqchen commented Jul 28, 2020

Sorry for the long delay, Thanks @dpankratz ! This PR is now merged

trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
… tir.floordiv. Fixed Topi testcase. (apache#5666)

* Improved uncommon case of floormod and floordiv. Removed dependence on np floor_div and fmod.

* Fixed clang-format complaints

* Streamlined floormod and floordiv lowering logic

* Improved build times by expressing int64 case of tir FloorMod and FloorDiv using let nodes

* Updated use-def analysis and llvm codegen to support duplicated letnodes.

* Corrected misuse of var_map_ in llvm codegen

* Updated backends that support LetNode

* Changed floormod and div lowering logic to avoid using FP on systems that don't support it.

* Fixed formatting

Co-authored-by: pankratz <pankratz@ualberta.ca>
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
… tir.floordiv. Fixed Topi testcase. (apache#5666)

* Improved uncommon case of floormod and floordiv. Removed dependence on np floor_div and fmod.

* Fixed clang-format complaints

* Streamlined floormod and floordiv lowering logic

* Improved build times by expressing int64 case of tir FloorMod and FloorDiv using let nodes

* Updated use-def analysis and llvm codegen to support duplicated letnodes.

* Corrected misuse of var_map_ in llvm codegen

* Updated backends that support LetNode

* Changed floormod and div lowering logic to avoid using FP on systems that don't support it.

* Fixed formatting

Co-authored-by: pankratz <pankratz@ualberta.ca>
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
… tir.floordiv. Fixed Topi testcase. (apache#5666)

* Improved uncommon case of floormod and floordiv. Removed dependence on np floor_div and fmod.

* Fixed clang-format complaints

* Streamlined floormod and floordiv lowering logic

* Improved build times by expressing int64 case of tir FloorMod and FloorDiv using let nodes

* Updated use-def analysis and llvm codegen to support duplicated letnodes.

* Corrected misuse of var_map_ in llvm codegen

* Updated backends that support LetNode

* Changed floormod and div lowering logic to avoid using FP on systems that don't support it.

* Fixed formatting

Co-authored-by: pankratz <pankratz@ualberta.ca>
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Sep 2, 2020
… tir.floordiv. Fixed Topi testcase. (apache#5666)

* Improved uncommon case of floormod and floordiv. Removed dependence on np floor_div and fmod.

* Fixed clang-format complaints

* Streamlined floormod and floordiv lowering logic

* Improved build times by expressing int64 case of tir FloorMod and FloorDiv using let nodes

* Updated use-def analysis and llvm codegen to support duplicated letnodes.

* Corrected misuse of var_map_ in llvm codegen

* Updated backends that support LetNode

* Changed floormod and div lowering logic to avoid using FP on systems that don't support it.

* Fixed formatting

Co-authored-by: pankratz <pankratz@ualberta.ca>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Sep 3, 2020
… tir.floordiv. Fixed Topi testcase. (apache#5666)

* Improved uncommon case of floormod and floordiv. Removed dependence on np floor_div and fmod.

* Fixed clang-format complaints

* Streamlined floormod and floordiv lowering logic

* Improved build times by expressing int64 case of tir FloorMod and FloorDiv using let nodes

* Updated use-def analysis and llvm codegen to support duplicated letnodes.

* Corrected misuse of var_map_ in llvm codegen

* Updated backends that support LetNode

* Changed floormod and div lowering logic to avoid using FP on systems that don't support it.

* Fixed formatting

Co-authored-by: pankratz <pankratz@ualberta.ca>
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.

None yet

3 participants