mxnet
node.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 
24 #ifndef NNVM_NODE_H_
25 #define NNVM_NODE_H_
26 
27 #include <memory>
28 #include <string>
29 #include <vector>
30 #include <utility>
31 #include <unordered_map>
32 #include "base.h"
33 #include "op.h"
34 #include "c_api.h"
35 
36 namespace nnvm {
37 
38 // Forward declare node.
39 class Node;
40 class Symbol;
41 
48 using ObjectPtr = std::shared_ptr<Node>;
49 
51 struct NodeEntry {
52  NodeEntry(ObjectPtr node, uint32_t index, uint32_t version):
53  node(std::move(node)),
54  index(index),
55  version(version)
56  {}
57 
59  node(std::move(node)),
60  index(),
61  version()
62  {}
63 
69  node(nullptr),
70  index(),
71  version()
72  {}
73 
77  uint32_t index;
84  uint32_t version;
85 };
86 
91 struct NodeEntryHash {
92  size_t operator()(const NodeEntry& e) const {
93  return std::hash<Node*>()(e.node.get()) ^
94  (std::hash<size_t>()(e.index) << 1 >> 1) ^
95  (std::hash<size_t>()(e.version) << 1);
96  }
97 };
98 
104  size_t operator()(const NodeEntry& a, const NodeEntry& b) const {
105  return (a.node.get() == b.node.get()) &&
106  (a.index == b.index) &&
107  (a.version == b.version);
108  }
109 };
110 
112 template<typename ValueType>
113 using NodeEntryMap = std::unordered_map<NodeEntry, ValueType, NodeEntryHash, NodeEntryEqual>;
114 
119 struct NodeAttrs {
124  const Op *op{nullptr};
126  std::string name;
128  std::unordered_map<std::string, std::string> dict;
134  any parsed;
149  std::vector<std::shared_ptr<Symbol> > subgraphs;
150 };
151 
155 class NNVM_DLL Node {
156  public:
157  Node() = default;
158  Node(const Op* op, const std::string& name) {
159  this->attrs.op = op;
160  this->attrs.name = name;
161  }
165  std::vector<NodeEntry> inputs;
170  std::vector<ObjectPtr> control_deps;
172  any info;
174  ~Node();
176  inline const Op* op() const;
182  inline bool is_variable() const;
184  inline uint32_t num_outputs() const;
186  inline uint32_t num_inputs() const;
191  template<class ...Args>
192  static ObjectPtr Create(Args&&... args) {
193  return std::make_shared<Node>(std::forward<Args>(args)...);
194  }
195 };
196 
206  const char* op_name,
207  std::string node_name,
208  std::vector<NodeEntry> inputs,
209  std::unordered_map<std::string, std::string> attrs =
210  std::unordered_map<std::string, std::string>()) {
211  ObjectPtr p = Node::Create();
212  p->attrs.op = nnvm::Op::Get(op_name);
213  p->attrs.name = std::move(node_name);
214  p->attrs.dict = attrs;
215  if (p->attrs.op->attr_parser) {
216  p->attrs.op->attr_parser(&(p->attrs));
217  }
218  p->inputs = std::move(inputs);
219  return NodeEntry(p, 0, 0);
220 }
221 
222 // implementation of functions.
223 inline const Op* Node::op() const {
224  return this->attrs.op;
225 }
226 
227 inline bool Node::is_variable() const {
228  return this->op() == nullptr;
229 }
230 
231 inline uint32_t Node::num_outputs() const {
232  if (is_variable()) return 1;
233  if (this->op()->get_num_outputs == nullptr) {
234  return this->op()->num_outputs;
235  } else {
236  return this->op()->get_num_outputs(this->attrs);
237  }
238 }
239 
240 inline uint32_t Node::num_inputs() const {
241  if (is_variable()) return 1;
242  if (this->op()->get_num_inputs == nullptr) {
243  return this->op()->num_inputs;
244  } else {
245  return this->op()->get_num_inputs(this->attrs);
246  }
247 }
248 
249 } // namespace nnvm
250 
251 #endif // NNVM_NODE_H_
uint32_t version
version of input Variable. This field can only be nonzero when this->node is a Variable node...
Definition: node.h:84
Definition: base.h:35
size_t operator()(const NodeEntry &a, const NodeEntry &b) const
Definition: node.h:104
Node(const Op *op, const std::string &name)
Definition: node.h:158
bool is_variable() const
return whether node is placeholder variable. This is equivalent to op == nullptr
Definition: node.h:227
The attributes of the current operation node. Usually are additional parameters like axis...
Definition: node.h:119
size_t operator()(const NodeEntry &e) const
Definition: node.h:92
This lets you use a NodeEntry as a key in a unordered_map of the form unordered_map<NodeEntry, ValueType, NodeEntryHash, NodeEntryEqual>
Definition: node.h:103
Definition: optional.h:241
ObjectPtr node
the source node of this data
Definition: node.h:75
std::vector< NodeEntry > inputs
inputs to this node
Definition: node.h:165
std::vector< ObjectPtr > control_deps
Optional control flow dependencies Gives operation must be performed before this operation.
Definition: node.h:170
NodeAttrs attrs
The attributes in the node.
Definition: node.h:163
std::vector< std::shared_ptr< Symbol > > subgraphs
Some operators take graphs as input. These operators include control flow operators and high-order fu...
Definition: node.h:149
Node represents an operation in a computation graph.
Definition: node.h:155
any parsed
A parsed version of attributes, This is generated if OpProperty.attr_parser is registered. The object can be used to quickly access attributes.
Definition: node.h:134
#define NNVM_DLL
NNVM_DLL prefix for windows.
Definition: c_api.h:37
This lets you use a NodeEntry as a key in a unordered_map of the form unordered_map<NodeEntry, ValueType, NodeEntryHash, NodeEntryEqual>
Definition: node.h:91
NodeEntry(ObjectPtr node)
Definition: node.h:58
std::string name
name of the node
Definition: node.h:126
std::string name
name of the operator
Definition: op.h:106
uint32_t num_inputs() const
Definition: node.h:240
uint32_t num_outputs() const
Definition: node.h:231
an entry that represents output data from a node
Definition: node.h:51
std::unordered_map< NodeEntry, ValueType, NodeEntryHash, NodeEntryEqual > NodeEntryMap
Definition: node.h:113
static ObjectPtr Create(Args &&...args)
create a new empty shared_ptr of Node.
Definition: node.h:192
NodeEntry()
Definition: node.h:68
NodeEntry MakeNode(const char *op_name, std::string node_name, std::vector< NodeEntry > inputs, std::unordered_map< std::string, std::string > attrs=std::unordered_map< std::string, std::string >())
Quick utilities make node.
Definition: node.h:205
std::unordered_map< std::string, std::string > dict
The dictionary representation of attributes.
Definition: node.h:128
Operator information structor.
static const Op * Get(const std::string &op_name)
Get an Op for a given operator name. Will raise an error if the op has not been registered.
NodeEntry(ObjectPtr node, uint32_t index, uint32_t version)
Definition: node.h:52
uint32_t index
index of output from the source.
Definition: node.h:77
C API of NNVM symbolic construction and pass. Enables construction and transformation of Graph in any...
std::shared_ptr< Node > ObjectPtr
we always used ObjectPtr for a reference pointer to the node, so this alias can be changed in case...
Definition: node.h:48
any info
additional fields for this node
Definition: node.h:172
Configuration of nnvm as well as basic data structure.
Operator structure.
Definition: op.h:103
const Op * op() const
Definition: node.h:223