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

[PIR]Support while op #57937

Merged
merged 15 commits into from
Oct 13, 2023
Merged

[PIR]Support while op #57937

merged 15 commits into from
Oct 13, 2023

Conversation

phlrain
Copy link
Collaborator

@phlrain phlrain commented Oct 9, 2023

PR types

New features

PR changes

Others

Description

支持最简单的while op cpu场景跑通
i = 0 while( i <= 10) i += 1

Pcard-67164

@paddle-bot
Copy link

paddle-bot bot commented Oct 9, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot
Copy link

paddle-bot bot commented Oct 9, 2023

❌ The PR is not created using PR's template. You can refer to this Demo.
Please use PR's template, it helps save our maintainers' time so that more developers get helped.

@phlrain phlrain changed the title Support while op [PIR]Support while op Oct 11, 2023
@@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

yield一定是block的最后一个算子。

Copy link
Contributor

@winter-wang winter-wang Oct 11, 2023

Choose a reason for hiding this comment

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

Suggested change
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));
}
}

Copy link
Collaborator Author

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)) {
Copy link
Contributor

@winter-wang winter-wang Oct 11, 2023

Choose a reason for hiding this comment

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

Suggested change
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));
}
}

Copy link
Collaborator Author

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
Copy link
Contributor

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之说。这儿应该是写错了吧

Copy link
Collaborator Author

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 =
Copy link
Contributor

Choose a reason for hiding this comment

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

如果body_block一次都没有执行的话,这儿的拷贝是否安全?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

是不安全的,这个地方需要适配下

winter-wang

This comment was marked as outdated.

std::vector<Variable*> while_op_inputs_;
std::vector<Variable*> while_op_outputs_;

NewIRInterpreter* cond_inter_;
Copy link
Contributor

Choose a reason for hiding this comment

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

析构函数没有对这两个指针delete,会存在内存泄露问题

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

winter-wang
winter-wang previously approved these changes Oct 11, 2023
Copy link
Contributor

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -190,5 +191,76 @@ OpFuncType AnalyseOpFuncType(pir::Operation* op, const platform::Place& place) {
return OpFuncType::kGpuAsync;
}

std::vector<pir::Value> GetCondYiedOpInputs(pir::Block* block) {
Copy link
Contributor

@winter-wang winter-wang Oct 13, 2023

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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_);
Copy link
Contributor

Choose a reason for hiding this comment

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

这个cond_block的输出拷贝是不是已经不需要了啊?

Copy link
Contributor

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@Aurelius84 Aurelius84 left a 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];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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_; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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"};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
std::string cond_name_{"while_instruction"};
std::string name_{"while_instruction"};


Variable* cond_var;

std::vector<Variable*> while_op_inputs_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (op_item->name() == "cf.yield" || op_item->name() == "cf.cond_yield") {
if (op_item->isa<YieldOp>() || op_item->isa<CondYieldOp>()) {

这里的判断建议解耦固定的string

@phlrain phlrain merged commit 3bed3dc into PaddlePaddle:develop Oct 13, 2023
28 checks passed
Frida-a pushed a commit to Frida-a/Paddle that referenced this pull request Oct 14, 2023
* [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 <1030748926@qq.com>
jiahy0825 pushed a commit to jiahy0825/Paddle that referenced this pull request Oct 16, 2023
* [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 <1030748926@qq.com>
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
* [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 <1030748926@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants