forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PIR] Add yield instruction (PaddlePaddle#64234)
* add yield_instruction * rm CopyBranchOutput * fix while_instruction
- Loading branch information
Showing
11 changed files
with
140 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
77 changes: 77 additions & 0 deletions
77
paddle/fluid/framework/new_executor/instruction/control_flow/yield_instruction.cc
This file contains 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,77 @@ | ||
// Copyright (c) 2024 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/yield_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" | ||
#include "paddle/fluid/pir/dialect/operator/ir/control_flow_op.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
YieldInstruction::YieldInstruction(size_t id, | ||
const platform::Place &place, | ||
::pir::Operation *op, | ||
ValueExecutionInfo *value_exe_info) | ||
: InstructionBase(id, place), op_(op) { | ||
VLOG(6) << "construct yield instruction"; | ||
|
||
auto parent_op = op->GetParentOp(); | ||
|
||
std::unordered_map<pir::Value, std::vector<int>> inputs; | ||
for (size_t i = 0; i < op->num_operands(); ++i) { | ||
// Skip the first input (cond) when the parent op is a while op. | ||
if (parent_op->isa<paddle::dialect::WhileOp>() && i == 0) { | ||
continue; | ||
} | ||
auto in = op->operand_source(i); | ||
inputs.emplace(in, GetValueIds(in, *value_exe_info)); | ||
input_vars_.push_back(value_exe_info->GetVarByValue(in)); | ||
} | ||
SetInputs(inputs); | ||
|
||
for (size_t i = 0; i < parent_op->num_results(); ++i) { | ||
output_vars_.push_back(value_exe_info->GetVarByValue(parent_op->result(i))); | ||
} | ||
|
||
PADDLE_ENFORCE_EQ( | ||
input_vars_.size(), | ||
output_vars_.size(), | ||
phi::errors::InvalidArgument("The number of inputs in YieldOp and " | ||
"outputs of parent op must be equal." | ||
"But received %d and %d.", | ||
input_vars_.size(), | ||
output_vars_.size())); | ||
} | ||
|
||
void YieldInstruction::Run() { | ||
for (size_t i = 0; i < input_vars_.size(); ++i) { | ||
if (input_vars_[i]->IsType<phi::DenseTensor>()) { | ||
output_vars_[i]->GetMutable<phi::DenseTensor>()->ShareDataWith( | ||
input_vars_[i]->Get<phi::DenseTensor>()); | ||
} else if (input_vars_[i]->IsType<phi::TensorArray>()) { | ||
const auto &inner_array = input_vars_[i]->Get<phi::TensorArray>(); | ||
auto *output_array = output_vars_[i]->GetMutable<phi::TensorArray>(); | ||
*output_array = inner_array; | ||
} else { | ||
PADDLE_THROW(phi::errors::Unimplemented("unsupported type %d", | ||
input_vars_[i]->Type())); | ||
} | ||
} | ||
} | ||
|
||
} // namespace framework | ||
} // namespace paddle |
47 changes: 47 additions & 0 deletions
47
paddle/fluid/framework/new_executor/instruction/control_flow/yield_instruction.h
This file contains 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,47 @@ | ||
// Copyright (c) 2024 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 "paddle/fluid/framework/new_executor/instruction/instruction_base.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
class ValueExecutionInfo; | ||
|
||
class YieldInstruction : public InstructionBase { | ||
public: | ||
YieldInstruction(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_; | ||
|
||
std::string name_{"yield_instruction"}; | ||
|
||
std::vector<Variable*> input_vars_; | ||
|
||
std::vector<Variable*> output_vars_; | ||
}; | ||
|
||
} // namespace framework | ||
} // namespace paddle |
This file contains 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 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.