Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 7d9a78e | 2023-05-31 13:18:33 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020,2023 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_GRAPH_GRAPH_H |
| 25 | #define ARM_COMPUTE_GRAPH_GRAPH_H |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 26 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/Edge.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/INode.h" |
| 29 | #include "arm_compute/graph/Tensor.h" |
| 30 | #include "arm_compute/graph/Types.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 31 | |
| 32 | #include "support/Mutex.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 33 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 34 | #include <map> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 35 | #include <memory> |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 36 | #include <string> |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 37 | #include <utility> |
| 38 | #include <vector> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 39 | |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 40 | #ifndef BARE_METAL |
| 41 | #include <thread> |
| 42 | #endif /* BARE_METAL */ |
| 43 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 44 | namespace arm_compute |
| 45 | { |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 46 | namespace graph |
| 47 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 48 | /** Graph class |
| 49 | * |
| 50 | * Represents a multiple source - multiple sink directed graph |
| 51 | */ |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 52 | class Graph final |
| 53 | { |
| 54 | public: |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 55 | Graph() = default; |
| 56 | /** Constructor |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 57 | * |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 58 | * @param[in] id Graph identification number. Can be used to differentiate between graphs. Default value 0 |
| 59 | * @param[in] name Graph name. Default value empty string |
Michele Di Giorgio | e3fba0a | 2018-02-14 14:18:01 +0000 | [diff] [blame] | 60 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 61 | Graph(GraphID id, std::string name); |
| 62 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 63 | Graph(const Graph &) = delete; |
| 64 | /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ |
| 65 | Graph &operator=(const Graph &) = delete; |
Georgios Pinitas | ca4111d | 2020-02-21 13:21:37 +0000 | [diff] [blame] | 66 | /** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 67 | Graph(Graph &&) = delete; |
| 68 | /** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 69 | Graph &operator=(Graph &&) = delete; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 70 | /** Adds a node to the graph |
| 71 | * |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 72 | * @note Models a single output node |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 73 | * |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 74 | * @tparam NT Node operation |
| 75 | * @tparam Ts Arguments to operation |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 76 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 77 | * @param[in] args Node arguments |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 78 | * |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 79 | * @return ID of the node |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 80 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 81 | template <typename NT, typename... Ts> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | NodeID add_node(Ts &&...args); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 83 | /** Remove the node with the given ID |
| 84 | * |
| 85 | * @param[in] nid ID of the node to remove |
| 86 | * |
| 87 | * @return True if the removal took place else false |
| 88 | */ |
| 89 | bool remove_node(NodeID nid); |
| 90 | /** Adds a connection between two nodes |
| 91 | * |
| 92 | * @param[in] source ID of the source node |
| 93 | * @param[in] source_idx Output index of the source node |
| 94 | * @param[in] sink ID of the sink node |
| 95 | * @param[in] sink_idx Input index of the sink node |
| 96 | * |
| 97 | * @return ID of this connection |
| 98 | */ |
| 99 | EdgeID add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx); |
| 100 | /** Removes an edge (connection) |
| 101 | * |
| 102 | * @param[in] eid Connection to remove |
| 103 | * |
| 104 | * @return True if the removal took place else false |
| 105 | */ |
| 106 | bool remove_connection(EdgeID eid); |
| 107 | /** Returns graph name |
| 108 | * |
| 109 | * @return Graph name |
| 110 | */ |
| 111 | std::string name() const; |
| 112 | /** Returns graph id |
| 113 | * |
| 114 | * @return Graph id |
| 115 | */ |
| 116 | GraphID id() const; |
| 117 | /** Returns graph input nodes |
| 118 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 119 | * @param[in] type Type of nodes to return |
| 120 | * |
| 121 | * @return vector containing the graph node of given type |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 122 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 123 | const std::vector<NodeID> &nodes(NodeType type); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 124 | /** Returns nodes of graph |
| 125 | * |
| 126 | * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph |
| 127 | * |
| 128 | * @return Nodes of graph |
| 129 | */ |
| 130 | std::vector<std::unique_ptr<INode>> &nodes(); |
| 131 | /** Returns nodes of graph |
| 132 | * |
| 133 | * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph |
| 134 | * |
| 135 | * @return Nodes of graph |
| 136 | */ |
| 137 | const std::vector<std::unique_ptr<INode>> &nodes() const; |
| 138 | /** Returns edges of graph |
| 139 | * |
| 140 | * @warning Edges can be nullptr if they have been removed during the mutation steps of the graph |
| 141 | * |
| 142 | * @return Edges of graph |
| 143 | */ |
| 144 | const std::vector<std::unique_ptr<Edge>> &edges() const; |
| 145 | /** Returns tensors of graph |
| 146 | * |
| 147 | * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph |
| 148 | * |
| 149 | * @return Tensors of graph |
| 150 | */ |
| 151 | std::vector<std::unique_ptr<Tensor>> &tensors(); |
| 152 | /** Returns tensors of graph |
| 153 | * |
| 154 | * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph |
| 155 | * |
| 156 | * @return Tensors of graph |
| 157 | */ |
| 158 | const std::vector<std::unique_ptr<Tensor>> &tensors() const; |
| 159 | /** Get node object given its id |
| 160 | * |
| 161 | * @warning Can be nullptr if node was removed during the mutation steps of the graph |
| 162 | * |
| 163 | * @param[in] id Node ID |
| 164 | * |
| 165 | * @return The actual node object |
| 166 | */ |
| 167 | const INode *node(NodeID id) const; |
| 168 | /** Get node object given its id |
| 169 | * |
| 170 | * @warning Can be nullptr if node was removed during the mutation steps of the graph |
| 171 | * |
| 172 | * @param[in] id Node ID |
| 173 | * |
| 174 | * @return The actual node object |
| 175 | */ |
| 176 | INode *node(NodeID id); |
| 177 | /** Get edge object given its id |
| 178 | * |
| 179 | * @warning Can be nullptr if node was removed during the mutation steps of the graph |
| 180 | * |
| 181 | * @param[in] id Edge ID |
| 182 | * |
| 183 | * @return The actual edge object |
| 184 | */ |
| 185 | const Edge *edge(EdgeID id) const; |
| 186 | /** Get edge object given its id |
| 187 | * |
| 188 | * @warning Can be nullptr if node was removed during the mutation steps of the graph |
| 189 | * |
| 190 | * @param[in] id Edge ID |
| 191 | * |
| 192 | * @return The actual edge object |
| 193 | */ |
| 194 | Edge *edge(EdgeID id); |
| 195 | /** Get tensor object given its id |
| 196 | * |
| 197 | * @warning Can be nullptr if tensor was removed during the mutation steps of the graph |
| 198 | * |
| 199 | * @param[in] id Tensor ID |
| 200 | * |
| 201 | * @return The actual tensor object |
| 202 | */ |
| 203 | const Tensor *tensor(TensorID id) const; |
| 204 | /** Get tensor object given its id |
| 205 | * |
| 206 | * @warning Can be nullptr if tensor was removed during the mutation steps of the graph |
| 207 | * |
| 208 | * @param[in] id Tensor ID |
| 209 | * |
| 210 | * @return The actual tensor object |
| 211 | */ |
| 212 | Tensor *tensor(TensorID id); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 213 | |
| 214 | private: |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 215 | /** Creates a tensor object |
| 216 | * |
| 217 | * @param[in] desc Tensor descriptor |
| 218 | * |
| 219 | * @return Tensor ID |
| 220 | */ |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 221 | TensorID create_tensor(const TensorDescriptor &desc = TensorDescriptor()); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 222 | |
| 223 | private: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 224 | GraphID _id = GraphID(0); /**< Graph id */ |
| 225 | std::string _name = {}; /**< Graph name */ |
| 226 | std::vector<std::unique_ptr<INode>> _nodes = {}; /**< Graph nodes */ |
| 227 | std::vector<std::unique_ptr<Edge>> _edges = {}; /**< Graph edges */ |
| 228 | std::vector<std::unique_ptr<Tensor>> _tensors = {}; /**< Graph tensors */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 229 | std::map<NodeType, std::vector<NodeID>> _tagged_nodes = {}; /**< Graph nodes map with the node type as key */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 230 | arm_compute::Mutex _mtx = {}; /**< Mutex used for graph construction */ |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 231 | }; |
| 232 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 233 | template <typename NT, typename... Ts> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 234 | inline NodeID Graph::add_node(Ts &&...args) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 235 | { |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 236 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 237 | |
| 238 | // Create node |
| 239 | NodeID nid = _nodes.size(); |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 240 | auto node = std::make_unique<NT>(std::forward<Ts>(args)...); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 241 | node->set_graph(this); |
| 242 | node->set_id(nid); |
| 243 | |
| 244 | // Keep track of input nodes |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 245 | _tagged_nodes[node->type()].push_back(nid); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 246 | |
| 247 | // Associate a new tensor with each output |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 248 | for (auto &output : node->_outputs) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 249 | { |
| 250 | output = create_tensor(); |
| 251 | } |
| 252 | |
| 253 | // Propagate node shape if possible |
| 254 | node->forward_descriptors(); |
| 255 | |
| 256 | // Add node to the graph nodes |
| 257 | _nodes.push_back(std::move(node)); |
| 258 | |
| 259 | return nid; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 260 | } |
| 261 | } // namespace graph |
| 262 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 263 | #endif /* ARM_COMPUTE_GRAPH_GRAPH_H */ |