You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Paddle/paddle/fluid/framework/ir/graph.h

83 lines
2.2 KiB

7 years ago
/* Copyright (c) 2016 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
7 years ago
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "paddle/fluid/framework/ir/node.h"
#include "paddle/fluid/framework/program_desc.h"
7 years ago
#include "paddle/fluid/platform/enforce.h"
7 years ago
#include "paddle/fluid/platform/variant.h"
7 years ago
namespace paddle {
7 years ago
namespace framework {
class Graph {
public:
7 years ago
explicit Graph(const ProgramDesc& program);
7 years ago
virtual ~Graph() {
7 years ago
for (auto& attr : attrs_) {
attr_dels_[attr.first]();
7 years ago
}
7 years ago
attrs_.clear();
attr_dels_.clear();
7 years ago
}
template <typename AttrType>
7 years ago
AttrType& Get(const std::string& attr_name) const {
return *boost::any_cast<AttrType*>(attrs_.at(attr_name));
7 years ago
}
template <typename AttrType>
void Set(const std::string& attr_name, AttrType* attr) {
7 years ago
PADDLE_ENFORCE(attrs_.count(attr_name) == 0);
7 years ago
attrs_[attr_name] = attr;
attr_dels_[attr_name] = [attr, attr_name]() {
VLOG(3) << "deleting " << attr_name;
delete attr;
};
7 years ago
}
7 years ago
ir::Node* CreateVarNode(VarDesc* var_desc) {
nodes.emplace_back(new ir::Node(var_desc));
return nodes.back().get();
}
ir::Node* CreateOpNode(OpDesc* op_desc) {
nodes.emplace_back(new ir::Node(op_desc));
return nodes.back().get();
}
7 years ago
ir::Node* CreateEmptyNode(const std::string& name, ir::Node::Type type) {
nodes.emplace_back(new ir::Node(name, type));
return nodes.back().get();
}
7 years ago
std::vector<std::unique_ptr<ir::Node>> nodes;
7 years ago
private:
// NOTE: program_ shouldn't be exposed to user.
const ProgramDesc& program_;
7 years ago
std::map<std::string, boost::any> attrs_;
std::map<std::string, std::function<void(void)>> attr_dels_;
7 years ago
};
} // namespace framework
7 years ago
} // namespace paddle