33 #include <unordered_map> 34 #include <unordered_set> 61 std::unordered_map<std::string, std::shared_ptr<any> >
attrs;
69 inline const T&
GetAttr(
const std::string& attr_name)
const;
75 inline bool HasAttr(
const std::string& attr_name)
const;
96 mutable std::shared_ptr<const IndexedGraph> indexed_graph_;
132 return nodes_.size();
136 return entry_rptr_.back();
145 inline uint32_t
entry_id(uint32_t node_id, uint32_t index)
const {
146 return entry_rptr_[node_id] + index;
164 return entry_rptr_[node_id(e.
node.get())] + e.
index;
172 return node2index_.at(node);
180 return nodes_[node_id];
188 return nodes_[node_id(node)];
196 return mutable_input_nodes_;
199 inline const std::vector<NodeEntry>&
outputs()
const {
205 return node2index_.count(node);
219 std::vector<Node> nodes_;
221 std::vector<uint32_t> input_nodes_;
223 std::unordered_set<uint32_t> mutable_input_nodes_;
225 std::vector<NodeEntry> outputs_;
227 std::unordered_map<const nnvm::Node*, uint32_t> node2index_;
229 std::vector<size_t> entry_rptr_;
231 std::vector<NodeEntry> input_entries_;
233 std::vector<uint32_t> control_deps_;
243 template<
typename FVisit>
244 inline void DFSVisit(
const std::vector<NodeEntry>& heads, FVisit fvisit);
249 auto it =
attrs.find(attr_name);
250 CHECK(it !=
attrs.end())
251 <<
"Cannot find attribute " << attr_name <<
" in the graph";
252 return nnvm::unsafe_get<T>(*it->second);
256 auto it =
attrs.find(attr_name);
257 return it !=
attrs.end();
262 auto it =
attrs.find(attr_name);
263 CHECK(it !=
attrs.end())
264 <<
"Cannot find attribute " << attr_name <<
" in the graph";
265 std::shared_ptr<any> sptr = it->second;
268 return std::move(nnvm::get<T>(*sptr));
270 return nnvm::get<T>(*sptr);
274 template <
typename GNode,
typename HashType,
275 typename FVisit,
typename HashFunc,
276 typename InDegree,
typename GetInput>
282 std::vector<std::pair<GNode, uint32_t> > stack;
283 std::unordered_set<HashType> visited;
284 for (
auto& head : heads) {
285 HashType head_hash = hash(head);
286 if (visited.count(head_hash) == 0) {
287 stack.push_back(std::make_pair(head, 0));
288 visited.insert(head_hash);
290 while (!stack.empty()) {
291 std::pair<GNode, uint32_t>& back = stack.back();
292 if (back.second == indegree(back.first)) {
296 const GNode& input = getinput(back.first, back.second++);
297 HashType input_hash = hash(input);
298 if (visited.count(input_hash) == 0) {
299 stack.push_back(std::make_pair(input, 0));
300 visited.insert(input_hash);
307 template<
typename FVisit>
308 inline void DFSVisit(
const std::vector<NodeEntry>& heads,
311 std::vector<GNode> head_nodes(heads.size());
312 std::transform(heads.begin(), heads.end(), head_nodes.begin(),
316 PostOrderDFSVisit<GNode, Node*>(
324 [](GNode n)->uint32_t {
326 return (*n)->
inputs.size() + (*n)->control_deps.size();
328 [](GNode n, uint32_t index)->GNode {
329 if (index < (*n)->inputs.size()) {
330 return &(*n)->inputs.at(index).node;
332 return &(*n)->control_deps.at(index - (*n)->inputs.size());
339 #endif // NNVM_GRAPH_H_ Read only data structure to reference continuous memory region of array. Provide unified view for vec...
Definition: array_view.h:36
bool HasAttr(const std::string &attr_name) const
Check whether has a specific attribute.
Definition: graph.h:255
void PostOrderDFSVisit(const std::vector< GNode > &heads, FVisit fvisit, HashFunc hash, InDegree indegree, GetInput getinput)
Definition: graph.h:277
Graph node data structure.
const std::unordered_set< uint32_t > & mutable_input_nodes() const
Definition: graph.h:195
const IndexedGraph & indexed_graph() const
get a indexed graph of current graph, if not exist, create it on demand
T MoveCopyAttr(const std::string &attr_name)
Get a move copy of the attribute, implement copy on write semantics. The content is moved if the refe...
Definition: graph.h:261
const std::vector< uint32_t > & input_nodes() const
Definition: graph.h:191
std::shared_ptr< Node > NodePtr
we always used NodePtr for a reference pointer to the node, so this alias can be changed in case...
Definition: node.h:49
std::vector< NodeEntry > inputs
inputs to this node
Definition: node.h:166
Node data structure in IndexedGraph.
Definition: graph.h:120
array_view< uint32_t > control_deps
control flow dependencies to the node
Definition: graph.h:126
const nnvm::Node * source
pointer to the source node
Definition: graph.h:122
NodePtr node
the source node of this data
Definition: node.h:76
std::unordered_map< std::string, std::shared_ptr< any > > attrs
attributes of a graph Note that attribute is shared pointer and can be shared across graphs...
Definition: graph.h:61
uint32_t entry_id(uint32_t node_id, uint32_t index) const
Get a unique entry id between 0 to num_node_entries() for a given IndexedGraph::NodeEntry.
Definition: graph.h:145
std::weak_ptr< nnvm::Node > weak_ref
weak reference to node
Definition: graph.h:128
bool exist(const nnvm::Node *node) const
Definition: graph.h:204
Node represents an operation in a computation graph.
Definition: node.h:156
size_t num_nodes() const
Definition: graph.h:131
void DFSVisit(const std::vector< NodeEntry > &heads, FVisit fvisit)
perform a Post Order DFS visit to each node in the graph. This order is deterministic and is also top...
Definition: graph.h:308
Symbolic computation graph. This is the intermediate representation for optimization pass...
Definition: graph.h:47
Auxiliary data structure to index a graph. It maps Nodes in the graph to consecutive integers node_id...
Definition: graph.h:108
uint32_t version
version of the node
Definition: graph.h:117
uint32_t node_id(const nnvm::Node *node) const
Get the corresponding node id for a given Node in the IndexedGraph.
Definition: graph.h:171
const Node & operator[](uint32_t node_id) const
Get the corresponding Node structure for a given node_id.
Definition: graph.h:179
const Node & operator[](const nnvm::Node *node) const
Get the corresponding Node structure.
Definition: graph.h:187
uint32_t entry_id(const nnvm::NodeEntry &e) const
Get a unique entry id between 0 to num_node_entries() for a given NodeEntry.
Definition: graph.h:163
an entry that represents output data from a node
Definition: node.h:52
uint32_t index
index of output from the source.
Definition: graph.h:115
const T & GetAttr(const std::string &attr_name) const
Get the immutable attribute from attrs.
Definition: graph.h:248
uint32_t node_id
the source node id in the computation graph
Definition: graph.h:113
size_t num_node_entries() const
Definition: graph.h:135
array_view< NodeEntry > inputs
inputs to the node
Definition: graph.h:124
represents a data in the graph
Definition: graph.h:111
std::vector< NodeEntry > outputs
outputs of the computation graph.
Definition: graph.h:50
uint32_t entry_id(const NodeEntry &e) const
Get a unique entry id between 0 to num_node_entries() for a given IndexedGraph::NodeEntry.
Definition: graph.h:154
const std::vector< NodeEntry > & outputs() const
Definition: graph.h:199
Symbolic graph construction API.
uint32_t index
index of output from the source.
Definition: node.h:78
Configuration of nnvm as well as basic data structure.