blob: 4ce53589d4f290a8019748856d40bc8f32e41e8a [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{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +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
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010044 if(node)
Isabella Gottardib28f29d2017-11-09 17:05:07 +000045 {
Georgios Pinitas4cb39092018-08-01 18:41:56 +010046 // Remove input connections
Georgios Pinitasd9eb2752018-04-03 13:44:29 +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();
Sheri Zhang747a2c62020-08-24 14:10:44 +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
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))
Anthony Barbier2a07e182017-08-04 18:20:27 +010085 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010086 return sink_node_edge->id();
Anthony Barbier2a07e182017-08-04 18:20:27 +010087 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010088
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)
Anthony Barbier2a07e182017-08-04 18:20:27 +010092 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010093 tid = create_tensor();
Anthony Barbier2a07e182017-08-04 18:20:27 +010094 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010095 std::unique_ptr<Tensor> &tensor = _tensors[tid];
96
97 // Create connections
98 EdgeID eid = _edges.size();
Georgios Pinitas40f51a62020-11-21 03:04:18 +000099 auto connection = std::make_unique<Edge>(eid, source_node.get(), source_idx, sink_node.get(), sink_idx, tensor.get());
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100100 _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}
117
118bool Graph::remove_connection(EdgeID eid)
119{
120 if(eid >= _edges.size())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100122 return false;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100123 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100124
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100125 std::unique_ptr<Edge> &edge = _edges[eid];
126
127 // Remove node connections
128 if(edge != nullptr)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100129 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100130 // 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 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100147 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100148
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100149 // Clear edge
150 edge = nullptr;
151
152 return true;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100153}
Gian Marco36a0a462018-01-12 10:21:40 +0000154
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100155TensorID Graph::create_tensor(const TensorDescriptor &desc)
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000156{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100157 TensorID tid = _tensors.size();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000158 auto tensor = std::make_unique<Tensor>(tid, desc);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100159 _tensors.push_back(std::move(tensor));
160
161 return tid;
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000162}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100163
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100164std::string Graph::name() const
Gian Marco36a0a462018-01-12 10:21:40 +0000165{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100166 return _name;
Gian Marco36a0a462018-01-12 10:21:40 +0000167}
168
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100169GraphID Graph::id() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100170{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100171 return _id;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100172}
173
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100174const std::vector<NodeID> &Graph::nodes(NodeType type)
Georgios Pinitasff421f22017-10-04 16:53:58 +0100175{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100176 return _tagged_nodes[type];
Georgios Pinitasff421f22017-10-04 16:53:58 +0100177}
178
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100179std::vector<std::unique_ptr<INode>> &Graph::nodes()
Anthony Barbier2a07e182017-08-04 18:20:27 +0100180{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100181 return _nodes;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100182}
183
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100184const std::vector<std::unique_ptr<INode>> &Graph::nodes() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100185{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100186 return _nodes;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100187}
188
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100189const std::vector<std::unique_ptr<Edge>> &Graph::edges() const
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100190{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100191 return _edges;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100192}
193
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100194std::vector<std::unique_ptr<Tensor>> &Graph::tensors()
Anthony Barbier2a07e182017-08-04 18:20:27 +0100195{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100196 return _tensors;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100197}
198
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100199const std::vector<std::unique_ptr<Tensor>> &Graph::tensors() const
Georgios Pinitasff421f22017-10-04 16:53:58 +0100200{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100201 return _tensors;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100202}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100203
204const INode *Graph::node(NodeID id) const
205{
206 return (id >= _nodes.size()) ? nullptr : _nodes[id].get();
207}
208
209INode *Graph::node(NodeID id)
210{
211 return (id >= _nodes.size()) ? nullptr : _nodes[id].get();
212}
213
214const Edge *Graph::edge(EdgeID id) const
215{
216 return (id >= _edges.size()) ? nullptr : _edges[id].get();
217}
218
219Edge *Graph::edge(EdgeID id)
220{
221 return (id >= _edges.size()) ? nullptr : _edges[id].get();
222}
223
224const Tensor *Graph::tensor(TensorID id) const
225{
226 return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
227}
228
229Tensor *Graph::tensor(TensorID id)
230{
231 return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
232}
233} // namespace graph
234} // namespace arm_compute