-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dependency for read op in standalone executor (#44362)
* Add dependency for read op in standalone executor * Fix CI errors * Add UT * add_dependency -> dependency_utils * Fix CI errors
- Loading branch information
Showing
8 changed files
with
199 additions
and
48 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
4 changes: 4 additions & 0 deletions
4
paddle/fluid/framework/new_executor/interpreter/CMakeLists.txt
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,4 @@ | ||
cc_library( | ||
dependency_utils | ||
SRCS dependency_utils.cc | ||
DEPS operator) |
116 changes: 116 additions & 0 deletions
116
paddle/fluid/framework/new_executor/interpreter/dependency_utils.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,116 @@ | ||
// Copyright (c) 2022 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/interpreter/dependency_utils.h" | ||
|
||
#include <queue> | ||
|
||
namespace paddle { | ||
namespace framework { | ||
namespace interpreter { | ||
|
||
void AddDownstreamOp(int prior_op_idx, | ||
int posterior_op_idx, | ||
std::map<int, std::list<int>>* op_downstream_map, | ||
const std::vector<std::vector<bool>>* op_happens_before) { | ||
if (op_downstream_map->find(prior_op_idx) == op_downstream_map->end()) { | ||
op_downstream_map->emplace(std::make_pair(prior_op_idx, std::list<int>())); | ||
} else { | ||
if (op_happens_before != nullptr) { | ||
for (int op_idx : op_downstream_map->at(prior_op_idx)) { | ||
if (op_happens_before->at(op_idx).at(posterior_op_idx)) { | ||
VLOG(7) << "Find dependencies " << prior_op_idx << "->" << op_idx | ||
<< "->" << posterior_op_idx << ", skip adding " | ||
<< prior_op_idx << "->" << posterior_op_idx; | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
op_downstream_map->at(prior_op_idx).push_back(posterior_op_idx); | ||
} | ||
|
||
// check whether exists prior_op -> ... -> posterior_op to avoid building loops | ||
bool IsDependency(int prior_op_idx, | ||
int posterior_op_idx, | ||
const std::map<int, std::list<int>>& downstream_map) { | ||
std::queue<int> q; | ||
q.push(prior_op_idx); | ||
|
||
while (!q.empty()) { | ||
int op_idx = q.front(); | ||
q.pop(); | ||
|
||
auto it = downstream_map.find(op_idx); | ||
if (it != downstream_map.end()) { | ||
for (int downstream_op_idx : it->second) { | ||
if (downstream_op_idx == posterior_op_idx) { | ||
return true; | ||
} | ||
|
||
// no need for double enqueue checking since DAG is assumed | ||
q.push(downstream_op_idx); | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void AddDependencyForReadOp( | ||
const std::vector<Instruction>& vec_instruction, | ||
std::map<int, std::list<int>>* downstream_map, | ||
const std::vector<std::vector<bool>>* op_happens_before) { | ||
size_t op_num = vec_instruction.size(); | ||
std::vector<bool> is_startup_ops(op_num, true); | ||
for (size_t op_idx = 0; op_idx < op_num; ++op_idx) { | ||
auto it = downstream_map->find(op_idx); | ||
if (it != downstream_map->end()) { | ||
for (size_t downstream_op_idx : it->second) { | ||
is_startup_ops[downstream_op_idx] = false; | ||
} | ||
} | ||
} | ||
|
||
std::vector<size_t> read_ops; | ||
std::vector<size_t> startup_ops; | ||
for (size_t op_idx = 0; op_idx < op_num; ++op_idx) { | ||
if (vec_instruction[op_idx].OpBase()->Type() == "read") { | ||
read_ops.push_back(op_idx); | ||
} | ||
|
||
if (is_startup_ops[op_idx]) { | ||
startup_ops.push_back(op_idx); | ||
} | ||
} | ||
|
||
for (size_t read_op_idx : read_ops) { | ||
for (size_t downstream_op_idx : startup_ops) { | ||
if (read_op_idx != downstream_op_idx && | ||
!IsDependency(downstream_op_idx, read_op_idx, *downstream_map)) | ||
AddDownstreamOp( | ||
read_op_idx, downstream_op_idx, downstream_map, op_happens_before); | ||
VLOG(4) << "Add depend from " | ||
<< vec_instruction[read_op_idx].OpBase()->Type() << "(" | ||
<< read_op_idx << ") to " | ||
<< vec_instruction[downstream_op_idx].OpBase()->Type() << "(" | ||
<< downstream_op_idx << ")"; | ||
} | ||
} | ||
} | ||
|
||
} // namespace interpreter | ||
} // namespace framework | ||
} // namespace paddle |
54 changes: 54 additions & 0 deletions
54
paddle/fluid/framework/new_executor/interpreter/dependency_utils.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,54 @@ | ||
// Copyright (c) 2022 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. | ||
|
||
// This file provides some dependency adding function to handle the implicit | ||
// dependency that cannot be explicitly expresed by a Program. It is a | ||
// compromise of the incomplete expression ability of the Program. Do not add | ||
// too many functions here at will, that will bring great burden to the | ||
// Interpretercore. | ||
|
||
// TODO(Ruibiao): | ||
// 1. Move other dependency adding codes from interpretercore_util.cc to | ||
// dependency_utils.cc | ||
// 2. Move other Interpretercore related codes to directory | ||
// new_executor/interpreter | ||
// 3. Try to remove parameter op_happens_before from the dependency adding | ||
// function | ||
|
||
#pragma once | ||
|
||
#include <map> | ||
#include <vector> | ||
|
||
#include "paddle/fluid/framework/new_executor/new_executor_defs.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
namespace interpreter { | ||
|
||
// equivalent to add_reader_dependency_pass | ||
void AddDependencyForReadOp( | ||
const std::vector<Instruction>& vec_instruction, | ||
std::map<int, std::list<int>>* downstream_map, | ||
const std::vector<std::vector<bool>>* op_happens_before = nullptr); | ||
|
||
void AddDownstreamOp( | ||
int prior_op_idx, | ||
int posterior_op_idx, | ||
std::map<int, std::list<int>>* op_downstream_map, | ||
const std::vector<std::vector<bool>>* op_happens_before = nullptr); | ||
|
||
} // namespace interpreter | ||
} // 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
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