Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 3 | * |
| 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 26 | namespace arm_compute |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 27 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 28 | namespace graph |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 29 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 30 | Graph::Graph(GraphID id, std::string name) |
| 31 | : _id(id), _name(std::move(name)), _nodes(), _edges(), _tensors(), _tagged_nodes(), _mtx() |
| 32 | { |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 35 | bool Graph::remove_node(NodeID nid) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 36 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 37 | if(nid >= _nodes.size()) |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 38 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 39 | return false; |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 40 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 41 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 42 | std::unique_ptr<INode> &node = _nodes[nid]; |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 43 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 44 | if(node) |
Isabella Gottardi | b28f29d | 2017-11-09 17:05:07 +0000 | [diff] [blame] | 45 | { |
Georgios Pinitas | 4cb3909 | 2018-08-01 18:41:56 +0100 | [diff] [blame] | 46 | // Remove input connections |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 47 | for(auto &input_eid : node->_input_edges) |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 48 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 49 | remove_connection(input_eid); |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 50 | } |
Georgios Pinitas | 4cb3909 | 2018-08-01 18:41:56 +0100 | [diff] [blame] | 51 | |
| 52 | // Remove output connections |
| 53 | std::set<EdgeID> output_edges_copy = node->output_edges(); |
| 54 | for(auto &outpud_eid : output_edges_copy) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 55 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 56 | remove_connection(outpud_eid); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 57 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 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()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 64 | node = nullptr; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 65 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 66 | return true; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 69 | EdgeID Graph::add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 70 | { |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 71 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 72 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 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) || (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 Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 85 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 86 | return sink_node_edge->id(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 87 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 88 | |
| 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 Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 92 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 93 | tid = create_tensor(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 94 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 95 | std::unique_ptr<Tensor> &tensor = _tensors[tid]; |
| 96 | |
| 97 | // Create connections |
| 98 | EdgeID eid = _edges.size(); |
| 99 | auto connection = arm_compute::support::cpp14::make_unique<Edge>(eid, source_node.get(), source_idx, sink_node.get(), sink_idx, tensor.get()); |
| 100 | _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 | |
| 118 | bool Graph::remove_connection(EdgeID eid) |
| 119 | { |
| 120 | if(eid >= _edges.size()) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 121 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 122 | return false; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 123 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 124 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 125 | std::unique_ptr<Edge> &edge = _edges[eid]; |
| 126 | |
| 127 | // Remove node connections |
| 128 | if(edge != nullptr) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 129 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 130 | // 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 Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 147 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 148 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 149 | // Clear edge |
| 150 | edge = nullptr; |
| 151 | |
| 152 | return true; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 153 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 154 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 155 | TensorID Graph::create_tensor(const TensorDescriptor &desc) |
Isabella Gottardi | b28f29d | 2017-11-09 17:05:07 +0000 | [diff] [blame] | 156 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 157 | TensorID tid = _tensors.size(); |
| 158 | auto tensor = support::cpp14::make_unique<Tensor>(tid, desc); |
| 159 | _tensors.push_back(std::move(tensor)); |
| 160 | |
| 161 | return tid; |
Isabella Gottardi | b28f29d | 2017-11-09 17:05:07 +0000 | [diff] [blame] | 162 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 163 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 164 | std::string Graph::name() const |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 165 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 166 | return _name; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 169 | GraphID Graph::id() const |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 170 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 171 | return _id; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 174 | const std::vector<NodeID> &Graph::nodes(NodeType type) |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 175 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 176 | return _tagged_nodes[type]; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 179 | std::vector<std::unique_ptr<INode>> &Graph::nodes() |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 180 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 181 | return _nodes; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 184 | const std::vector<std::unique_ptr<INode>> &Graph::nodes() const |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 185 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 186 | return _nodes; |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 189 | const std::vector<std::unique_ptr<Edge>> &Graph::edges() const |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 190 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 191 | return _edges; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 194 | std::vector<std::unique_ptr<Tensor>> &Graph::tensors() |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 195 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 196 | return _tensors; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 199 | const std::vector<std::unique_ptr<Tensor>> &Graph::tensors() const |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 200 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 201 | return _tensors; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 202 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 203 | |
| 204 | const INode *Graph::node(NodeID id) const |
| 205 | { |
| 206 | return (id >= _nodes.size()) ? nullptr : _nodes[id].get(); |
| 207 | } |
| 208 | |
| 209 | INode *Graph::node(NodeID id) |
| 210 | { |
| 211 | return (id >= _nodes.size()) ? nullptr : _nodes[id].get(); |
| 212 | } |
| 213 | |
| 214 | const Edge *Graph::edge(EdgeID id) const |
| 215 | { |
| 216 | return (id >= _edges.size()) ? nullptr : _edges[id].get(); |
| 217 | } |
| 218 | |
| 219 | Edge *Graph::edge(EdgeID id) |
| 220 | { |
| 221 | return (id >= _edges.size()) ? nullptr : _edges[id].get(); |
| 222 | } |
| 223 | |
| 224 | const Tensor *Graph::tensor(TensorID id) const |
| 225 | { |
| 226 | return (id >= _tensors.size()) ? nullptr : _tensors[id].get(); |
| 227 | } |
| 228 | |
| 229 | Tensor *Graph::tensor(TensorID id) |
| 230 | { |
| 231 | return (id >= _tensors.size()) ? nullptr : _tensors[id].get(); |
| 232 | } |
| 233 | } // namespace graph |
| 234 | } // namespace arm_compute |