Compute Library
 23.11
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 52 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 }

◆ 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) ||
75  (source_idx >= _nodes[source]->num_outputs()));
76  ARM_COMPUTE_ERROR_ON((sink >= _nodes.size()) || (_nodes[sink] == nullptr) ||
77  (sink_idx >= _nodes[sink]->num_inputs()));
78 
79  // Get nodes
80  std::unique_ptr<INode> &source_node = _nodes[source];
81  std::unique_ptr<INode> &sink_node = _nodes[sink];
82 
83  // Check for duplicate connections (Check only sink node)
84  Edge *sink_node_edge = sink_node->input_edge(sink_idx);
85  if ((sink_node_edge != nullptr) && (sink_node_edge->producer_id() == source) &&
86  (sink_node_edge->producer_idx() == source_idx) && (sink_node_edge->consumer_id() == sink) &&
87  (sink_node_edge->consumer_idx() == sink_idx))
88  {
89  return sink_node_edge->id();
90  }
91 
92  // Check if there is already a tensor associated with output if not create one
93  TensorID tid = source_node->output_id(source_idx);
94  if (tid == NullTensorID)
95  {
96  tid = create_tensor();
97  }
98  std::unique_ptr<Tensor> &tensor = _tensors[tid];
99 
100  // Create connections
101  EdgeID eid = _edges.size();
102  auto connection =
103  std::make_unique<Edge>(eid, source_node.get(), source_idx, sink_node.get(), sink_idx, tensor.get());
104  _edges.push_back(std::move(connection));
105 
106  // Add connections to source and sink nodes
107  source_node->_output_edges.insert(eid);
108  sink_node->_input_edges[sink_idx] = eid;
109 
110  // Set tensor output node
111  source_node->_outputs[source_idx] = tid;
112 
113  // Bind tensor to the edge
114  tensor->bind_edge(eid);
115 
116  // Try and propagate shapes in sink node
117  sink_node->forward_descriptors();
118 
119  return eid;
120 }

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_pad_with_convolution(), GroupedConvolutionMutator::mutate(), and arm_compute::graph::detail::transfer_driving_nodes_and_remove_old_node().

◆ 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 234 of file Graph.h.

235 {
237 
238  // Create node
239  NodeID nid = _nodes.size();
240  auto node = std::make_unique<NT>(std::forward<Ts>(args)...);
241  node->set_graph(this);
242  node->set_id(nid);
243 
244  // Keep track of input nodes
245  _tagged_nodes[node->type()].push_back(nid);
246 
247  // Associate a new tensor with each output
248  for (auto &output : node->_outputs)
249  {
250  output = create_tensor();
251  }
252 
253  // Propagate node shape if possible
255 
256  // Add node to the graph nodes
257  _nodes.push_back(std::move(node));
258 
259  return nid;
260 }

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]

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 223 of file Graph.cpp.

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

References Graph::id().

