blob: e1ffeed6688695cf22f6cd17d6d7580006a1f7cc [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Georgios Pinitasd9eb2752018-04-03 13:44:29 +01002 * Copyright (c) 2018 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 // Remove node connections
45 if(node)
Isabella Gottardib28f29d2017-11-09 17:05:07 +000046 {
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 Pinitasd9eb2752018-04-03 13:44:29 +010051 for(auto &outpud_eid : node->_output_edges)
Anthony Barbier2a07e182017-08-04 18:20:27 +010052 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010053 remove_connection(outpud_eid);
Anthony Barbier2a07e182017-08-04 18:20:27 +010054 }
55 }
56
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010057 node = nullptr;
Anthony Barbier2a07e182017-08-04 18:20:27 +010058
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010059 return true;
Anthony Barbier2a07e182017-08-04 18:20:27 +010060}
61
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010062EdgeID Graph::add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx)
Anthony Barbier2a07e182017-08-04 18:20:27 +010063{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010064 std::lock_guard<arm_compute::Mutex> lock(_mtx);
Anthony Barbier2a07e182017-08-04 18:20:27 +010065
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010066 // Check if node index is valid, if node exists and finally if the connection index is valid
67 ARM_COMPUTE_ERROR_ON((source >= _nodes.size()) || (_nodes[source] == nullptr) || (source_idx >= _nodes[source]->num_outputs()));
68 ARM_COMPUTE_ERROR_ON((sink >= _nodes.size()) || (_nodes[sink] == nullptr) || (sink_idx >= _nodes[sink]->num_inputs()));
69
70 // Get nodes
71 std::unique_ptr<INode> &source_node = _nodes[source];
72 std::unique_ptr<INode> &sink_node = _nodes[sink];
73
74 // Check for duplicate connections (Check only sink node)
75 Edge *sink_node_edge = sink_node->input_edge(sink_idx);
76 if((sink_node_edge != nullptr) && (sink_node_edge->producer_id() == source) && (sink_node_edge->producer_idx() == source_idx)
77 && (sink_node_edge->consumer_id() == sink) && (sink_node_edge->consumer_idx() == sink_idx))
Anthony Barbier2a07e182017-08-04 18:20:27 +010078 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010079 return sink_node_edge->id();
Anthony Barbier2a07e182017-08-04 18:20:27 +010080 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010081
82 // Check if there is already a tensor associated with output if not create one
83 TensorID tid = source_node->output_id(source_idx);
84 if(tid == NullTensorID)
Anthony Barbier2a07e182017-08-04 18:20:27 +010085 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010086 tid = create_tensor();
Anthony Barbier2a07e182017-08-04 18:20:27 +010087 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010088 std::unique_ptr<Tensor> &tensor = _tensors[tid];
89
90 // Create connections
91 EdgeID eid = _edges.size();
92 auto connection = arm_compute::support::cpp14::make_unique<Edge>(eid, source_node.get(), source_idx, sink_node.get(), sink_idx, tensor.get());
93 _edges.push_back(std::move(connection));
94
95 // Add connections to source and sink nodes
96 source_node->_output_edges.insert(eid);
97 sink_node->_input_edges[sink_idx] = eid;
98
99 // Set tensor output node
100 source_node->_outputs[source_idx] = tid;
101
102 // Bind tensor to the edge
103 tensor->bind_edge(eid);
104
105 // Try and propagate shapes in sink node
106 sink_node->forward_descriptors();
107
108 return eid;
109}
110
111bool Graph::remove_connection(EdgeID eid)
112{
113 if(eid >= _edges.size())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100114 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100115 return false;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100116 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100117
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100118 std::unique_ptr<Edge> &edge = _edges[eid];
119
120 // Remove node connections
121 if(edge != nullptr)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100122 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100123 // Get tensor bound to the edge
124 if(edge->tensor() != nullptr)
125 {
126 edge->tensor()->unbind_edge(eid);
127 }
128
129 // Remove edges from source node
130 if(edge->producer() != nullptr)
131 {
132 edge->producer()->_output_edges.erase(eid);
133 }
134
135 // Remove edges from sink node
136 if((edge->consumer() != nullptr) && (edge->consumer_idx() < edge->consumer()->_input_edges.size()))
137 {
138 edge->consumer()->_input_edges[edge->consumer_idx()] = EmptyEdgeID;
139 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100140 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100141
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100142 // Clear edge
143 edge = nullptr;
144
145 return true;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100146}
Gian Marco36a0a462018-01-12 10:21:40 +0000147
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100148TensorID Graph::create_tensor(TensorDescriptor desc)
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000149{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100150 TensorID tid = _tensors.size();
151 auto tensor = support::cpp14::make_unique<Tensor>(tid, desc);
152 _tensors.push_back(std::move(tensor));
153
154 return tid;
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000155}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100156
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100157std::string Graph::name() const
Gian Marco36a0a462018-01-12 10:21:40 +0000158{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100159 return _name;
Gian Marco36a0a462018-01-12 10:21:40 +0000160}
161
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100162GraphID Graph::id() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100163{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100164 return _id;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100165}
166
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100167const std::vector<NodeID> &Graph::inputs()
Georgios Pinitasff421f22017-10-04 16:53:58 +0100168{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100169 return _tagged_nodes[NodeType::Input];
Georgios Pinitasff421f22017-10-04 16:53:58 +0100170}
171
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100172std::vector<std::unique_ptr<INode>> &Graph::nodes()
Anthony Barbier2a07e182017-08-04 18:20:27 +0100173{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100174 return _nodes;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100175}
176
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100177const std::vector<std::unique_ptr<INode>> &Graph::nodes() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100178{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100179 return _nodes;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100180}
181
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100182const std::vector<std::unique_ptr<Edge>> &Graph::edges() const
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100183{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100184 return _edges;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100185}
186
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100187std::vector<std::unique_ptr<Tensor>> &Graph::tensors()
Anthony Barbier2a07e182017-08-04 18:20:27 +0100188{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100189 return _tensors;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100190}
191
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100192const std::vector<std::unique_ptr<Tensor>> &Graph::tensors() const
Georgios Pinitasff421f22017-10-04 16:53:58 +0100193{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100194 return _tensors;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100195}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100196
197const INode *Graph::node(NodeID id) const
198{
199 return (id >= _nodes.size()) ? nullptr : _nodes[id].get();
200}
201
202INode *Graph::node(NodeID id)
203{
204 return (id >= _nodes.size()) ? nullptr : _nodes[id].get();
205}
206
207const Edge *Graph::edge(EdgeID id) const
208{
209 return (id >= _edges.size()) ? nullptr : _edges[id].get();
210}
211
212Edge *Graph::edge(EdgeID id)
213{
214 return (id >= _edges.size()) ? nullptr : _edges[id].get();
215}
216
217const Tensor *Graph::tensor(TensorID id) const
218{
219 return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
220}
221
222Tensor *Graph::tensor(TensorID id)
223{
224 return (id >= _tensors.size()) ? nullptr : _tensors[id].get();
225}
226} // namespace graph
227} // namespace arm_compute