sequential_execution_pass.cc 4.0 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2018 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/details/sequential_execution_pass.h"
S
sneaxiy 已提交
16
#include <string>
S
sneaxiy 已提交
17 18 19
#include <unordered_map>
#include <unordered_set>
#include <vector>
D
dzhwinter 已提交
20
#include "paddle/fluid/framework/details/memory_optimize_helper.h"
S
sneaxiy 已提交
21
#include "paddle/fluid/framework/op_proto_maker.h"
S
sneaxiy 已提交
22 23 24 25 26 27 28 29 30 31 32 33

namespace paddle {
namespace framework {
namespace details {

static bool IsSameOpDesc(OpDesc *op1, OpDesc *op2) {
  return op1->Type() == op2->Type() && op1->Inputs() == op2->Inputs() &&
         op1->Outputs() == op2->Outputs();
}

std::unique_ptr<ir::Graph> SequentialExecutionPass::ApplyImpl(
    std::unique_ptr<ir::Graph> graph) const {
S
sneaxiy 已提交
34 35 36 37 38 39 40 41 42
  // FIXME(zjl): Insert dependencies between some distributed ops may cause
  // the multi_devices_graph_pass fails. So we skip these ops here.
  // Indeed, maybe we should not insert dependencies between these ops
  // casually, which may cause deadlock easily.
  // We should add more skipped distributed ops when found errors in
  // multi_devices_graph_pass
  static std::unordered_set<std::string> skip_dist_ops{
      "send", "recv", "send_barrier", "fetch_barrier"};

X
polish  
Xin Pan 已提交
43
  auto &ops = graph->Get<const std::vector<OpDesc *>>(kAllOpDescs);
S
sneaxiy 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  std::vector<ir::Node *> op_node_list;
  op_node_list.reserve(ops.size());

  std::unordered_map<ir::Node *, size_t> op_deps;
  std::unordered_map<ir::Node *, std::unordered_set<ir::Node *>> pending_ops;
  std::unordered_set<ir::Node *> ready_ops;

  for (ir::Node *node : graph->Nodes()) {
    if (!node->IsOp()) continue;
    std::unordered_set<ir::Node *> preceding_ops;
    for (auto *in : node->inputs) {
      PADDLE_ENFORCE(in->IsVar(),
                     "Preceding Node of Op Nodes must be Var Node");
      if (in->inputs.empty()) continue;
      PADDLE_ENFORCE(in->inputs.size() == 1 && in->inputs[0]->IsOp(),
                     "Preceding Op Node of Var Node must be unique");
      preceding_ops.insert(in->inputs[0]);
      pending_ops[in->inputs[0]].insert(node);
    }
    op_deps[node] = preceding_ops.size();
    if (preceding_ops.empty()) {
      ready_ops.insert(node);
    }
  }

  for (auto *op_desc : ops) {
    ir::Node *found_node = nullptr;
    for (auto *node : ready_ops) {
      if (IsSameOpDesc(op_desc, node->Op())) {
        PADDLE_ENFORCE(found_node == nullptr,
                       "Found multiple op_desc in graph: %s", op_desc->Type());
        found_node = node;
      }
    }

    PADDLE_ENFORCE_NOT_NULL(found_node, "Cannot find op_desc in graph: %s",
S
sneaxiy 已提交
80 81
                            op_desc->Type());
    for (auto *pending_op : pending_ops[found_node]) {
S
sneaxiy 已提交
82 83 84 85 86
      if (--op_deps.at(pending_op) == 0) {
        ready_ops.insert(pending_op);
      }
    }
    ready_ops.erase(found_node);
S
sneaxiy 已提交
87 88 89
    if (skip_dist_ops.count(op_desc->Type()) == 0) {
      op_node_list.push_back(found_node);
    }
S
sneaxiy 已提交
90 91 92 93 94 95 96 97
  }

  for (size_t i = 1; i < op_node_list.size(); ++i) {
    auto *dep_var = graph->CreateControlDepVar();
    op_node_list[i]->inputs.push_back(dep_var);
    op_node_list[i - 1]->outputs.push_back(dep_var);
    dep_var->outputs.push_back(op_node_list[i]);
    dep_var->inputs.push_back(op_node_list[i - 1]);
M
minqiyang 已提交
98 99
    VLOG(10) << "Add dependencies between " << op_node_list[i - 1]->Name()
             << " and " << op_node_list[i]->Name();
S
sneaxiy 已提交
100 101 102 103 104 105 106 107 108 109
  }
  return graph;
}

}  // namespace details
}  // namespace framework
}  // namespace paddle

REGISTER_PASS(sequential_execution_pass,
              paddle::framework::details::SequentialExecutionPass)
X
polish  
Xin Pan 已提交
110
    .RequireGraphAttr(paddle::framework::details::kAllOpDescs);