-
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] Bound for Shape Variables #4486
Closed
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2a43573
add AssertLowerBound
yzhliu eb63b3a
add AssertLowerBound to ir_deep_compare.cc
yzhliu d1d3047
Make Range for AsserLowerBound; add support in codegen
yzhliu f3e3305
Expr bound
yzhliu d801459
use intrinsic assert_bound
yzhliu 105a077
simplify nested assert_bound, deal with assert_bound in InternalVal
yzhliu 81386e2
add test case for assert_bound rewrite simplify
yzhliu 55bd45e
fix deduce bound accordingly
yzhliu c405d7b
fix floordiv IntervalSetEvaluator when b \in [0, n]
yzhliu 92e319b
merge upstream
yzhliu e3639b9
fix lint
yzhliu 9c94b5d
fix compile error
yzhliu 38fb4d3
add assert_bound in hybrid script
yzhliu d078c83
fix lint
yzhliu ac26b27
fix auto buffer bind for assert_bound
yzhliu fe4975c
Merge remote-tracking branch 'upstream/master' into assert_bound_expr
yzhliu 35d71b8
debug ci
yzhliu 82adeab
revoke
yzhliu cfecf9d
retrigger
yzhliu 10bc349
fix out of bound in path_ visit
yzhliu 68ae224
fix test_any.py
yzhliu 3fd47d4
Merge remote-tracking branch 'upstream/master' into assert_bound_expr
yzhliu 8d16054
fix lint
yzhliu 4f31cce
fix gpu unittest
yzhliu 5277e81
merge from upstream
yzhliu 3d15b40
polish bound deducer
yzhliu 768b95b
fix build
yzhliu a905f1f
fix bound remover
yzhliu 894681d
Merge remote-tracking branch 'upstream/master' into assert_bound_expr
yzhliu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,41 @@ std::vector<const Object*> GetPath(Expr target, Expr expr) { | |
return v.path_; | ||
} | ||
|
||
class BoundRemover : public IRMutator { | ||
public: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE, we are in progress of updating IRMutators to new style, would be great if we can directly change it here related #4607 |
||
Expr Remove(Expr e) { | ||
remove_bounded_ = true; | ||
return IRMutator::Mutate(ir::Simplify(e)); | ||
} | ||
|
||
Expr Reset(Expr e) { | ||
remove_bounded_ = false; | ||
return IRMutator::Mutate(e); | ||
} | ||
|
||
Expr Mutate_(const Call* op, const Expr& e) final { | ||
if (op->is_intrinsic(intrinsic::tvm_assert_bound) && remove_bounded_) { | ||
Expr value = op->args[0]; | ||
const Variable* var = value.as<Variable>(); | ||
CHECK(var) << "Invalid value in " << e << ". It should have been simplified."; | ||
bounded_var_map_[var] = GetRef<Expr>(op); | ||
return value; | ||
} | ||
return IRMutator::Mutate_(op, e); | ||
} | ||
|
||
Expr Mutate_(const Variable* op, const Expr& e) final { | ||
if (!remove_bounded_ && bounded_var_map_.count(op)) { | ||
return bounded_var_map_[op]; | ||
} | ||
return e; | ||
} | ||
|
||
private: | ||
bool remove_bounded_ = false; | ||
std::unordered_map<const Variable*, Expr> bounded_var_map_; | ||
}; | ||
|
||
enum CompareOp {kGreater, kLess, kEqual}; | ||
|
||
// a visitor to deduce the bound of a variable from a expression | ||
|
@@ -84,7 +119,7 @@ class BoundDeducer: public IRVisitor { | |
|
||
void Visit(const ObjectRef& e) final { | ||
if (!success_) return; | ||
if (e.get() == path_[iter_++]) { | ||
if (iter_ < path_.size() && e.get() == path_[iter_++]) { | ||
IRVisitor::Visit(e); | ||
} else { | ||
success_ = false; | ||
|
@@ -295,6 +330,18 @@ void BoundDeducer::Transform() { | |
void BoundDeducer::Deduce() { | ||
Init(); | ||
if (!success_) return; | ||
|
||
// Any variable appears in both expr and result, | ||
// they should not be eagerly simplified according to its bound | ||
// e.g., i + n/4 >= n | ||
// => i >= n - n/4 | ||
// If we eagerly simplified the left side given assert_bound(n, 0, +inf) | ||
// we would get i + 0 >= n => i >= n, which is obviously incorrect. | ||
// Thus we remove assert_bound here and reset later. | ||
BoundRemover bound_remover; | ||
expr_ = bound_remover.Remove(expr_); | ||
result_ = bound_remover.Remove(result_); | ||
|
||
Relax(); | ||
if (!success_) return; | ||
// get the path | ||
|
@@ -306,6 +353,9 @@ void BoundDeducer::Deduce() { | |
expr_map_ = EvalSetForEachSubExpr(expr_, hint_map_); | ||
|
||
Visit(expr_); | ||
|
||
expr_ = bound_remover.Reset(expr_); | ||
result_ = bound_remover.Reset(result_); | ||
} | ||
|
||
void BoundDeducer::Relax() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
rename -> is_var_or_assert_bound