blob: 3ae83f2e80f6243a6d6f026d29bec9abae11e6ff [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Sheri Zhang747a2c62020-08-24 14:10:44 +01002 * Copyright (c) 2018-2020 Arm Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/graph/Graph.h"
25
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026namespace arm_compute
Anthony Barbier8b811952018-02-28 13:47:58 +000027{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010028namespace graph
Anthony Barbier8b811952018-02-28 13:47:58 +000029{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010030Graph::Graph(GraphID id, std::string name)
31 : _id(id), _name(std::move(name)), _nodes(), _edges(), _tensors(), _tagged_nodes(), _mtx()
32{
Anthony Barbier8b811952018-02-28 13:47:58 +000033}
34
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010035bool Graph::remove_node(NodeID nid)
Anthony Barbier2a07e182017-08-04 18:20:27 +010036{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037 if (nid >= _nodes.size())
Anthony Barbier8b811952018-02-28 13:47:58 +000038 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010039 return false;
Anthony Barbier8b811952018-02-28 13:47:58 +000040 }
Anthony Barbier2a07e182017-08-04 18:20:27 +010041
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010042 std::unique_ptr<INode> &node = _nodes[nid];
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000043
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044 if (node)
Isabella Gottardib28f29d2017-11-09 17:05:07 +000045 {
Georgios Pinitas4cb39092018-08-01 18:41:56 +010046 // Remove input connections
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 for (auto &input_eid : node->_input_edges)
Anthony Barbier8b811952018-02-28 13:47:58 +000048 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010049 remove_connection(input_eid);
Anthony Barbier8b811952018-02-28 13:47:58 +000050 }
Georgios Pinitas4cb39092018-08-01 18:41:56 +010051
52 // Remove output connections
53 std::set<EdgeID> output_edges_copy = node->output_edges();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 for (auto &output_eid : output_edges_copy)
Anthony Barbier2a07e182017-08-04 18:20:27 +010055 {
Sheri Zhang747a2c62020-08-24 14:10:44 +010056 remove_connection(output_eid);
Anthony Barbier2a07e182017-08-04 18:20:27 +010057 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010058
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());
Anthony Barbier2a07e182017-08-04 18:20:27 +010062 }
63
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010064 node = nullptr;
Anthony Barbier2a07e182017-08-04 18:20:27 +010065
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010066 return true;
Anthony Barbier2a07e182017-08-04 18:20:27 +010067}
68
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010069EdgeID Graph::add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx)
Anthony Barbier2a07e182017-08-04 18:20:27 +010070{
Georgios Pinitas879d1312019-09-30 13:25:53 +010071 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
Anthony Barbier2a07e182017-08-04 18:20:27 +010072
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010073 // Check if node index is valid, if node exists and finally if the connection index is valid
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 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()));
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010078
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);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 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))
Anthony Barbier2a07e182017-08-04 18:20:27 +010088 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010089 return sink_node_edge->id();
Anthony Barbier2a07e182017-08-04 18:20:27 +010090 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010091
92 // Check if there is already a tensor associated with output if not create one
93 TensorID tid = source_node->output_id(source_idx);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if (tid == NullTensorID)
Anthony Barbier2a07e182017-08-04 18:20:27 +010095 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010096 tid = create_tensor();
Anthony Barbier2a07e182017-08-04 18:20:27 +010097 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010098 std::unique_ptr<Tensor> &tensor = _tensors[tid];
99
100 // Create connections
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101 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());
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100104 _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}
121
122bool Graph::remove_connection(EdgeID eid)
123{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 if (eid >= _edges.size())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100125 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100126 return false;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100127 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100128
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100129 std::unique_ptr<Edge> &edge = _edges[eid];
130
131 // Remove node connections
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 if (edge != nullptr)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100133 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100134 // Get tensor bound to the edge
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 if (edge->tensor() != nullptr)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100136 {
137 edge->tensor()->unbind_edge(eid);
138 }
139
140 // Remove edges from source node
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 if (edge->producer() != nullptr)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100142 {
143 edge->producer()->_output_edges.erase(eid);
144 }
145
146 // Remove edges from sink node
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 if ((edge->consumer() != nullptr) && (edge->consumer_idx() < edge->consumer()->_input_edges.size()))
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100148 {
149 edge->consumer()->_input_edges[edge->consumer_idx()] = EmptyEdgeID;
150 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100151 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100152
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100153 // Clear edge
154 edge = nullptr;
155
156 return true;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100157}
Gian Marco36a0a462018-01-12 10:21:40 +0000158
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100159TensorID Graph::create_tensor(const TensorDescriptor &desc)
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000160{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100161 TensorID tid = _tensors.size();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000162 auto tensor = std::make_unique<Tensor>(tid, desc);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100163 _tensors.push_back(std::move(tensor));
164
165 return tid;
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000166}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100167
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100168std::string Graph::name() const
Gian Marco36a0a462018-01-12 10:21:40 +0000169{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100170 return _name;
Gian Marco36a0a462018-01-12 10:21:40 +0000171}
172
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100173GraphID Graph::id() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100174{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100175 return _id;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100176}
177
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100178const std::vector<NodeID> &Graph::nodes(NodeType type)
Georgios Pinitasff421f22017-10-04 16:53:58 +0100179{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100180 return _tagged_nodes[type];
Georgios Pinitasff421f22017-10-04 16:53:58 +0100181}
182
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100183std::vector<std::unique_ptr<INode>> &Graph::nodes()
Anthony Barbier2a07e182017-08-04 18:20:27 +0100184{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100185 return _nodes;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100186}
187
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100188const std::vector<std::unique_ptr<INode>> &Graph::nodes() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100189{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100190 return _nodes;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100191}
192
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100193const std::vector<std::unique_ptr<Edge>> &Graph::edges() const
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100194{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100195 return _edges;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100196}
197
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100198std::vector<std::unique_ptr<Tensor>> &Graph::tensors()
Anthony Barbier2a07e182017-08-04 18:20:27 +0100199{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100200 return _tensors;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100201}
202
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100203const std::vector<std::unique_ptr<Tensor>> &Graph::tensors() const
Georgios Pinitasff421f22017-10-04 16:53:58 +0100204{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100205 return _tensors;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100206}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100207
208const INode *Graph::node(NodeID id) const
209{
210 return (id >= _nodes.size()) ? nullptr : _nodes[id].get();
211}
212
213INode *Graph::node(NodeID id)
214{
215 return (id >= _nodes.size()) ? nullptr : _nodes[id].get();
216}
217
218const Edge *Graph::edge(EdgeID id) const
219{
220 return (id >= _edges.size()) ? nullptr : _edges[id].get();
221}
222
223Edge *Graph::edge(EdgeID id)
224{
225 return (id >= _edges.size()) ? nullptr : _edges[id].get();
226}
227
228const Tensor *Graph::tensor(TensorID id) const
229{
230 return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
231}
232
233Tensor *Graph::tensor(TensorID id)
234{
235 return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
236}
237} // namespace graph
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100238} // namespace arm_compute