blob: 0d7210f7f8645d335a23031cbe38cb417483a585 [file] [log] [blame]
Georgios Pinitasda2491f2018-06-01 17:49:09 +01001/*
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_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__
25#define __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__
26
27#include "arm_compute/graph/Logger.h"
28#include "arm_compute/graph/Tensor.h"
29#include "arm_compute/graph/TypePrinter.h"
30#include "arm_compute/graph/Types.h"
31#include "arm_compute/graph/backends/Utils.h"
32#include "arm_compute/graph/nodes/Nodes.h"
33
34#include "arm_compute/core/Error.h"
35#include "arm_compute/core/Helpers.h"
36#include "arm_compute/core/ITensorInfo.h"
37#include "arm_compute/core/utils/misc/Cast.h"
38
39namespace arm_compute
40{
41namespace graph
42{
43namespace backends
44{
45namespace detail
46{
47/** Returns backing tensor of a given tensor
48 *
49 * @tparam TargetInfo Target information
50 *
51 * @param[in] tensor Tensor to extract the backing tensor from
52 *
53 * @return Backing tensor if present else nullptr
54 */
55template <typename TargetInfo>
56typename TargetInfo::TensorType *get_backing_tensor(arm_compute::graph::Tensor *tensor)
57{
58 typename TargetInfo::TensorType *backing_tensor = nullptr;
59 if(tensor != nullptr)
60 {
61 ARM_COMPUTE_ERROR_ON(tensor->desc().target != TargetInfo::TargetType);
62 // Get backing tensor handle
63 ITensorHandle *tensor_handle = tensor->handle();
64 // Get backing tensor
65 backing_tensor = (tensor_handle != nullptr) ? arm_compute::utils::cast::polymorphic_cast<typename TargetInfo::TensorType *>(&tensor_handle->tensor()) : nullptr;
66 }
67
68 return backing_tensor;
69}
70
71template <typename TargetInfo>
72void validate_node(const INode &node, size_t num_expected_inputs, size_t num_expected_outputs)
73{
74 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Creating " << node.type()
Pablo Tello32521432018-11-15 14:43:10 +000075 << " Target: " << TargetInfo::TargetType
76 << " ID: " << node.id()
77 << node.name()
Georgios Pinitasda2491f2018-06-01 17:49:09 +010078 << std::endl);
79
80 ARM_COMPUTE_ERROR_ON(TargetInfo::TargetType != node.assigned_target());
81 ARM_COMPUTE_ERROR_ON(node.num_inputs() != num_expected_inputs);
82 ARM_COMPUTE_ERROR_ON(node.num_outputs() != num_expected_outputs);
83}
84
85/** Creates a backend activation layer function
86 *
87 * @tparam ActivationLayerFunction Backend activation function
88 * @tparam TargetInfo Target-specific information
89 *
90 * @param[in] node Node to create the backend function for
91 *
92 * @return Backend activation layer function
93 */
94template <typename ActivationLayerFunction, typename TargetInfo>
95std::unique_ptr<IFunction> create_activation_layer(ActivationLayerNode &node)
96{
97 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
98
99 // Extract IO and info
100 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
101 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
102 const ActivationLayerInfo act_info = node.activation_info();
103
104 // Create function
105 auto func = support::cpp14::make_unique<ActivationLayerFunction>();
106 func->configure(input, output, act_info);
107
Pablo Tello32521432018-11-15 14:43:10 +0000108 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
109 << node.name()
110 << " Type: " << node.type()
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100111 << " Target " << TargetInfo::TargetType
112 << " Data Type: " << input->info()->data_type()
113 << " Shape: " << input->info()->tensor_shape()
114 << " Activation function: " << act_info.activation()
115 << " a: " << act_info.a()
116 << " b: " << act_info.b()
117 << " InPlace : " << is_in_place_operation(input, output)
118 << std::endl);
119
120 return std::move(func);
121}
122
123/** Create a backend batch normalization layer function
124 *
125 * @tparam BatchNormalizationLayerFunction Backend batch normalization function
126 * @tparam TargetInfo Target-specific information
127 *
128 * @param[in] node Node to create the backend function for
129 *
130 * @return Backend batch normalization layer function
131 */
132template <typename BatchNormalizationLayerFunction, typename TargetInfo>
133std::unique_ptr<IFunction> create_batch_normalization_layer(BatchNormalizationLayerNode &node)
134{
135 validate_node<TargetInfo>(node, 5 /* expected inputs */, 1 /* expected outputs */);
136
137 // Extract IO and info
138 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
139 typename TargetInfo::TensorType *mean = get_backing_tensor<TargetInfo>(node.input(1));
140 typename TargetInfo::TensorType *var = get_backing_tensor<TargetInfo>(node.input(2));
141 typename TargetInfo::TensorType *beta = get_backing_tensor<TargetInfo>(node.input(3));
142 typename TargetInfo::TensorType *gamma = get_backing_tensor<TargetInfo>(node.input(4));
143 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
144 const float epsilon = node.epsilon();
145 const ActivationLayerInfo fused_act = node.fused_activation();
146
147 // Create and configure function
148 auto func = support::cpp14::make_unique<BatchNormalizationLayerFunction>();
149 func->configure(input, output, mean, var, beta, gamma, epsilon, fused_act);
150
151 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000152 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
153 << node.name()
154 << " Type: " << node.type()
155 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100156 << " Data Type: " << input->info()->data_type()
157 << " Shape: " << input->info()->tensor_shape()
158 << " Epsilon: " << epsilon << " "
159 << (fused_act.enabled() ? to_string(fused_act.activation()) : "")
Pablo Tello32521432018-11-15 14:43:10 +0000160 << " InPlace: " << is_in_place_operation(input, output)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100161 << std::endl);
162
163 return std::move(func);
164}
165
Manuel Bottinid2048ce2018-10-23 17:00:42 +0100166/** Create a backend bounding box transform layer function
167 *
168 * @tparam BoundingBoxTransformLayerFunction Backend bounding box transform function
169 * @tparam TargetInfo Target-specific information
170 *
171 * @param[in] node Node to create the backend function for
172 *
173 * @return Backend bounding box transform layer function
174 */
175template <typename BoundingBoxTransformLayerFunction, typename TargetInfo>
176std::unique_ptr<IFunction> create_bounding_box_transform_layer(BoundingBoxTransformLayerNode &node)
177{
178 validate_node<TargetInfo>(node, 2 /* expected inputs */, 1 /* expected outputs */);
179
180 // Extract IO and info
181 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
182 typename TargetInfo::TensorType *deltas = get_backing_tensor<TargetInfo>(node.input(1));
183 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
184 const BoundingBoxTransformInfo bbox_info = node.info();
185
186 // Create and configure function
187 auto func = support::cpp14::make_unique<BoundingBoxTransformLayerFunction>();
188 func->configure(input, output, deltas, bbox_info);
189
190 // Log info
191 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
192 << " Target " << TargetInfo::TargetType
193 << " Data Type: " << input->info()->data_type()
194 << " Shape: " << input->info()->tensor_shape()
195 << " BoundingBox Info img W: " << bbox_info.img_width() << " "
196 << " BoundingBox Info img H: " << bbox_info.img_height() << " "
197 << std::endl);
198
199 return std::move(func);
200}
201
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100202/** Create a backend channel shuffle layer function
203 *
204 * @tparam ChannelShuffleLayerFunction Backend channel shuffle function
205 * @tparam TargetInfo Target-specific information
206 *
207 * @param[in] node Node to create the backend function for
208 *
209 * @return Backend channel shuffle layer function
210 */
211template <typename ChannelShuffleLayerFunction, typename TargetInfo>
212std::unique_ptr<IFunction> create_channel_shuffle_layer(ChannelShuffleLayerNode &node)
213{
214 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
215
216 // Extract IO and info
217 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
218 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
219 const unsigned int num_groups = node.num_groups();
220
221 // Create function
222 auto func = support::cpp14::make_unique<ChannelShuffleLayerFunction>();
223 func->configure(input, output, num_groups);
224
Pablo Tello32521432018-11-15 14:43:10 +0000225 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
226 << node.name()
227 << " Type: " << node.type()
228 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100229 << " Data Type: " << input->info()->data_type()
230 << " Shape: " << input->info()->tensor_shape()
231 << " Num groups: " << num_groups
232 << std::endl);
233
234 return std::move(func);
235}
236
Georgios Pinitase2220552018-07-20 13:23:44 +0100237/** Create a backend layer concatenate function
238 *
239 * @tparam ConcatenateLayerFunction Backend concatenate function
240 * @tparam TargetInfo Target-specific information
241 *
242 * @param[in] node Node to create the backend function for
243 *
244 * @return Backend concatenate layer function
245 */
246template <typename ConcatenateLayerFunction, typename TargetInfo>
247std::unique_ptr<arm_compute::IFunction> create_concatenate_layer(ConcatenateLayerNode &node)
248{
249 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Creating Concatenate node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
250 ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1);
251
252 // Return nullptr if depth concatenate is switched off
253 if(!node.is_enabled())
254 {
255 return nullptr;
256 }
257
258 // Extract IO and info
259 std::vector<typename TargetInfo::TensorType *> inputs;
260 for(unsigned int i = 0; i < node.num_inputs(); ++i)
261 {
262 inputs.push_back(get_backing_tensor<TargetInfo>(node.input(i)));
263 }
264 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
265 const DataLayoutDimension concat_axis = node.concatenation_axis();
266
267 // Create and configure function
268 auto func = support::cpp14::make_unique<ConcatenateLayerFunction>();
269 func->configure(inputs, output, concat_axis);
270
271 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000272 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
273 << node.name()
274 << " Type: " << node.type()
275 << " Target: " << TargetInfo::TargetType
Georgios Pinitase2220552018-07-20 13:23:44 +0100276 << " Data Type: " << output->info()->data_type()
277 << " Shape: " << output->info()->tensor_shape()
278 << " Num Inputs: " << inputs.size()
279 << " Axis: " << concat_axis
280 << std::endl);
281
282 return std::move(func);
283}
284
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100285/** Create a backend convolution layer function
286 *
287 * @tparam ConvolutionLayerFunctions Backend convolution functions
288 * @tparam TargetInfo Target-specific information
289 *
290 * @param[in] node Node to create the backend function for
291 * @param[in] ctx Graph context
292 *
293 * @return Backend convolution layer function
294 */
295template <typename ConvolutionLayerFunctions, typename TargetInfo>
296std::unique_ptr<IFunction> create_convolution_layer(ConvolutionLayerNode &node, GraphContext &ctx)
297{
298 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
299
300 // Extract IO and info
301 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
302 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
303 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
304 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
305
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100306 const bool is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
307
308 if(is_quantized)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100309 {
310 biases->info()->set_data_type(DataType::S32);
311 }
312
Georgios Pinitas08346e92018-10-16 19:10:46 +0100313 const PadStrideInfo conv_info = node.convolution_info();
314 const unsigned int num_groups = node.num_groups();
315 const ConvolutionMethod conv_algorithm = node.convolution_method();
316 const bool fast_math = node.fast_math_hint() == FastMathHint::Enabled;
317 const ActivationLayerInfo fused_act = node.fused_activation();
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100318
319 // Create and configure function (we assume that functions have been validated before creation)
320 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType);
321 std::unique_ptr<IFunction> func;
322 std::string func_name;
323
Georgios Pinitase2220552018-07-20 13:23:44 +0100324 if(conv_algorithm == ConvolutionMethod::Winograd)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100325 {
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100326 ARM_COMPUTE_ERROR_ON_MSG(num_groups != 1, "WinogradConvolutionLayer does not support grouping!");
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100327 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::WinogradConvolutionLayer>(
328 std::string("WinogradConvolutionLayer"), mm,
Georgios Pinitas08346e92018-10-16 19:10:46 +0100329 input, weights, biases, output, conv_info, fused_act, fast_math);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100330 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100331 else if(conv_algorithm == ConvolutionMethod::Direct)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100332 {
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100333 ARM_COMPUTE_ERROR_ON_MSG(num_groups != 1, "DirectConvolutionLayer does not support grouping!");
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100334 std::tie(func, func_name) = create_named_function<typename ConvolutionLayerFunctions::DirectConvolutionLayer>(
335 std::string("DirectConvolutionLayer"),
Georgios Pinitas08346e92018-10-16 19:10:46 +0100336 input, weights, biases, output, conv_info, fused_act);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100337 }
338 else if(conv_algorithm == ConvolutionMethod::GEMM)
339 {
340 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GEMMConvolutionLayer>(
341 std::string("GEMMConvolutionLayer"), mm,
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100342 input, weights, biases, output, conv_info,
Georgios Pinitas08346e92018-10-16 19:10:46 +0100343 WeightsInfo(), Size2D(1U, 1U), fused_act, num_groups);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100344 }
345 else
346 {
347 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GenericConvolutionLayer>(
348 std::string("GenericConvolutionLayer"), mm,
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100349 input, weights, biases, output, conv_info,
Georgios Pinitas08346e92018-10-16 19:10:46 +0100350 WeightsInfo(), Size2D(1U, 1U), fused_act, fast_math, num_groups);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100351 }
352
353 // Log info
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100354 std::ostringstream qss;
355 if(is_quantized)
356 {
357 qss << " Input QuantInfo: " << input->info()->quantization_info()
358 << " Weights QuantInfo: " << weights->info()->quantization_info()
359 << " Output QuantInfo: " << output->info()->quantization_info();
360 }
Pablo Tello32521432018-11-15 14:43:10 +0000361 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
362 << node.name()
363 << " Type: " << func_name
364 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100365 << " Data Type: " << input->info()->data_type()
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100366 << " Groups: " << num_groups
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100367 << qss.str()
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100368 << " Input shape: " << input->info()->tensor_shape()
369 << " Weights shape: " << weights->info()->tensor_shape()
370 << " Output shape: " << output->info()->tensor_shape()
Georgios Pinitas08346e92018-10-16 19:10:46 +0100371 << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "")
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100372 << std::endl);
373 return func;
374}
375
376/** Create a backend deconvolution layer function
377 *
378 * @tparam DeconvolutionLayerFunction Backend deconvolution function
379 * @tparam TargetInfo Target-specific information
380 *
381 * @param[in] node Node to create the backend function for
382 * @param[in] ctx Graph context
383 *
384 * @return Backend deconvolution layer function
385 */
386template <typename DeconvolutionLayerFunction, typename TargetInfo>
387std::unique_ptr<IFunction> create_deconvolution_layer(DeconvolutionLayerNode &node, GraphContext &ctx)
388{
389 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
390
391 // Extract IO and info
392 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
393 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
394 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
395 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
396
397 const PadStrideInfo deconv_info = node.deconvolution_info();
398 const Size2D inner_border = node.inner_border();
399
400 // Create and configure function (we assume that functions have been validated before creation)
401 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType);
402 std::unique_ptr<IFunction> func;
403
404 std::tie(func, std::ignore) = create_named_memory_managed_function<DeconvolutionLayerFunction>(
405 std::string(), mm,
406 input, weights, biases, output, deconv_info, inner_border.x(), inner_border.y());
407
408 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000409 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
410 << node.name()
411 << " Type: " << node.type()
412 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100413 << " Data Type: " << input->info()->data_type()
414 << " Input shape: " << input->info()->tensor_shape()
415 << " Weights shape: " << weights->info()->tensor_shape()
416 << " Output shape: " << output->info()->tensor_shape()
417 << std::endl);
418 return func;
419}
420
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100421/** Create a backend layer depth-wise convolution function
422 *
423 * @tparam DepthwiseConvolutionLayerFunctions Backend depthwise convolution function
424 * @tparam TargetInfo Target-specific information
425 *
426 * @param[in] node Node to create the backend function for
427 *
428 * @return Backend depth-wise convolution layer function
429 */
430template <typename DepthwiseConvolutionLayerFunctions, typename TargetInfo>
431std::unique_ptr<IFunction> create_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node)
432{
433 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
434
435 // Extract IO and info
436 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
437 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
438 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
439 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
440
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100441 const bool is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
442
443 if(is_quantized)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100444 {
445 biases->info()->set_data_type(DataType::S32);
446 }
447
Georgios Pinitas60e98252018-10-22 16:17:20 +0100448 const PadStrideInfo conv_info = node.convolution_info();
449 const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method();
450 const unsigned int depth_multiplier = 1;
451 const ActivationLayerInfo fused_act = node.fused_activation();
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100452
453 // Create and configure function (we assume that functions have been validated before creation)
454 std::unique_ptr<IFunction> func;
455 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100456 if(dwc_algorithm == DepthwiseConvolutionMethod::Optimized3x3)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100457 {
458 std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::DepthwiseConvolutionLayer3x3>(
459 std::string("DepthwiseConvolutionLayer3x3"),
Georgios Pinitas60e98252018-10-22 16:17:20 +0100460 input, weights, biases, output, conv_info, depth_multiplier, fused_act);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100461 }
462 else
463 {
464 std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::GenericDepthwiseConvolutionLayer>(
465 std::string("DepthwiseConvolutionLayer"),
Georgios Pinitas60e98252018-10-22 16:17:20 +0100466 input, weights, biases, output, conv_info, depth_multiplier, fused_act);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100467 }
468
469 // Log info
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100470 std::ostringstream qss;
471 if(is_quantized)
472 {
473 qss << " Input QuantInfo: " << input->info()->quantization_info()
474 << " Weights QuantInfo: " << weights->info()->quantization_info()
475 << " Output QuantInfo: " << output->info()->quantization_info();
476 }
Pablo Tello32521432018-11-15 14:43:10 +0000477 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
478 << node.name()
479 << " Type: " << func_name
480 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100481 << " Data Type: " << input->info()->data_type()
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100482 << qss.str()
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100483 << " Input shape: " << input->info()->tensor_shape()
484 << " Weights shape: " << weights->info()->tensor_shape()
485 << " Output shape: " << output->info()->tensor_shape()
Georgios Pinitas60e98252018-10-22 16:17:20 +0100486 << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "")
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100487 << std::endl);
488 return func;
489}
490
491/** Create a backend element-wise operation layer function
492 *
493 * @tparam EltwiseFunctions Backend element-wise function
494 * @tparam TargetInfo Target-specific information
495 *
496 * @param[in] node Node to create the backend function for
497 *
498 * @return Backend element-wise operation layer function
499 */
500template <typename EltwiseFunctions, typename TargetInfo>
501std::unique_ptr<IFunction> create_eltwise_layer(EltwiseLayerNode &node)
502{
503 validate_node<TargetInfo>(node, 2 /* expected inputs */, 1 /* expected outputs */);
504
505 // Extract IO and info
506 typename TargetInfo::TensorType *input1 = get_backing_tensor<TargetInfo>(node.input(0));
507 typename TargetInfo::TensorType *input2 = get_backing_tensor<TargetInfo>(node.input(1));
508 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
509 const EltwiseOperation eltwise_op = node.eltwise_operation();
510 const ConvertPolicy convert_policy = node.convert_policy();
511 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
512 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
513 ARM_COMPUTE_ERROR_ON(output == nullptr);
514
515 std::unique_ptr<IFunction> func = nullptr;
516 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100517 if(eltwise_op == EltwiseOperation::Add)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100518 {
519 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Addition>(
520 std::string("ArithmeticAddition"),
521 input1, input2, output, convert_policy);
522 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100523 else if(eltwise_op == EltwiseOperation::Sub)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100524 {
525 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Subtraction>(
526 std::string("ArithmeticSubtraction"),
527 input1, input2, output, convert_policy);
528 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100529 else if(eltwise_op == EltwiseOperation::Mul)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100530 {
531 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Multiplication>(
532 std::string("PixelWiseMultiplication"),
533 input1, input2, output, 1.f, convert_policy, node.rounding_policy());
534 }
535 else
536 {
537 ARM_COMPUTE_ERROR("Unsupported element-wise operation!");
538 }
539
540 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000541 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
542 << node.name()
543 << " Type: " << node.type()
544 << " Target: " << TargetInfo::TargetType
545 << " Operation: " << func_name
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100546 << " Data Type: " << input1->info()->data_type()
Pablo Tello32521432018-11-15 14:43:10 +0000547 << " Shape: " << input1->info()->tensor_shape()
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100548 << std::endl);
549
550 return func;
551}
552
553/** Create a backend flatten layer function
554 *
555 * @tparam FlattenLayerFunction Backend flatten function
556 * @tparam TargetInfo Target-specific information
557 *
558 * @param[in] node Node to create the backend function for
559 *
560 * @return Backend flatten layer function
561 */
562template <typename FlattenLayerFunction, typename TargetInfo>
563std::unique_ptr<IFunction> create_flatten_layer(FlattenLayerNode &node)
564{
565 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
566
567 // Extract IO and info
568 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
569 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
570
Georgios Pinitase2220552018-07-20 13:23:44 +0100571 ARM_COMPUTE_ERROR_ON(input == nullptr);
572 ARM_COMPUTE_ERROR_ON(output == nullptr);
573
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100574 // Create and configure function
575 auto func = support::cpp14::make_unique<FlattenLayerFunction>();
576 func->configure(input, output);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100577
578 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000579 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
580 << node.name()
581 << " Type: " << node.type()
582 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100583 << " Data Type: " << input->info()->data_type()
584 << " Input shape: " << input->info()->tensor_shape()
585 << " Output shape: " << output->info()->tensor_shape()
586 << std::endl);
587
588 return std::move(func);
589}
590
591/** Create a backend fully connected layer function
592 *
593 * @tparam FullyConnectedLayerFunction Backend fully-connected function
594 * @tparam TargetInfo Target-specific information
595 *
596 * @param[in] node Node to create the backend function for
597 * @param[in] ctx Graph context
598 *
599 * @return Backend fully connected layer function
600 */
601template <typename FullyConnectedLayerFunction, typename TargetInfo>
602std::unique_ptr<IFunction> create_fully_connected_layer(FullyConnectedLayerNode &node, GraphContext &ctx)
603{
604 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
605
606 // Extract IO and info
607 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
608 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
609 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
610 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100611 const FullyConnectedLayerInfo fc_info = node.info();
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100612
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100613 ARM_COMPUTE_ERROR_ON(input == nullptr);
614 ARM_COMPUTE_ERROR_ON(weights == nullptr);
615 ARM_COMPUTE_ERROR_ON(output == nullptr);
616
Georgios Pinitase2220552018-07-20 13:23:44 +0100617 // Create and configure function
618 auto func = support::cpp14::make_unique<FullyConnectedLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
619 func->configure(input, weights, biases, output, fc_info);
620
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100621 const bool is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
622
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100623 // Log info
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100624 std::ostringstream qss;
625 if(is_quantized)
626 {
627 qss << " Input QuantInfo: " << input->info()->quantization_info()
628 << " Weights QuantInfo: " << weights->info()->quantization_info()
629 << " Output QuantInfo: " << output->info()->quantization_info();
630 }
Pablo Tello32521432018-11-15 14:43:10 +0000631 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
632 << node.name()
633 << " Type: " << node.type()
634 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100635 << " Data Type: " << input->info()->data_type()
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100636 << qss.str()
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100637 << " Input shape: " << input->info()->tensor_shape()
638 << " Weights shape: " << weights->info()->tensor_shape()
639 << " Output shape: " << output->info()->tensor_shape()
640 << std::endl);
641
642 return std::move(func);
643}
644
Michele Di Giorgio47e6fed2018-11-13 12:04:25 +0000645/** Create a backend generate proposals layer function
646 *
647 * @tparam GenerateProposalsLayerFunction Backend generate proposals function
648 * @tparam TargetInfo Target-specific information
649 *
650 * @param[in] node Node to create the backend function for
651 * @param[in] ctx Graph context
652 *
653 * @return Backend generate proposals layer function
654 */
655template <typename GenerateProposalsLayerFunction, typename TargetInfo>
656std::unique_ptr<IFunction> create_generate_proposals_layer(GenerateProposalsLayerNode &node, GraphContext &ctx)
657{
658 validate_node<TargetInfo>(node, 3 /* expected inputs */, 3 /* expected outputs */);
659
660 // Extract IO and info
661 typename TargetInfo::TensorType *scores = get_backing_tensor<TargetInfo>(node.input(0));
662 typename TargetInfo::TensorType *deltas = get_backing_tensor<TargetInfo>(node.input(1));
663 typename TargetInfo::TensorType *anchors = get_backing_tensor<TargetInfo>(node.input(2));
664 typename TargetInfo::TensorType *proposals = get_backing_tensor<TargetInfo>(node.output(0));
665 typename TargetInfo::TensorType *scores_out = get_backing_tensor<TargetInfo>(node.output(1));
666 typename TargetInfo::TensorType *num_valid_proposals = get_backing_tensor<TargetInfo>(node.output(2));
667 const GenerateProposalsInfo info = node.info();
668
669 ARM_COMPUTE_ERROR_ON(scores == nullptr);
670 ARM_COMPUTE_ERROR_ON(deltas == nullptr);
671 ARM_COMPUTE_ERROR_ON(anchors == nullptr);
672 ARM_COMPUTE_ERROR_ON(proposals == nullptr);
673 ARM_COMPUTE_ERROR_ON(scores_out == nullptr);
674
675 // Create and configure function
676 auto func = support::cpp14::make_unique<GenerateProposalsLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
677 func->configure(scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info);
678
679 // Log info
680 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
681 << " Target " << TargetInfo::TargetType
682 << " Data Type: " << scores->info()->data_type()
683 << " Scores shape: " << scores->info()->tensor_shape()
684 << " Deltas shape: " << deltas->info()->tensor_shape()
685 << " Anchors shape: " << anchors->info()->tensor_shape()
686 << " Proposals shape: " << proposals->info()->tensor_shape()
687 << " Num valid proposals shape: " << num_valid_proposals->info()->tensor_shape()
688 << " Scores Out shape: " << scores_out->info()->tensor_shape()
689 << std::endl);
690
691 return std::move(func);
692}
693
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100694/** Create a backend normalization layer function
695 *
696 * @tparam NormalizationLayerFunction Backend normalization function
697 * @tparam TargetInfo Target-specific information
698 *
699 * @param[in] node Node to create the backend function for
700 * @param[in] ctx Graph context
701 *
702 * @return Backend normalization layer function
703 */
704template <typename NormalizationLayerFunction, typename TargetInfo>
705std::unique_ptr<IFunction> create_normalization_layer(NormalizationLayerNode &node, GraphContext &ctx)
706{
707 ARM_COMPUTE_UNUSED(ctx);
708
709 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
710
711 // Extract IO and info
712 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
713 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
714 const NormalizationLayerInfo norm_info = node.normalization_info();
715 ARM_COMPUTE_ERROR_ON(input == nullptr);
716 ARM_COMPUTE_ERROR_ON(output == nullptr);
717
718 // Create and configure function
719 auto func = support::cpp14::make_unique<NormalizationLayerFunction>();
720 func->configure(input, output, norm_info);
721
722 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000723 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
724 << node.name()
725 << " Type: " << node.type()
726 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100727 << " Data Type: " << input->info()->data_type()
728 << " Input shape: " << input->info()->tensor_shape()
729 << " Output shape: " << output->info()->tensor_shape()
730 << " Normalization info: " << norm_info.type()
731 << std::endl);
732
733 return std::move(func);
734}
735
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100736/** Create a backend normalize planar YUV layer function
737 *
738 * @tparam NormalizePlanarYUVLayerFunction Backend normalize planar YUV function
739 * @tparam TargetInfo Target-specific information
740 *
741 * @param[in] node Node to create the backend function for
742 *
743 * @return Backend normalize plnar YUV layer function
744 */
745template <typename NormalizePlanarYUVLayerFunction, typename TargetInfo>
746std::unique_ptr<IFunction> create_normalize_planar_yuv_layer(NormalizePlanarYUVLayerNode &node)
747{
748 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
749
750 // Extract IO and info
751 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
752 typename TargetInfo::TensorType *mean = get_backing_tensor<TargetInfo>(node.input(1));
753 typename TargetInfo::TensorType *std = get_backing_tensor<TargetInfo>(node.input(2));
754 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
755 ARM_COMPUTE_ERROR_ON(input == nullptr);
756 ARM_COMPUTE_ERROR_ON(mean == nullptr);
757 ARM_COMPUTE_ERROR_ON(std == nullptr);
758 ARM_COMPUTE_ERROR_ON(output == nullptr);
759
760 // Create and configure function
761 auto func = support::cpp14::make_unique<NormalizePlanarYUVLayerFunction>();
762 func->configure(input, output, mean, std);
763
764 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000765 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
766 << node.name()
767 << " Type: " << node.type()
768 << " Target: " << TargetInfo::TargetType
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100769 << " Data Type: " << input->info()->data_type()
770 << " Shape: " << input->info()->tensor_shape()
771 << std::endl);
772
773 return std::move(func);
774}
775
Michele Di Giorgio4bb17332018-09-26 13:56:51 +0100776/** Create a backend pad layer function
777 *
778 * @tparam PadLayerFunction Backend pad function
779 * @tparam TargetInfo Target-specific information
780 *
781 * @param[in] node Node to create the backend function for
782 *
783 * @return Backend pad layer function
784 */
785template <typename PadLayerFunction, typename TargetInfo>
786std::unique_ptr<IFunction> create_pad_layer(PadLayerNode &node)
787{
788 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
789
790 // Extract IO and info
791 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
792 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
793 const PaddingList &padding = node.padding();
794 ARM_COMPUTE_ERROR_ON(input == nullptr);
795 ARM_COMPUTE_ERROR_ON(output == nullptr);
796
797 // Create and configure function
798 auto func = support::cpp14::make_unique<PadLayerFunction>();
799 func->configure(input, output, padding);
800
801 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000802 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
803 << node.name()
804 << " Type: " << node.type()
805 << " Target: " << TargetInfo::TargetType
Michele Di Giorgio4bb17332018-09-26 13:56:51 +0100806 << " Data Type: " << input->info()->data_type()
807 << " Input shape: " << input->info()->tensor_shape()
808 << " Output shape: " << output->info()->tensor_shape()
809 << std::endl);
810
811 return std::move(func);
812}
813
Georgios Pinitas57c48242018-08-02 13:41:49 +0100814/** Create a backend permute layer function
815 *
816 * @tparam PermuteLayerFunction Backend permute function
817 * @tparam TargetInfo Target-specific information
818 *
819 * @param[in] node Node to create the backend function for
820 *
821 * @return Backend permute layer function
822 */
823template <typename PermuteLayerFunction, typename TargetInfo>
824std::unique_ptr<IFunction> create_permute_layer(PermuteLayerNode &node)
825{
826 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
827
828 // Extract IO and info
829 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
830 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
831 const PermutationVector &perm = node.permutation_vector();
832 ARM_COMPUTE_ERROR_ON(input == nullptr);
833 ARM_COMPUTE_ERROR_ON(output == nullptr);
834
835 // Create and configure function
836 auto func = support::cpp14::make_unique<PermuteLayerFunction>();
837 func->configure(input, output, perm);
838
839 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000840 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
841 << node.name()
842 << " Type: " << node.type()
843 << " Target: " << TargetInfo::TargetType
Georgios Pinitas57c48242018-08-02 13:41:49 +0100844 << " Data Type: " << input->info()->data_type()
845 << " Input shape: " << input->info()->tensor_shape()
846 << " Output shape: " << output->info()->tensor_shape()
847 << " Permutation vector: " << perm
848 << std::endl);
849
850 return std::move(func);
851}
852
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100853/** Create a backend pooling layer function
854 *
855 * @tparam PoolingLayerFunction Backend pooling function
856 * @tparam TargetInfo Target-specific information
857 *
858 * @param[in] node Node to create the backend function for
859 *
860 * @return Backend pooling layer function
861 */
862template <typename PoolingLayerFunction, typename TargetInfo>
863std::unique_ptr<IFunction> create_pooling_layer(PoolingLayerNode &node)
864{
865 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
866
867 // Extract IO and info
868 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
869 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
870 const PoolingLayerInfo pool_info = node.pooling_info();
871 ARM_COMPUTE_ERROR_ON(input == nullptr);
872 ARM_COMPUTE_ERROR_ON(output == nullptr);
873
874 // Create and configure function
875 auto func = support::cpp14::make_unique<PoolingLayerFunction>();
876 func->configure(input, output, pool_info);
877
878 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000879 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
880 << node.name()
881 << " Type: " << node.type()
882 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100883 << " Data Type: " << input->info()->data_type()
884 << " Input shape: " << input->info()->tensor_shape()
885 << " Output shape: " << output->info()->tensor_shape()
886 << " Pooling info: " << pool_info.pool_type()
887 << std::endl);
888
889 return std::move(func);
890}
891
Pablo Tello32521432018-11-15 14:43:10 +0000892/** Create a backend priorbox layer function
893 *
894 * @tparam PriorBoxLayerFunction Backend priorbox function
895 * @tparam TargetInfo Target-specific information
896 *
897 * @param[in] node Node to create the backend function for
898 *
899 * @return Backend priorbox layer function
900 */
901template <typename PriorBoxLayerFunction, typename TargetInfo>
902std::unique_ptr<IFunction> create_priorbox_layer(PriorBoxLayerNode &node)
903{
904 validate_node<TargetInfo>(node, 2 /* expected inputs */, 1 /* expected outputs */);
905
906 // Extract IO and info
907 typename TargetInfo::TensorType *input0 = get_backing_tensor<TargetInfo>(node.input(0));
908 typename TargetInfo::TensorType *input1 = get_backing_tensor<TargetInfo>(node.input(1));
909 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
910 const PriorBoxLayerInfo prior_info = node.priorbox_info();
911 ARM_COMPUTE_ERROR_ON(input0 == nullptr);
912 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
913 ARM_COMPUTE_ERROR_ON(output == nullptr);
914
915 // Create and configure function
916 auto func = support::cpp14::make_unique<PriorBoxLayerFunction>();
917 func->configure(input0, input1, output, prior_info);
918
919 // Log info
920 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
921 << node.name()
922 << " Type: " << node.type()
923 << " Target: " << TargetInfo::TargetType
924 << " Data Type: " << input0->info()->data_type()
925 << " Input0 shape: " << input0->info()->tensor_shape()
926 << " Input1 shape: " << input1->info()->tensor_shape()
927 << " Output shape: " << output->info()->tensor_shape()
928 << " PriorBoxLayer info: " << prior_info
929 << std::endl);
930
931 return std::move(func);
932}
933
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100934/** Create a backend reorg layer function
935 *
Michele Di Giorgioc30b6682018-09-12 17:44:08 +0100936 * @tparam ReorgLayerFunction Backend reorg function
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100937 * @tparam TargetInfo Target-specific information
938 *
939 * @param[in] node Node to create the backend function for
940 *
941 * @return Backend reshape layer function
942 */
943template <typename ReorgLayerFunction, typename TargetInfo>
944std::unique_ptr<IFunction> create_reorg_layer(ReorgLayerNode &node)
945{
946 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
947
948 // Extract IO and info
949 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
950 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
951 ARM_COMPUTE_ERROR_ON(input == nullptr);
952 ARM_COMPUTE_ERROR_ON(output == nullptr);
953
954 // Create and configure function
955 auto func = support::cpp14::make_unique<ReorgLayerFunction>();
956 func->configure(input, output, node.stride());
957
958 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000959 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
960 << node.name()
961 << " Type: " << node.type()
962 << " Target: " << TargetInfo::TargetType
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100963 << " Data Type: " << input->info()->data_type()
964 << " Input shape: " << input->info()->tensor_shape()
965 << " Output shape: " << output->info()->tensor_shape()
966 << std::endl);
967
968 return std::move(func);
969}
970
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100971/** Create a backend reshape layer function
972 *
973 * @tparam ReshapeLayerFunction Backend reshape function
974 * @tparam TargetInfo Target-specific information
975 *
976 * @param[in] node Node to create the backend function for
977 *
978 * @return Backend reshape layer function
979 */
980template <typename ReshapeLayerFunction, typename TargetInfo>
981std::unique_ptr<IFunction> create_reshape_layer(ReshapeLayerNode &node)
982{
983 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
984
985 // Extract IO and info
986 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
987 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
988 ARM_COMPUTE_ERROR_ON(input == nullptr);
989 ARM_COMPUTE_ERROR_ON(output == nullptr);
990
991 // Create and configure function
992 auto func = support::cpp14::make_unique<ReshapeLayerFunction>();
993 func->configure(input, output);
994
995 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000996 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
997 << node.name()
998 << " Type: " << node.type()
999 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +01001000 << " Data Type: " << input->info()->data_type()
1001 << " Input shape: " << input->info()->tensor_shape()
1002 << " Output shape: " << output->info()->tensor_shape()
1003 << std::endl);
1004
1005 return std::move(func);
1006}
1007
1008/** Create a backend resize layer function
1009 *
1010 * @tparam ResizeLayerFunction Backend resize function
1011 * @tparam TargetInfo Target-specific information
1012 *
1013 * @param[in] node Node to create the backend function for
1014 *
1015 * @return Backend resize layer function
1016 */
1017template <typename ResizeLayerFunction, typename TargetInfo>
1018std::unique_ptr<IFunction> create_resize_layer(ResizeLayerNode &node)
1019{
1020 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
1021
1022 // Extract IO and info
1023 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
1024 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
1025 ARM_COMPUTE_ERROR_ON(input == nullptr);
1026 ARM_COMPUTE_ERROR_ON(output == nullptr);
1027 const InterpolationPolicy policy = node.policy();
1028
1029 // Create and configure function
1030 auto func = support::cpp14::make_unique<ResizeLayerFunction>();
1031 func->configure(input, output, policy, BorderMode::CONSTANT);
1032
1033 // Log info
Pablo Tello32521432018-11-15 14:43:10 +00001034 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
1035 << node.name()
1036 << " Type: " << node.type()
1037 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +01001038 << " Data Type: " << input->info()->data_type()
1039 << " Input shape: " << input->info()->tensor_shape()
1040 << " Output shape: " << output->info()->tensor_shape()
1041 << " Interpolation: " << policy
1042 << std::endl);
1043
1044 return std::move(func);
1045}
1046
Manuel Bottini3f9d4d72018-10-19 14:04:42 +01001047/** Create a backend ROI align layer function
1048 *
1049 * @tparam ROIAlignLayerFunction ROI Align function
1050 * @tparam TargetInfo Target-specific information
1051 *
1052 * @param[in] node Node to create the backend function for
1053 *
1054 * @return ROI Align layer function
1055 */
1056template <typename ROIAlignLayerFunction, typename TargetInfo>
1057std::unique_ptr<IFunction> create_roi_align_layer(ROIAlignLayerNode &node)
1058{
1059 validate_node<TargetInfo>(node, 2 /* expected inputs */, 1 /* expected outputs */);
1060
1061 // Extract IO and info
1062 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
1063 typename TargetInfo::TensorType *rois = get_backing_tensor<TargetInfo>(node.input(1));
1064 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
1065 ARM_COMPUTE_ERROR_ON(input == nullptr);
1066 ARM_COMPUTE_ERROR_ON(output == nullptr);
1067 ARM_COMPUTE_ERROR_ON(rois == nullptr);
1068
1069 const ROIPoolingLayerInfo pool_info = node.pooling_info();
1070
1071 // Create and configure function
1072 auto func = support::cpp14::make_unique<ROIAlignLayerFunction>();
1073
1074 func->configure(input, rois, output, pool_info);
1075
1076 // Log info
1077 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
1078 << " Target " << TargetInfo::TargetType
1079 << " Data Type: " << input->info()->data_type()
1080 << " Input shape: " << input->info()->tensor_shape()
1081 << " Output shape: " << output->info()->tensor_shape()
1082 << " ROIs shape: " << rois->info()->tensor_shape()
1083 << " ROIPooling width: " << pool_info.pooled_width()
1084 << " ROIPooling height: " << pool_info.pooled_height()
1085 << std::endl);
1086
1087 return std::move(func);
1088}
1089
Michele Di Giorgioc30b6682018-09-12 17:44:08 +01001090/** Create a backend slice layer function
1091 *
1092 * @tparam SliceLayerFunction Backend slice function
1093 * @tparam TargetInfo Target-specific information
1094 *
1095 * @param[in] node Node to create the backend function for
1096 *
1097 * @return Backend slice layer function
1098 */
1099template <typename SliceLayerFunction, typename TargetInfo>
1100std::unique_ptr<IFunction> create_slice_layer(SliceLayerNode &node)
1101{
1102 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
1103
1104 // Extract IO and info
1105 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
1106 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
1107 ARM_COMPUTE_ERROR_ON(input == nullptr);
1108 ARM_COMPUTE_ERROR_ON(output == nullptr);
1109
1110 // Create and configure function
1111 auto func = support::cpp14::make_unique<SliceLayerFunction>();
1112 func->configure(input, output, node.starts(), node.ends());
1113
1114 // Log info
Pablo Tello32521432018-11-15 14:43:10 +00001115 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
1116 << node.name()
1117 << " Type: " << node.type()
1118 << " Target: " << TargetInfo::TargetType
Michele Di Giorgioc30b6682018-09-12 17:44:08 +01001119 << " Data Type: " << input->info()->data_type()
1120 << " Input shape: " << input->info()->tensor_shape()
1121 << " Output shape: " << output->info()->tensor_shape()
1122 << std::endl);
1123
1124 return std::move(func);
1125}
1126
Georgios Pinitasda2491f2018-06-01 17:49:09 +01001127/** Create a backend softmax layer function
1128 *
1129 * @tparam SoftmaxLayerFunction Backend softmax function
1130 * @tparam TargetInfo Target-specific information
1131 *
1132 * @param[in] node Node to create the backend function for
1133 * @param[in] ctx Graph context
1134 *
1135 * @return Backend softmax layer function
1136 */
1137template <typename SoftmaxLayerFunction, typename TargetInfo>
1138std::unique_ptr<IFunction> create_softmax_layer(SoftmaxLayerNode &node, GraphContext &ctx)
1139{
1140 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
1141
1142 // Extract IO and info
1143 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
1144 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
1145 const float beta = node.beta();
1146 ARM_COMPUTE_ERROR_ON(input == nullptr);
1147 ARM_COMPUTE_ERROR_ON(output == nullptr);
1148
1149 // Create and configure function
1150 auto func = support::cpp14::make_unique<SoftmaxLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
1151 func->configure(input, output, beta);
1152
1153 // Log info
Pablo Tello32521432018-11-15 14:43:10 +00001154 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
1155 << node.name()
1156 << " Type: " << node.type()
1157 << " Target: " << TargetInfo::TargetType
Georgios Pinitasda2491f2018-06-01 17:49:09 +01001158 << " Data Type: " << input->info()->data_type()
1159 << " Input shape: " << input->info()->tensor_shape()
1160 << " Output shape: " << output->info()->tensor_shape()
1161 << std::endl);
1162
1163 return std::move(func);
1164}
Michalis Spyrou4e1c3f32018-09-20 17:14:03 +01001165/** Create a backend Upsample layer function
1166 *
1167 * @tparam UpsampleLayerFunction Backend Upsample function
1168 * @tparam TargetInfo Target-specific information
1169 *
1170 * @param[in] node Node to create the backend function for
1171 * @param[in] ctx Graph context
1172 *
1173 * @return Backend Upsample layer function
1174 */
1175template <typename UpsampleLayerFunction, typename TargetInfo>
1176std::unique_ptr<IFunction> create_upsample_layer(UpsampleLayerNode &node, GraphContext &ctx)
1177{
1178 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
1179
1180 // Extract IO and info
1181 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
1182 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
1183 const Size2D info = node.info();
1184 const InterpolationPolicy upsampling_policy = node.upsampling_policy();
1185 ARM_COMPUTE_ERROR_ON(upsampling_policy != InterpolationPolicy::NEAREST_NEIGHBOR);
1186 ARM_COMPUTE_ERROR_ON(info.x() != 2 || info.y() != 2);
1187 ARM_COMPUTE_ERROR_ON(input == nullptr);
1188 ARM_COMPUTE_ERROR_ON(output == nullptr);
1189
1190 // Create and configure function
1191 auto func = support::cpp14::make_unique<UpsampleLayerFunction>();
1192 func->configure(input, output, info, upsampling_policy);
1193
1194 // Log info
Pablo Tello32521432018-11-15 14:43:10 +00001195 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
1196 << node.name()
1197 << " Type: " << node.type()
1198 << " Target: " << TargetInfo::TargetType
Michalis Spyrou4e1c3f32018-09-20 17:14:03 +01001199 << " Data Type: " << input->info()->data_type()
1200 << " Input shape: " << input->info()->tensor_shape()
1201 << " Output shape: " << output->info()->tensor_shape()
1202 << " Strides: " << info
1203 << " Upsampling policy: " << upsampling_policy
1204 << std::endl);
1205
1206 return std::move(func);
1207}
Michalis Spyrou96f67692018-09-13 11:39:28 +01001208/** Create a backend YOLO layer function
1209 *
1210 * @tparam YoloLayerFunction Backend YOLO function
1211 * @tparam TargetInfo Target-specific information
1212 *
1213 * @param[in] node Node to create the backend function for
1214 * @param[in] ctx Graph context
1215 *
1216 * @return Backend YOLO layer function
1217 */
1218template <typename YOLOlayerFunction, typename TargetInfo>
1219std::unique_ptr<IFunction> create_yolo_layer(YOLOLayerNode &node, GraphContext &ctx)
1220{
1221 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
1222
1223 // Extract IO and info
1224 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
1225 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
1226 const ActivationLayerInfo act_info = node.activation_info();
1227 const int32_t num_classes = node.num_classes();
1228 ARM_COMPUTE_ERROR_ON(num_classes <= 0);
1229 ARM_COMPUTE_ERROR_ON(input == nullptr);
1230 ARM_COMPUTE_ERROR_ON(output == nullptr);
1231
1232 // Create and configure function
1233 auto func = support::cpp14::make_unique<YOLOlayerFunction>();
1234 func->configure(input, output, act_info, num_classes);
1235
1236 // Log info
Pablo Tello32521432018-11-15 14:43:10 +00001237 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
1238 << node.name()
1239 << " Type: " << node.type()
1240 << " Target: " << TargetInfo::TargetType
Michalis Spyrou96f67692018-09-13 11:39:28 +01001241 << " Data Type: " << input->info()->data_type()
1242 << " Input shape: " << input->info()->tensor_shape()
1243 << " Output shape: " << output->info()->tensor_shape()
1244 << " Activation function: " << act_info.activation()
1245 << " Num classes: " << num_classes
1246 << std::endl);
1247
1248 return std::move(func);
1249}
Georgios Pinitasda2491f2018-06-01 17:49:09 +01001250} // namespace detail
1251} // namespace backends
1252} // namespace graph
1253} // namespace arm_compute
1254
1255#endif /* __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__ */