blob: e1ffeed6688695cf22f6cd17d6d7580006a1f7cc [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Jenkinsb3a371b2018-05-23 11:36:53 +01002 * Copyright (c) 2018 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +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
Jenkinsb3a371b2018-05-23 11:36:53 +010026namespace arm_compute
Jenkinsc3f34a42018-03-02 12:38:09 +000027{
Jenkinsb3a371b2018-05-23 11:36:53 +010028namespace graph
Jenkinsc3f34a42018-03-02 12:38:09 +000029{
Jenkinsb3a371b2018-05-23 11:36:53 +010030Graph::Graph(GraphID id, std::string name)
31 : _id(id), _name(std::move(name)), _nodes(), _edges(), _tensors(), _tagged_nodes(), _mtx()
32{
Jenkinsc3f34a42018-03-02 12:38:09 +000033}
34
Jenkinsb3a371b2018-05-23 11:36:53 +010035bool Graph::remove_node(NodeID nid)
Kaizen8938bd32017-09-28 14:38:23 +010036{
Jenkinsb3a371b2018-05-23 11:36:53 +010037 if(nid >= _nodes.size())
Jenkinsc3f34a42018-03-02 12:38:09 +000038 {
Jenkinsb3a371b2018-05-23 11:36:53 +010039 return false;
Jenkinsc3f34a42018-03-02 12:38:09 +000040 }
Kaizen8938bd32017-09-28 14:38:23 +010041
Jenkinsb3a371b2018-05-23 11:36:53 +010042 std::unique_ptr<INode> &node = _nodes[nid];
Anthony Barbier06ea0482018-02-22 15:45:35 +000043
Jenkinsb3a371b2018-05-23 11:36:53 +010044 // Remove node connections
45 if(node)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000046 {
Jenkinsb3a371b2018-05-23 11:36:53 +010047 for(auto &input_eid : node->_input_edges)
Anthony Barbier06ea0482018-02-22 15:45:35 +000048 {
Jenkinsb3a371b2018-05-23 11:36:53 +010049 remove_connection(input_eid);
Anthony Barbier06ea0482018-02-22 15:45:35 +000050 }
Jenkinsb3a371b2018-05-23 11:36:53 +010051 for(auto &outpud_eid : node->_output_edges)
Kaizen8938bd32017-09-28 14:38:23 +010052 {
Jenkinsb3a371b2018-05-23 11:36:53 +010053 remove_connection(outpud_eid);
Kaizen8938bd32017-09-28 14:38:23 +010054 }
55 }
56
Jenkinsb3a371b2018-05-23 11:36:53 +010057 node = nullptr;
Kaizen8938bd32017-09-28 14:38:23 +010058
Jenkinsb3a371b2018-05-23 11:36:53 +010059 return true;
Kaizen8938bd32017-09-28 14:38:23 +010060}
61
Jenkinsb3a371b2018-05-23 11:36:53 +010062EdgeID Graph::add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx)
Kaizen8938bd32017-09-28 14:38:23 +010063{
Jenkinsb3a371b2018-05-23 11:36:53 +010064 std::lock_guard<arm_compute::Mutex> lock(_mtx);
Kaizen8938bd32017-09-28 14:38:23 +010065
Jenkinsb3a371b2018-05-23 11:36:53 +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))
Kaizen8938bd32017-09-28 14:38:23 +010078 {
Jenkinsb3a371b2018-05-23 11:36:53 +010079 return sink_node_edge->id();
Kaizen8938bd32017-09-28 14:38:23 +010080 }
Jenkinsb3a371b2018-05-23 11:36:53 +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)
Kaizen8938bd32017-09-28 14:38:23 +010085 {
Jenkinsb3a371b2018-05-23 11:36:53 +010086 tid = create_tensor();
Kaizen8938bd32017-09-28 14:38:23 +010087 }
Jenkinsb3a371b2018-05-23 11:36:53 +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())
Kaizen8938bd32017-09-28 14:38:23 +0100114 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100115 return false;
Kaizen8938bd32017-09-28 14:38:23 +0100116 }
Kaizen8938bd32017-09-28 14:38:23 +0100117
Jenkinsb3a371b2018-05-23 11:36:53 +0100118 std::unique_ptr<Edge> &edge = _edges[eid];
119
120 // Remove node connections
121 if(edge != nullptr)
Kaizen8938bd32017-09-28 14:38:23 +0100122 {
Jenkinsb3a371b2018-05-23 11:36:53 +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 }
Kaizen8938bd32017-09-28 14:38:23 +0100140 }
Kaizen8938bd32017-09-28 14:38:23 +0100141
Jenkinsb3a371b2018-05-23 11:36:53 +0100142 // Clear edge
143 edge = nullptr;
144
145 return true;
Kaizen8938bd32017-09-28 14:38:23 +0100146}
Anthony Barbier06ea0482018-02-22 15:45:35 +0000147
Jenkinsb3a371b2018-05-23 11:36:53 +0100148TensorID Graph::create_tensor(TensorDescriptor desc)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000149{
Jenkinsb3a371b2018-05-23 11:36:53 +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;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000155}
Kaizen8938bd32017-09-28 14:38:23 +0100156
Jenkinsb3a371b2018-05-23 11:36:53 +0100157std::string Graph::name() const
Anthony Barbier06ea0482018-02-22 15:45:35 +0000158{
Jenkinsb3a371b2018-05-23 11:36:53 +0100159 return _name;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000160}
161
Jenkinsb3a371b2018-05-23 11:36:53 +0100162GraphID Graph::id() const
Kaizen8938bd32017-09-28 14:38:23 +0100163{
Jenkinsb3a371b2018-05-23 11:36:53 +0100164 return _id;
Kaizen8938bd32017-09-28 14:38:23 +0100165}
166
Jenkinsb3a371b2018-05-23 11:36:53 +0100167const std::vector<NodeID> &Graph::inputs()
Kaizenbf8b01d2017-10-12 14:26:51 +0100168{
Jenkinsb3a371b2018-05-23 11:36:53 +0100169 return _tagged_nodes[NodeType::Input];
Kaizenbf8b01d2017-10-12 14:26:51 +0100170}
171
Jenkinsb3a371b2018-05-23 11:36:53 +0100172std::vector<std::unique_ptr<INode>> &Graph::nodes()
Kaizen8938bd32017-09-28 14:38:23 +0100173{
Jenkinsb3a371b2018-05-23 11:36:53 +0100174 return _nodes;
Kaizen8938bd32017-09-28 14:38:23 +0100175}
176
Jenkinsb3a371b2018-05-23 11:36:53 +0100177const std::vector<std::unique_ptr<INode>> &Graph::nodes() const
Kaizen8938bd32017-09-28 14:38:23 +0100178{
Jenkinsb3a371b2018-05-23 11:36:53 +0100179 return _nodes;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000180}
181
Jenkinsb3a371b2018-05-23 11:36:53 +0100182const std::vector<std::unique_ptr<Edge>> &Graph::edges() const
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000183{
Jenkinsb3a371b2018-05-23 11:36:53 +0100184 return _edges;
Kaizen8938bd32017-09-28 14:38:23 +0100185}
186
Jenkinsb3a371b2018-05-23 11:36:53 +0100187std::vector<std::unique_ptr<Tensor>> &Graph::tensors()
Kaizen8938bd32017-09-28 14:38:23 +0100188{
Jenkinsb3a371b2018-05-23 11:36:53 +0100189 return _tensors;
Kaizenbf8b01d2017-10-12 14:26:51 +0100190}
191
Jenkinsb3a371b2018-05-23 11:36:53 +0100192const std::vector<std::unique_ptr<Tensor>> &Graph::tensors() const
Kaizenbf8b01d2017-10-12 14:26:51 +0100193{
Jenkinsb3a371b2018-05-23 11:36:53 +0100194 return _tensors;
Kaizen8938bd32017-09-28 14:38:23 +0100195}
Jenkinsb3a371b2018-05-23 11:36:53 +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