32 #include <unordered_map> 33 #include <unordered_set> 60 std::unordered_map<std::string, std::shared_ptr<any> >
attrs;
68 inline const T&
GetAttr(
const std::string& attr_name)
const;
74 inline bool HasAttr(
const std::string& attr_name)
const;
95 mutable std::shared_ptr<const IndexedGraph> indexed_graph_;
131 return nodes_.size();
135 return entry_rptr_.back();
144 inline uint32_t
entry_id(uint32_t node_id, uint32_t index)
const {
145 return entry_rptr_[node_id] + index;
163 return entry_rptr_[node_id(e.
node.get())] + e.
index;
171 return node2index_.at(node);
179 return nodes_[node_id];
187 return nodes_[node_id(node)];
195 return mutable_input_nodes_;
198 inline const std::vector<NodeEntry>&
outputs()
const {
204 return node2index_.count(node);
218 std::vector<Node> nodes_;
220 std::vector<uint32_t> input_nodes_;
222 std::unordered_set<uint32_t> mutable_input_nodes_;
224 std::vector<NodeEntry> outputs_;
226 std::unordered_map<const nnvm::Node*, uint32_t> node2index_;
228 std::vector<size_t> entry_rptr_;
230 std::vector<NodeEntry> input_entries_;
232 std::vector<uint32_t> control_deps_;
242 template<
typename FVisit>
243 inline void DFSVisit(
const std::vector<NodeEntry>& heads, FVisit fvisit);
248 auto it =
attrs.find(attr_name);
249 CHECK(it !=
attrs.end())
250 <<
"Cannot find attribute " << attr_name <<
" in the graph";
251 return nnvm::unsafe_get<T>(*it->second);
255 auto it =
attrs.find(attr_name);
256 return it !=
attrs.end();
261 auto it =
attrs.find(attr_name);
262 CHECK(it !=
attrs.end())
263 <<
"Cannot find attribute " << attr_name <<
" in the graph";
264 std::shared_ptr<any> sptr = it->second;
267 return std::move(nnvm::get<T>(*sptr));
269 return nnvm::get<T>(*sptr);
273 template <
typename GNode,
typename HashType,
274 typename FVisit,
typename HashFunc,
275 typename InDegree,
typename GetInput>
281 std::vector<std::pair<GNode, uint32_t> > stack;
282 std::unordered_set<HashType> visited;
283 for (
auto& head : heads) {
284 HashType head_hash = hash(head);
285 if (visited.count(head_hash) == 0) {
286 stack.push_back(std::make_pair(head, 0));
287 visited.insert(head_hash);
289 while (!stack.empty()) {
290 std::pair<GNode, uint32_t>& back = stack.back();
291 if (back.second == indegree(back.first)) {
295 const GNode& input = getinput(back.first, back.second++);
296 HashType input_hash = hash(input);
297 if (visited.count(input_hash) == 0) {
298 stack.push_back(std::make_pair(input, 0));
299 visited.insert(input_hash);
306 template<
typename FVisit>
307 inline void DFSVisit(
const std::vector<NodeEntry>& heads,
310 std::vector<GNode> head_nodes(heads.size());
311 std::transform(heads.begin(), heads.end(), head_nodes.begin(),
315 PostOrderDFSVisit<GNode, Node*>(
323 [](GNode n)->uint32_t {
325 return (*n)->
inputs.size() + (*n)->control_deps.size();
327 [](GNode n, uint32_t index)->GNode {
328 if (index < (*n)->inputs.size()) {
329 return &(*n)->inputs.at(index).node;
331 return &(*n)->control_deps.at(index - (*n)->inputs.size());
338 #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:254
void PostOrderDFSVisit(const std::vector< GNode > &heads, FVisit fvisit, HashFunc hash, InDegree indegree, GetInput getinput)
Definition: graph.h:276
const std::unordered_set< uint32_t > & mutable_input_nodes() const
Definition: graph.h:194
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:260
const std::vector< uint32_t > & input_nodes() const
Definition: graph.h:190
ObjectPtr node
the source node of this data
Definition: node.h:75
std::vector< NodeEntry > inputs
inputs to this node
Definition: node.h:165
Node data structure in IndexedGraph.
Definition: graph.h:119
array_view< uint32_t > control_deps
control flow dependencies to the node
Definition: graph.h:125
const nnvm::Node * source
pointer to the source node
Definition: graph.h:121
Graph node data structure.
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:60
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:144
std::weak_ptr< nnvm::Node > weak_ref
weak reference to node
Definition: graph.h:127
bool exist(const nnvm::Node *node) const
Definition: graph.h:203
Node represents an operation in a computation graph.
Definition: node.h:155
size_t num_nodes() const
Definition: graph.h:130
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:307
Symbolic computation graph. This is the intermediate representation for optimization pass...
Definition: graph.h:46
Auxiliary data structure to index a graph. It maps Nodes in the graph to consecutive integers node_id...
Definition: graph.h:107
uint32_t version
version of the node
Definition: graph.h:116
uint32_t node_id(const nnvm::Node *node) const
Get the corresponding node id for a given Node in the IndexedGraph.
Definition: graph.h:170
const Node & operator[](uint32_t node_id) const
Get the corresponding Node structure for a given node_id.
Definition: graph.h:178
const Node & operator[](const nnvm::Node *node) const
Get the corresponding Node structure.
Definition: graph.h:186
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:162
an entry that represents output data from a node
Definition: node.h:51
uint32_t index
index of output from the source.
Definition: graph.h:114
const T & GetAttr(const std::string &attr_name) const
Get the immutable attribute from attrs.
Definition: graph.h:247
uint32_t node_id
the source node id in the computation graph
Definition: graph.h:112
size_t num_node_entries() const
Definition: graph.h:134
array_view< NodeEntry > inputs
inputs to the node
Definition: graph.h:123
represents a data in the graph
Definition: graph.h:110
std::vector< NodeEntry > outputs
outputs of the computation graph.
Definition: graph.h:49
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:153
const std::vector< NodeEntry > & outputs() const
Definition: graph.h:198
Symbolic graph construction API.
uint32_t index
index of output from the source.
Definition: node.h:77
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
Configuration of nnvm as well as basic data structure.