-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add CanProveDivisible for symbolic calculation #60572
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,85 @@ 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; | ||
|
|
||
| std::vector<ir::Expr> ops{}; | ||
| bool res = false; | ||
| ir::Expr zero(0); | ||
| bool is_positive = ProveGT(lhs, zero).value_or(false); | ||
| lhs_copy = (is_positive ? 1 : -1) * lhs; | ||
| bool is_ge = ProveGE(lhs_copy, rhs).value_or(false); | ||
|
|
||
| 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 = res && this->ProveDivisible(expr, rhs).value_or(false); | ||
| }); | ||
| res = 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 = res || this->ProveDivisible(expr, rhs).value_or(false); | ||
| }); | ||
| res = res && is_ge; | ||
| return res; | ||
| case cinn::ir::IrNodeTy::FracOp: | ||
| return ProveDivisible(lhs.As<ir::FracOp>()->a(), rhs).value_or(false) && | ||
| is_ge; | ||
| case cinn::ir::IrNodeTy::FloatImm: | ||
| return false; | ||
| case cinn::ir::IrNodeTy::Add: | ||
| return ProveDivisible(lhs.As<ir::Add>()->a(), rhs).value_or(false) && | ||
| ProveDivisible(lhs.As<ir::Add>()->b(), rhs).value_or(false) && | ||
| is_ge; | ||
| case cinn::ir::IrNodeTy::Sub: | ||
| return ProveDivisible(lhs.As<ir::Sub>()->a(), rhs).value_or(false) && | ||
| ProveDivisible(lhs.As<ir::Sub>()->b(), rhs).value_or(false) && | ||
| is_ge; | ||
| case cinn::ir::IrNodeTy::Div: | ||
| return ProveDivisible(lhs.As<ir::Div>()->a(), rhs).value_or(false) && | ||
|
||
| is_ge; | ||
| case cinn::ir::IrNodeTy::Mul: | ||
| return (ProveDivisible(lhs.As<ir::Mul>()->a(), rhs).value_or(false) || | ||
| ProveDivisible(lhs.As<ir::Mul>()->b(), rhs).value_or(false)) && | ||
| is_ge; | ||
| case cinn::ir::IrNodeTy::Mod: | ||
| return false; | ||
| case cinn::ir::IrNodeTy::Minus: | ||
| return ProveDivisible(lhs.As<ir::Minus>()->v(), rhs).value_or(false); | ||
| default: | ||
| LOG(FATAL) << "Not supported yet!"; | ||
|
Contributor
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 +259,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 +330,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_; | ||
|
|
||
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