◆ edge() [2/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 218 of file Graph.cpp.

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

References Graph::id().

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_driver_nodes(), arm_compute::graph::get_driving_nodes(), INode::input(), INode::input_edge(), INode::input_id(), NodeFusionMutator::mutate(), Graph::remove_connection(), and INode::set_output_tensor().

◆ 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 193 of file Graph.cpp.

194 {
195  return _edges;
196 }

◆ id()

GraphID id ( ) const

Returns graph id.

Returns
Graph id

Definition at line 173 of file Graph.cpp.

174 {
175  return _id;
176 }

Referenced by Graph::edge(), GraphManager::execute_graph(), GraphManager::finalize_graph(), GraphManager::invalidate_graph(), Graph::node(), and Graph::tensor().

◆ name()

std::string name ( ) const

Returns graph name.

Returns
Graph name

Definition at line 168 of file Graph.cpp.

169 {
170  return _name;
171 }

◆ node() [1/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 213 of file Graph.cpp.

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

References Graph::id().

◆ node() [2/2]

◆ nodes() [1/3]

const 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 183 of file Graph.cpp.

184 {
185  return _nodes;
186 }

◆ nodes() [2/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

◆ nodes() [3/3]

◆ 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 122 of file Graph.cpp.

123 {
124  if (eid >= _edges.size())
125  {
126  return false;
127  }
128 
129  std::unique_ptr<Edge> &edge = _edges[eid];
130 
131  // Remove node connections
132  if (edge != nullptr)
133  {
134  // Get tensor bound to the edge
135  if (edge->tensor() != nullptr)
136  {
137  edge->tensor()->unbind_edge(eid);
138  }
139 
140  // Remove edges from source node
141  if (edge->producer() != nullptr)
142  {
143  edge->producer()->_output_edges.erase(eid);
144  }
145 
146  // Remove edges from sink node
147  if ((edge->consumer() != nullptr) && (edge->consumer_idx() < edge->consumer()->_input_edges.size()))
148  {
149  edge->consumer()->_input_edges[edge->consumer_idx()] = EmptyEdgeID;
150  }
151  }
152 
153  // Clear edge
154  edge = nullptr;
155 
156  return true;
157 }

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 }

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_pad_with_convolution(), GroupedConvolutionMutator::mutate(), and arm_compute::graph::detail::transfer_driving_nodes_and_remove_old_node().

◆ tensor() [1/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 233 of file Graph.cpp.

234 {
235  return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
236 }

References Graph::id().

◆ tensor() [2/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 228 of file Graph.cpp.

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

References Graph::id().

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

◆ tensors() [1/2]

const 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 198 of file Graph.cpp.

199 {
200  return _tensors;
201 }

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

The documentation for this class was generated from the following files:
arm_compute::graph::Graph::edge
const Edge * edge(EdgeID id) const
Get edge object given its id.
Definition: Graph.cpp:218
GemmTuner.args
args
Definition: GemmTuner.py:679
arm_compute::lock_guard
std::lock_guard< Mutex > lock_guard
Wrapper of lock_guard data-object.
Definition: Mutex.h:37
arm_compute::graph::EmptyEdgeID
constexpr EdgeID EmptyEdgeID
Definition: Types.h:81
type
decltype(strategy::transforms) typedef type
Definition: gemm_interleaved.hpp:261
arm_compute::graph::INode::output_edges
const std::set< EdgeID > & output_edges() const
Returns output edge set.
Definition: INode.cpp:132
arm_compute::graph::Graph::id
GraphID id() const
Returns graph id.
Definition: Graph.cpp:173
arm_compute::graph::INode::forward_descriptors
virtual bool forward_descriptors()=0
Forwards descriptor information to outputs if possible.
arm_compute::graph::Edge::consumer
INode * consumer() const
Returns consumer node.
Definition: Edge.h:102
ARM_COMPUTE_ERROR_ON
#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
arm_compute::graph::Edge::tensor
Tensor * tensor() const
Returns the tensor associated with this edge.
Definition: Edge.h:126
arm_compute::graph::Tensor::bind_edge
void bind_edge(EdgeID eid)
Binds the tensor with an edge.
Definition: Tensor.cpp:109
arm_compute::graph::Tensor::unbind_edge
void unbind_edge(EdgeID eid)
Unbinds an edge from a tensor.
Definition: Tensor.cpp:114
arm_compute::graph::INode::set_graph
void set_graph(Graph *g)
Sets the graph that this node is registered to.
Definition: INode.cpp:50
arm_compute::graph::NodeID
unsigned int NodeID
Definition: Types.h:72
arm_compute::graph::INode::type
virtual NodeType type() const =0
Returns node's type.
arm_compute::graph::Graph::node
const INode * node(NodeID id) const
Get node object given its id.
Definition: Graph.cpp:208
arm_compute::graph::Graph::tensor
const Tensor * tensor(TensorID id) const
Get tensor object given its id.
Definition: Graph.cpp:228
arm_compute::graph::Graph::name
std::string name() const
Returns graph name.
Definition: Graph.cpp:168
arm_compute::graph::Edge::consumer_idx
unsigned int consumer_idx() const
Returns the index of the input that consumes the result in the consumer node.
Definition: Edge.h:118
arm_compute::graph::NullTensorID
constexpr TensorID NullTensorID
Constant NodeID specifying an equivalent of null node.
Definition: Types.h:77
arm_compute::graph::Edge::producer
INode * producer() const
Returns producer node.
Definition: Edge.h:94
arm_compute::graph::INode::set_id
void set_id(NodeID id)
Sets the node id.
Definition: INode.cpp:56
arm_compute::graph::TensorID
unsigned int TensorID
Definition: Types.h:71
arm_compute::graph::Graph::remove_connection
bool remove_connection(EdgeID eid)
Removes an edge (connection)
Definition: Graph.cpp:122
arm_compute::graph::EdgeID
unsigned int EdgeID
Definition: Types.h:73