graph.h 3.9 KB
Newer Older
X
Xin Pan 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
X
start  
Xin Pan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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. */

#pragma once

X
Xin Pan 已提交
17 18 19 20 21 22
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "paddle/fluid/framework/ir/node.h"
X
Xin Pan 已提交
23
#include "paddle/fluid/framework/program_desc.h"
X
Xin Pan 已提交
24
#include "paddle/fluid/platform/enforce.h"
X
Xin Pan 已提交
25 26
#include "paddle/fluid/platform/variant.h"

X
start  
Xin Pan 已提交
27
namespace paddle {
X
Xin Pan 已提交
28
namespace framework {
X
Xin Pan 已提交
29
namespace ir {
X
Xin Pan 已提交
30

X
Xin Pan 已提交
31 32
class Graph {
 public:
X
Xin Pan 已提交
33
  explicit Graph(const ProgramDesc &program);
X
Xin Pan 已提交
34

X
Xin Pan 已提交
35
  virtual ~Graph() {
X
Xin Pan 已提交
36
    for (auto &attr : attrs_) {
X
Xin Pan 已提交
37
      attr_dels_[attr.first]();
X
Xin Pan 已提交
38
    }
X
Xin Pan 已提交
39 40
    attrs_.clear();
    attr_dels_.clear();
X
Xin Pan 已提交
41 42
  }

X
Xin Pan 已提交
43 44 45 46
  bool Has(const std::string &attr_name) const {
    return attrs_.find(attr_name) != attrs_.end();
  }

X
Xin Pan 已提交
47
  template <typename AttrType>
X
Xin Pan 已提交
48
  AttrType &Get(const std::string &attr_name) const {
X
Xin Pan 已提交
49 50
    PADDLE_ENFORCE(Has(attr_name), "%s attr not registered for graph.",
                   attr_name);
X
Xin Pan 已提交
51
    return *boost::any_cast<AttrType *>(attrs_.at(attr_name));
X
Xin Pan 已提交
52 53 54
  }

  template <typename AttrType>
X
Xin Pan 已提交
55
  void Set(const std::string &attr_name, AttrType *attr) {
X
clean  
Xin Pan 已提交
56
    PADDLE_ENFORCE(attrs_.count(attr_name) == 0);
X
Xin Pan 已提交
57 58 59 60 61
    attrs_[attr_name] = attr;
    attr_dels_[attr_name] = [attr, attr_name]() {
      VLOG(3) << "deleting " << attr_name;
      delete attr;
    };
X
Xin Pan 已提交
62
  }
X
Xin Pan 已提交
63

X
Xin Pan 已提交
64 65
  const std::unordered_set<ir::Node *> &Nodes() const { return node_set_; }

X
Xin Pan 已提交
66
  // Create a normal variable with non-null VarDesc.
X
Xin Pan 已提交
67
  ir::Node *CreateVarNode(VarDesc *var_desc) {
X
Xin Pan 已提交
68
    return AddNode(new ir::Node(var_desc));
69 70
  }

X
Xin Pan 已提交
71
  // Create a normal runnable operator with OpDesc.
X
Xin Pan 已提交
72
  ir::Node *CreateOpNode(OpDesc *op_desc) {
X
Xin Pan 已提交
73 74 75
    return AddNode(new ir::Node(op_desc));
  }

X
Xin Pan 已提交
76 77 78
  // Create a control dependency var that connects 2 operations. The
  // var doesn't hold any data. Other than that, it's no different from
  // other var, considering dependency analysis.
X
Xin Pan 已提交
79
  ir::Node *CreateControlDepVar() {
X
Xin Pan 已提交
80
    // TODO(panyx0718): control var name should be really unique.
X
Xin Pan 已提交
81 82 83
    const std::string name = string::Sprintf(
        "%s@%llu", ir::Node::kControlDepVarName, node_set_.size());
    return AddNode(new ir::Node(name, ir::Node::Type::kVariable));
84 85
  }

X
Xin Pan 已提交
86 87
  // A more free style way of creating a graph node. Mostly use for test
  // or "copy" from another node. Avoid using it if possible.
X
Xin Pan 已提交
88
  ir::Node *CreateEmptyNode(const std::string &name, ir::Node::Type type) {
X
Xin Pan 已提交
89
    return AddNode(new ir::Node(name, type));
90 91
  }

X
Xin Pan 已提交
92 93
  // Clear all node information of the graph and return the ownership of the
  // nodes.
X
Xin Pan 已提交
94 95 96 97 98 99 100 101 102
  std::vector<std::unique_ptr<ir::Node>> ReleaseNodes() {
    std::vector<std::unique_ptr<ir::Node>> ret;
    for (auto &n : nodes_) {
      ret.emplace_back(n.second.release());
    }
    nodes_.clear();
    node_set_.clear();
    return ret;
  }
X
Xin Pan 已提交
103 104

 private:
X
Xin Pan 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118
  // This method takes ownership of `node`.
  ir::Node *AddNode(ir::Node *node) {
    PADDLE_ENFORCE(node_set_.find(node) == node_set_.end());
    nodes_[node].reset(node);
    node_set_.insert(node);
    return node;
  }

  void RemoveNode(ir::Node *node) {
    PADDLE_ENFORCE(node_set_.find(node) != node_set_.end());
    node_set_.erase(node);
    nodes_.erase(node);
  }

119
  // NOTE: program_ shouldn't be exposed to user.
X
Xin Pan 已提交
120
  const ProgramDesc &program_;
X
Xin Pan 已提交
121 122
  std::map<std::string, boost::any> attrs_;
  std::map<std::string, std::function<void(void)>> attr_dels_;
X
Xin Pan 已提交
123 124
  std::map<ir::Node *, std::unique_ptr<ir::Node>> nodes_;
  std::unordered_set<ir::Node *> node_set_;
X
Xin Pan 已提交
125
};
X
Xin Pan 已提交
126 127

bool IsControlDepVar(const ir::Node &var);
X
Xin Pan 已提交
128
}  // namespace ir
X
Xin Pan 已提交
129
}  // namespace framework
X
start  
Xin Pan 已提交
130
}  // namespace paddle