-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[PIR] Refine conditional_block op translator #59723
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
Merged
zhangbo9674
merged 23 commits into
PaddlePaddle:develop
from
zhangbo9674:dev/reconstruct_if_translate
Dec 11, 2023
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6d1d3b3
refine
zhangbo9674 ed4eb36
fix
zhangbo9674 41d3326
add select_input
zhangbo9674 0ac12cc
fix
zhangbo9674 86ffb07
fix
zhangbo9674 639b601
fix
zhangbo9674 fb1d157
fix
zhangbo9674 368edfb
refine if_op without falseblock
zhangbo9674 0346fdd
fix bug
zhangbo9674 2ecd269
fix
zhangbo9674 14a7114
fix
zhangbo9674 29b9d25
fix
zhangbo9674 731bb65
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zhangbo9674 7b61ae0
fix
zhangbo9674 6e1127f
fix
zhangbo9674 48cb766
fix
zhangbo9674 5a6c28f
fix
zhangbo9674 a3349dc
fix
zhangbo9674 b172157
fix
zhangbo9674 59cedeb
fix
zhangbo9674 9ae3de0
fix conflict
zhangbo9674 1b37472
fix
zhangbo9674 66dd4d3
fix
zhangbo9674 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -27,15 +27,15 @@ class Value; | |
| class PirInterpreter; | ||
| class ValueExecutionInfo; | ||
|
|
||
| class CondInstruction : public InstructionBase { | ||
| class IfInstruction : public InstructionBase { | ||
| public: | ||
| CondInstruction(size_t id, | ||
| const platform::Place& place, | ||
| ::pir::Operation* op, | ||
| ValueExecutionInfo* value_exe_info, | ||
| const std::set<std::string>& skip_gc_vars); | ||
| IfInstruction(size_t id, | ||
| const platform::Place& place, | ||
| ::pir::Operation* op, | ||
| ValueExecutionInfo* value_exe_info, | ||
| const std::set<std::string>& skip_gc_vars); | ||
|
|
||
| ~CondInstruction(); | ||
| ~IfInstruction(); | ||
|
|
||
| void Run() override; | ||
|
|
||
|
|
@@ -53,15 +53,15 @@ class CondInstruction : public InstructionBase { | |
|
|
||
| ::pir::Operation* op_; | ||
|
|
||
| std::string cond_name_{"cond_instruction"}; | ||
| std::string cond_name_{"if_instruction"}; | ||
|
|
||
| Variable* cond_var_; | ||
|
|
||
| std::vector<Variable*> output_vars_; | ||
|
|
||
| PirInterpreter* true_branch_inter_; | ||
| PirInterpreter* true_branch_inter_ = nullptr; | ||
|
|
||
| PirInterpreter* false_branch_inter_; | ||
| PirInterpreter* false_branch_inter_ = nullptr; | ||
|
Comment on lines
+62
to
+64
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. 换用unique_ptr表明所有权更好吧 |
||
|
|
||
| std::vector<std::string> true_branch_outputs_; | ||
|
|
||
|
|
||
140 changes: 140 additions & 0 deletions
140
paddle/fluid/framework/new_executor/instruction/select_input_instruction.cc
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "paddle/fluid/framework/new_executor/instruction/select_input_instruction.h" | ||
| #include "paddle/fluid/framework/new_executor/instruction/instruction_util.h" | ||
| #include "paddle/fluid/framework/new_executor/new_executor_defs.h" | ||
| #include "paddle/fluid/framework/new_executor/pir_adaptor/pir_adaptor_util.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
|
|
||
| SelectInputInstruction::SelectInputInstruction( | ||
| size_t id, | ||
| const platform::Place &place, | ||
| ::pir::Operation *op, | ||
| ValueExecutionInfo *value_exe_info) | ||
| : InstructionBase(id, place), op_(op) { | ||
| VLOG(6) << "construct select_input instruction"; | ||
|
|
||
| std::unordered_map<pir::Value, std::vector<int>> inputs; | ||
| mask_ = value_exe_info->GetVarByValue(op->operand_source(0)); | ||
| inputs.emplace(op->operand_source(0), | ||
| GetValueIds(op->operand_source(0), *value_exe_info)); | ||
|
|
||
| for (size_t i = 1; i < op->num_operands(); ++i) { | ||
| inputs_.push_back(value_exe_info->GetVarByValue(op->operand_source(i))); | ||
| inputs.emplace(op->operand_source(i), | ||
| GetValueIds(op->operand_source(i), *value_exe_info)); | ||
| } | ||
| SetInputs(inputs); | ||
|
|
||
| std::unordered_map<pir::Value, std::vector<int>> outputs; | ||
| out_ = value_exe_info->GetVarByValue(op->result(0)); | ||
| outputs.emplace(op->result(0), GetValueIds(op->result(0), *value_exe_info)); | ||
| SetOutputs(outputs); | ||
| } | ||
|
|
||
| inline int GetBranchNumber(const phi::DenseTensor &mask) { | ||
| PADDLE_ENFORCE_EQ( | ||
| mask.numel(), | ||
| 1, | ||
| phi::errors::Fatal("The numel of Input(Mask) in SelectInputOp or " | ||
| "SelectOutputOp must be 1. " | ||
| "But received %d, and it's shape is [%s].", | ||
| mask.numel(), | ||
| mask.dims())); | ||
| if (platform::is_cpu_place(mask.place())) { | ||
| return mask.data<int>()[0]; | ||
| } | ||
| // when platform::is_gpu_place(mask.place()) is true | ||
| std::unique_ptr<phi::DenseTensor> cpu_mask{new phi::DenseTensor()}; | ||
| #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \ | ||
| defined(PADDLE_WITH_CUSTOM_DEVICE) || defined(PADDLE_WITH_XPU) | ||
| framework::TensorCopySync(mask, platform::CPUPlace(), cpu_mask.get()); | ||
| #else | ||
| PADDLE_THROW(phi::errors::Fatal( | ||
| "This version of PaddlePaddle does NOT support GPU, " | ||
| "but got GPU tensor 'Mask' in SelectInputOp or SelectOutputOp. " | ||
| "Please compile PaddlePaddle WITH_GPU first.")); | ||
| #endif | ||
| return cpu_mask->data<int>()[0]; | ||
| } | ||
|
|
||
| class AssignFunctor { | ||
| public: | ||
| explicit AssignFunctor(Variable *out) : out_(out) {} | ||
|
|
||
| void operator()(const phi::DenseTensor &lod_tensor) const { | ||
| auto &out_tensor = *out_->GetMutable<phi::DenseTensor>(); | ||
| copy_tensor(lod_tensor, &out_tensor); | ||
| } | ||
|
|
||
| void operator()(const phi::TensorArray &array) const { | ||
| auto &out_array = *out_->GetMutable<phi::TensorArray>(); | ||
| out_array.resize(array.size()); | ||
| for (size_t i = 0; i < array.size(); ++i) { | ||
| copy_tensor(array[i], &out_array[i]); | ||
| } | ||
| } | ||
|
|
||
| void operator()(const phi::SelectedRows &rows) const { | ||
| phi::SelectedRows &out_rows = *out_->GetMutable<phi::SelectedRows>(); | ||
| out_rows.set_rows(rows.rows()); | ||
| out_rows.set_height(rows.height()); | ||
| auto &t = rows.value(); | ||
| auto *m = out_rows.mutable_value(); | ||
| TensorCopy(t, t.place(), m); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void operator()(const T &v UNUSED) const { | ||
| PADDLE_ENFORCE_EQ( | ||
| true, | ||
| false, | ||
| platform::errors::PermissionDenied( | ||
| "Not support type for assign op with type %s", typeid(T).name())); | ||
| } | ||
|
|
||
| private: | ||
| void copy_tensor(const phi::DenseTensor &lod_tensor, | ||
| phi::DenseTensor *out) const { | ||
| if (!lod_tensor.IsInitialized()) return; | ||
|
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. 这里为什么直接return了,而不是Throw Error? |
||
| auto &out_tensor = *out; | ||
| TensorCopy(lod_tensor, lod_tensor.place(), &out_tensor); | ||
| out_tensor.set_lod(lod_tensor.lod()); | ||
| } | ||
|
|
||
| Variable *out_; | ||
| }; | ||
|
|
||
| void SelectInputInstruction::Run() { | ||
| VLOG(6) << "run select_input instruction"; | ||
| auto &mask = mask_->Get<phi::DenseTensor>(); | ||
| size_t output_branch = static_cast<size_t>(GetBranchNumber(mask)); | ||
| PADDLE_ENFORCE_LT( | ||
| output_branch, | ||
| inputs_.size(), | ||
| phi::errors::Fatal( | ||
| "Input 'Mask' in SelectInputOp is invalid. " | ||
| "'Mask' must be less than the size of input vector 'X'. " | ||
| "But received Mask = %d, X's size = %d.", | ||
| output_branch, | ||
| inputs_.size())); | ||
| Variable *selected = inputs_[output_branch]; | ||
| VisitVarType(*selected, AssignFunctor(out_)); | ||
| } | ||
|
|
||
| } // namespace framework | ||
| } // namespace paddle | ||
52 changes: 52 additions & 0 deletions
52
paddle/fluid/framework/new_executor/instruction/select_input_instruction.h
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include "paddle/fluid/framework/new_executor/instruction/instruction_base.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
| class ValueExecutionInfo; | ||
|
|
||
| class SelectInputInstruction : public InstructionBase { | ||
| public: | ||
| SelectInputInstruction(size_t id, | ||
| const platform::Place& place, | ||
| ::pir::Operation* op, | ||
| ValueExecutionInfo* value_exe_info); | ||
|
|
||
| void Run() override; | ||
|
|
||
| const std::string& Name() const override { return name_; } | ||
winter-wang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ::pir::Operation* Operation() const override { return op_; } | ||
winter-wang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private: | ||
| ::pir::Operation* op_; | ||
|
|
||
| OpFuncType type_; | ||
|
|
||
| std::string name_{"pd_op.select_input"}; | ||
|
|
||
| Variable* mask_; // not owned | ||
|
|
||
| std::vector<Variable*> inputs_; // not owned | ||
|
|
||
| Variable* out_; // not owned | ||
| }; | ||
|
|
||
| } // namespace framework | ||
| } // namespace paddle | ||
This file contains hidden or 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 hidden or 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.
建议改用make_unique