-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[PIR]Support while op #57937
[PIR]Support while op #57937
Conversation
…e/Paddle into support_while_op
… support_while_op
你的PR提交成功,感谢你对开源项目的贡献! |
❌ The PR is not created using PR's template. You can refer to this Demo. |
… support_while_op
… support_while_op
… support_while_op
@@ -190,5 +190,76 @@ OpFuncType AnalyseOpFuncType(pir::Operation* op, const platform::Place& place) { | |||
return OpFuncType::kGpuAsync; | |||
} | |||
|
|||
std::vector<pir::Value> GetCondYiedOpInputs(pir::Block* block) { | |||
std::vector<pir::Value> vec_res; | |||
for (auto op : (*block)) { |
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.
yield一定是block的最后一个算子。
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.
for (auto op : (*block)) { | |
if (block && !block->empty() && block->back()->isa<pir::CondYieldOp>()) { | |
auto *op = block->back(); | |
for (size_t i = 0; i < op->num_operands(); ++i) { | |
vec_res.emplace_back(op->operand_source(i)); | |
} | |
} |
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
|
||
std::vector<pir::Value> GetYiedOpInputs(pir::Block* block) { | ||
std::vector<pir::Value> vec_res; | ||
for (auto op : (*block)) { |
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.
for (auto op : (*block)) { | |
if (block && !block->empty() && block->back()->isa<pir::YieldOp>()) { | |
auto *op = block->back(); | |
for (size_t i = 0; i < op->num_operands(); ++i) { | |
vec_res.emplace_back(op->operand_source(i)); | |
} | |
} |
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
} | ||
body_inter_->SetSkipGcVars(body_skip_gc_names_set); | ||
|
||
// the true branch and false branch input will be the if_op inputs |
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.
while里面没有true branch和false branch之说。这儿应该是写错了吧
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
|
||
void WhileInstruction::CopyStepOutput() { | ||
for (size_t i = 0; i < body_skip_gc_names_.size(); ++i) { | ||
auto* inner_var = |
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.
如果body_block一次都没有执行的话,这儿的拷贝是否安全?
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.
是不安全的,这个地方需要适配下
std::vector<Variable*> while_op_inputs_; | ||
std::vector<Variable*> while_op_outputs_; | ||
|
||
NewIRInterpreter* cond_inter_; |
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.
析构函数没有对这两个指针delete,会存在内存泄露问题
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
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.
LGTM
… support_while_op
… support_while_op
… support_while_op
@@ -190,5 +191,76 @@ OpFuncType AnalyseOpFuncType(pir::Operation* op, const platform::Place& place) { | |||
return OpFuncType::kGpuAsync; | |||
} | |||
|
|||
std::vector<pir::Value> GetCondYiedOpInputs(pir::Block* block) { |
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.
CondYieldOp已经被我删了。 现在cond_block的最后一个算子也是YieldOp, 且只Yield一个布尔值。这个函数应该只返回一个Value就可以吧。没必要返回 vector了
@@ -190,5 +191,76 @@ OpFuncType AnalyseOpFuncType(pir::Operation* op, const platform::Place& place) { | |||
return OpFuncType::kGpuAsync; | |||
} | |||
|
|||
std::vector<pir::Value> GetCondYiedOpInputs(pir::Block* block) { |
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.
std::vector<pir::Value> GetCondYiedOpInputs(pir::Block* block) { | |
pir::Value GetCondYiedOpInputs(pir::Block* block) { | |
// 可以用paddle_enforce判断一下block非空,且最后一个op是YieldOp, 且只有一个输入,且为bool类型。 | |
return block->back()->operand_source(0); | |
} |
if (cond_var->Get<phi::DenseTensor>().data<bool>()[0]) { | ||
body_inter_->Run({}, false); | ||
|
||
CopyStepOutputToBlockArgs(cond_inter_.get(), cond_block_); |
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.
这个cond_block的输出拷贝是不是已经不需要了啊?
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.
LGTM
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.
LGTM overall, comment 可以单独提PR fix
void WhileInstruction::CopyStepOutputToBlockArgs(const NewIRInterpreter* inter, | ||
::pir::Block* block) { | ||
for (size_t i = 0; i < block->args_size(); ++i) { | ||
auto out_var_name = body_skip_gc_names_[i]; |
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.
auto out_var_name = body_skip_gc_names_[i]; | |
auto& out_var_name = body_skip_gc_names_[i]; |
|
||
void Run() override; | ||
|
||
const std::string& Name() const override { return cond_name_; } |
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.
const std::string& Name() const override { return cond_name_; } | |
const std::string& Name() const override { return name_; } |
void CopyStepOutputToBlockArgs(const NewIRInterpreter* inter, | ||
::pir::Block* block); | ||
|
||
std::string cond_name_{"while_instruction"}; |
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.
std::string cond_name_{"while_instruction"}; | |
std::string name_{"while_instruction"}; |
|
||
Variable* cond_var; | ||
|
||
std::vector<Variable*> while_op_inputs_; |
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.
std::vector<Variable*> while_op_inputs_; | |
std::vector<Variable*> inputs_; |
Variable* cond_var; | ||
|
||
std::vector<Variable*> while_op_inputs_; | ||
std::vector<Variable*> while_op_outputs_; |
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.
std::vector<Variable*> while_op_outputs_; | |
std::vector<Variable*> outputs_; |
const pir::Value cur_in, | ||
const std::unordered_map<pir::Value, pir::OpResult>& map_value_pair, | ||
const std::unordered_map<pir::Value, pir::Value>& map_value_pair, | ||
const int index, | ||
const std::string op_name) { |
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.
const std::string op_name) { | |
const std::string& op_name) { |
@@ -863,7 +935,7 @@ void HandleForSpecialOp( | |||
} | |||
} | |||
|
|||
if (op_item->name() == "cf.yield") { | |||
if (op_item->name() == "cf.yield" || op_item->name() == "cf.cond_yield") { |
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.
if (op_item->name() == "cf.yield" || op_item->name() == "cf.cond_yield") { | |
if (op_item->isa<YieldOp>() || op_item->isa<CondYieldOp>()) { |
这里的判断建议解耦固定的string
* [PIR] add print function for pd_op.while * support while op lowering * update * update * update * fix bug * remove useless code --------- Co-authored-by: winter-wang <[email protected]>
* [PIR] add print function for pd_op.while * support while op lowering * update * update * update * fix bug * remove useless code --------- Co-authored-by: winter-wang <[email protected]>
* [PIR] add print function for pd_op.while * support while op lowering * update * update * update * fix bug * remove useless code --------- Co-authored-by: winter-wang <[email protected]>
PR types
New features
PR changes
Others
Description
支持最简单的while op cpu场景跑通
i = 0 while( i <= 10) i += 1
Pcard-67164