Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | 747a2c6 | 2020-08-24 14:10:44 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 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 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +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 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +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 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +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(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | for (auto &output_eid : output_edges_copy) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 55 | { |
Sheri Zhang | 747a2c6 | 2020-08-24 14:10:44 +0100 | [diff] [blame] | 56 | remove_connection(output_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 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 78 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | 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 Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 88 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 89 | return sink_node_edge->id(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 90 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 91 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | if (tid == NullTensorID) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 95 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 96 | tid = create_tensor(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 97 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 98 | std::unique_ptr<Tensor> &tensor = _tensors[tid]; |
| 99 | |
| 100 | // Create connections |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 101 | 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 104 | _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 | |
| 122 | bool Graph::remove_connection(EdgeID eid) |
| 123 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 124 | if (eid >= _edges.size()) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 125 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 126 | return false; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 127 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 128 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 129 | std::unique_ptr<Edge> &edge = _edges[eid]; |
| 130 | |
| 131 | // Remove node connections |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | if (edge != nullptr) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 133 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 134 | // Get tensor bound to the edge |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 135 | if (edge->tensor() != nullptr) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 136 | { |
| 137 | edge->tensor()->unbind_edge(eid); |
| 138 | } |
| 139 | |
| 140 | // Remove edges from source node |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | if (edge->producer() != nullptr) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 142 | { |
| 143 | edge->producer()->_output_edges.erase(eid); |
| 144 | } |
| 145 | |
| 146 | // Remove edges from sink node |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | if ((edge->consumer() != nullptr) && (edge->consumer_idx() < edge->consumer()->_input_edges.size())) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 148 | { |
| 149 | edge->consumer()->_input_edges[edge->consumer_idx()] = EmptyEdgeID; |
| 150 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 151 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 152 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 153 | // Clear edge |
| 154 | edge = nullptr; |
| 155 | |
| 156 | return true; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 157 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 158 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 159 | TensorID Graph::create_tensor(const TensorDescriptor &desc) |
Isabella Gottardi | b28f29d | 2017-11-09 17:05:07 +0000 | [diff] [blame] | 160 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 161 | TensorID tid = _tensors.size(); |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 162 | auto tensor = std::make_unique<Tensor>(tid, desc); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 163 | _tensors.push_back(std::move(tensor)); |
| 164 | |
| 165 | return tid; |
Isabella Gottardi | b28f29d | 2017-11-09 17:05:07 +0000 | [diff] [blame] | 166 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 167 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 168 | std::string Graph::name() const |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 169 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 170 | return _name; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 173 | GraphID Graph::id() const |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 174 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 175 | return _id; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 178 | const std::vector<NodeID> &Graph::nodes(NodeType type) |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 179 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 180 | return _tagged_nodes[type]; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 183 | std::vector<std::unique_ptr<INode>> &Graph::nodes() |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 184 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 185 | return _nodes; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 188 | const std::vector<std::unique_ptr<INode>> &Graph::nodes() const |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 189 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 190 | return _nodes; |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 193 | const std::vector<std::unique_ptr<Edge>> &Graph::edges() const |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 194 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 195 | return _edges; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 198 | std::vector<std::unique_ptr<Tensor>> &Graph::tensors() |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 199 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 200 | return _tensors; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 203 | const std::vector<std::unique_ptr<Tensor>> &Graph::tensors() const |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 204 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 205 | return _tensors; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 206 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 207 | |
| 208 | const INode *Graph::node(NodeID id) const |
| 209 | { |
| 210 | return (id >= _nodes.size()) ? nullptr : _nodes[id].get(); |
| 211 | } |
| 212 | |
| 213 | INode *Graph::node(NodeID id) |
| 214 | { |
| 215 | return (id >= _nodes.size()) ? nullptr : _nodes[id].get(); |
| 216 | } |
| 217 | |
| 218 | const Edge *Graph::edge(EdgeID id) const |
| 219 | { |
| 220 | return (id >= _edges.size()) ? nullptr : _edges[id].get(); |
| 221 | } |
| 222 | |
| 223 | Edge *Graph::edge(EdgeID id) |
| 224 | { |
| 225 | return (id >= _edges.size()) ? nullptr : _edges[id].get(); |
| 226 | } |
| 227 | |
| 228 | const Tensor *Graph::tensor(TensorID id) const |
| 229 | { |
| 230 | return (id >= _tensors.size()) ? nullptr : _tensors[id].get(); |
| 231 | } |
| 232 | |
| 233 | Tensor *Graph::tensor(TensorID id) |
| 234 | { |
| 235 | return (id >= _tensors.size()) ? nullptr : _tensors[id].get(); |
| 236 | } |
| 237 | } // namespace graph |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 238 | } // namespace arm_compute |