graph_to_program_pass.cc 2.6 KB
Newer Older
X
Xin Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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/ir/graph_to_program_pass.h"

#include <map>
18
#include <memory>
X
Xin Pan 已提交
19
#include <string>
20
#include <unordered_set>
X
Xin Pan 已提交
21 22 23 24 25 26 27 28 29 30
#include <vector>

#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/framework/program_desc.h"

namespace paddle {
namespace framework {
namespace ir {

31
void GraphToProgramPass::ApplyImpl(ir::Graph* graph) const {
Y
Yan Chunwei 已提交
32 33 34 35 36 37 38 39
  // Remove the unneeded variables after memory optimization.
  std::unordered_set<std::string> vars2remove;
  if (graph->Has(kGraphToProgramVarsToRemove)) {
    vars2remove = graph->Get<std::unordered_set<std::string>>(
        kGraphToProgramVarsToRemove);
    VLOG(2) << "graph to program remove " << vars2remove.size() << " nodes";
  }

X
Xin Pan 已提交
40 41 42 43 44 45
  ProgramDesc& program = Get<ProgramDesc>("program");

  std::unique_ptr<proto::ProgramDesc> program_pb(
      new proto::ProgramDesc(*program.Proto()));

  auto block = program_pb->mutable_blocks(kRootBlockIndex);
46
  block->set_idx(kRootBlockIndex);
X
Xin Pan 已提交
47 48 49
  block->clear_vars();
  std::unordered_set<std::string> visited_vars;
  for (ir::Node* n : graph->Nodes()) {
50
    if (n->IsVar()) {
Y
Yan Chunwei 已提交
51 52
      if (n->Var() && visited_vars.count(n->Var()->Name()) == 0 &&
          !vars2remove.count(n->Var()->Name())) {
X
Xin Pan 已提交
53 54 55 56 57 58
        visited_vars.insert(n->Var()->Name());
        block->add_vars()->MergeFrom(*n->Var()->Proto());
      }
    }
  }
  block->clear_ops();
Y
Yan Chunwei 已提交
59 60 61 62 63 64 65 66 67 68 69

  std::vector<ir::Node*> nodes;
  if (Has(kGraphToProgramSortKind)) {
    // Inference Memory Optimize relays on this branch.
    int sort_kind = Get<int>(kGraphToProgramSortKind);
    nodes = TopologyVarientSort(
        *graph, static_cast<framework::ir::SortKind>(sort_kind));
  } else {
    nodes = TopologySortOperations(*graph);
  }

X
Xin Pan 已提交
70
  for (ir::Node* n : nodes) {
Y
Yan Chunwei 已提交
71 72
    if (!n->Op()) continue;

X
Xin Pan 已提交
73 74 75 76 77
    block->add_ops()->MergeFrom(*n->Op()->Proto());
  }

  program.CopyFrom(*program_pb);
}
Y
Yan Chunwei 已提交
78

X
Xin Pan 已提交
79 80 81 82 83
}  // namespace ir
}  // namespace framework
}  // namespace paddle

REGISTER_PASS(graph_to_program_pass, paddle::framework::ir::GraphToProgramPass);