/* 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. */ #pragma once #include #include #include #include #include "paddle/fluid/framework/ir/node.h" #include "paddle/fluid/framework/program_desc.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/variant.h" namespace paddle { namespace framework { namespace ir { class Graph { public: explicit Graph(const ProgramDesc &program); virtual ~Graph() { for (auto &attr : attrs_) { attr_dels_[attr.first](); } attrs_.clear(); attr_dels_.clear(); } template AttrType &Get(const std::string &attr_name) const { return *boost::any_cast(attrs_.at(attr_name)); } template void Set(const std::string &attr_name, AttrType *attr) { PADDLE_ENFORCE(attrs_.count(attr_name) == 0); attrs_[attr_name] = attr; attr_dels_[attr_name] = [attr, attr_name]() { VLOG(3) << "deleting " << attr_name; delete attr; }; } const std::unordered_set &Nodes() const { return node_set_; } ir::Node *CreateVarNode(VarDesc *var_desc) { return AddNode(new ir::Node(var_desc)); } ir::Node *CreateOpNode(OpDesc *op_desc) { return AddNode(new ir::Node(op_desc)); } ir::Node *CreateControlDepVar() { // TODO(panyx0718): control var name should be unique. const std::string name = string::Sprintf( "%s@%llu", ir::Node::kControlDepVarName, node_set_.size()); return AddNode(new ir::Node(name, ir::Node::Type::kVariable)); } ir::Node *CreateEmptyNode(const std::string &name, ir::Node::Type type) { return AddNode(new ir::Node(name, type)); } std::vector> ReleaseNodes() { std::vector> ret; for (auto &n : nodes_) { ret.emplace_back(n.second.release()); } nodes_.clear(); node_set_.clear(); return ret; } private: // 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); } // NOTE: program_ shouldn't be exposed to user. const ProgramDesc &program_; std::map attrs_; std::map> attr_dels_; std::map> nodes_; std::unordered_set node_set_; }; } // namespace ir } // namespace framework } // namespace paddle