graph.h 2.3 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 31
class Graph {
 public:
X
Xin Pan 已提交
32
  explicit Graph(const ProgramDesc &program);
X
Xin Pan 已提交
33

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

  template <typename AttrType>
X
Xin Pan 已提交
43 44
  AttrType &Get(const std::string &attr_name) const {
    return *boost::any_cast<AttrType *>(attrs_.at(attr_name));
X
Xin Pan 已提交
45 46 47
  }

  template <typename AttrType>
X
Xin Pan 已提交
48
  void Set(const std::string &attr_name, AttrType *attr) {
X
clean  
Xin Pan 已提交
49
    PADDLE_ENFORCE(attrs_.count(attr_name) == 0);
X
Xin Pan 已提交
50 51 52 53 54
    attrs_[attr_name] = attr;
    attr_dels_[attr_name] = [attr, attr_name]() {
      VLOG(3) << "deleting " << attr_name;
      delete attr;
    };
X
Xin Pan 已提交
55
  }
X
Xin Pan 已提交
56

X
Xin Pan 已提交
57
  ir::Node *CreateVarNode(VarDesc *var_desc) {
58 59 60 61
    nodes.emplace_back(new ir::Node(var_desc));
    return nodes.back().get();
  }

X
Xin Pan 已提交
62
  ir::Node *CreateOpNode(OpDesc *op_desc) {
63 64 65 66
    nodes.emplace_back(new ir::Node(op_desc));
    return nodes.back().get();
  }

X
Xin Pan 已提交
67
  ir::Node *CreateEmptyNode(const std::string &name, ir::Node::Type type) {
X
polish  
Xin Pan 已提交
68
    nodes.emplace_back(new ir::Node(name, type));
69 70 71
    return nodes.back().get();
  }

X
tmp  
Xin Pan 已提交
72
  std::vector<std::unique_ptr<ir::Node>> nodes;
X
Xin Pan 已提交
73 74

 private:
75
  // NOTE: program_ shouldn't be exposed to user.
X
Xin Pan 已提交
76
  const ProgramDesc &program_;
X
Xin Pan 已提交
77 78
  std::map<std::string, boost::any> attrs_;
  std::map<std::string, std::function<void(void)>> attr_dels_;
X
Xin Pan 已提交
79
};
X
Xin Pan 已提交
80
}  // namespace ir
X
Xin Pan 已提交
81
}  // namespace framework
X
start  
Xin Pan 已提交
82
}  // namespace paddle