Skip to content
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

Add dependency for read op in standalone executor #44362

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions paddle/fluid/framework/new_executor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory(workqueue)
add_subdirectory(garbage_collector)
add_subdirectory(interpreter)
add_subdirectory(workqueue)

set(STANDALONE_EXECUTOR_SRCS
data_transfer.cc
Expand All @@ -11,8 +12,9 @@ set(STANDALONE_EXECUTOR_SRCS
standalone_executor.cc)

set(STANDALONE_EXECUTOR_DEPS
op_registry
dependency_utils
device_context
op_registry
scope
framework_proto
data_feed_proto
Expand All @@ -36,9 +38,6 @@ set(STANDALONE_EXECUTOR_DEPS
enforce
scope
glog
enforce
glog
scope
workqueue
interpretercore_event_garbage_collector
${DEVICE_EVENT_LIBS}
Expand Down
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 paddle/fluid/framework/new_executor/interpreter/dependency_utils.cc
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 paddle/fluid/framework/new_executor/interpreter/dependency_utils.h
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
48 changes: 12 additions & 36 deletions paddle/fluid/framework/new_executor/interpretercore_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "paddle/fluid/framework/details/nan_inf_utils.h"
#include "paddle/fluid/framework/executor_gc_helper.h"
#include "paddle/fluid/framework/new_executor/data_transfer.h"
#include "paddle/fluid/framework/new_executor/interpreter/dependency_utils.h"
#include "paddle/fluid/operators/controlflow/conditional_block_op_helper.h"
#include "paddle/fluid/operators/controlflow/recurrent_op_helper.h"
#include "paddle/fluid/operators/controlflow/while_op_helper.h"
Expand Down Expand Up @@ -704,33 +705,6 @@ void update_var_min_rw_op(const std::map<int, std::set<int>>& op2dependences,
var2min_rw_op->at(rw_var).push_back(cur_op);
}

void AddDownstreamOp(int prior_op_idx,
int posterior_op_idx,
std::map<int, std::list<int>>* op_downstream_map) {
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>()));
}
op_downstream_map->at(prior_op_idx).push_back(posterior_op_idx);
}

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()) {
for (int op_idx : op_downstream_map->at(prior_op_idx)) {
if (op_happens_before[op_idx][posterior_op_idx]) {
VLOG(7) << "Find dependencies " << prior_op_idx << "->" << op_idx
<< "->" << posterior_op_idx << ", skip adding " << prior_op_idx
<< "->" << posterior_op_idx;
return;
}
}
}

AddDownstreamOp(prior_op_idx, posterior_op_idx, op_downstream_map);
}

