blob: 16536bcb6577f3f996afbf5b51a032410f167e56 [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()
75 << " Target : " << TargetInfo::TargetType
76 << " ID : " << node.id()
77 << " Name: " << node.name()
78 << 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
108 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
109 << " Target " << TargetInfo::TargetType
110 << " Data Type: " << input->info()->data_type()
111 << " Shape: " << input->info()->tensor_shape()
112 << " Activation function: " << act_info.activation()
113 << " a: " << act_info.a()
114 << " b: " << act_info.b()
115 << " InPlace : " << is_in_place_operation(input, output)
116 << std::endl);
117
118 return std::move(func);
119}
120
121/** Create a backend batch normalization layer function
122 *
123 * @tparam BatchNormalizationLayerFunction Backend batch normalization function
124 * @tparam TargetInfo Target-specific information
125 *
126 * @param[in] node Node to create the backend function for
127 *
128 * @return Backend batch normalization layer function
129 */
130template <typename BatchNormalizationLayerFunction, typename TargetInfo>
131std::unique_ptr<IFunction> create_batch_normalization_layer(BatchNormalizationLayerNode &node)
132{
133 validate_node<TargetInfo>(node, 5 /* expected inputs */, 1 /* expected outputs */);
134
135 // Extract IO and info
136 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
137 typename TargetInfo::TensorType *mean = get_backing_tensor<TargetInfo>(node.input(1));
138 typename TargetInfo::TensorType *var = get_backing_tensor<TargetInfo>(node.input(2));
139 typename TargetInfo::TensorType *beta = get_backing_tensor<TargetInfo>(node.input(3));
140 typename TargetInfo::TensorType *gamma = get_backing_tensor<TargetInfo>(node.input(4));
141 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
142 const float epsilon = node.epsilon();
143 const ActivationLayerInfo fused_act = node.fused_activation();
144
145 // Create and configure function
146 auto func = support::cpp14::make_unique<BatchNormalizationLayerFunction>();
147 func->configure(input, output, mean, var, beta, gamma, epsilon, fused_act);
148
149 // Log info
150 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
151 << " Target " << TargetInfo::TargetType
152 << " Data Type: " << input->info()->data_type()
153 << " Shape: " << input->info()->tensor_shape()
154 << " Epsilon: " << epsilon << " "
155 << (fused_act.enabled() ? to_string(fused_act.activation()) : "")
156 << " InPlace : " << is_in_place_operation(input, output)
157 << std::endl);
158
159 return std::move(func);
160}
161
162/** Create a backend channel shuffle layer function
163 *
164 * @tparam ChannelShuffleLayerFunction Backend channel shuffle function
165 * @tparam TargetInfo Target-specific information
166 *
167 * @param[in] node Node to create the backend function for
168 *
169 * @return Backend channel shuffle layer function
170 */
171template <typename ChannelShuffleLayerFunction, typename TargetInfo>
172std::unique_ptr<IFunction> create_channel_shuffle_layer(ChannelShuffleLayerNode &node)
173{
174 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
175
176 // Extract IO and info
177 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
178 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
179 const unsigned int num_groups = node.num_groups();
180
181 // Create function
182 auto func = support::cpp14::make_unique<ChannelShuffleLayerFunction>();
183 func->configure(input, output, num_groups);
184
185 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
186 << " Target " << TargetInfo::TargetType
187 << " Data Type: " << input->info()->data_type()
188 << " Shape: " << input->info()->tensor_shape()
189 << " Num groups: " << num_groups
190 << std::endl);
191
192 return std::move(func);
193}
194
195/** Create a backend convolution layer function
196 *
197 * @tparam ConvolutionLayerFunctions Backend convolution functions
198 * @tparam TargetInfo Target-specific information
199 *
200 * @param[in] node Node to create the backend function for
201 * @param[in] ctx Graph context
202 *
203 * @return Backend convolution layer function
204 */
205template <typename ConvolutionLayerFunctions, typename TargetInfo>
206std::unique_ptr<IFunction> create_convolution_layer(ConvolutionLayerNode &node, GraphContext &ctx)
207{
208 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
209
210 // Extract IO and info
211 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
212 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
213 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
214 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
215
216 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
217 {
218 biases->info()->set_data_type(DataType::S32);
219 }
220
221 const PadStrideInfo conv_info = node.convolution_info();
222 const ConvolutionMethod conv_algorithm = node.convolution_method();
223 const bool fast_math = node.fast_math_hint() == FastMathHint::ENABLED;
224
225 // Create and configure function (we assume that functions have been validated before creation)
226 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType);
227 std::unique_ptr<IFunction> func;
228 std::string func_name;
229
230 if(conv_algorithm == ConvolutionMethod::WINOGRAD)
231 {
232 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::WinogradConvolutionLayer>(
233 std::string("WinogradConvolutionLayer"), mm,
234 input, weights, biases, output, conv_info, ActivationLayerInfo(), fast_math);
235 }
236 else if(conv_algorithm == ConvolutionMethod::DIRECT)
237 {
238 std::tie(func, func_name) = create_named_function<typename ConvolutionLayerFunctions::DirectConvolutionLayer>(
239 std::string("DirectConvolutionLayer"),
240 input, weights, biases, output, conv_info);
241 }
242 else if(conv_algorithm == ConvolutionMethod::GEMM)
243 {
244 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GEMMConvolutionLayer>(
245 std::string("GEMMConvolutionLayer"), mm,
246 input, weights, biases, output, conv_info);
247 }
248 else
249 {
250 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GenericConvolutionLayer>(
251 std::string("GenericConvolutionLayer"), mm,
252 input, weights, biases, output, conv_info, WeightsInfo(), Size2D(1U, 1U), ActivationLayerInfo(), fast_math);
253 }
254
255 // Log info
256 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name
257 << " Target " << TargetInfo::TargetType
258 << " Data Type: " << input->info()->data_type()
259 << " Input QuantInfo: " << input->info()->quantization_info()
260 << " Weights QuantInfo: " << weights->info()->quantization_info()
261 << " Input shape: " << input->info()->tensor_shape()
262 << " Weights shape: " << weights->info()->tensor_shape()
263 << " Output shape: " << output->info()->tensor_shape()
264 << std::endl);
265 return func;
266}
267
268/** Create a backend deconvolution layer function
269 *
270 * @tparam DeconvolutionLayerFunction Backend deconvolution function
271 * @tparam TargetInfo Target-specific information
272 *
273 * @param[in] node Node to create the backend function for
274 * @param[in] ctx Graph context
275 *
276 * @return Backend deconvolution layer function
277 */
278template <typename DeconvolutionLayerFunction, typename TargetInfo>
279std::unique_ptr<IFunction> create_deconvolution_layer(DeconvolutionLayerNode &node, GraphContext &ctx)
280{
281 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
282
283 // Extract IO and info
284 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
285 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
286 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
287 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
288
289 const PadStrideInfo deconv_info = node.deconvolution_info();
290 const Size2D inner_border = node.inner_border();
291
292 // Create and configure function (we assume that functions have been validated before creation)
293 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType);
294 std::unique_ptr<IFunction> func;
295
296 std::tie(func, std::ignore) = create_named_memory_managed_function<DeconvolutionLayerFunction>(
297 std::string(), mm,
298 input, weights, biases, output, deconv_info, inner_border.x(), inner_border.y());
299
300 // Log info
301 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
302 << " Target " << TargetInfo::TargetType
303 << " Data Type: " << input->info()->data_type()
304 << " Input shape: " << input->info()->tensor_shape()
305 << " Weights shape: " << weights->info()->tensor_shape()
306 << " Output shape: " << output->info()->tensor_shape()
307 << std::endl);
308 return func;
309}
310
311/** Create a backend layer depth concatenate function
312 *
313 * @tparam DepthConcatenateLayerFunction Backend depth concatenate function
314 * @tparam TargetInfo Target-specific information
315 *
316 * @param[in] node Node to create the backend function for
317 *
318 * @return Backend depth concatenate layer function
319 */
320template <typename DepthConcatenateLayerFunction, typename TargetInfo>
321std::unique_ptr<arm_compute::IFunction> create_depth_concatenate_layer(DepthConcatenateLayerNode &node)
322{
323 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Creating DepthConcatenate node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
324 ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1);
325
326 // Return nullptr if depth concatenate is switched off
327 if(!node.is_enabled())
328 {
329 return nullptr;
330 }
331
332 // Extract IO and info
333 std::vector<typename TargetInfo::TensorType *> inputs;
334 for(unsigned int i = 0; i < node.num_inputs(); ++i)
335 {
336 inputs.push_back(get_backing_tensor<TargetInfo>(node.input(i)));
337 }
338 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
339
340 // Create and configure function
341 auto func = support::cpp14::make_unique<DepthConcatenateLayerFunction>();
342 func->configure(inputs, output);
343
344 // Log info
345 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
346 << " Target " << TargetInfo::TargetType
347 << " Data Type: " << output->info()->data_type()
348 << " Shape: " << output->info()->tensor_shape()
349 << " Num Inputs: " << inputs.size()
350 << std::endl);
351
352 return std::move(func);
353}
354
355/** Create a backend layer depth-wise convolution function
356 *
357 * @tparam DepthwiseConvolutionLayerFunctions Backend depthwise convolution function
358 * @tparam TargetInfo Target-specific information
359 *
360 * @param[in] node Node to create the backend function for
361 *
362 * @return Backend depth-wise convolution layer function
363 */
364template <typename DepthwiseConvolutionLayerFunctions, typename TargetInfo>
365std::unique_ptr<IFunction> create_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node)
366{
367 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
368
369 // Extract IO and info
370 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
371 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
372 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
373 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
374
375 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
376 {
377 biases->info()->set_data_type(DataType::S32);
378 }
379
380 const PadStrideInfo conv_info = node.convolution_info();
381 const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method();
382
383 // Create and configure function (we assume that functions have been validated before creation)
384 std::unique_ptr<IFunction> func;
385 std::string func_name;
386 if(dwc_algorithm == DepthwiseConvolutionMethod::OPTIMIZED_3x3)
387 {
388 std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::DepthwiseConvolutionLayer3x3>(
389 std::string("DepthwiseConvolutionLayer3x3"),
390 input, weights, biases, output, conv_info);
391 }
392 else
393 {
394 std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::GenericDepthwiseConvolutionLayer>(
395 std::string("DepthwiseConvolutionLayer"),
396 input, weights, biases, output, conv_info);
397 }
398
399 // Log info
400 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name
401 << " Target " << TargetInfo::TargetType
402 << " Data Type: " << input->info()->data_type()
403 << " Input QuantInfo: " << input->info()->quantization_info()
404 << " Weights QuantInfo: " << weights->info()->quantization_info()
405 << " Input shape: " << input->info()->tensor_shape()
406 << " Weights shape: " << weights->info()->tensor_shape()
407 << " Output shape: " << output->info()->tensor_shape()
408 << std::endl);
409 return func;
410}
411
412/** Create a backend element-wise operation layer function
413 *
414 * @tparam EltwiseFunctions Backend element-wise function
415 * @tparam TargetInfo Target-specific information
416 *
417 * @param[in] node Node to create the backend function for
418 *
419 * @return Backend element-wise operation layer function
420 */
421template <typename EltwiseFunctions, typename TargetInfo>
422std::unique_ptr<IFunction> create_eltwise_layer(EltwiseLayerNode &node)
423{
424 validate_node<TargetInfo>(node, 2 /* expected inputs */, 1 /* expected outputs */);
425
426 // Extract IO and info
427 typename TargetInfo::TensorType *input1 = get_backing_tensor<TargetInfo>(node.input(0));
428 typename TargetInfo::TensorType *input2 = get_backing_tensor<TargetInfo>(node.input(1));
429 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
430 const EltwiseOperation eltwise_op = node.eltwise_operation();
431 const ConvertPolicy convert_policy = node.convert_policy();
432 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
433 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
434 ARM_COMPUTE_ERROR_ON(output == nullptr);
435
436 std::unique_ptr<IFunction> func = nullptr;
437 std::string func_name;
438 if(eltwise_op == EltwiseOperation::ADD)
439 {
440 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Addition>(
441 std::string("ArithmeticAddition"),
442 input1, input2, output, convert_policy);
443 }
444 else if(eltwise_op == EltwiseOperation::SUB)
445 {
446 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Subtraction>(
447 std::string("ArithmeticSubtraction"),
448 input1, input2, output, convert_policy);
449 }
450 else if(eltwise_op == EltwiseOperation::MUL)
451 {
452 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Multiplication>(
453 std::string("PixelWiseMultiplication"),
454 input1, input2, output, 1.f, convert_policy, node.rounding_policy());
455 }
456 else
457 {
458 ARM_COMPUTE_ERROR("Unsupported element-wise operation!");
459 }
460
461 // Log info
462 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
463 << " Target " << TargetInfo::TargetType
464 << " Operation " << func_name
465 << " Data Type: " << input1->info()->data_type()
466 << " Shape : " << input1->info()->tensor_shape()
467 << std::endl);
468
469 return func;
470}
471
472/** Create a backend flatten layer function
473 *
474 * @tparam FlattenLayerFunction Backend flatten function
475 * @tparam TargetInfo Target-specific information
476 *
477 * @param[in] node Node to create the backend function for
478 *
479 * @return Backend flatten layer function
480 */
481template <typename FlattenLayerFunction, typename TargetInfo>
482std::unique_ptr<IFunction> create_flatten_layer(FlattenLayerNode &node)
483{
484 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
485
486 // Extract IO and info
487 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
488 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
489
490 // Create and configure function
491 auto func = support::cpp14::make_unique<FlattenLayerFunction>();
492 func->configure(input, output);
493 ARM_COMPUTE_ERROR_ON(input == nullptr);
494 ARM_COMPUTE_ERROR_ON(output == nullptr);
495
496 // Log info
497 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
498 << " Target " << TargetInfo::TargetType
499 << " Data Type: " << input->info()->data_type()
500 << " Input shape: " << input->info()->tensor_shape()
501 << " Output shape: " << output->info()->tensor_shape()
502 << std::endl);
503
504 return std::move(func);
505}
506
507/** Create a backend fully connected layer function
508 *
509 * @tparam FullyConnectedLayerFunction Backend fully-connected function
510 * @tparam TargetInfo Target-specific information
511 *
512 * @param[in] node Node to create the backend function for
513 * @param[in] ctx Graph context
514 *
515 * @return Backend fully connected layer function
516 */
517template <typename FullyConnectedLayerFunction, typename TargetInfo>
518std::unique_ptr<IFunction> create_fully_connected_layer(FullyConnectedLayerNode &node, GraphContext &ctx)
519{
520 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
521
522 // Extract IO and info
523 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
524 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
525 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
526 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
527
528 // Create and configure function
529 auto func = support::cpp14::make_unique<FullyConnectedLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
530 func->configure(input, weights, biases, output);
531 ARM_COMPUTE_ERROR_ON(input == nullptr);
532 ARM_COMPUTE_ERROR_ON(weights == nullptr);
533 ARM_COMPUTE_ERROR_ON(output == nullptr);
534
535 // Log info
536 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
537 << " Target " << TargetInfo::TargetType
538 << " Data Type: " << input->info()->data_type()
539 << " Input shape: " << input->info()->tensor_shape()
540 << " Weights shape: " << weights->info()->tensor_shape()
541 << " Output shape: " << output->info()->tensor_shape()
542 << std::endl);
543
544 return std::move(func);
545}
546
547/** Create a backend normalization layer function
548 *
549 * @tparam NormalizationLayerFunction Backend normalization function
550 * @tparam TargetInfo Target-specific information
551 *
552 * @param[in] node Node to create the backend function for
553 * @param[in] ctx Graph context
554 *
555 * @return Backend normalization layer function
556 */
557template <typename NormalizationLayerFunction, typename TargetInfo>
558std::unique_ptr<IFunction> create_normalization_layer(NormalizationLayerNode &node, GraphContext &ctx)
559{
560 ARM_COMPUTE_UNUSED(ctx);
561
562 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
563
564 // Extract IO and info
565 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
566 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
567 const NormalizationLayerInfo norm_info = node.normalization_info();
568 ARM_COMPUTE_ERROR_ON(input == nullptr);
569 ARM_COMPUTE_ERROR_ON(output == nullptr);
570
571 // Create and configure function
572 auto func = support::cpp14::make_unique<NormalizationLayerFunction>();
573 func->configure(input, output, norm_info);
574
575 // Log info
576 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
577 << " Target " << TargetInfo::TargetType
578 << " Data Type: " << input->info()->data_type()
579 << " Input shape: " << input->info()->tensor_shape()
580 << " Output shape: " << output->info()->tensor_shape()
581 << " Normalization info: " << norm_info.type()
582 << std::endl);
583
584 return std::move(func);
585}
586
587/** Create a backend pooling layer function
588 *
589 * @tparam PoolingLayerFunction Backend pooling function
590 * @tparam TargetInfo Target-specific information
591 *
592 * @param[in] node Node to create the backend function for
593 *
594 * @return Backend pooling layer function
595 */
596template <typename PoolingLayerFunction, typename TargetInfo>
597std::unique_ptr<IFunction> create_pooling_layer(PoolingLayerNode &node)
598{
599 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
600
601 // Extract IO and info
602 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
603 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
604 const PoolingLayerInfo pool_info = node.pooling_info();
605 ARM_COMPUTE_ERROR_ON(input == nullptr);
606 ARM_COMPUTE_ERROR_ON(output == nullptr);
607
608 // Create and configure function
609 auto func = support::cpp14::make_unique<PoolingLayerFunction>();
610 func->configure(input, output, pool_info);
611
612 // Log info
613 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
614 << " Target " << TargetInfo::TargetType
615 << " Data Type: " << input->info()->data_type()
616 << " Input shape: " << input->info()->tensor_shape()
617 << " Output shape: " << output->info()->tensor_shape()
618 << " Pooling info: " << pool_info.pool_type()
619 << std::endl);
620
621 return std::move(func);
622}
623
624/** Create a backend reshape layer function
625 *
626 * @tparam ReshapeLayerFunction Backend reshape function
627 * @tparam TargetInfo Target-specific information
628 *
629 * @param[in] node Node to create the backend function for
630 *
631 * @return Backend reshape layer function
632 */
633template <typename ReshapeLayerFunction, typename TargetInfo>
634std::unique_ptr<IFunction> create_reshape_layer(ReshapeLayerNode &node)
635{
636 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
637
638 // Extract IO and info
639 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
640 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
641 ARM_COMPUTE_ERROR_ON(input == nullptr);
642 ARM_COMPUTE_ERROR_ON(output == nullptr);
643
644 // Create and configure function
645 auto func = support::cpp14::make_unique<ReshapeLayerFunction>();
646 func->configure(input, output);
647
648 // Log info
649 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
650 << " Target " << TargetInfo::TargetType
651 << " Data Type: " << input->info()->data_type()
652 << " Input shape: " << input->info()->tensor_shape()
653 << " Output shape: " << output->info()->tensor_shape()
654 << std::endl);
655
656 return std::move(func);
657}
658
659/** Create a backend resize layer function
660 *
661 * @tparam ResizeLayerFunction Backend resize function
662 * @tparam TargetInfo Target-specific information
663 *
664 * @param[in] node Node to create the backend function for
665 *
666 * @return Backend resize layer function
667 */
668template <typename ResizeLayerFunction, typename TargetInfo>
669std::unique_ptr<IFunction> create_resize_layer(ResizeLayerNode &node)
670{
671 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
672
673 // Extract IO and info
674 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
675 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
676 ARM_COMPUTE_ERROR_ON(input == nullptr);
677 ARM_COMPUTE_ERROR_ON(output == nullptr);
678 const InterpolationPolicy policy = node.policy();
679
680 // Create and configure function
681 auto func = support::cpp14::make_unique<ResizeLayerFunction>();
682 func->configure(input, output, policy, BorderMode::CONSTANT);
683
684 // Log info
685 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
686 << " Target " << TargetInfo::TargetType
687 << " Data Type: " << input->info()->data_type()
688 << " Input shape: " << input->info()->tensor_shape()
689 << " Output shape: " << output->info()->tensor_shape()
690 << " Interpolation: " << policy
691 << std::endl);
692
693 return std::move(func);
694}
695
696/** Create a backend softmax layer function
697 *
698 * @tparam SoftmaxLayerFunction Backend softmax function
699 * @tparam TargetInfo Target-specific information
700 *
701 * @param[in] node Node to create the backend function for
702 * @param[in] ctx Graph context
703 *
704 * @return Backend softmax layer function
705 */
706template <typename SoftmaxLayerFunction, typename TargetInfo>
707std::unique_ptr<IFunction> create_softmax_layer(SoftmaxLayerNode &node, GraphContext &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 float beta = node.beta();
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<SoftmaxLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
720 func->configure(input, output, beta);
721
722 // Log info
723 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
724 << " Target " << TargetInfo::TargetType
725 << " Data Type: " << input->info()->data_type()
726 << " Input shape: " << input->info()->tensor_shape()
727 << " Output shape: " << output->info()->tensor_shape()
728 << std::endl);
729
730 return std::move(func);
731}
732} // namespace detail
733} // namespace backends
734} // namespace graph
735} // namespace arm_compute
736
737#endif /* __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__ */