blob: 04edf673d1b44537498398b2d31f2ac2ee6096ac [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
2 * Copyright (c) 2018 ARM Limited.
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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#ifndef __ARM_COMPUTE_GRAPH_GRAPH_BUILDER_H__
25#define __ARM_COMPUTE_GRAPH_GRAPH_BUILDER_H__
Georgios Pinitasd8734b52017-12-22 15:27:52 +000026
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/ITensorAccessor.h"
28#include "arm_compute/graph/Types.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000029
30namespace arm_compute
31{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000033{
34// Forward declaration
35class Graph;
36
37/** Graph builder class
38 *
39 * Builds and compiles a graph
40 */
41class GraphBuilder final
42{
43public:
44 /** Adds a Const node to the graph
45 *
46 * @param[in] g Graph to add the node to
47 * @param[in] params Common node parameters
48 * @param[in] desc Tensor descriptor of the node
49 * @param[in] accessor (Optional) Accessor of the const node data
50 *
51 * @return Node ID of the created node, EmptyNodeID in case of error
52 */
53 static NodeID add_const_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor = nullptr);
54 /** Adds an input layer node to the graph
55 *
56 * @param[in] g Graph to add the node to
57 * @param[in] params Common node parameters
58 * @param[in] desc Tensor descriptor of the Tensor
59 * @param[in] accessor (Optional) Accessor of the input node data
60 *
61 * @return Node ID of the created node, EmptyNodeID in case of error
62 */
63 static NodeID add_input_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor = nullptr);
64 /** Adds an output layer node to the graph
65 *
66 * @param[in] g Graph to add the node to
67 * @param[in] params Common node parameters
68 * @param[in] input Input to the output node as a NodeID-Index pair
69 * @param[in] accessor (Optional) Accessor of the output node data
70 *
71 * @return Node ID of the created node, EmptyNodeID in case of error
72 */
73 static NodeID add_output_node(Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr accessor = nullptr);
74 /** Adds an activation layer node to the graph
75 *
76 * @param[in] g Graph to add the node to
77 * @param[in] params Common node parameters
78 * @param[in] input Input to the activation layer node as a NodeID-Index pair
79 * @param[in] act_info Activation layer information
80 *
81 * @return Node ID of the created node, EmptyNodeID in case of error
82 */
83 static NodeID add_activation_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info);
84 /** Adds a batch normalization layer node to the graph
85 *
86 * @param[in] g Graph to add the node to
87 * @param[in] params Common node parameters
88 * @param[in] input Input to the batch normalization layer node as a NodeID-Index pair
89 * @param[in] epsilon Epsilon parameter
90 * @param[in] mean_accessor Const Node ID that contains the mean values
91 * @param[in] var_accessor Const Node ID that contains the variance values
92 * @param[in] beta_accessor Const Node ID that contains the beta values. Can be EmptyNodeID
93 * @param[in] gamma_accessor Const Node ID that contains the gamma values. Can be EmptyNodeID
94 *
95 * @return Node ID of the created node, EmptyNodeID in case of error
96 */
97 static NodeID add_batch_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, float epsilon,
98 ITensorAccessorUPtr mean_accessor = nullptr, ITensorAccessorUPtr var_accessor = nullptr,
99 ITensorAccessorUPtr beta_accessor = nullptr, ITensorAccessorUPtr gamma_accessor = nullptr);
100 /** Adds a convolution layer node to the graph
101 *
Giorgio Arena59631a12018-05-02 13:59:04 +0100102 * TODO (COMPMID-1113): Add a graph descriptor for convolution layer node
103 *
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000104 * @param[in] g Graph to add the node to
105 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000106 * @param[in] input Input to the convolution layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000107 * @param[in] kernel_spatial_extend Spatial extend of convolution kernels
108 * @param[in] depth Number of convolution kernels
109 * @param[in] conv_info Convolution layer information
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000110 * @param[in] num_groups (Optional) Number of groups for a grouped convolution. Defaults to 1
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000111 * @param[in] method (Optional) Convolution method to use
Giorgio Arena59631a12018-05-02 13:59:04 +0100112 * @param[in] fast_math_hint (Optional) Fast math hint
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000113 * @param[in] weights_accessor (Optional) Accessor of the weights node data
114 * @param[in] bias_accessor (Optional) Accessor of the bias node data
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100115 * @param[in] weights_quant_info (Optional) Weights quantization info
116 * @param[in] out_quant_info (Optional) Output quantization info
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000117 *
118 * @return Node ID of the created node, EmptyNodeID in case of error
119 */
120 static NodeID add_convolution_node(Graph &g, NodeParams params, NodeIdxPair input,
121 Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo conv_info,
Giorgio Arena59631a12018-05-02 13:59:04 +0100122 unsigned int num_groups = 1, ConvolutionMethod method = ConvolutionMethod::DEFAULT, FastMathHint fast_math_hint = FastMathHint::DISABLED,
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100123 ITensorAccessorUPtr weights_accessor = nullptr, ITensorAccessorUPtr bias_accessor = nullptr,
124 const QuantizationInfo weights_quant_info = QuantizationInfo(),
125 const QuantizationInfo out_quant_info = QuantizationInfo());
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000126 /** Adds a depth concatenate node to the graph
127 *
128 * @param[in] g Graph to add the node to
129 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000130 * @param[in] inputs Inputs to the depth concatenate layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000131 *
132 * @return Node ID of the created node, EmptyNodeID in case of error
133 */
134 static NodeID add_depth_concatenate_node(Graph &g, NodeParams params, std::vector<NodeIdxPair> inputs);
135 /** Adds a depth-wise convolution layer node to the graph
136 *
137 * @param[in] g Graph to add the node to
138 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000139 * @param[in] input Input to the depthwise convolution layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000140 * @param[in] kernel_spatial_extend Spatial extend of convolution kernels
141 * @param[in] conv_info Convolution layer information
142 * @param[in] method (Optional) Convolution method to use
143 * @param[in] weights_accessor (Optional) Accessor of the weights node data
144 * @param[in] bias_accessor (Optional) Accessor of the bias node data
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100145 * @param[in] quant_info (Optional) Weights quantization info
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000146 *
147 * @return Node ID of the created node, EmptyNodeID in case of error
148 */
149 static NodeID add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input,
150 Size2D kernel_spatial_extend, PadStrideInfo conv_info,
151 DepthwiseConvolutionMethod method = DepthwiseConvolutionMethod::DEFAULT,
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100152 ITensorAccessorUPtr weights_accessor = nullptr, ITensorAccessorUPtr bias_accessor = nullptr, const QuantizationInfo quant_info = QuantizationInfo());
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000153 /** Adds an element-wise layer node to the graph
154 *
155 * @param[in] g Graph to add the node to
156 * @param[in] params Common node parameters
157 * @param[in] input0 First input to the element-wise operation layer node as a NodeID-Index pair
158 * @param[in] input1 Second input to the element-wise operation layer node as a NodeID-Index pair
159 * @param[in] operation Element-wise operation to perform
160 *
161 * @return Node ID of the created node, EmptyNodeID in case of error
162 */
163 static NodeID add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation);
164 /** Adds a flatten layer node to the graph
165 *
166 * @param[in] g Graph to add the node to
167 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000168 * @param[in] input Input to the flatten layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000169 *
170 * @return Node ID of the created node, EmptyNodeID in case of error
171 */
172 static NodeID add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input);
173 /** Adds a fully connected layer node to the graph
174 *
175 * @param[in] g Graph to add the layer to
176 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000177 * @param[in] input Input to the fully connected layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000178 * @param[in] num_outputs Number of output neurons
179 * @param[in] weights_accessor (Optional) Accessor of the weights node data
180 * @param[in] bias_accessor (Optional) Accessor of the bias node data
181 *
182 * @return Node ID of the created node, EmptyNodeID in case of error
183 */
184 static NodeID add_fully_connected_layer(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_outputs,
185 ITensorAccessorUPtr weights_accessor = nullptr, ITensorAccessorUPtr bias_accessor = nullptr);
186 /** Adds a normalization layer node to the graph
187 *
188 * @param[in] g Graph to add the node to
189 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000190 * @param[in] input Input to the normalization layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000191 * @param[in] norm_info Normalization layer information
192 *
193 * @return Node ID of the created node, EmptyNodeID in case of error
194 */
195 static NodeID add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info);
196 /** Adds a pooling layer node to the graph
197 *
198 * @param[in] g Graph to add the node to
199 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000200 * @param[in] input Input to the pooling layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000201 * @param[in] pool_info Pooling layer information
202 *
203 * @return Node ID of the created node, EmptyNodeID in case of error
204 */
205 static NodeID add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info);
206 /** Adds a reshape layer node to the graph
207 *
208 * @param[in] g Graph to add the node to
209 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000210 * @param[in] input Input to the reshape layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000211 * @param[in] shape Output reshaped shape
212 *
213 * @return Node ID of the created node, EmptyNodeID in case of error
214 */
215 static NodeID add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100216 /** Adds a scale layer node to the graph
217 * This layer computes a product of the input with a scale (read from mul_accessor) and it applies an offset (read from add_accessor).
218 * output = input * mul_w + add_w
219 *
220 * @param[in] g Graph to add the layer to
221 * @param[in] params Common node parameters
222 * @param[in] input Input to the fully connected layer node as a NodeID-Index pair
223 * @param[in] mul_accessor (Optional) Accessor of the mul node data
224 * @param[in] add_accessor (Optional) Accessor of the add node data
225 *
226 * @return Node ID of the created node, EmptyNodeID in case of error
227 */
228 static NodeID add_scale_layer(Graph &g, const NodeParams &params, NodeIdxPair input,
229 ITensorAccessorUPtr mul_accessor = nullptr, ITensorAccessorUPtr add_accessor = nullptr);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000230 /** Adds a softmax node to the graph
231 *
232 * @param[in] g Graph to add the node to
233 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000234 * @param[in] input Input to the softmax layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000235 * @param[in] beta Beta parameter
236 *
237 * @return Node ID of the created node, EmptyNodeID in case of error
238 */
239 static NodeID add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta = 1.f);
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000240 /** Adds a split node to the graph
241 *
242 * @param[in] g Graph to add the node to
243 * @param[in] params Common node parameters
244 * @param[in] input Input to the split layer node as a NodeID-Index pair
245 * @param[in] num_splits Number of different splits
246 * @param[in] axis (Optional) Split axis. Defaults to 0
247 *
248 * @return Node ID of the created node, EmptyNodeID in case of error
249 */
250 static NodeID add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis = 0);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000251};
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100252} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000253} // namespace arm_compute
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100254#endif /* __ARM_COMPUTE_GRAPH_GRAPH_BUILDER_H__ */