-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[PIR] Support translate conditional_block_grad op #60809
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 13 commits into
PaddlePaddle:develop
from
zhangbo9674:dev/support_pt_if_grad
Jan 22, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
34b3e6c
change data to full
zhangbo9674 add2717
fix
zhangbo9674 d64af96
add select_output op define
zhangbo9674 99c4e32
fix
zhangbo9674 686c997
fix
zhangbo9674 ed9d679
fix
zhangbo9674 484569c
fix
zhangbo9674 bc33603
fix
zhangbo9674 4fad3b4
fix
zhangbo9674 832f9a4
fix
zhangbo9674 436f9c8
fix
zhangbo9674 d858374
fix
zhangbo9674 5de39f1
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
139 changes: 139 additions & 0 deletions
139
paddle/fluid/framework/new_executor/instruction/control_flow/select_output_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,139 @@ | ||
| // 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/control_flow/select_output_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 { | ||
|
|
||
| SelectOutputInstruction::SelectOutputInstruction( | ||
| size_t id, | ||
| const platform::Place &place, | ||
| ::pir::Operation *op, | ||
| ValueExecutionInfo *value_exe_info) | ||
| : InstructionBase(id, place), op_(op) { | ||
| VLOG(6) << "construct select_output 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)); | ||
| input_ = value_exe_info->GetVarByValue(op->operand_source(1)); | ||
| inputs.emplace(op->operand_source(1), | ||
| GetValueIds(op->operand_source(1), *value_exe_info)); | ||
| SetInputs(inputs); | ||
|
|
||
| std::unordered_map<pir::Value, std::vector<int>> outputs; | ||
| for (size_t i = 0; i < op->num_results(); ++i) { | ||
| outputs_.push_back(value_exe_info->GetVarByValue(op->result(i))); | ||
| outputs.emplace(op->result(i), GetValueIds(op->result(i), *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; | ||
| auto &out_tensor = *out; | ||
| TensorCopy(lod_tensor, lod_tensor.place(), &out_tensor); | ||
| out_tensor.set_lod(lod_tensor.lod()); | ||
| } | ||
|
|
||
| Variable *out_; | ||
| }; | ||
|
|
||
| void SelectOutputInstruction::Run() { | ||
| VLOG(6) << "run select_output instruction"; | ||
| auto &mask = mask_->Get<phi::DenseTensor>(); | ||
| size_t output_branch = static_cast<size_t>(GetBranchNumber(mask)); | ||
| PADDLE_ENFORCE_LE( | ||
| output_branch, | ||
| outputs_.size(), | ||
| phi::errors::Fatal( | ||
| "Input 'Mask' in SelectInputOp is invalid. " | ||
| "'Mask' must be less than the size of output vector 'X'. " | ||
| "But received Mask = %d, Out's size = %d.", | ||
| output_branch, | ||
| outputs_.size())); | ||
| Variable *selected = outputs_[output_branch]; | ||
| VisitVarType(*input_, AssignFunctor(selected)); | ||
| } | ||
|
|
||
| } // namespace framework | ||
| } // namespace paddle |
52 changes: 52 additions & 0 deletions
52
paddle/fluid/framework/new_executor/instruction/control_flow/select_output_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 SelectOutputInstruction : public InstructionBase { | ||
| public: | ||
| SelectOutputInstruction(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_; } | ||
|
|
||
| ::pir::Operation* Operation() const override { return op_; } | ||
|
|
||
| private: | ||
| ::pir::Operation* op_; | ||
|
|
||
| OpFuncType type_; | ||
|
|
||
| std::string name_{"pd_op.select_output"}; | ||
|
|
||
| Variable* mask_; // not owned | ||
|
|
||
| Variable* input_; // not owned | ||
|
|
||
| std::vector<Variable*> outputs_; // 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.
这个看起来没必要做特殊转换?
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.
目前仅支持输出个数为2的 select_output,所以暂时写了一个特殊转换
IR_ENFORCE(Out_names.size() == 2, "Expected SelectOutput's output size is 2.");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.
如果只是为了Enforce一下的话是不是先Enforce下,再直接调基类方法就可以了?
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.
SelectOuput 旧算子体系下,其输出 Out 是含有多个元素的 list,新算子定义体系下,我们定义 SelectOuput 有两个输出,在旧算子某个输出多个元素的场景下,GenerateOperationOutput 仅支持了输出是 VectorType 或者只取多个元素的第一个元素。