blob: eab91b2347965521c26d43dc1c2c05238cdc46c9 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Matthew Bentham043613f2023-05-30 16:43:14 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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#include "arm_compute/graph/GraphBuilder.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Matthew Bentham314d3e22023-06-23 10:53:52 +000026#include "arm_compute/core/utils/DataTypeUtils.h"
Georgios Pinitas2a2db592018-08-15 12:14:46 +010027#include "arm_compute/graph/algorithms/TopologicalSort.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/graph/Graph.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010029#include "arm_compute/graph/nodes/Nodes.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "arm_compute/graph/Utils.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000031
Georgios Pinitas087eaf62018-05-16 15:52:35 +010032#include "support/ToolchainSupport.h"
33
Georgios Pinitasd8734b52017-12-22 15:27:52 +000034namespace arm_compute
35{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010036namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000037{
38namespace
39{
Michalis Spyrou299fdd32019-05-01 13:03:59 +010040inline void check_nodeidx_pair(const NodeIdxPair &pair, const Graph &g)
41{
42 ARM_COMPUTE_UNUSED(pair);
43 ARM_COMPUTE_UNUSED(g);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044 ARM_COMPUTE_ERROR_ON((pair.node_id >= g.nodes().size()) || (g.node((pair).node_id) == nullptr) ||
45 (pair.index >= g.node(pair.node_id)->num_outputs()));
Michalis Spyrou299fdd32019-05-01 13:03:59 +010046}
47
Georgios Pinitasd8734b52017-12-22 15:27:52 +000048Status set_node_params(Graph &g, NodeID nid, NodeParams &params)
49{
50 INode *node = g.node(nid);
51 ARM_COMPUTE_RETURN_ERROR_ON(!node);
52
53 node->set_common_node_parameters(params);
54
55 return Status{};
56}
Georgios Pinitasee33ea52018-03-08 16:01:29 +000057
Georgios Pinitasd8734b52017-12-22 15:27:52 +000058Status set_accessor_on_node(Graph &g, NodeID nid, bool is_output, size_t idx, ITensorAccessorUPtr accessor)
59{
60 INode *node = g.node(nid);
61 ARM_COMPUTE_RETURN_ERROR_ON(!node);
62
63 Tensor *tensor = is_output ? node->output(idx) : node->input(idx);
64 ARM_COMPUTE_RETURN_ERROR_ON(!tensor);
65
66 tensor->set_accessor(std::move(accessor));
67
68 return Status{};
69}
70
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071NodeID add_const_node_with_name(
72 Graph &g, NodeParams params, const std::string &name, const TensorDescriptor &desc, ITensorAccessorUPtr accessor)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000073{
74 params.name = params.name.empty() ? "" : params.name + name;
Michalis Spyrou299fdd32019-05-01 13:03:59 +010075 auto nid = GraphBuilder::add_const_node(g, params, desc, std::move(accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +000076 set_node_params(g, nid, params);
77 return nid;
78}
Georgios Pinitasee33ea52018-03-08 16:01:29 +000079
80template <typename NT, typename... Args>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081NodeID create_simple_single_input_output_node(Graph &g, NodeParams &params, NodeIdxPair input, Args &&...args)
Georgios Pinitasee33ea52018-03-08 16:01:29 +000082{
Michalis Spyrou299fdd32019-05-01 13:03:59 +010083 check_nodeidx_pair(input, g);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000084
85 NodeID nid = g.add_node<NT>(std::forward<Args>(args)...);
86 g.add_connection(input.node_id, input.index, nid, 0);
87 set_node_params(g, nid, params);
88
89 return nid;
90}
Michele Di Giorgioec699752019-03-22 15:25:32 +000091
92template <typename NT, typename... Args>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093NodeID create_simple_multiple_input_single_output_node(Graph &g,
94 NodeParams &params,
95 const std::vector<NodeIdxPair> &inputs,
96 Args &&...args)
Michele Di Giorgioec699752019-03-22 15:25:32 +000097{
98 ARM_COMPUTE_ERROR_ON(inputs.size() == 0);
99
100 NodeID nid = g.add_node<NT>(std::forward<Args>(args)...);
101
102 unsigned int i = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 for (const auto &input : inputs)
Michele Di Giorgioec699752019-03-22 15:25:32 +0000104 {
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100105 check_nodeidx_pair(input, g);
Michele Di Giorgioec699752019-03-22 15:25:32 +0000106 g.add_connection(input.node_id, input.index, nid, i++);
107 }
108 set_node_params(g, nid, params);
109
110 return nid;
111}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000112} // namespace
113
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114NodeID
115GraphBuilder::add_const_node(Graph &g, NodeParams params, const TensorDescriptor &desc, ITensorAccessorUPtr accessor)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000116{
117 auto nid = g.add_node<ConstNode>(desc);
118 set_node_params(g, nid, params);
119 set_accessor_on_node(g, nid, true, 0, std::move(accessor));
120 return nid;
121}
122
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123NodeID
124GraphBuilder::add_input_node(Graph &g, NodeParams params, const TensorDescriptor &desc, ITensorAccessorUPtr accessor)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000125{
126 auto nid = g.add_node<InputNode>(desc);
127 set_node_params(g, nid, params);
128 set_accessor_on_node(g, nid, true, 0, std::move(accessor));
129 return nid;
130}
131
132NodeID GraphBuilder::add_output_node(Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr accessor)
133{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100134 check_nodeidx_pair(input, g);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000135
136 NodeID nid = g.add_node<OutputNode>();
137 g.add_connection(input.node_id, input.index, nid, 0);
138 set_node_params(g, nid, params);
139 set_accessor_on_node(g, nid, false, 0, std::move(accessor));
140
141 return nid;
142}
143
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144NodeID GraphBuilder::add_activation_node(Graph &g,
145 NodeParams params,
146 NodeIdxPair input,
147 ActivationLayerInfo act_info,
Michalis Spyrou18574c12019-06-05 10:51:07 +0100148 const QuantizationInfo &out_quant_info)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000149{
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000150 return create_simple_single_input_output_node<ActivationLayerNode>(g, params, input, act_info, out_quant_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000151}
152
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153NodeID GraphBuilder::add_arg_min_max_node(Graph &g,
154 NodeParams params,
155 NodeIdxPair input,
156 ReductionOperation op,
157 unsigned int axis,
158 DataType out_data_type,
159 const QuantizationInfo &out_quant_info)
thecha01e8f05da2020-08-24 17:21:41 +0100160{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100161 return create_simple_single_input_output_node<ArgMinMaxLayerNode>(g, params, input, op, axis, out_data_type,
162 out_quant_info);
thecha01e8f05da2020-08-24 17:21:41 +0100163}
164
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100165NodeID GraphBuilder::add_batch_normalization_node(Graph &g,
166 NodeParams params,
167 NodeIdxPair input,
168 float epsilon,
169 ITensorAccessorUPtr mean_accessor,
170 ITensorAccessorUPtr var_accessor,
171 ITensorAccessorUPtr beta_accessor,
172 ITensorAccessorUPtr gamma_accessor)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000173{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100174 check_nodeidx_pair(input, g);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000175
176 bool has_beta = (beta_accessor != nullptr);
177 bool has_gamma = (gamma_accessor != nullptr);
178
179 // Get input tensor descriptor
180 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
181
182 // Calculate Common Descriptor
183 TensorDescriptor common_desc = input_tensor_desc;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100184 common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000185
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100186 // Create mean and var nodes
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000187 auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor));
188 auto var_nid = add_const_node_with_name(g, params, "Variance", common_desc, std::move(var_accessor));
189
190 // Create beta node
191 NodeID beta_nid = EmptyNodeID;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 if (has_beta)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000193 {
194 beta_nid = add_const_node_with_name(g, params, "Beta", common_desc, std::move(beta_accessor));
195 }
196
197 // Create gamma node
198 NodeID gamma_nid = EmptyNodeID;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 if (has_gamma)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000200 {
201 gamma_nid = add_const_node_with_name(g, params, "Gamma", common_desc, std::move(gamma_accessor));
202 }
203
204 // Create batch normalization node and add connections
205 NodeID batch_norm_nid = g.add_node<BatchNormalizationLayerNode>(epsilon);
206 g.add_connection(input.node_id, input.index, batch_norm_nid, 0);
207 g.add_connection(mean_nid, 0, batch_norm_nid, 1);
208 g.add_connection(var_nid, 0, batch_norm_nid, 2);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 if (has_beta)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000210 {
211 g.add_connection(beta_nid, 0, batch_norm_nid, 3);
212 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100213 if (has_gamma)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000214 {
215 g.add_connection(gamma_nid, 0, batch_norm_nid, 4);
216 }
217 set_node_params(g, batch_norm_nid, params);
218
219 return batch_norm_nid;
220}
221
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100222NodeID GraphBuilder::add_bounding_box_transform_node(
223 Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair deltas, BoundingBoxTransformInfo info)
Manuel Bottinid2048ce2018-10-23 17:00:42 +0100224{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100225 check_nodeidx_pair(input, g);
226 check_nodeidx_pair(deltas, g);
Manuel Bottinid2048ce2018-10-23 17:00:42 +0100227
228 NodeID nid = g.add_node<BoundingBoxTransformLayerNode>(info);
229
230 g.add_connection(input.node_id, input.index, nid, 0);
231 g.add_connection(deltas.node_id, deltas.index, nid, 1);
232
233 set_node_params(g, nid, params);
234 return nid;
235}
236
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100237NodeID GraphBuilder::add_channel_shuffle_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_groups)
238{
239 return create_simple_single_input_output_node<ChannelShuffleLayerNode>(g, params, input, num_groups);
240}
241
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100242NodeID GraphBuilder::add_convolution_node(Graph &g,
243 NodeParams params,
244 NodeIdxPair input,
245 Size2D kernel_spatial_extend,
246 unsigned int depth,
247 PadStrideInfo conv_info,
248 unsigned int num_groups,
249 ConvolutionMethod method,
250 FastMathHint fast_math_hint,
251 ITensorAccessorUPtr weights_accessor,
252 ITensorAccessorUPtr bias_accessor,
Michalis Spyrou18574c12019-06-05 10:51:07 +0100253 const QuantizationInfo &weights_quant_info,
254 const QuantizationInfo &out_quant_info)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000255{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100256 check_nodeidx_pair(input, g);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000257 ARM_COMPUTE_ERROR_ON(depth == 0);
258 ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0));
259
260 bool has_bias = (bias_accessor != nullptr);
261
262 // Get input tensor descriptor
263 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100264 const DataLayout input_data_layout = input_tensor_desc.layout;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000265
266 // Create weights node
267 TensorDescriptor w_desc = input_tensor_desc;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100268 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width);
269 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height);
270 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL),
Georgios Pinitascac13b12018-04-27 19:07:19 +0100271 get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) / num_groups);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100272 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::BATCHES), depth);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100273 if (!weights_quant_info.empty())
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100274 {
275 w_desc.quant_info = weights_quant_info;
276 }
277
278 NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000279
280 // Create bias nodes
281 NodeID b_nid = EmptyNodeID;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100282 if (has_bias)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000283 {
284 TensorDescriptor b_desc = input_tensor_desc;
285 b_desc.shape = TensorShape(depth);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100286 if (is_data_type_quantized_asymmetric(input_tensor_desc.data_type))
Michele Di Giorgio3a3b4312018-07-06 12:34:19 +0100287 {
288 b_desc.data_type = DataType::S32;
289 }
290 b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000291 }
292
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100293 // Create convolution node and connect
294 NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, num_groups, method, fast_math_hint, out_quant_info);
295 g.add_connection(input.node_id, input.index, conv_nid, 0);
296 g.add_connection(w_nid, 0, conv_nid, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100297 if (has_bias)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000298 {
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100299 g.add_connection(b_nid, 0, conv_nid, 2);
300 }
301 set_node_params(g, conv_nid, params);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000302
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100303 return conv_nid;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000304}
305
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100306NodeID GraphBuilder::add_deconvolution_node(Graph &g,
307 NodeParams params,
308 NodeIdxPair input,
309 Size2D kernel_spatial_extend,
310 unsigned int depth,
311 PadStrideInfo deconv_info,
Manuel Bottinic1b76fa2019-06-17 12:04:40 +0100312 ITensorAccessorUPtr weights_accessor,
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100313 ITensorAccessorUPtr bias_accessor)
314{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100315 check_nodeidx_pair(input, g);
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100316 ARM_COMPUTE_ERROR_ON(depth == 0);
317 ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0));
318
319 bool has_bias = (bias_accessor != nullptr);
320
321 // Get input tensor descriptor
322 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100323 const DataLayout input_data_layout = input_tensor_desc.layout;
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100324
325 // Create weights node
326 TensorDescriptor w_desc = input_tensor_desc;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100327 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width);
328 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height);
329 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL),
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100330 get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100331 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::BATCHES), depth);
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100332
333 NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor));
334
335 // Create bias nodes
336 NodeID b_nid = EmptyNodeID;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100337 if (has_bias)
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100338 {
339 TensorDescriptor b_desc = input_tensor_desc;
340 b_desc.shape = TensorShape(depth);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100341 if (is_data_type_quantized_asymmetric(input_tensor_desc.data_type))
Michele Di Giorgio3a3b4312018-07-06 12:34:19 +0100342 {
343 b_desc.data_type = DataType::S32;
344 }
345 b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor));
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100346 }
347
348 // Create convolution node and connect
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100349 NodeID deconv_nid = g.add_node<DeconvolutionLayerNode>(descriptors::DeconvolutionLayerDescriptor{deconv_info});
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100350 g.add_connection(input.node_id, input.index, deconv_nid, 0);
351 g.add_connection(w_nid, 0, deconv_nid, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100352 if (has_bias)
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100353 {
354 g.add_connection(b_nid, 0, deconv_nid, 2);
355 }
356 set_node_params(g, deconv_nid, params);
357
358 return deconv_nid;
359}
360
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100361NodeID GraphBuilder::add_concatenate_node(Graph &g,
362 NodeParams params,
363 const std::vector<NodeIdxPair> &inputs,
364 const descriptors::ConcatLayerDescriptor &concat_descriptor)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000365{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100366 return create_simple_multiple_input_single_output_node<ConcatenateLayerNode>(g, params, inputs, inputs.size(),
367 concat_descriptor);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000368}
369
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100370NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g,
371 NodeParams params,
372 NodeIdxPair input,
373 Size2D kernel_spatial_extend,
374 PadStrideInfo conv_info,
375 int depth_multiplier,
376 DepthwiseConvolutionMethod method,
377 ITensorAccessorUPtr weights_accessor,
378 ITensorAccessorUPtr bias_accessor,
379 const QuantizationInfo &quant_info,
380 const QuantizationInfo &out_quant_info)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000381{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100382 check_nodeidx_pair(input, g);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000383 ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0));
384
385 bool has_bias = (bias_accessor != nullptr);
386
387 // Get input tensor descriptor
388 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100389 const DataLayout input_data_layout = input_tensor_desc.layout;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000390
391 // Create weights node
392 TensorDescriptor w_desc = input_tensor_desc;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100393 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width);
394 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height);
395 w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL),
Georgios Pinitas05045c12018-12-07 18:31:47 +0000396 get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100397 if (!quant_info.empty())
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100398 {
399 w_desc.quant_info = quant_info;
400 }
401
402 NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000403
404 // Create bias nodes
405 NodeID b_nid = EmptyNodeID;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100406 if (has_bias)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000407 {
408 TensorDescriptor b_desc = input_tensor_desc;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100409 b_desc.shape =
410 TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier);
Giorgio Arena2aa0ec42018-08-29 14:28:38 +0100411
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100412 if (is_data_type_quantized_asymmetric(b_desc.data_type))
Giorgio Arena2aa0ec42018-08-29 14:28:38 +0100413 {
414 b_desc.data_type = DataType::S32;
415 }
416
417 b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000418 }
419
420 // Create convolution node and connect
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000421 NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, depth_multiplier, method, out_quant_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000422 g.add_connection(input.node_id, input.index, conv_nid, 0);
423 g.add_connection(w_nid, 0, conv_nid, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100424 if (has_bias)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000425 {
426 g.add_connection(b_nid, 0, conv_nid, 2);
427 }
428 set_node_params(g, conv_nid, params);
429
430 return conv_nid;
431}
thecha010a05e6a2020-08-28 18:40:38 +0100432
433NodeID GraphBuilder::add_depth_to_space_node(Graph &g, NodeParams params, NodeIdxPair input, int32_t block_shape)
434{
435 return create_simple_single_input_output_node<DepthToSpaceLayerNode>(g, params, input, block_shape);
436}
437
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000438NodeID GraphBuilder::add_dequantization_node(Graph &g, NodeParams params, NodeIdxPair input)
439{
440 return create_simple_single_input_output_node<DequantizationLayerNode>(g, params, input);
441}
442
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100443NodeID GraphBuilder::add_detection_output_node(Graph &g,
444 NodeParams params,
445 NodeIdxPair input_loc,
446 NodeIdxPair input_conf,
447 NodeIdxPair input_priorbox,
448 const DetectionOutputLayerInfo &detect_info)
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000449{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100450 check_nodeidx_pair(input_loc, g);
451 check_nodeidx_pair(input_conf, g);
452 check_nodeidx_pair(input_priorbox, g);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000453
454 // Create detection_output node and connect
455 NodeID detect_nid = g.add_node<DetectionOutputLayerNode>(detect_info);
456 g.add_connection(input_loc.node_id, input_loc.index, detect_nid, 0);
457 g.add_connection(input_conf.node_id, input_conf.index, detect_nid, 1);
458 g.add_connection(input_priorbox.node_id, input_priorbox.index, detect_nid, 2);
459
460 set_node_params(g, detect_nid, params);
461
462 return detect_nid;
463}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000464
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100465NodeID GraphBuilder::add_detection_post_process_node(Graph &g,
466 NodeParams params,
467 NodeIdxPair input_box_encoding,
468 NodeIdxPair input_class_prediction,
469 const DetectionPostProcessLayerInfo &detect_info,
470 ITensorAccessorUPtr anchors_accessor,
471 const QuantizationInfo &anchor_quant_info)
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000472{
473 check_nodeidx_pair(input_box_encoding, g);
474 check_nodeidx_pair(input_class_prediction, g);
475
476 // Get input tensor descriptor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100477 const TensorDescriptor input_box_encoding_tensor_desc =
478 get_tensor_descriptor(g, g.node(input_box_encoding.node_id)->outputs()[0]);
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000479
480 // Calculate anchor descriptor
481 TensorDescriptor anchor_desc = input_box_encoding_tensor_desc;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100482 if (!anchor_quant_info.empty())
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000483 {
484 anchor_desc.quant_info = anchor_quant_info;
485 }
486
487 // Create anchors nodes
488 auto anchors_nid = add_const_node_with_name(g, params, "Anchors", anchor_desc, std::move(anchors_accessor));
489
490 // Create detection_output node and connect
491 NodeID detect_nid = g.add_node<DetectionPostProcessLayerNode>(detect_info);
492 g.add_connection(input_box_encoding.node_id, input_box_encoding.index, detect_nid, 0);
493 g.add_connection(input_class_prediction.node_id, input_class_prediction.index, detect_nid, 1);
494 g.add_connection(anchors_nid, 0, detect_nid, 2);
495
496 set_node_params(g, detect_nid, params);
497
498 return detect_nid;
499}
500
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100501NodeID GraphBuilder::add_dummy_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape)
502{
Georgios Pinitasb7a20232018-07-02 16:12:54 +0100503 return create_simple_single_input_output_node<DummyNode>(g, params, input, shape);
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100504}
505
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100506NodeID GraphBuilder::add_elementwise_node(
507 Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000508{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100509 check_nodeidx_pair(input0, g);
510 check_nodeidx_pair(input1, g);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000511
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100512 NodeID nid = g.add_node<EltwiseLayerNode>(descriptors::EltwiseLayerDescriptor{operation});
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000513
514 g.add_connection(input0.node_id, input0.index, nid, 0);
515 g.add_connection(input1.node_id, input1.index, nid, 1);
516
517 set_node_params(g, nid, params);
518
519 return nid;
520}
521
522NodeID GraphBuilder::add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input)
523{
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000524 return create_simple_single_input_output_node<FlattenLayerNode>(g, params, input);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000525}
526
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100527NodeID GraphBuilder::add_fully_connected_layer(Graph &g,
528 NodeParams params,
529 NodeIdxPair input,
530 unsigned int num_outputs,
531 NodeID weights_nid,
532 NodeID bias_nid,
533 const FullyConnectedLayerInfo fc_info,
534 const QuantizationInfo &out_quant_info,
535 FastMathHint fast_math_hint)
Michele Di Giorgioa42f55f2019-03-08 14:52:17 +0000536{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100537 check_nodeidx_pair(input, g);
Michele Di Giorgioa42f55f2019-03-08 14:52:17 +0000538 ARM_COMPUTE_ERROR_ON(num_outputs == 0);
539 ARM_COMPUTE_ERROR_ON(weights_nid == EmptyNodeID);
540
541 const bool has_bias = (bias_nid != EmptyNodeID);
542
543 // Get input tensor descriptor
544 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
545
546 // Create fully connected node and connect
cfRodf2c022e2021-11-05 11:29:53 +0000547 NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs, out_quant_info, fc_info, fast_math_hint);
Michele Di Giorgioa42f55f2019-03-08 14:52:17 +0000548 g.add_connection(input.node_id, input.index, fc_nid, 0);
549 g.add_connection(weights_nid, 0, fc_nid, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100550 if (has_bias)
Michele Di Giorgioa42f55f2019-03-08 14:52:17 +0000551 {
552 g.add_connection(bias_nid, 0, fc_nid, 2);
553 }
554
555 set_node_params(g, fc_nid, params);
556
557 return fc_nid;
558}
559
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100560NodeID GraphBuilder::add_fully_connected_layer(Graph &g,
561 NodeParams params,
562 NodeIdxPair input,
563 unsigned int num_outputs,
564 ITensorAccessorUPtr weights_accessor,
565 ITensorAccessorUPtr bias_accessor,
Georgios Pinitasc55cef12018-08-01 15:24:18 +0100566 const FullyConnectedLayerInfo fc_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100567 const QuantizationInfo &weights_quant_info,
568 const QuantizationInfo &out_quant_info,
569 FastMathHint fast_math_hint)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000570{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100571 check_nodeidx_pair(input, g);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000572 ARM_COMPUTE_ERROR_ON(num_outputs == 0);
573
574 bool has_bias = (bias_accessor != nullptr);
575
576 // Get input tensor descriptor
577 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
578
579 // Create weights node
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100580 TensorDescriptor w_desc = FullyConnectedLayerNode::compute_weights_descriptor(input_tensor_desc, num_outputs,
581 fc_info, weights_quant_info);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100582 NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000583
584 // Create bias nodes
585 NodeID b_nid = EmptyNodeID;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100586 if (has_bias)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000587 {
588 TensorDescriptor b_desc = input_tensor_desc;
589 b_desc.shape = TensorShape(num_outputs);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100590 if (is_data_type_quantized_asymmetric(input_tensor_desc.data_type))
Michele Di Giorgio3a3b4312018-07-06 12:34:19 +0100591 {
592 b_desc.data_type = DataType::S32;
593 }
594 b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000595 }
596
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100597 // Create fully connected node and connect
cfRodf2c022e2021-11-05 11:29:53 +0000598 NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs, out_quant_info, fc_info, fast_math_hint);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000599 g.add_connection(input.node_id, input.index, fc_nid, 0);
600 g.add_connection(w_nid, 0, fc_nid, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100601 if (has_bias)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000602 {
603 g.add_connection(b_nid, 0, fc_nid, 2);
604 }
605
606 set_node_params(g, fc_nid, params);
607
608 return fc_nid;
609}
610
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100611NodeID GraphBuilder::add_generate_proposals_node(Graph &g,
612 NodeParams params,
613 NodeIdxPair scores,
614 NodeIdxPair deltas,
615 NodeIdxPair anchors,
616 GenerateProposalsInfo info)
Manuel Bottini5209be52019-02-13 16:34:56 +0000617{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100618 check_nodeidx_pair(scores, g);
619 check_nodeidx_pair(deltas, g);
620 check_nodeidx_pair(anchors, g);
Manuel Bottini5209be52019-02-13 16:34:56 +0000621
622 NodeID nid = g.add_node<GenerateProposalsLayerNode>(info);
623
624 g.add_connection(scores.node_id, scores.index, nid, 0);
625 g.add_connection(deltas.node_id, deltas.index, nid, 1);
626 g.add_connection(anchors.node_id, anchors.index, nid, 2);
627
628 set_node_params(g, nid, params);
629 return nid;
630}
631
thecha013603aff2020-09-01 14:52:38 +0100632NodeID GraphBuilder::add_l2_normalize_node(Graph &g, NodeParams params, NodeIdxPair input, int axis, float epsilon)
633{
634 return create_simple_single_input_output_node<L2NormalizeLayerNode>(g, params, input, axis, epsilon);
635}
636
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100637NodeID
638GraphBuilder::add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000639{
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000640 return create_simple_single_input_output_node<NormalizationLayerNode>(g, params, input, norm_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000641}
642
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100643NodeID GraphBuilder::add_normalize_planar_yuv_node(
644 Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr std_accessor)
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100645{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100646 check_nodeidx_pair(input, g);
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100647
648 // Get input tensor descriptor
649 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
650
651 // Calculate Common Descriptor
652 TensorDescriptor common_desc = input_tensor_desc;
653 common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL));
654
655 // Create mean and std nodes
656 auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor));
657 auto std_nid = add_const_node_with_name(g, params, "Std", common_desc, std::move(std_accessor));
658
659 // Create normalize planar YUV node and add connections
660 NodeID norm_planar_yuv_nid = g.add_node<NormalizePlanarYUVLayerNode>();
661 g.add_connection(input.node_id, input.index, norm_planar_yuv_nid, 0);
662 g.add_connection(mean_nid, 0, norm_planar_yuv_nid, 1);
663 g.add_connection(std_nid, 0, norm_planar_yuv_nid, 2);
664 set_node_params(g, norm_planar_yuv_nid, params);
665
666 return norm_planar_yuv_nid;
667}
668
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100669NodeID GraphBuilder::add_pad_node(
670 Graph &g, NodeParams params, NodeIdxPair input, const PaddingList &paddings, PixelValue pad_value)
Michele Di Giorgio4bb17332018-09-26 13:56:51 +0100671{
Georgios Pinitas102b0ce2020-02-13 17:59:09 +0000672 return create_simple_single_input_output_node<PadLayerNode>(g, params, input, paddings, pad_value);
Michele Di Giorgio4bb17332018-09-26 13:56:51 +0100673}
674
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100675NodeID GraphBuilder::add_permute_node(
676 Graph &g, NodeParams params, NodeIdxPair input, PermutationVector perm, DataLayout layout)
Georgios Pinitas57c48242018-08-02 13:41:49 +0100677{
678 return create_simple_single_input_output_node<PermuteLayerNode>(g, params, input, perm, layout);
679}
680
Georgios Pinitasf8c47492020-02-04 17:39:59 +0000681NodeID GraphBuilder::add_prelu_node(Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair alpha)
682{
683 check_nodeidx_pair(input, g);
684 check_nodeidx_pair(alpha, g);
685
686 NodeID prelu_nid = g.add_node<PReluLayerNode>();
687 g.add_connection(input.node_id, input.index, prelu_nid, 0);
688 g.add_connection(alpha.node_id, alpha.index, prelu_nid, 1);
689
690 set_node_params(g, prelu_nid, params);
691
692 return prelu_nid;
693}
694
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000695NodeID GraphBuilder::add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info)
696{
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000697 return create_simple_single_input_output_node<PoolingLayerNode>(g, params, input, pool_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000698}
699
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100700NodeID GraphBuilder::add_print_node(Graph &g,
701 NodeParams params,
702 NodeIdxPair input,
703 std::ostream &stream,
704 const IOFormatInfo &format_info,
705 const std::function<ITensor *(ITensor *)> transform)
Giorgio Arena6e9d0e02020-01-03 15:02:04 +0000706{
707 return create_simple_single_input_output_node<PrintLayerNode>(g, params, input, stream, format_info, transform);
708}
709
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100710NodeID GraphBuilder::add_priorbox_node(
711 Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, const PriorBoxLayerInfo &prior_info)
Pablo Tello32521432018-11-15 14:43:10 +0000712{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100713 check_nodeidx_pair(input0, g);
714 check_nodeidx_pair(input1, g);
Pablo Tello32521432018-11-15 14:43:10 +0000715
716 // Create priorbox node and connect
717 NodeID prior_nid = g.add_node<PriorBoxLayerNode>(prior_info);
718 g.add_connection(input0.node_id, input0.index, prior_nid, 0);
719 g.add_connection(input1.node_id, input1.index, prior_nid, 1);
720
721 set_node_params(g, prior_nid, params);
722
723 return prior_nid;
724}
725
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100726NodeID GraphBuilder::add_quantization_node(Graph &g,
727 NodeParams params,
728 NodeIdxPair input,
729 const QuantizationInfo &out_quant_info)
Isabella Gottardi3db1ba92019-05-17 12:35:20 +0100730{
731 return create_simple_single_input_output_node<QuantizationLayerNode>(g, params, input, out_quant_info);
732}
733
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100734NodeID GraphBuilder::add_reduction_operation_node(
735 Graph &g, NodeParams params, NodeIdxPair input, ReductionOperation op, int axis, bool keep_dims)
thecha01d64444b2020-09-07 14:50:21 +0100736{
737 return create_simple_single_input_output_node<ReductionLayerNode>(g, params, input, op, axis, keep_dims);
738}
739
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100740NodeID GraphBuilder::add_reorg_node(Graph &g, NodeParams params, NodeIdxPair input, int stride)
741{
742 return create_simple_single_input_output_node<ReorgLayerNode>(g, params, input, stride);
743}
744
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000745NodeID GraphBuilder::add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape)
746{
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000747 return create_simple_single_input_output_node<ReshapeLayerNode>(g, params, input, shape);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000748}
749
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100750NodeID GraphBuilder::add_resize_node(
751 Graph &g, NodeParams params, NodeIdxPair input, InterpolationPolicy policy, float width_scale, float height_scale)
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100752{
753 return create_simple_single_input_output_node<ResizeLayerNode>(g, params, input, policy, width_scale, height_scale);
754}
755
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100756NodeID GraphBuilder::add_roi_align_node(
757 Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair rois, ROIPoolingLayerInfo pool_info)
Manuel Bottini3f9d4d72018-10-19 14:04:42 +0100758{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100759 check_nodeidx_pair(input, g);
760 check_nodeidx_pair(rois, g);
Manuel Bottini3f9d4d72018-10-19 14:04:42 +0100761
762 NodeID nid = g.add_node<ROIAlignLayerNode>(pool_info);
763
764 g.add_connection(input.node_id, input.index, nid, 0);
765 g.add_connection(rois.node_id, rois.index, nid, 1);
766
767 set_node_params(g, nid, params);
768 return nid;
769}
770
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100771NodeID GraphBuilder::add_scale_layer(Graph &g,
772 const NodeParams &params,
773 NodeIdxPair input,
774 ITensorAccessorUPtr mul_accessor,
775 ITensorAccessorUPtr add_accessor)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100776{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100777 check_nodeidx_pair(input, g);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100778
779 // Get input tensor descriptor
780 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100781 const DataLayout input_data_layout = input_tensor_desc.layout;
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100782
783 // Create mul node
784 TensorDescriptor mul_desc = input_tensor_desc;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100785 const size_t C = input_tensor_desc.shape[get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL)];
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100786 mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), 1);
787 mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), 1);
788 mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), C);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100789 NodeID mul_const_nid = add_const_node_with_name(g, params, "Mul", mul_desc, std::move(mul_accessor));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100790 NodeIdxPair mul_const_nidxp = {mul_const_nid, 0};
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100791
792 // Create add node
793 TensorDescriptor add_desc = mul_desc;
794 NodeID add_const_nid = add_const_node_with_name(g, params, "Add", add_desc, std::move(add_accessor));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100795 NodeIdxPair add_const_nidxp = {add_const_nid, 0};
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100796
797 // Create node and connect
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100798 NodeID mul_node = GraphBuilder::add_elementwise_node(g, params, input, mul_const_nidxp, EltwiseOperation::Mul);
799 NodeIdxPair mulnode_nidxp = {mul_node, 0};
800 NodeID add_node =
801 GraphBuilder::add_elementwise_node(g, params, mulnode_nidxp, add_const_nidxp, EltwiseOperation::Add);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100802
803 return add_node;
804}
805
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000806NodeID GraphBuilder::add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta)
807{
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000808 return create_simple_single_input_output_node<SoftmaxLayerNode>(g, params, input, beta);
809}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000810
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100811NodeID
812GraphBuilder::add_slice_node(Graph &g, NodeParams params, NodeIdxPair input, Coordinates &starts, Coordinates &ends)
Michele Di Giorgioc30b6682018-09-12 17:44:08 +0100813{
814 return create_simple_single_input_output_node<SliceLayerNode>(g, params, input, starts, ends);
815}
816
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100817NodeID
818GraphBuilder::add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis)
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000819{
820 return create_simple_single_input_output_node<SplitLayerNode>(g, params, input, num_splits, axis);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000821}
Michalis Spyrou96f67692018-09-13 11:39:28 +0100822
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100823NodeID GraphBuilder::add_strided_slice_node(Graph &g,
824 NodeParams params,
825 NodeIdxPair input,
826 Coordinates &starts,
827 Coordinates &ends,
828 BiStrides &strides,
829 StridedSliceLayerInfo info)
thecha012bfadd92020-08-12 17:25:51 +0100830{
831 return create_simple_single_input_output_node<StridedSliceLayerNode>(g, params, input, starts, ends, strides, info);
832}
833
Michele Di Giorgioec699752019-03-22 15:25:32 +0000834NodeID GraphBuilder::add_stack_node(Graph &g, NodeParams params, const std::vector<NodeIdxPair> &inputs, int axis)
835{
836 return create_simple_multiple_input_single_output_node<StackLayerNode>(g, params, inputs, inputs.size(), axis);
837}
838
Georgios Pinitas0b1c2db2020-12-04 15:51:34 +0000839NodeID GraphBuilder::add_yolo_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info)
Michalis Spyrou96f67692018-09-13 11:39:28 +0100840{
Georgios Pinitas0b1c2db2020-12-04 15:51:34 +0000841 check_nodeidx_pair(input, g);
842
843 // Get input tensor descriptor
844 const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]);
845 const bool is_nhwc = input_tensor_desc.layout == DataLayout::NHWC;
846
847 // Box format: [Objectness:1][Box:4][Classes:N]
848
849 // Activate objectness and front part of the box
850 const Coordinates box_start(0, 0, 0);
851 const Coordinates box_end = is_nhwc ? Coordinates(3, -1, -1) : Coordinates(-1, -1, 3);
852 NodeID box = g.add_node<SliceLayerNode>(box_start, box_end);
853 NodeID act_box = g.add_node<ActivationLayerNode>(act_info);
854 set_node_params(g, box, params);
855 set_node_params(g, act_box, params);
856 g.add_connection(input.node_id, input.index, box, 0);
857 g.add_connection(box, 0, act_box, 0);
858
859 // Immutable part
860 const Coordinates imm_start = is_nhwc ? Coordinates(3, 0, 0) : Coordinates(0, 0, 3);
861 const Coordinates imm_end = is_nhwc ? Coordinates(5, -1, -1) : Coordinates(-1, -1, 5);
862 NodeID imm = g.add_node<SliceLayerNode>(imm_start, imm_end);
863 set_node_params(g, imm, params);
864 g.add_connection(input.node_id, input.index, imm, 0);
865
866 // Activation classes and end part of box
867 const Coordinates cls_start = is_nhwc ? Coordinates(5, 0, 0) : Coordinates(0, 0, 5);
Georgios Pinitas2ae08602021-02-23 00:04:26 +0000868 const Coordinates cls_end = Coordinates(-1, -1, -1);
Georgios Pinitas0b1c2db2020-12-04 15:51:34 +0000869 NodeID cls = g.add_node<SliceLayerNode>(cls_start, cls_end);
870 NodeID cls_act = g.add_node<ActivationLayerNode>(act_info);
871 set_node_params(g, cls, params);
872 set_node_params(g, cls_act, params);
873 g.add_connection(input.node_id, input.index, cls, 0);
874 g.add_connection(cls, 0, cls_act, 0);
875
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100876 NodeID concat =
877 g.add_node<ConcatenateLayerNode>(3, descriptors::ConcatLayerDescriptor(DataLayoutDimension::CHANNEL));
Georgios Pinitas0b1c2db2020-12-04 15:51:34 +0000878 set_node_params(g, concat, params);
879 g.add_connection(act_box, 0, concat, 0);
880 g.add_connection(imm, 0, concat, 1);
881 g.add_connection(cls_act, 0, concat, 2);
882
883 return concat;
Michalis Spyrou96f67692018-09-13 11:39:28 +0100884}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100885} // namespace graph
Michele Di Giorgio3a3b4312018-07-06 12:34:19 +0100886} // namespace arm_compute