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