mxnet
graph.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
25 #ifndef NNVM_GRAPH_H_
26 #define NNVM_GRAPH_H_
27 
28 #include <vector>
29 #include <string>
30 #include <utility>
31 #include <algorithm>
32 #include <memory>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include "base.h"
36 #include "node.h"
37 #include "symbolic.h"
38 
39 namespace nnvm {
40 
41 class IndexedGraph;
42 
47 class Graph {
48  public:
50  std::vector<NodeEntry> outputs;
61  std::unordered_map<std::string, std::shared_ptr<any> > attrs;
68  template<typename T>
69  inline const T& GetAttr(const std::string& attr_name) const;
75  inline bool HasAttr(const std::string& attr_name) const;
85  template<typename T>
86  inline T MoveCopyAttr(const std::string& attr_name);
92  const IndexedGraph& indexed_graph() const;
93 
94  private:
95  // internal structure of indexed graph
96  mutable std::shared_ptr<const IndexedGraph> indexed_graph_;
97 };
98 
109  public:
111  struct NodeEntry {
113  uint32_t node_id;
115  uint32_t index;
117  uint32_t version;
118  };
120  struct Node {
128  std::weak_ptr<nnvm::Node> weak_ref;
129  };
131  inline size_t num_nodes() const {
132  return nodes_.size();
133  }
135  inline size_t num_node_entries() const {
136  return entry_rptr_.back();
137  }
145  inline uint32_t entry_id(uint32_t node_id, uint32_t index) const {
146  return entry_rptr_[node_id] + index;
147  }
154  inline uint32_t entry_id(const NodeEntry& e) const {
155  return entry_rptr_[e.node_id] + e.index;
156  }
163  inline uint32_t entry_id(const nnvm::NodeEntry& e) const {
164  return entry_rptr_[node_id(e.node.get())] + e.index;
165  }
171  inline uint32_t node_id(const nnvm::Node* node) const {
172  return node2index_.at(node);
173  }
179  inline const Node& operator[](uint32_t node_id) const {
180  return nodes_[node_id];
181  }
187  inline const Node& operator[](const nnvm::Node* node) const {
188  return nodes_[node_id(node)];
189  }
191  inline const std::vector<uint32_t>& input_nodes() const {
192  return input_nodes_;
193  }
195  inline const std::unordered_set<uint32_t>& mutable_input_nodes() const {
196  return mutable_input_nodes_;
197  }
199  inline const std::vector<NodeEntry>& outputs() const {
200  return outputs_;
201  }
202 
204  inline bool exist(const nnvm::Node* node) const {
205  return node2index_.count(node);
206  }
207 
208  // disalllow copy assign
209  IndexedGraph(const IndexedGraph&) = delete;
210 
211  private:
212  friend class Graph;
217  explicit IndexedGraph(const Graph& other);
218  // Node pointers in CSR structure.
219  std::vector<Node> nodes_;
220  // Index to all input nodes.
221  std::vector<uint32_t> input_nodes_;
222  // Index to all mutable input nodes.
223  std::unordered_set<uint32_t> mutable_input_nodes_;
224  // space to store the outputs entries
225  std::vector<NodeEntry> outputs_;
226  // mapping from node to index.
227  std::unordered_map<const nnvm::Node*, uint32_t> node2index_;
228  // CSR pointer of node entries
229  std::vector<size_t> entry_rptr_;
230  // space to store input entries of each
231  std::vector<NodeEntry> input_entries_;
232  // control flow dependencies
233  std::vector<uint32_t> control_deps_;
234 };
235 
243 template<typename FVisit>
244 inline void DFSVisit(const std::vector<NodeEntry>& heads, FVisit fvisit);
245 
246 // inline function implementations
247 template<typename T>
248 inline const T& Graph::GetAttr(const std::string& attr_name) const {
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);
253 }
254 
255 inline bool Graph::HasAttr(const std::string& attr_name) const {
256  auto it = attrs.find(attr_name);
257  return it != attrs.end();
258 }
259 
260 template<typename T>
261 inline T Graph::MoveCopyAttr(const std::string& attr_name) {
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;
266  attrs.erase(it);
267  if (sptr.unique()) {
268  return std::move(nnvm::get<T>(*sptr));
269  } else {
270  return nnvm::get<T>(*sptr);
271  }
272 }
273 
274 template <typename GNode, typename HashType,
275  typename FVisit, typename HashFunc,
276  typename InDegree, typename GetInput>
277 void PostOrderDFSVisit(const std::vector<GNode>& heads,
278  FVisit fvisit,
279  HashFunc hash,
280  InDegree indegree,
281  GetInput 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);
289  }
290  while (!stack.empty()) {
291  std::pair<GNode, uint32_t>& back = stack.back();
292  if (back.second == indegree(back.first)) {
293  fvisit(back.first);
294  stack.pop_back();
295  } else {
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);
301  }
302  }
303  }
304  }
305 }
306 
307 template<typename FVisit>
308 inline void DFSVisit(const std::vector<NodeEntry>& heads,
309  FVisit fvisit) {
310  typedef const NodePtr* GNode;
311  std::vector<GNode> head_nodes(heads.size());
312  std::transform(heads.begin(), heads.end(), head_nodes.begin(),
313  [](const NodeEntry& e)->GNode {
314  return &e.node;
315  });
316  PostOrderDFSVisit<GNode, Node*>(
317  head_nodes,
318  [fvisit](GNode n) {
319  fvisit(*n);
320  }, // FVisit
321  [](GNode n)->Node* {
322  return n->get();
323  }, // HashFunc
324  [](GNode n)->uint32_t { // InDegree
325  if (!(*n)) return 0;
326  return (*n)->inputs.size() + (*n)->control_deps.size();
327  },
328  [](GNode n, uint32_t index)->GNode { // GetInput
329  if (index < (*n)->inputs.size()) {
330  return &(*n)->inputs.at(index).node;
331  } else {
332  return &(*n)->control_deps.at(index - (*n)->inputs.size());
333  }
334  });
335 }
336 
337 } // namespace nnvm
338 
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
Definition: base.h:36
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.