blob: f9fb251fc5cc0bc61318719fdd6d9bab87ae67db [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 */
24#ifndef __ARM_COMPUTE_GRAPH2_GRAPH_BUILDER_H__
25#define __ARM_COMPUTE_GRAPH2_GRAPH_BUILDER_H__
26
27#include "arm_compute/graph2/ITensorAccessor.h"
28#include "arm_compute/graph2/Types.h"
29
30namespace arm_compute
31{
32namespace graph2
33{
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 *
102 * @param[in] g Graph to add the node to
103 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000104 * @param[in] input Input to the convolution layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000105 * @param[in] kernel_spatial_extend Spatial extend of convolution kernels
106 * @param[in] depth Number of convolution kernels
107 * @param[in] conv_info Convolution layer information
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000108 * @param[in] num_groups (Optional) Number of groups for a grouped convolution. Defaults to 1
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000109 * @param[in] method (Optional) Convolution method to use
110 * @param[in] weights_accessor (Optional) Accessor of the weights node data
111 * @param[in] bias_accessor (Optional) Accessor of the bias node data
112 *
113 * @return Node ID of the created node, EmptyNodeID in case of error
114 */
115 static NodeID add_convolution_node(Graph &g, NodeParams params, NodeIdxPair input,
116 Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo conv_info,
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000117 unsigned int num_groups = 1, ConvolutionMethod method = ConvolutionMethod::DEFAULT,
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000118 ITensorAccessorUPtr weights_accessor = nullptr, ITensorAccessorUPtr bias_accessor = nullptr);
119 /** Adds a depth concatenate node to the graph
120 *
121 * @param[in] g Graph to add the node to
122 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000123 * @param[in] inputs Inputs to the depth concatenate layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000124 *
125 * @return Node ID of the created node, EmptyNodeID in case of error
126 */
127 static NodeID add_depth_concatenate_node(Graph &g, NodeParams params, std::vector<NodeIdxPair> inputs);
128 /** Adds a depth-wise convolution layer node to the graph
129 *
130 * @param[in] g Graph to add the node to
131 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000132 * @param[in] input Input to the depthwise convolution layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000133 * @param[in] kernel_spatial_extend Spatial extend of convolution kernels
134 * @param[in] conv_info Convolution layer information
135 * @param[in] method (Optional) Convolution method to use
136 * @param[in] weights_accessor (Optional) Accessor of the weights node data
137 * @param[in] bias_accessor (Optional) Accessor of the bias node data
138 *
139 * @return Node ID of the created node, EmptyNodeID in case of error
140 */
141 static NodeID add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input,
142 Size2D kernel_spatial_extend, PadStrideInfo conv_info,
143 DepthwiseConvolutionMethod method = DepthwiseConvolutionMethod::DEFAULT,
144 ITensorAccessorUPtr weights_accessor = nullptr, ITensorAccessorUPtr bias_accessor = nullptr);
145 /** Adds an element-wise layer node to the graph
146 *
147 * @param[in] g Graph to add the node to
148 * @param[in] params Common node parameters
149 * @param[in] input0 First input to the element-wise operation layer node as a NodeID-Index pair
150 * @param[in] input1 Second input to the element-wise operation layer node as a NodeID-Index pair
151 * @param[in] operation Element-wise operation to perform
152 *
153 * @return Node ID of the created node, EmptyNodeID in case of error
154 */
155 static NodeID add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation);
156 /** Adds a flatten layer node to the graph
157 *
158 * @param[in] g Graph to add the node to
159 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000160 * @param[in] input Input to the flatten layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000161 *
162 * @return Node ID of the created node, EmptyNodeID in case of error
163 */
164 static NodeID add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input);
165 /** Adds a fully connected layer node to the graph
166 *
167 * @param[in] g Graph to add the layer to
168 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000169 * @param[in] input Input to the fully connected layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000170 * @param[in] num_outputs Number of output neurons
171 * @param[in] weights_accessor (Optional) Accessor of the weights node data
172 * @param[in] bias_accessor (Optional) Accessor of the bias node data
173 *
174 * @return Node ID of the created node, EmptyNodeID in case of error
175 */
176 static NodeID add_fully_connected_layer(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_outputs,
177 ITensorAccessorUPtr weights_accessor = nullptr, ITensorAccessorUPtr bias_accessor = nullptr);
178 /** Adds a normalization layer node to the graph
179 *
180 * @param[in] g Graph to add the node to
181 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000182 * @param[in] input Input to the normalization layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000183 * @param[in] norm_info Normalization layer information
184 *
185 * @return Node ID of the created node, EmptyNodeID in case of error
186 */
187 static NodeID add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info);
188 /** Adds a pooling layer node to the graph
189 *
190 * @param[in] g Graph to add the node to
191 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000192 * @param[in] input Input to the pooling layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000193 * @param[in] pool_info Pooling layer information
194 *
195 * @return Node ID of the created node, EmptyNodeID in case of error
196 */
197 static NodeID add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info);
198 /** Adds a reshape layer node to the graph
199 *
200 * @param[in] g Graph to add the node to
201 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000202 * @param[in] input Input to the reshape layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000203 * @param[in] shape Output reshaped shape
204 *
205 * @return Node ID of the created node, EmptyNodeID in case of error
206 */
207 static NodeID add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape);
208 /** Adds a softmax node to the graph
209 *
210 * @param[in] g Graph to add the node to
211 * @param[in] params Common node parameters
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000212 * @param[in] input Input to the softmax layer node as a NodeID-Index pair
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000213 * @param[in] beta Beta parameter
214 *
215 * @return Node ID of the created node, EmptyNodeID in case of error
216 */
217 static NodeID add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta = 1.f);
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000218 /** Adds a split node to the graph
219 *
220 * @param[in] g Graph to add the node to
221 * @param[in] params Common node parameters
222 * @param[in] input Input to the split layer node as a NodeID-Index pair
223 * @param[in] num_splits Number of different splits
224 * @param[in] axis (Optional) Split axis. Defaults to 0
225 *
226 * @return Node ID of the created node, EmptyNodeID in case of error
227 */
228 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 +0000229};
230} // namespace graph2
231} // namespace arm_compute
232#endif /* __ARM_COMPUTE_GRAPH2_GRAPH_BUILDER_H__ */