1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018-2020 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_GRAPH_GRAPH_H
25*c217d954SCole Faust #define ARM_COMPUTE_GRAPH_GRAPH_H
26*c217d954SCole Faust
27*c217d954SCole Faust #include "arm_compute/graph/Edge.h"
28*c217d954SCole Faust #include "arm_compute/graph/INode.h"
29*c217d954SCole Faust #include "arm_compute/graph/Tensor.h"
30*c217d954SCole Faust #include "arm_compute/graph/Types.h"
31*c217d954SCole Faust
32*c217d954SCole Faust #include "support/Mutex.h"
33*c217d954SCole Faust #include "support/ToolchainSupport.h"
34*c217d954SCole Faust
35*c217d954SCole Faust #include <map>
36*c217d954SCole Faust #include <memory>
37*c217d954SCole Faust #include <string>
38*c217d954SCole Faust #include <utility>
39*c217d954SCole Faust #include <vector>
40*c217d954SCole Faust
41*c217d954SCole Faust #ifndef BARE_METAL
42*c217d954SCole Faust #include <thread>
43*c217d954SCole Faust #endif /* BARE_METAL */
44*c217d954SCole Faust
45*c217d954SCole Faust namespace arm_compute
46*c217d954SCole Faust {
47*c217d954SCole Faust namespace graph
48*c217d954SCole Faust {
49*c217d954SCole Faust /** Graph class
50*c217d954SCole Faust *
51*c217d954SCole Faust * Represents a multiple source - multiple sink directed graph
52*c217d954SCole Faust */
53*c217d954SCole Faust class Graph final
54*c217d954SCole Faust {
55*c217d954SCole Faust public:
56*c217d954SCole Faust Graph() = default;
57*c217d954SCole Faust /** Constructor
58*c217d954SCole Faust *
59*c217d954SCole Faust * @param[in] id Graph identification number. Can be used to differentiate between graphs. Default value 0
60*c217d954SCole Faust * @param[in] name Graph name. Default value empty string
61*c217d954SCole Faust */
62*c217d954SCole Faust Graph(GraphID id, std::string name);
63*c217d954SCole Faust /** Prevent instances of this class from being copied (As this class contains pointers) */
64*c217d954SCole Faust Graph(const Graph &) = delete;
65*c217d954SCole Faust /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
66*c217d954SCole Faust Graph &operator=(const Graph &) = delete;
67*c217d954SCole Faust /** Prevent instances of this class from being moved (As this class contains non movable objects) */
68*c217d954SCole Faust Graph(Graph &&) = delete;
69*c217d954SCole Faust /** Prevent instances of this class from being moved (As this class contains non movable objects) */
70*c217d954SCole Faust Graph &operator=(Graph &&) = delete;
71*c217d954SCole Faust /** Adds a node to the graph
72*c217d954SCole Faust *
73*c217d954SCole Faust * @note Models a single output node
74*c217d954SCole Faust *
75*c217d954SCole Faust * @tparam NT Node operation
76*c217d954SCole Faust * @tparam Ts Arguments to operation
77*c217d954SCole Faust *
78*c217d954SCole Faust * @param[in] args Node arguments
79*c217d954SCole Faust *
80*c217d954SCole Faust * @return ID of the node
81*c217d954SCole Faust */
82*c217d954SCole Faust template <typename NT, typename... Ts>
83*c217d954SCole Faust NodeID add_node(Ts &&... args);
84*c217d954SCole Faust /** Remove the node with the given ID
85*c217d954SCole Faust *
86*c217d954SCole Faust * @param[in] nid ID of the node to remove
87*c217d954SCole Faust *
88*c217d954SCole Faust * @return True if the removal took place else false
89*c217d954SCole Faust */
90*c217d954SCole Faust bool remove_node(NodeID nid);
91*c217d954SCole Faust /** Adds a connection between two nodes
92*c217d954SCole Faust *
93*c217d954SCole Faust * @param[in] source ID of the source node
94*c217d954SCole Faust * @param[in] source_idx Output index of the source node
95*c217d954SCole Faust * @param[in] sink ID of the sink node
96*c217d954SCole Faust * @param[in] sink_idx Input index of the sink node
97*c217d954SCole Faust *
98*c217d954SCole Faust * @return ID of this connection
99*c217d954SCole Faust */
100*c217d954SCole Faust EdgeID add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx);
101*c217d954SCole Faust /** Removes an edge (connection)
102*c217d954SCole Faust *
103*c217d954SCole Faust * @param[in] eid Connection to remove
104*c217d954SCole Faust *
105*c217d954SCole Faust * @return True if the removal took place else false
106*c217d954SCole Faust */
107*c217d954SCole Faust bool remove_connection(EdgeID eid);
108*c217d954SCole Faust /** Returns graph name
109*c217d954SCole Faust *
110*c217d954SCole Faust * @return Graph name
111*c217d954SCole Faust */
112*c217d954SCole Faust std::string name() const;
113*c217d954SCole Faust /** Returns graph id
114*c217d954SCole Faust *
115*c217d954SCole Faust * @return Graph id
116*c217d954SCole Faust */
117*c217d954SCole Faust GraphID id() const;
118*c217d954SCole Faust /** Returns graph input nodes
119*c217d954SCole Faust *
120*c217d954SCole Faust * @param[in] type Type of nodes to return
121*c217d954SCole Faust *
122*c217d954SCole Faust * @return vector containing the graph node of given type
123*c217d954SCole Faust */
124*c217d954SCole Faust const std::vector<NodeID> &nodes(NodeType type);
125*c217d954SCole Faust /** Returns nodes of graph
126*c217d954SCole Faust *
127*c217d954SCole Faust * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph
128*c217d954SCole Faust *
129*c217d954SCole Faust * @return Nodes of graph
130*c217d954SCole Faust */
131*c217d954SCole Faust std::vector<std::unique_ptr<INode>> &nodes();
132*c217d954SCole Faust /** Returns nodes of graph
133*c217d954SCole Faust *
134*c217d954SCole Faust * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph
135*c217d954SCole Faust *
136*c217d954SCole Faust * @return Nodes of graph
137*c217d954SCole Faust */
138*c217d954SCole Faust const std::vector<std::unique_ptr<INode>> &nodes() const;
139*c217d954SCole Faust /** Returns edges of graph
140*c217d954SCole Faust *
141*c217d954SCole Faust * @warning Edges can be nullptr if they have been removed during the mutation steps of the graph
142*c217d954SCole Faust *
143*c217d954SCole Faust * @return Edges of graph
144*c217d954SCole Faust */
145*c217d954SCole Faust const std::vector<std::unique_ptr<Edge>> &edges() const;
146*c217d954SCole Faust /** Returns tensors of graph
147*c217d954SCole Faust *
148*c217d954SCole Faust * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph
149*c217d954SCole Faust *
150*c217d954SCole Faust * @return Tensors of graph
151*c217d954SCole Faust */
152*c217d954SCole Faust std::vector<std::unique_ptr<Tensor>> &tensors();
153*c217d954SCole Faust /** Returns tensors of graph
154*c217d954SCole Faust *
155*c217d954SCole Faust * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph
156*c217d954SCole Faust *
157*c217d954SCole Faust * @return Tensors of graph
158*c217d954SCole Faust */
159*c217d954SCole Faust const std::vector<std::unique_ptr<Tensor>> &tensors() const;
160*c217d954SCole Faust /** Get node object given its id
161*c217d954SCole Faust *
162*c217d954SCole Faust * @warning Can be nullptr if node was removed during the mutation steps of the graph
163*c217d954SCole Faust *
164*c217d954SCole Faust * @param[in] id Node ID
165*c217d954SCole Faust *
166*c217d954SCole Faust * @return The actual node object
167*c217d954SCole Faust */
168*c217d954SCole Faust const INode *node(NodeID id) const;
169*c217d954SCole Faust /** Get node object given its id
170*c217d954SCole Faust *
171*c217d954SCole Faust * @warning Can be nullptr if node was removed during the mutation steps of the graph
172*c217d954SCole Faust *
173*c217d954SCole Faust * @param[in] id Node ID
174*c217d954SCole Faust *
175*c217d954SCole Faust * @return The actual node object
176*c217d954SCole Faust */
177*c217d954SCole Faust INode *node(NodeID id);
178*c217d954SCole Faust /** Get edge object given its id
179*c217d954SCole Faust *
180*c217d954SCole Faust * @warning Can be nullptr if node was removed during the mutation steps of the graph
181*c217d954SCole Faust *
182*c217d954SCole Faust * @param[in] id Edge ID
183*c217d954SCole Faust *
184*c217d954SCole Faust * @return The actual edge object
185*c217d954SCole Faust */
186*c217d954SCole Faust const Edge *edge(EdgeID id) const;
187*c217d954SCole Faust /** Get edge object given its id
188*c217d954SCole Faust *
189*c217d954SCole Faust * @warning Can be nullptr if node was removed during the mutation steps of the graph
190*c217d954SCole Faust *
191*c217d954SCole Faust * @param[in] id Edge ID
192*c217d954SCole Faust *
193*c217d954SCole Faust * @return The actual edge object
194*c217d954SCole Faust */
195*c217d954SCole Faust Edge *edge(EdgeID id);
196*c217d954SCole Faust /** Get tensor object given its id
197*c217d954SCole Faust *
198*c217d954SCole Faust * @warning Can be nullptr if tensor was removed during the mutation steps of the graph
199*c217d954SCole Faust *
200*c217d954SCole Faust * @param[in] id Tensor ID
201*c217d954SCole Faust *
202*c217d954SCole Faust * @return The actual tensor object
203*c217d954SCole Faust */
204*c217d954SCole Faust const Tensor *tensor(TensorID id) const;
205*c217d954SCole Faust /** Get tensor object given its id
206*c217d954SCole Faust *
207*c217d954SCole Faust * @warning Can be nullptr if tensor was removed during the mutation steps of the graph
208*c217d954SCole Faust *
209*c217d954SCole Faust * @param[in] id Tensor ID
210*c217d954SCole Faust *
211*c217d954SCole Faust * @return The actual tensor object
212*c217d954SCole Faust */
213*c217d954SCole Faust Tensor *tensor(TensorID id);
214*c217d954SCole Faust
215*c217d954SCole Faust private:
216*c217d954SCole Faust /** Creates a tensor object
217*c217d954SCole Faust *
218*c217d954SCole Faust * @param[in] desc Tensor descriptor
219*c217d954SCole Faust *
220*c217d954SCole Faust * @return Tensor ID
221*c217d954SCole Faust */
222*c217d954SCole Faust TensorID create_tensor(const TensorDescriptor &desc = TensorDescriptor());
223*c217d954SCole Faust
224*c217d954SCole Faust private:
225*c217d954SCole Faust GraphID _id = GraphID(0); /**< Graph id */
226*c217d954SCole Faust std::string _name = {}; /**< Graph name */
227*c217d954SCole Faust std::vector<std::unique_ptr<INode>> _nodes = {}; /**< Graph nodes */
228*c217d954SCole Faust std::vector<std::unique_ptr<Edge>> _edges = {}; /**< Graph edges */
229*c217d954SCole Faust std::vector<std::unique_ptr<Tensor>> _tensors = {}; /**< Graph tensors */
230*c217d954SCole Faust std::map<NodeType, std::vector<NodeID>> _tagged_nodes = {}; /**< Graph nodes map with the node type as key */
231*c217d954SCole Faust arm_compute::Mutex _mtx = {}; /**< Mutex used for graph construction */
232*c217d954SCole Faust };
233*c217d954SCole Faust
234*c217d954SCole Faust template <typename NT, typename... Ts>
add_node(Ts &&...args)235*c217d954SCole Faust inline NodeID Graph::add_node(Ts &&... args)
236*c217d954SCole Faust {
237*c217d954SCole Faust arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
238*c217d954SCole Faust
239*c217d954SCole Faust // Create node
240*c217d954SCole Faust NodeID nid = _nodes.size();
241*c217d954SCole Faust auto node = std::make_unique<NT>(std::forward<Ts>(args)...);
242*c217d954SCole Faust node->set_graph(this);
243*c217d954SCole Faust node->set_id(nid);
244*c217d954SCole Faust
245*c217d954SCole Faust // Keep track of input nodes
246*c217d954SCole Faust _tagged_nodes[node->type()].push_back(nid);
247*c217d954SCole Faust
248*c217d954SCole Faust // Associate a new tensor with each output
249*c217d954SCole Faust for(auto &output : node->_outputs)
250*c217d954SCole Faust {
251*c217d954SCole Faust output = create_tensor();
252*c217d954SCole Faust }
253*c217d954SCole Faust
254*c217d954SCole Faust // Propagate node shape if possible
255*c217d954SCole Faust node->forward_descriptors();
256*c217d954SCole Faust
257*c217d954SCole Faust // Add node to the graph nodes
258*c217d954SCole Faust _nodes.push_back(std::move(node));
259*c217d954SCole Faust
260*c217d954SCole Faust return nid;
261*c217d954SCole Faust }
262*c217d954SCole Faust } // namespace graph
263*c217d954SCole Faust } // namespace arm_compute
264*c217d954SCole Faust #endif /* ARM_COMPUTE_GRAPH_GRAPH_H */
265