- Notifications
You must be signed in to change notification settings - Fork 5.9k
[Fleet Executor] Construct runtime graph #37158
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
Merged
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
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
221 changes: 221 additions & 0 deletions 221 paddle/fluid/distributed/fleet_executor/runtime_graph.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,221 @@ | ||
| // Copyright (c) 2021 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/distributed/fleet_executor/runtime_graph.h" | ||
| #include "paddle/fluid/distributed/fleet_executor/task_node.h" | ||
| #include "paddle/fluid/framework/op_registry.h" | ||
| #include "paddle/fluid/framework/operator.h" | ||
| #include "paddle/fluid/framework/program_desc.h" | ||
| | ||
| namespace paddle { | ||
| namespace distributed { | ||
| namespace { | ||
| | ||
| using OperatorBase = RuntimeGraph::OperatorBase; | ||
| using OpRole = paddle::framework::OpRole; | ||
| using OpRegistry = paddle::framework::OpRegistry; | ||
| using ProgramDesc = paddle::framework::ProgramDesc; | ||
| | ||
| bool IsForward(int64_t op_role) { | ||
| return (op_role == static_cast<int64_t>(OpRole::kForward)) || | ||
| (op_role == (static_cast<int64_t>(OpRole::kForward) | | ||
| static_cast<int64_t>(OpRole::kLoss))); | ||
| } | ||
| | ||
| bool IsLRSched(int64_t op_role) { | ||
| return op_role == static_cast<int64_t>(OpRole::kLRSched); | ||
| } | ||
| | ||
| bool IsBackward(int64_t op_role) { | ||
| return (op_role == static_cast<int64_t>(OpRole::kBackward)) || | ||
| (op_role == (static_cast<int64_t>(OpRole::kBackward) | | ||
| static_cast<int64_t>(OpRole::kLoss))); | ||
| } | ||
| | ||
| bool IsOptimize(int64_t op_role) { | ||
| return op_role == static_cast<int64_t>(OpRole::kOptimize); | ||
| } | ||
| | ||
| struct DistCoord { | ||
| int32_t dp_idx; | ||
| int32_t pp_idx; | ||
| int32_t mp_idx; | ||
| }; | ||
| | ||
| class DistCoordSys final { | ||
LiYuRio marked this conversation as resolved. Show resolved Hide resolved | ||
| public: | ||
| DistCoordSys(int32_t dp_degree, int32_t pp_degree, int32_t mp_degree) | ||
| : dp_degree_(dp_degree), pp_degree_(pp_degree), mp_degree_(mp_degree) {} | ||
| DistCoord RankToCoord(int64_t rank) const; | ||
| int64_t CoordToRank(const DistCoord& coord) const; | ||
| | ||
| private: | ||
| DISABLE_COPY_AND_ASSIGN(DistCoordSys); | ||
| bool InvalidCoord(const DistCoord& coord) const; | ||
| int32_t dp_degree_; | ||
| int32_t pp_degree_; | ||
| int32_t mp_degree_; | ||
| }; | ||
| | ||
| DistCoord DistCoordSys::RankToCoord(int64_t rank) const { | ||
| DistCoord coord; | ||
| coord.mp_idx = rank % mp_degree_; | ||
| rank /= mp_degree_; | ||
| coord.pp_idx = rank % pp_degree_; | ||
| rank /= pp_degree_; | ||
| coord.dp_idx = rank % dp_degree_; | ||
| return coord; | ||
| } | ||
| | ||
| int64_t DistCoordSys::CoordToRank(const DistCoord& coord) const { | ||
| if (InvalidCoord(coord)) { | ||
| return -1; | ||
| } | ||
| return coord.dp_idx * pp_degree_ * mp_degree_ + coord.pp_idx * mp_degree_ + | ||
| coord.mp_idx; | ||
| } | ||
| | ||
| bool DistCoordSys::InvalidCoord(const DistCoord& coord) const { | ||
| return coord.mp_idx < 0 || coord.mp_idx >= mp_degree_ || coord.pp_idx < 0 || | ||
| coord.pp_idx >= pp_degree_ || coord.dp_idx < 0 || | ||
| coord.dp_idx >= dp_degree_; | ||
| } | ||
| | ||
| } // namespace | ||
| | ||
| std::vector<OpRole> RuntimeGraph::functionality_order = { | ||
| OpRole::kLRSched, OpRole::kForward, OpRole::kBackward, OpRole::kOptimize}; | ||
| | ||
| RuntimeGraph::RuntimeGraph(const ProgramDesc& program, | ||
| const FleetExecutorDesc& exe_desc) | ||
| : exe_desc_(exe_desc) { | ||
| if (exe_desc.grain() == "coarse") { | ||
| SplitProgramBasedFunctionality(program); | ||
| AssignTaskToIntercepter(); | ||
| FakeDependence(); | ||
| FakeRuntimeInfo(); | ||
| } | ||
| } | ||
| | ||
| void RuntimeGraph::SplitProgramBasedFunctionality(const ProgramDesc& program) { | ||
| for (const auto& op_desc : program.Block(0).AllOps()) { | ||
| ops_.emplace_back(OpRegistry::CreateOp(*op_desc)); | ||
| } | ||
| std::unordered_map<int64_t, std::vector<OperatorBase*>> role_to_ops; | ||
| for (const auto& op : ops_) { | ||
| int64_t op_role = op->Attr<int64_t>("op_role"); | ||
| OpRole new_op_role; | ||
| if (IsLRSched(op_role)) { | ||
| new_op_role = OpRole::kLRSched; | ||
| } else if (IsForward(op_role)) { | ||
| new_op_role = OpRole::kForward; | ||
| } else if (IsBackward(op_role)) { | ||
| new_op_role = OpRole::kBackward; | ||
| } else if (IsOptimize(op_role)) { | ||
| new_op_role = OpRole::kOptimize; | ||
| } else { | ||
| PADDLE_THROW(platform::errors::PreconditionNotMet( | ||
| "The op %s is None of LRSched, Forward, Backward or Optimize.", | ||
| op->Type())); | ||
| } | ||
| int64_t new_op_role_id = static_cast<int64_t>(new_op_role); | ||
| if (role_to_ops.find(new_op_role_id) == role_to_ops.end()) { | ||
| role_to_ops.insert({new_op_role_id, {}}); | ||
| } | ||
| role_to_ops.at(new_op_role_id).emplace_back(op.get()); | ||
| } | ||
| int64_t cur_rank = exe_desc_.cur_rank(); | ||
| int64_t task_id = cur_rank * functionality_order.size(); | ||
| for (std::size_t i = 0; i < functionality_order.size(); ++i) { | ||
| OpRole role = functionality_order[i]; | ||
| int64_t role_id = static_cast<int64_t>(role); | ||
| if (role_to_ops.find(role_id) == role_to_ops.end()) { | ||
| task_nodes_.emplace_back( | ||
| TaskNode::CreateEmptyTaskNode(role_id, cur_rank, task_id)); | ||
| } else { | ||
| task_nodes_.emplace_back(TaskNode::CreateTaskNode( | ||
| role_id, role_to_ops.at(role_id), cur_rank, task_id)); | ||
| } | ||
LiYuRio marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ++task_id; | ||
| } | ||
| } | ||
| | ||
| void RuntimeGraph::FakeDependence() { | ||
| int64_t cur_rank = exe_desc_.cur_rank(); | ||
| DistCoordSys coord_sys(exe_desc_.dp_degree(), exe_desc_.pp_degree(), | ||
| exe_desc_.mp_degree()); | ||
| const auto& coord = coord_sys.RankToCoord(cur_rank); | ||
| DistCoord upstream_coord = coord, downstream_coord = coord; | ||
| upstream_coord.pp_idx -= 1; | ||
| downstream_coord.pp_idx += 1; | ||
| int64_t pp_upstream = coord_sys.CoordToRank(upstream_coord); | ||
| int64_t pp_downstream = coord_sys.CoordToRank(downstream_coord); | ||
| int32_t num_of_functionality = functionality_order.size(); | ||
| // lr -> forward -> backward -> optimize | ||
| // | | | ||
| // lr -> forward -> backward -> optimize | ||
| for (std::size_t i = 0; i < task_nodes_.size(); ++i) { | ||
LiYuRio marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| if (i != 0) { | ||
| task_nodes_[i]->AddUpstreamTask(cur_rank * num_of_functionality + i - 1); | ||
| } | ||
| if (i != task_nodes_.size() - 1) { | ||
| task_nodes_[i]->AddDownstreamTask(cur_rank * num_of_functionality + i + | ||
| 1); | ||
| } | ||
| if (IsForward(task_nodes_[i]->role())) { | ||
| if (pp_upstream != -1) { | ||
| task_nodes_[i]->AddUpstreamTask(pp_upstream * num_of_functionality + i); | ||
| } | ||
| if (pp_downstream != -1) { | ||
| task_nodes_[i]->AddDownstreamTask(pp_downstream * num_of_functionality + | ||
| i); | ||
| } | ||
| } else if (IsBackward(task_nodes_[i]->role())) { | ||
| if (pp_downstream != -1) { | ||
| task_nodes_[i]->AddUpstreamTask(pp_downstream * num_of_functionality + | ||
| i); | ||
| } | ||
| if (pp_upstream != -1) { | ||
| task_nodes_[i]->AddDownstreamTask(pp_upstream * num_of_functionality + | ||
| i); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| | ||
| void RuntimeGraph::AssignTaskToIntercepter() { | ||
| for (const auto& task : task_nodes_) { | ||
| int64_t intercepter_id = task->task_id(); | ||
| if (intercepter_id_to_node_.find(intercepter_id) != | ||
| intercepter_id_to_node_.end()) { | ||
| PADDLE_THROW(platform::errors::PreconditionNotMet( | ||
| "Repeated intercepter id: %d", intercepter_id)); | ||
| } | ||
| intercepter_id_to_node_.insert({intercepter_id, task.get()}); | ||
| } | ||
| } | ||
| | ||
| void RuntimeGraph::FakeRuntimeInfo() { | ||
| int64_t nrank = exe_desc_.cluster_info().size(); | ||
| int32_t num_of_functionality = functionality_order.size(); | ||
| for (int64_t i = 0; i < nrank; ++i) { | ||
| for (int64_t j = 0; j < num_of_functionality; ++j) { | ||
| int64_t intercepter_id = i * num_of_functionality + j; | ||
| intercepter_id_to_rank_.insert({intercepter_id, i}); | ||
LiYuRio marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| } | ||
| } | ||
| } | ||
| | ||
| } // namespace distributed | ||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (c) 2021 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/distributed/fleet_executor/task_node.h" | ||
| #include "paddle/fluid/framework/operator.h" | ||
| | ||
| namespace paddle { | ||
| namespace distributed { | ||
| namespace { | ||
| using OperatorBase = TaskNode::OperatorBase; | ||
| } | ||
| | ||
| TaskNode::TaskNode(int64_t role, const std::vector<OperatorBase*>& ops, | ||
| int64_t rank, int64_t task_id) | ||
| : ops_(ops), role_(role), rank_(rank), task_id_(task_id) {} | ||
| | ||
| TaskNode::TaskNode(int64_t role, int64_t rank, int64_t task_id) | ||
| : role_(role), rank_(rank), task_id_(task_id) {} | ||
| | ||
| std::unique_ptr<TaskNode> TaskNode::CreateEmptyTaskNode(int64_t role, | ||
| int64_t rank, | ||
| int64_t task_id) { | ||
| return std::make_unique<TaskNode>(role, rank, task_id); | ||
| } | ||
| | ||
| std::unique_ptr<TaskNode> TaskNode::CreateTaskNode( | ||
| int64_t role, const std::vector<OperatorBase*>& ops, int64_t rank, | ||
| int64_t task_id) { | ||
| return std::make_unique<TaskNode>(role, ops, rank, task_id); | ||
| } | ||
| | ||
| void TaskNode::AddUpstreamTask(int64_t task_id) { upstream_.insert(task_id); } | ||
| | ||
| void TaskNode::AddDownstreamTask(int64_t task_id) { | ||
| downstream_.insert(task_id); | ||
| } | ||
| } // namespace distributed | ||
| } // namespace paddle |
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.
后面复用distributed_strategy是不是更好些,可能还会有sharding_degree
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.
因为distributed_strategy.proto在framework目录下,和这个proto不在一个文件夹,在当前文件夹下的CMakeList里调用generic.cmake里定义的proto_library函数,会将protobuf的搜索路径设置为当前文件夹,同时protobuf的import不支持相对路径,所以暂时没想到怎么直接引用distributed_strategy.proto里的定义。