size_t CountDownstreamMap(const std::map<int, std::list<int>>& downstream_map) {
size_t count = 0;
for (auto pair : downstream_map) {
Expand Down Expand Up @@ -972,7 +946,7 @@ std::map<int, std::list<int>> build_op_downstream_map(
if (random_op_set.count(vec_instruction[op_idx].OpBase()->Type())) {
if (dependence_op_idx != -1) {
AddDownstreamOp(
dependence_op_idx, op_idx, &op_downstream_map, *op_happens_before);
dependence_op_idx, op_idx, &op_downstream_map, op_happens_before);
}
dependence_op_idx = op_idx;
}
Expand All @@ -999,7 +973,7 @@ std::map<int, std::list<int>> build_op_downstream_map(
if (is_comm_op(vec_instruction[op_idx].OpBase()->Type())) {
if (dependence_op_idx != -1) {
AddDownstreamOp(
dependence_op_idx, op_idx, &op_downstream_map, *op_happens_before);
dependence_op_idx, op_idx, &op_downstream_map, op_happens_before);
VLOG(4) << "Add depend from "
<< vec_instruction[dependence_op_idx].OpBase()->Type() << " to "
<< vec_instruction[op_idx].OpBase()->Type();
Expand Down Expand Up @@ -1028,7 +1002,7 @@ std::map<int, std::list<int>> build_op_downstream_map(
<< vec_instruction[dependence_op_idx].OpBase()->Type() << " to "
<< vec_instruction[op_idx].OpBase()->Type();
AddDownstreamOp(
dependence_op_idx, op_idx, &op_downstream_map, *op_happens_before);
dependence_op_idx, op_idx, &op_downstream_map, op_happens_before);
}
}
}
Expand Down Expand Up @@ -1088,7 +1062,7 @@ std::map<int, std::list<int>> build_op_downstream_map(
AddDownstreamOp(j,
first_read_fused_out_op,
&op_downstream_map,
*op_happens_before);
op_happens_before);
VLOG(4) << j << " -> " << first_read_fused_out_op;
VLOG(4)
<< "Add depend from " << vec_instruction[j].OpBase()->Type()
Expand Down Expand Up @@ -1121,7 +1095,7 @@ std::map<int, std::list<int>> build_op_downstream_map(

for (auto var_id : outputs) {
if (is_read(vec_instruction[j], var_id)) {
AddDownstreamOp(target, j, &op_downstream_map, *op_happens_before);
AddDownstreamOp(target, j, &op_downstream_map, op_happens_before);
VLOG(4) << target << " -> " << j;
VLOG(4) << "Add depend from "
<< vec_instruction[target].OpBase()->Type() << " to "
Expand All @@ -1137,10 +1111,8 @@ std::map<int, std::list<int>> build_op_downstream_map(
for (size_t op_idx = 0; op_idx < op_num; ++op_idx) {
if (!IsCpuOp(vec_instruction[op_idx])) {
if (dependence_op_idx != -1) {
AddDownstreamOp(dependence_op_idx,
op_idx,
&op_downstream_map,
*op_happens_before);
AddDownstreamOp(
dependence_op_idx, op_idx, &op_downstream_map, op_happens_before);
VLOG(4) << "Add depend from "
<< vec_instruction[dependence_op_idx].OpBase()->Type() << "("
<< dependence_op_idx << ") to "
Expand All @@ -1152,6 +1124,10 @@ std::map<int, std::list<int>> build_op_downstream_map(
}
}

AddDependencyForReadOp(
vec_instruction, &op_downstream_map, op_happens_before);

VLOG(8) << "build_op_downstream_map finished";
VLOG(8) << "downstream count: " << CountDownstreamMap(op_downstream_map);
VLOG(8) << "downstream_map: " << std::endl
<< StringizeDownstreamMap(op_downstream_map);
Expand Down
7 changes: 0 additions & 7 deletions paddle/fluid/framework/new_executor/interpretercore_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/*************************************************************************
> File Name: interpretercore_util.h
> Author: [email protected]
> Created Time: Fri 23 Jul 2021 06:19:19 AM UTC
************************************************************************/

#pragma once

#include <chrono>
Expand Down Expand Up @@ -48,7 +42,6 @@ using AtomicVectorSizeT = std::vector<std::atomic<size_t>>;

namespace paddle {
namespace framework {

namespace interpreter {

class AsyncWorkQueue {
Expand Down
7 changes: 7 additions & 0 deletions python/paddle/fluid/tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1713,3 +1713,10 @@ if(WITH_CINN AND WITH_TESTING)
FLAGS_allow_cinn_ops="conv2d;conv2d_grad;elementwise_add;elementwise_add_grad;relu;relu_grad;sum"
)
endif()

py_test_modules(
test_add_reader_dependency_for_interpretercore MODULES
test_add_reader_dependency ENVS FLAGS_CONVERT_GRAPH_TO_PROGRAM=true)

set_tests_properties(test_add_reader_dependency_for_interpretercore
PROPERTIES TIMEOUT 120)
2 changes: 2 additions & 0 deletions tools/windows/run_unittests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
disable_wingpu_test="^test_model$|\
^test_dataloader_early_reset$|\
^test_add_reader_dependency$|\
^test_add_reader_dependency_for_interpretercore$|\
^test_decoupled_py_reader$|\
^test_generator_dataloader$|\
^test_parallel_dygraph_sync_batch_norm$|\
Expand Down Expand Up @@ -152,6 +153,7 @@ disable_win_inference_test="^trt_quant_int8_yolov3_r50_test$|\
^test_multiprocess_dataloader_iterable_dataset_dynamic$|\
^test_multiprocess_dataloader_iterable_dataset_static$|\
^test_add_reader_dependency$|\
^test_add_reader_dependency_for_interpretercore$|\
^test_compat$|\
^test_decoupled_py_reader$|\
^test_generator_dataloader$|\
Expand Down