Compute Library
 21.05
Graph Class Referencefinal

Graph class. More...

#include <Graph.h>

Public Member Functions

 Graph ()=default
 
 Graph (GraphID id, std::string name)
 Constructor. More...
 
 Graph (const Graph &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
Graphoperator= (const Graph &)=delete
 Prevent instances of this class from being copy assigned (As this class contains pointers) More...
 
 Graph (Graph &&)=delete
 Prevent instances of this class from being moved (As this class contains non movable objects) More...
 
Graphoperator= (Graph &&)=delete
 Prevent instances of this class from being moved (As this class contains non movable objects) More...
 
template<typename NT , typename... Ts>
NodeID add_node (Ts &&... args)
 Adds a node to the graph. More...
 
bool remove_node (NodeID nid)
 Remove the node with the given ID. More...
 
EdgeID add_connection (NodeID source, size_t source_idx, NodeID sink, size_t sink_idx)
 Adds a connection between two nodes. More...
 
bool remove_connection (EdgeID eid)
 Removes an edge (connection) More...
 
std::string name () const
 Returns graph name. More...
 
GraphID id () const
 Returns graph id. More...
 
const std::vector< NodeID > & nodes (NodeType type)
 Returns graph input nodes. More...
 
std::vector< std::unique_ptr< INode > > & nodes ()
 Returns nodes of graph. More...
 
const std::vector< std::unique_ptr< INode > > & nodes () const
 Returns nodes of graph. More...
 
const std::vector< std::unique_ptr< Edge > > & edges () const
 Returns edges of graph. More...
 
std::vector< std::unique_ptr< Tensor > > & tensors ()
 Returns tensors of graph. More...
 
const std::vector< std::unique_ptr< Tensor > > & tensors () const
 Returns tensors of graph. More...
 
const INodenode (NodeID id) const
 Get node object given its id. More...
 
INodenode (NodeID id)
 Get node object given its id. More...
 
const Edgeedge (EdgeID id) const
 Get edge object given its id. More...
 
Edgeedge (EdgeID id)
 Get edge object given its id. More...
 
const Tensortensor (TensorID id) const
 Get tensor object given its id. More...
 
Tensortensor (TensorID id)
 Get tensor object given its id. More...
 

Detailed Description

Graph class.

Represents a multiple source - multiple sink directed graph

Definition at line 53 of file Graph.h.

Constructor & Destructor Documentation

◆ Graph() [1/4]

Graph ( )
default

◆ Graph() [2/4]

Graph ( GraphID  id,
std::string  name 
)

Constructor.

Parameters
[in]idGraph identification number. Can be used to differentiate between graphs. Default value 0
[in]nameGraph name. Default value empty string

Definition at line 30 of file Graph.cpp.

31  : _id(id), _name(std::move(name)), _nodes(), _edges(), _tensors(), _tagged_nodes(), _mtx()
32 {
33 }
std::string name() const
Returns graph name.
Definition: Graph.cpp:164

◆ Graph() [3/4]

Graph ( const Graph )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ Graph() [4/4]

Graph ( Graph &&  )
delete

Prevent instances of this class from being moved (As this class contains non movable objects)

Member Function Documentation

◆ add_connection()

EdgeID add_connection ( NodeID  source,
size_t  source_idx,
NodeID  sink,
size_t  sink_idx 
)

Adds a connection between two nodes.

Parameters
[in]sourceID of the source node
[in]source_idxOutput index of the source node
[in]sinkID of the sink node
[in]sink_idxInput index of the sink node
Returns
ID of this connection

Definition at line 69 of file Graph.cpp.

70 {
72 
73  // Check if node index is valid, if node exists and finally if the connection index is valid
74  ARM_COMPUTE_ERROR_ON((source >= _nodes.size()) || (_nodes[source] == nullptr) || (source_idx >= _nodes[source]->num_outputs()));
75  ARM_COMPUTE_ERROR_ON((sink >= _nodes.size()) || (_nodes[sink] == nullptr) || (sink_idx >= _nodes[sink]->num_inputs()));
76 
77  // Get nodes
78  std::unique_ptr<INode> &source_node = _nodes[source];
79  std::unique_ptr<INode> &sink_node = _nodes[sink];
80 
81  // Check for duplicate connections (Check only sink node)
82  Edge *sink_node_edge = sink_node->input_edge(sink_idx);
83  if((sink_node_edge != nullptr) && (sink_node_edge->producer_id() == source) && (sink_node_edge->producer_idx() == source_idx)
84  && (sink_node_edge->consumer_id() == sink) && (sink_node_edge->consumer_idx() == sink_idx))
85  {
86  return sink_node_edge->id();
87  }
88 
89  // Check if there is already a tensor associated with output if not create one
90  TensorID tid = source_node->output_id(source_idx);
91  if(tid == NullTensorID)
92  {
93  tid = create_tensor();
94  }
95  std::unique_ptr<Tensor> &tensor = _tensors[tid];
96 
97  // Create connections
98  EdgeID eid = _edges.size();
99  auto connection = std::make_unique<Edge>(eid, source_node.get(), source_idx, sink_node.get(), sink_idx, tensor.get());
100  _edges.push_back(std::move(connection));
101 
102  // Add connections to source and sink nodes
103  source_node->_output_edges.insert(eid);
104  sink_node->_input_edges[sink_idx] = eid;
105 
106  // Set tensor output node
107  source_node->_outputs[source_idx] = tid;
108 
109  // Bind tensor to the edge
110  tensor->bind_edge(eid);
111 
112  // Try and propagate shapes in sink node
113  sink_node->forward_descriptors();
114 
115  return eid;
116 }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
void bind_edge(EdgeID eid)
Binds the tensor with an edge.
Definition: Tensor.cpp:101
unsigned int EdgeID
Definition: Types.h:69
constexpr TensorID NullTensorID
Constant NodeID specifying an equivalent of null node.
Definition: Types.h:73
std::lock_guard< Mutex > lock_guard
Wrapper of lock_guard data-object.
Definition: Mutex.h:37
unsigned int TensorID
Definition: Types.h:67
const Tensor * tensor(TensorID id) const
Get tensor object given its id.
Definition: Graph.cpp:224

References ARM_COMPUTE_ERROR_ON, Tensor::bind_edge(), Edge::consumer_id(), Edge::consumer_idx(), Edge::id(), arm_compute::graph::NullTensorID, Edge::producer_id(), Edge::producer_idx(), and Graph::tensor().

Referenced by GraphBuilder::add_batch_normalization_node(), GraphBuilder::add_bounding_box_transform_node(), GraphBuilder::add_convolution_node(), GraphBuilder::add_deconvolution_node(), GraphBuilder::add_depthwise_convolution_node(), GraphBuilder::add_detection_output_node(), GraphBuilder::add_detection_post_process_node(), GraphBuilder::add_elementwise_node(), GraphBuilder::add_fully_connected_layer(), GraphBuilder::add_generate_proposals_node(), GraphBuilder::add_normalize_planar_yuv_node(), GraphBuilder::add_output_node(), GraphBuilder::add_prelu_node(), GraphBuilder::add_priorbox_node(), GraphBuilder::add_roi_align_node(), GraphBuilder::add_yolo_node(), arm_compute::graph::detail::fuse_convolution_with_batch_normalization(), arm_compute::graph::detail::fuse_depthwise_convolution_with_batch_normalization(), arm_compute::graph::detail::fuse_node_with_activation(), and GroupedConvolutionMutator::mutate().

◆ add_node()

NodeID add_node ( Ts &&...  args)
inline

Adds a node to the graph.

Note
Models a single output node
Template Parameters
NTNode operation
TsArguments to operation
Parameters
[in]argsNode arguments
Returns
ID of the node

Definition at line 235 of file Graph.h.

236 {
238 
239  // Create node
240  NodeID nid = _nodes.size();
241  auto node = std::make_unique<NT>(std::forward<Ts>(args)...);
242  node->set_graph(this);
243  node->set_id(nid);
244 
245  // Keep track of input nodes
246  _tagged_nodes[node->type()].push_back(nid);
247 
248  // Associate a new tensor with each output
249  for(auto &output : node->_outputs)
250  {
251  output = create_tensor();
252  }
253 
254  // Propagate node shape if possible
256 
257  // Add node to the graph nodes
258  _nodes.push_back(std::move(node));
259 
260  return nid;
261 }
void set_graph(Graph *g)
Sets the graph that this node is registered to.
Definition: INode.cpp:50
void set_id(NodeID id)
Sets the node id.
Definition: INode.cpp:56
unsigned int NodeID
Definition: Types.h:68
const INode * node(NodeID id) const
Get node object given its id.
Definition: Graph.cpp:204
virtual bool forward_descriptors()=0
Forwards descriptor information to outputs if possible.
virtual NodeType type() const =0
Returns node's type.
std::lock_guard< Mutex > lock_guard
Wrapper of lock_guard data-object.
Definition: Mutex.h:37

References GemmTuner::args, INode::forward_descriptors(), Graph::node(), INode::set_graph(), INode::set_id(), and INode::type().

Referenced by GraphBuilder::add_batch_normalization_node(), GraphBuilder::add_bounding_box_transform_node(), GraphBuilder::add_const_node(), GraphBuilder::add_convolution_node(), GraphBuilder::add_deconvolution_node(), GraphBuilder::add_depthwise_convolution_node(), GraphBuilder::add_detection_output_node(), GraphBuilder::add_detection_post_process_node(), GraphBuilder::add_elementwise_node(), GraphBuilder::add_fully_connected_layer(), GraphBuilder::add_generate_proposals_node(), GraphBuilder::add_input_node(), GraphBuilder::add_normalize_planar_yuv_node(), GraphBuilder::add_output_node(), GraphBuilder::add_prelu_node(), GraphBuilder::add_priorbox_node(), GraphBuilder::add_roi_align_node(), GraphBuilder::add_yolo_node(), arm_compute::graph::detail::fuse_convolution_with_batch_normalization(), and arm_compute::graph::detail::fuse_depthwise_convolution_with_batch_normalization().

◆ edge() [1/2]

const Edge * edge ( EdgeID  id) const

Get edge object given its id.

Warning
Can be nullptr if node was removed during the mutation steps of the graph
Parameters
[in]idEdge ID
Returns
The actual edge object

Definition at line 214 of file Graph.cpp.

215 {
216  return (id >= _edges.size()) ? nullptr : _edges[id].get();
217 }

Referenced by arm_compute::graph::detail::all_inputs_are_visited(), arm_compute::graph::bfs(), arm_compute::graph::dfs(), arm_compute::graph::detail::fuse_layer(), arm_compute::graph::get_driving_nodes(), INode::input(), INode::input_edge(), INode::input_id(), NodeFusionMutator::mutate(), DepthConcatSubTensorMutator::mutate(), Graph::remove_connection(), and INode::set_output_tensor().

◆ edge() [2/2]

Edge * edge ( EdgeID  id)

Get edge object given its id.

Warning
Can be nullptr if node was removed during the mutation steps of the graph
Parameters
[in]idEdge ID
Returns
The actual edge object

Definition at line 219 of file Graph.cpp.

220 {
221  return (id >= _edges.size()) ? nullptr : _edges[id].get();
222 }

◆ edges()

const std::vector< std::unique_ptr< Edge > > & edges ( ) const

Returns edges of graph.

Warning
Edges can be nullptr if they have been removed during the mutation steps of the graph
Returns
Edges of graph

Definition at line 189 of file Graph.cpp.

190 {
191  return _edges;
192 }

◆ id()

GraphID id ( ) const

Returns graph id.

Returns
Graph id

Definition at line 169 of file Graph.cpp.

170 {
171  return _id;
172 }

Referenced by GraphManager::execute_graph(), GraphManager::finalize_graph(), and GraphManager::invalidate_graph().

◆ name()

std::string name ( ) const

Returns graph name.

Returns
Graph name

Definition at line 164 of file Graph.cpp.

165 {
166  return _name;
167 }

◆ node() [1/2]

◆ node() [2/2]

INode * node ( NodeID  id)

Get node object given its id.

Warning
Can be nullptr if node was removed during the mutation steps of the graph
Parameters
[in]idNode ID
Returns
The actual node object

Definition at line 209 of file Graph.cpp.

210 {
211  return (id >= _nodes.size()) ? nullptr : _nodes[id].get();
212 }

◆ nodes() [1/3]

◆ nodes() [2/3]

std::vector< std::unique_ptr< INode > > & nodes ( )

Returns nodes of graph.

Warning
Nodes can be nullptr if they have been removed during the mutation steps of the graph
Returns
Nodes of graph

Definition at line 179 of file Graph.cpp.

180 {
181  return _nodes;
182 }

◆ nodes() [3/3]

const std::vector< std::unique_ptr< INode > > & nodes ( ) const

Returns nodes of graph.

Warning
Nodes can be nullptr if they have been removed during the mutation steps of the graph
Returns
Nodes of graph

Definition at line 184 of file Graph.cpp.

185 {
186  return _nodes;
187 }

◆ operator=() [1/2]

Graph& operator= ( const Graph )
delete

Prevent instances of this class from being copy assigned (As this class contains pointers)

◆ operator=() [2/2]

Graph& operator= ( Graph &&  )
delete

Prevent instances of this class from being moved (As this class contains non movable objects)

◆ remove_connection()

bool remove_connection ( EdgeID  eid)

Removes an edge (connection)

Parameters
[in]eidConnection to remove
Returns
True if the removal took place else false

Definition at line 118 of file Graph.cpp.

119 {
120  if(eid >= _edges.size())
121  {
122  return false;
123  }
124 
125  std::unique_ptr<Edge> &edge = _edges[eid];
126 
127  // Remove node connections
128  if(edge != nullptr)
129  {
130  // Get tensor bound to the edge
131  if(edge->tensor() != nullptr)
132  {
133  edge->tensor()->unbind_edge(eid);
134  }
135 
136  // Remove edges from source node
137  if(edge->producer() != nullptr)
138  {
139  edge->producer()->_output_edges.erase(eid);
140  }
141 
142  // Remove edges from sink node
143  if((edge->consumer() != nullptr) && (edge->consumer_idx() < edge->consumer()->_input_edges.size()))
144  {
145  edge->consumer()->_input_edges[edge->consumer_idx()] = EmptyEdgeID;
146  }
147  }
148 
149  // Clear edge
150  edge = nullptr;
151 
152  return true;
153 }
unsigned int consumer_idx() const
Returns the index of the input that consumes the result in the consumer node.
Definition: Edge.h:108
INode * consumer() const
Returns consumer node.
Definition: Edge.h:92
Tensor * tensor() const
Returns the tensor associated with this edge.
Definition: Edge.h:116
void unbind_edge(EdgeID eid)
Unbinds an edge from a tensor.
Definition: Tensor.cpp:106
constexpr EdgeID EmptyEdgeID
Definition: Types.h:77
const Edge * edge(EdgeID id) const
Get edge object given its id.
Definition: Graph.cpp:214
INode * producer() const
Returns producer node.
Definition: Edge.h:84

References Edge::consumer(), Edge::consumer_idx(), Graph::edge(), arm_compute::graph::EmptyEdgeID, Edge::producer(), Edge::tensor(), and Tensor::unbind_edge().

Referenced by Graph::remove_node().

◆ remove_node()

bool remove_node ( NodeID  nid)

Remove the node with the given ID.

Parameters
[in]nidID of the node to remove
Returns
True if the removal took place else false

Definition at line 35 of file Graph.cpp.

36 {
37  if(nid >= _nodes.size())
38  {
39  return false;
40  }
41 
42  std::unique_ptr<INode> &node = _nodes[nid];
43 
44  if(node)
45  {
46  // Remove input connections
47  for(auto &input_eid : node->_input_edges)
48  {
49  remove_connection(input_eid);
50  }
51 
52  // Remove output connections
53  std::set<EdgeID> output_edges_copy = node->output_edges();
54  for(auto &output_eid : output_edges_copy)
55  {
56  remove_connection(output_eid);
57  }
58 
59  // Remove nid from tagged nodes
60  std::vector<NodeID> &tnodes = _tagged_nodes.at(node->type());
61  tnodes.erase(std::remove(tnodes.begin(), tnodes.end(), nid), tnodes.end());
62  }
63 
64  node = nullptr;
65 
66  return true;
67 }
const std::set< EdgeID > & output_edges() const
Returns output edge set.
Definition: INode.cpp:132
bool remove_connection(EdgeID eid)
Removes an edge (connection)
Definition: Graph.cpp:118
const INode * node(NodeID id) const
Get node object given its id.
Definition: Graph.cpp:204
virtual NodeType type() const =0
Returns node's type.

References Graph::node(), INode::output_edges(), Graph::remove_connection(), and INode::type().

Referenced by arm_compute::graph::detail::fuse_convolution_with_batch_normalization(), arm_compute::graph::detail::fuse_depthwise_convolution_with_batch_normalization(), arm_compute::graph::detail::fuse_node_with_activation(), and GroupedConvolutionMutator::mutate().

◆ tensor() [1/2]

const Tensor * tensor ( TensorID  id) const

Get tensor object given its id.

Warning
Can be nullptr if tensor was removed during the mutation steps of the graph
Parameters
[in]idTensor ID
Returns
The actual tensor object

Definition at line 224 of file Graph.cpp.

225 {
226  return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
227 }

Referenced by Graph::add_connection(), arm_compute::graph::get_tensor_descriptor(), SplitLayerSubTensorMutator::mutate(), INode::output(), and INode::set_output_tensor().

◆ tensor() [2/2]

Tensor * tensor ( TensorID  id)

Get tensor object given its id.

Warning
Can be nullptr if tensor was removed during the mutation steps of the graph
Parameters
[in]idTensor ID
Returns
The actual tensor object

Definition at line 229 of file Graph.cpp.

230 {
231  return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
232 }

◆ tensors() [1/2]

std::vector< std::unique_ptr< Tensor > > & tensors ( )

Returns tensors of graph.

Warning
Tensor can be nullptr if they have been removed during the mutation steps of the graph
Returns
Tensors of graph

Definition at line 194 of file Graph.cpp.

195 {
196  return _tensors;
197 }

Referenced by arm_compute::graph::detail::allocate_all_tensors(), arm_compute::graph::detail::configure_all_tensors(), arm_compute::graph::force_target_to_graph(), GroupedConvolutionMutator::mutate(), and arm_compute::graph::detail::release_unused_tensors().

◆ tensors() [2/2]

const std::vector< std::unique_ptr< Tensor > > & tensors ( ) const

Returns tensors of graph.

Warning
Tensor can be nullptr if they have been removed during the mutation steps of the graph
Returns
Tensors of graph

Definition at line 199 of file Graph.cpp.

200 {
201  return _tensors;
202 }

The documentation for this class was generated from the following files: