-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add CanProveDivisible for symbolic calculation #60572
Merged
Courtesy-Xs
merged 3 commits into
PaddlePaddle:develop
from
Courtesy-Xs:cinn_add_canprovedivisible
Jan 6, 2024
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
// limitations under the License. | ||
|
||
#include "paddle/cinn/common/integer_set.h" | ||
|
||
#include "paddle/cinn/common/arithmatic.h" | ||
#include "paddle/cinn/ir/ir_mutator.h" | ||
#include "paddle/cinn/ir/op/ir_operators.h" | ||
#include "paddle/cinn/ir/utils/ir_copy.h" | ||
|
@@ -164,11 +166,115 @@ std::optional<bool> SymbolicExprAnalyzer::ProveLT(const ir::Expr& lhs, | |
return ProveGT(rhs, lhs); | ||
} | ||
|
||
// Tell whether lhs can be divisible by rhs, lhs must be a pure math expression | ||
// and rhs must be a var | ||
std::optional<bool> SymbolicExprAnalyzer::ProveDivisible( | ||
const ir::Expr& lhs, const ir::Expr& rhs) const { | ||
CHECK(rhs.is_var()) << "Rhs in ProveDivisible must be a var temporarily!\n"; | ||
CHECK(lhs.defined()); | ||
CHECK(rhs.defined()); | ||
CHECK(cinn::common::IsPureMath(lhs)); | ||
|
||
ir::Expr lhs_copy = ir::ir_utils::IRCopy(lhs); | ||
if (cinn::common::is_zero(lhs_copy)) return true; | ||
|
||
auto OptionalAnd = [](const std::optional<bool>& lhs, | ||
const std::optional<bool>& rhs) -> std::optional<bool> { | ||
if (lhs.has_value() && rhs.has_value()) { | ||
return lhs.value() && rhs.value(); | ||
} else { | ||
return std::nullopt; | ||
} | ||
}; | ||
auto OptionalOr = [](const std::optional<bool>& lhs, | ||
const std::optional<bool>& rhs) -> std::optional<bool> { | ||
if (lhs.has_value() && rhs.has_value()) { | ||
return lhs.value() || rhs.value(); | ||
} else if ((!lhs.has_value()) && (!rhs.has_value())) { | ||
return std::nullopt; | ||
} else if (lhs.has_value() && (!rhs.has_value())) { | ||
return lhs.value() ? std::optional<bool>(lhs.value()) | ||
: std::optional<bool>(std::nullopt); | ||
} else { | ||
return rhs.value() ? std::optional<bool>(rhs.value()) | ||
: std::optional<bool>(std::nullopt); | ||
} | ||
}; | ||
|
||
std::vector<ir::Expr> ops{}; | ||
std::optional<bool> res = std::nullopt; | ||
ir::Expr zero(0); | ||
ir::Expr tmp_expr; | ||
|
||
auto is_ge = ProveGE(lhs, rhs); | ||
|
||
switch (lhs.node_type()) { | ||
case cinn::ir::IrNodeTy::_Var_: | ||
return ProveEQ(lhs, rhs); | ||
case cinn::ir::IrNodeTy::IntImm: | ||
return false; | ||
case cinn::ir::IrNodeTy::Sum: | ||
res = true; | ||
ops = lhs.As<ir::Sum>()->operands(); | ||
CHECK(!ops.empty()); | ||
std::for_each(ops.begin(), ops.end(), [&](const ir::Expr& expr) { | ||
res = OptionalAnd(res, this->ProveDivisible(expr, rhs)); | ||
}); | ||
res = OptionalAnd(res, is_ge); | ||
return res; | ||
case cinn::ir::IrNodeTy::Product: | ||
res = false; | ||
ops = lhs.As<ir::Product>()->operands(); | ||
CHECK(!ops.empty()); | ||
std::for_each(ops.begin(), ops.end(), [&](const ir::Expr& expr) { | ||
res = OptionalOr(res, this->ProveDivisible(expr, rhs)); | ||
if (res.has_value() && res.value()) return; | ||
}); | ||
res = OptionalAnd(res, is_ge); | ||
return res; | ||
case cinn::ir::IrNodeTy::FracOp: | ||
tmp_expr = cinn::common::AutoSimplify(lhs); | ||
if (tmp_expr.node_type() == cinn::ir::IrNodeTy::FracOp) | ||
return std::nullopt; | ||
return OptionalAnd(ProveDivisible(tmp_expr, rhs), is_ge); | ||
case cinn::ir::IrNodeTy::FloatImm: | ||
return false; | ||
case cinn::ir::IrNodeTy::Add: | ||
return OptionalAnd( | ||
OptionalAnd(ProveDivisible(lhs.As<ir::Add>()->a(), rhs), | ||
ProveDivisible(lhs.As<ir::Add>()->b(), rhs)), | ||
is_ge); | ||
case cinn::ir::IrNodeTy::Sub: | ||
return OptionalAnd( | ||
OptionalAnd(ProveDivisible(lhs.As<ir::Sub>()->a(), rhs), | ||
ProveDivisible(lhs.As<ir::Sub>()->b(), rhs)), | ||
is_ge); | ||
case cinn::ir::IrNodeTy::Div: | ||
tmp_expr = cinn::common::AutoSimplify(lhs); | ||
if (tmp_expr.node_type() == cinn::ir::IrNodeTy::Div) return std::nullopt; | ||
return OptionalAnd(ProveDivisible(tmp_expr, rhs), is_ge); | ||
case cinn::ir::IrNodeTy::Mul: | ||
return OptionalAnd( | ||
OptionalOr(ProveDivisible(lhs.As<ir::Mul>()->a(), rhs), | ||
ProveDivisible(lhs.As<ir::Mul>()->b(), rhs)), | ||
is_ge); | ||
case cinn::ir::IrNodeTy::Mod: | ||
return false; | ||
case cinn::ir::IrNodeTy::Minus: | ||
return ProveDivisible(lhs.As<ir::Minus>()->v(), rhs); | ||
default: | ||
LOG(FATAL) << "Not supported yet!"; | ||
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. 我一直对这些地方的完备性担忧。 |
||
break; | ||
} | ||
} | ||
|
||
class BoundReplacer : public ir::IRMutator<> { | ||
public: | ||
explicit BoundReplacer(const cas_intervals_t& var_intervals, | ||
bool is_lower_bound) | ||
: var_intervals_(var_intervals), sign_(is_lower_bound) {} | ||
: var_intervals_(var_intervals), | ||
sign_(is_lower_bound), | ||
var_visited_({}) {} | ||
|
||
void operator()(ir::Expr* expr) { IRMutator::Visit(expr, expr); } | ||
|
||
|
@@ -183,10 +289,16 @@ class BoundReplacer : public ir::IRMutator<> { | |
upper_bound = | ||
interval.e_r.defined() ? interval.e_r : ir::Expr(interval.r); | ||
} | ||
if (sign_) { | ||
*op = ir::ir_utils::IRCopy(lower_bound); | ||
if (!var_visited_.count(var->name)) { | ||
if (sign_) { | ||
*op = ir::ir_utils::IRCopy(lower_bound); | ||
var_visited_.insert({var->name, lower_bound}); | ||
} else { | ||
*op = ir::ir_utils::IRCopy(upper_bound); | ||
var_visited_.insert({var->name, upper_bound}); | ||
} | ||
} else { | ||
*op = ir::ir_utils::IRCopy(upper_bound); | ||
*op = ir::ir_utils::IRCopy(var_visited_.at(var->name)); | ||
} | ||
} | ||
|
||
|
@@ -248,6 +360,7 @@ class BoundReplacer : public ir::IRMutator<> { | |
|
||
private: | ||
const cas_intervals_t& var_intervals_; | ||
std::unordered_map<std::string, ir::Expr> var_visited_; | ||
// Determine replacing with upper or lower bound, | ||
// True means lower bound and False means upper bound. | ||
bool sign_; | ||
|
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
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.
判断不了或暂时没有实现的情况返回nullopt
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.
Done