blob: 5739773dfc00dd1f9866c88486b306904088f91e [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
Georgios Pinitase2220552018-07-20 13:23:44 +0100195/** Create a backend layer concatenate function
196 *
197 * @tparam ConcatenateLayerFunction Backend concatenate function
198 * @tparam TargetInfo Target-specific information
199 *
200 * @param[in] node Node to create the backend function for
201 *
202 * @return Backend concatenate layer function
203 */
204template <typename ConcatenateLayerFunction, typename TargetInfo>
205std::unique_ptr<arm_compute::IFunction> create_concatenate_layer(ConcatenateLayerNode &node)
206{
207 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Creating Concatenate node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
208 ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1);
209
210 // Return nullptr if depth concatenate is switched off
211 if(!node.is_enabled())
212 {
213 return nullptr;
214 }
215
216 // Extract IO and info
217 std::vector<typename TargetInfo::TensorType *> inputs;
218 for(unsigned int i = 0; i < node.num_inputs(); ++i)
219 {
220 inputs.push_back(get_backing_tensor<TargetInfo>(node.input(i)));
221 }
222 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
223 const DataLayoutDimension concat_axis = node.concatenation_axis();
224
225 // Create and configure function
226 auto func = support::cpp14::make_unique<ConcatenateLayerFunction>();
227 func->configure(inputs, output, concat_axis);
228
229 // Log info
230 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
231 << " Target " << TargetInfo::TargetType
232 << " Data Type: " << output->info()->data_type()
233 << " Shape: " << output->info()->tensor_shape()
234 << " Num Inputs: " << inputs.size()
235 << " Axis: " << concat_axis
236 << std::endl);
237
238 return std::move(func);
239}
240
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100241/** Create a backend convolution layer function
242 *
243 * @tparam ConvolutionLayerFunctions Backend convolution functions
244 * @tparam TargetInfo Target-specific information
245 *
246 * @param[in] node Node to create the backend function for
247 * @param[in] ctx Graph context
248 *
249 * @return Backend convolution layer function
250 */
251template <typename ConvolutionLayerFunctions, typename TargetInfo>
252std::unique_ptr<IFunction> create_convolution_layer(ConvolutionLayerNode &node, GraphContext &ctx)
253{
254 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
255
256 // Extract IO and info
257 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
258 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
259 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
260 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
261
262 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
263 {
264 biases->info()->set_data_type(DataType::S32);
265 }
266
267 const PadStrideInfo conv_info = node.convolution_info();
268 const ConvolutionMethod conv_algorithm = node.convolution_method();
Georgios Pinitase2220552018-07-20 13:23:44 +0100269 const bool fast_math = node.fast_math_hint() == FastMathHint::Enabled;
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100270
271 // Create and configure function (we assume that functions have been validated before creation)
272 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType);
273 std::unique_ptr<IFunction> func;
274 std::string func_name;
275
Georgios Pinitase2220552018-07-20 13:23:44 +0100276 if(conv_algorithm == ConvolutionMethod::Winograd)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100277 {
278 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::WinogradConvolutionLayer>(
279 std::string("WinogradConvolutionLayer"), mm,
280 input, weights, biases, output, conv_info, ActivationLayerInfo(), fast_math);
281 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100282 else if(conv_algorithm == ConvolutionMethod::Direct)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100283 {
284 std::tie(func, func_name) = create_named_function<typename ConvolutionLayerFunctions::DirectConvolutionLayer>(
285 std::string("DirectConvolutionLayer"),
286 input, weights, biases, output, conv_info);
287 }
288 else if(conv_algorithm == ConvolutionMethod::GEMM)
289 {
290 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GEMMConvolutionLayer>(
291 std::string("GEMMConvolutionLayer"), mm,
292 input, weights, biases, output, conv_info);
293 }
294 else
295 {
296 std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GenericConvolutionLayer>(
297 std::string("GenericConvolutionLayer"), mm,
298 input, weights, biases, output, conv_info, WeightsInfo(), Size2D(1U, 1U), ActivationLayerInfo(), fast_math);
299 }
300
301 // Log info
302 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name
303 << " Target " << TargetInfo::TargetType
304 << " Data Type: " << input->info()->data_type()
305 << " Input QuantInfo: " << input->info()->quantization_info()
306 << " Weights QuantInfo: " << weights->info()->quantization_info()
307 << " Input shape: " << input->info()->tensor_shape()
308 << " Weights shape: " << weights->info()->tensor_shape()
309 << " Output shape: " << output->info()->tensor_shape()
310 << std::endl);
311 return func;
312}
313
314/** Create a backend deconvolution layer function
315 *
316 * @tparam DeconvolutionLayerFunction Backend deconvolution function
317 * @tparam TargetInfo Target-specific information
318 *
319 * @param[in] node Node to create the backend function for
320 * @param[in] ctx Graph context
321 *
322 * @return Backend deconvolution layer function
323 */
324template <typename DeconvolutionLayerFunction, typename TargetInfo>
325std::unique_ptr<IFunction> create_deconvolution_layer(DeconvolutionLayerNode &node, GraphContext &ctx)
326{
327 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
328
329 // Extract IO and info
330 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
331 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
332 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
333 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
334
335 const PadStrideInfo deconv_info = node.deconvolution_info();
336 const Size2D inner_border = node.inner_border();
337
338 // Create and configure function (we assume that functions have been validated before creation)
339 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType);
340 std::unique_ptr<IFunction> func;
341
342 std::tie(func, std::ignore) = create_named_memory_managed_function<DeconvolutionLayerFunction>(
343 std::string(), mm,
344 input, weights, biases, output, deconv_info, inner_border.x(), inner_border.y());
345
346 // Log info
347 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
348 << " Target " << TargetInfo::TargetType
349 << " Data Type: " << input->info()->data_type()
350 << " Input shape: " << input->info()->tensor_shape()
351 << " Weights shape: " << weights->info()->tensor_shape()
352 << " Output shape: " << output->info()->tensor_shape()
353 << std::endl);
354 return func;
355}
356
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100357/** Create a backend layer depth-wise convolution function
358 *
359 * @tparam DepthwiseConvolutionLayerFunctions Backend depthwise convolution function
360 * @tparam TargetInfo Target-specific information
361 *
362 * @param[in] node Node to create the backend function for
363 *
364 * @return Backend depth-wise convolution layer function
365 */
366template <typename DepthwiseConvolutionLayerFunctions, typename TargetInfo>
367std::unique_ptr<IFunction> create_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node)
368{
369 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
370
371 // Extract IO and info
372 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
373 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
374 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
375 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
376
377 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
378 {
379 biases->info()->set_data_type(DataType::S32);
380 }
381
382 const PadStrideInfo conv_info = node.convolution_info();
383 const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method();
384
385 // Create and configure function (we assume that functions have been validated before creation)
386 std::unique_ptr<IFunction> func;
387 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100388 if(dwc_algorithm == DepthwiseConvolutionMethod::Optimized3x3)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100389 {
390 std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::DepthwiseConvolutionLayer3x3>(
391 std::string("DepthwiseConvolutionLayer3x3"),
392 input, weights, biases, output, conv_info);
393 }
394 else
395 {
396 std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::GenericDepthwiseConvolutionLayer>(
397 std::string("DepthwiseConvolutionLayer"),
398 input, weights, biases, output, conv_info);
399 }
400
401 // Log info
402 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name
403 << " Target " << TargetInfo::TargetType
404 << " Data Type: " << input->info()->data_type()
405 << " Input QuantInfo: " << input->info()->quantization_info()
406 << " Weights QuantInfo: " << weights->info()->quantization_info()
407 << " Input shape: " << input->info()->tensor_shape()
408 << " Weights shape: " << weights->info()->tensor_shape()
409 << " Output shape: " << output->info()->tensor_shape()
410 << std::endl);
411 return func;
412}
413
414/** Create a backend element-wise operation layer function
415 *
416 * @tparam EltwiseFunctions Backend element-wise function
417 * @tparam TargetInfo Target-specific information
418 *
419 * @param[in] node Node to create the backend function for
420 *
421 * @return Backend element-wise operation layer function
422 */
423template <typename EltwiseFunctions, typename TargetInfo>
424std::unique_ptr<IFunction> create_eltwise_layer(EltwiseLayerNode &node)
425{
426 validate_node<TargetInfo>(node, 2 /* expected inputs */, 1 /* expected outputs */);
427
428 // Extract IO and info
429 typename TargetInfo::TensorType *input1 = get_backing_tensor<TargetInfo>(node.input(0));
430 typename TargetInfo::TensorType *input2 = get_backing_tensor<TargetInfo>(node.input(1));
431 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
432 const EltwiseOperation eltwise_op = node.eltwise_operation();
433 const ConvertPolicy convert_policy = node.convert_policy();
434 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
435 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
436 ARM_COMPUTE_ERROR_ON(output == nullptr);
437
438 std::unique_ptr<IFunction> func = nullptr;
439 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100440 if(eltwise_op == EltwiseOperation::Add)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100441 {
442 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Addition>(
443 std::string("ArithmeticAddition"),
444 input1, input2, output, convert_policy);
445 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100446 else if(eltwise_op == EltwiseOperation::Sub)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100447 {
448 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Subtraction>(
449 std::string("ArithmeticSubtraction"),
450 input1, input2, output, convert_policy);
451 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100452 else if(eltwise_op == EltwiseOperation::Mul)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100453 {
454 std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Multiplication>(
455 std::string("PixelWiseMultiplication"),
456 input1, input2, output, 1.f, convert_policy, node.rounding_policy());
457 }
458 else
459 {
460 ARM_COMPUTE_ERROR("Unsupported element-wise operation!");
461 }
462
463 // Log info
464 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
465 << " Target " << TargetInfo::TargetType
466 << " Operation " << func_name
467 << " Data Type: " << input1->info()->data_type()
468 << " Shape : " << input1->info()->tensor_shape()
469 << std::endl);
470
471 return func;
472}
473
474/** Create a backend flatten layer function
475 *
476 * @tparam FlattenLayerFunction Backend flatten function
477 * @tparam TargetInfo Target-specific information
478 *
479 * @param[in] node Node to create the backend function for
480 *
481 * @return Backend flatten layer function
482 */
483template <typename FlattenLayerFunction, typename TargetInfo>
484std::unique_ptr<IFunction> create_flatten_layer(FlattenLayerNode &node)
485{
486 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
487
488 // Extract IO and info
489 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
490 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
491
Georgios Pinitase2220552018-07-20 13:23:44 +0100492 ARM_COMPUTE_ERROR_ON(input == nullptr);
493 ARM_COMPUTE_ERROR_ON(output == nullptr);
494
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100495 // Create and configure function
496 auto func = support::cpp14::make_unique<FlattenLayerFunction>();
497 func->configure(input, output);
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100498
499 // Log info
500 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
501 << " Target " << TargetInfo::TargetType
502 << " Data Type: " << input->info()->data_type()
503 << " Input shape: " << input->info()->tensor_shape()
504 << " Output shape: " << output->info()->tensor_shape()
505 << std::endl);
506
507 return std::move(func);
508}
509
510/** Create a backend fully connected layer function
511 *
512 * @tparam FullyConnectedLayerFunction Backend fully-connected function
513 * @tparam TargetInfo Target-specific information
514 *
515 * @param[in] node Node to create the backend function for
516 * @param[in] ctx Graph context
517 *
518 * @return Backend fully connected layer function
519 */
520template <typename FullyConnectedLayerFunction, typename TargetInfo>
521std::unique_ptr<IFunction> create_fully_connected_layer(FullyConnectedLayerNode &node, GraphContext &ctx)
522{
523 validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
524
525 // Extract IO and info
526 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
527 typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1));
528 typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2));
529 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100530 const FullyConnectedLayerInfo fc_info = node.info();
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100531
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100532 ARM_COMPUTE_ERROR_ON(input == nullptr);
533 ARM_COMPUTE_ERROR_ON(weights == nullptr);
534 ARM_COMPUTE_ERROR_ON(output == nullptr);
535
Georgios Pinitase2220552018-07-20 13:23:44 +0100536 // Create and configure function
537 auto func = support::cpp14::make_unique<FullyConnectedLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
538 func->configure(input, weights, biases, output, fc_info);
539
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100540 // Log info
541 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
542 << " Target " << TargetInfo::TargetType
543 << " Data Type: " << input->info()->data_type()
Georgios Pinitas2f1366a2018-07-31 16:33:06 +0100544 << " Input QuantInfo: " << input->info()->quantization_info()
545 << " Weights QuantInfo: " << weights->info()->quantization_info()
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100546 << " Input shape: " << input->info()->tensor_shape()
547 << " Weights shape: " << weights->info()->tensor_shape()
548 << " Output shape: " << output->info()->tensor_shape()
549 << std::endl);
550
551 return std::move(func);
552}
553
554/** Create a backend normalization layer function
555 *
556 * @tparam NormalizationLayerFunction Backend normalization function
557 * @tparam TargetInfo Target-specific information
558 *
559 * @param[in] node Node to create the backend function for
560 * @param[in] ctx Graph context
561 *
562 * @return Backend normalization layer function
563 */
564template <typename NormalizationLayerFunction, typename TargetInfo>
565std::unique_ptr<IFunction> create_normalization_layer(NormalizationLayerNode &node, GraphContext &ctx)
566{
567 ARM_COMPUTE_UNUSED(ctx);
568
569 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
570
571 // Extract IO and info
572 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
573 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
574 const NormalizationLayerInfo norm_info = node.normalization_info();
575 ARM_COMPUTE_ERROR_ON(input == nullptr);
576 ARM_COMPUTE_ERROR_ON(output == nullptr);
577
578 // Create and configure function
579 auto func = support::cpp14::make_unique<NormalizationLayerFunction>();
580 func->configure(input, output, norm_info);
581
582 // Log info
583 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
584 << " Target " << TargetInfo::TargetType
585 << " Data Type: " << input->info()->data_type()
586 << " Input shape: " << input->info()->tensor_shape()
587 << " Output shape: " << output->info()->tensor_shape()
588 << " Normalization info: " << norm_info.type()
589 << std::endl);
590
591 return std::move(func);
592}
593
Georgios Pinitas57c48242018-08-02 13:41:49 +0100594/** Create a backend permute layer function
595 *
596 * @tparam PermuteLayerFunction Backend permute function
597 * @tparam TargetInfo Target-specific information
598 *
599 * @param[in] node Node to create the backend function for
600 *
601 * @return Backend permute layer function
602 */
603template <typename PermuteLayerFunction, typename TargetInfo>
604std::unique_ptr<IFunction> create_permute_layer(PermuteLayerNode &node)
605{
606 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
607
608 // Extract IO and info
609 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
610 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
611 const PermutationVector &perm = node.permutation_vector();
612 ARM_COMPUTE_ERROR_ON(input == nullptr);
613 ARM_COMPUTE_ERROR_ON(output == nullptr);
614
615 // Create and configure function
616 auto func = support::cpp14::make_unique<PermuteLayerFunction>();
617 func->configure(input, output, perm);
618
619 // Log info
620 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
621 << " Target " << TargetInfo::TargetType
622 << " Data Type: " << input->info()->data_type()
623 << " Input shape: " << input->info()->tensor_shape()
624 << " Output shape: " << output->info()->tensor_shape()
625 << " Permutation vector: " << perm
626 << std::endl);
627
628 return std::move(func);
629}
630
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100631/** Create a backend pooling layer function
632 *
633 * @tparam PoolingLayerFunction Backend pooling function
634 * @tparam TargetInfo Target-specific information
635 *
636 * @param[in] node Node to create the backend function for
637 *
638 * @return Backend pooling layer function
639 */
640template <typename PoolingLayerFunction, typename TargetInfo>
641std::unique_ptr<IFunction> create_pooling_layer(PoolingLayerNode &node)
642{
643 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
644
645 // Extract IO and info
646 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
647 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
648 const PoolingLayerInfo pool_info = node.pooling_info();
649 ARM_COMPUTE_ERROR_ON(input == nullptr);
650 ARM_COMPUTE_ERROR_ON(output == nullptr);
651
652 // Create and configure function
653 auto func = support::cpp14::make_unique<PoolingLayerFunction>();
654 func->configure(input, output, pool_info);
655
656 // Log info
657 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
658 << " Target " << TargetInfo::TargetType
659 << " Data Type: " << input->info()->data_type()
660 << " Input shape: " << input->info()->tensor_shape()
661 << " Output shape: " << output->info()->tensor_shape()
662 << " Pooling info: " << pool_info.pool_type()
663 << std::endl);
664
665 return std::move(func);
666}
667
668/** Create a backend reshape layer function
669 *
670 * @tparam ReshapeLayerFunction Backend reshape function
671 * @tparam TargetInfo Target-specific information
672 *
673 * @param[in] node Node to create the backend function for
674 *
675 * @return Backend reshape layer function
676 */
677template <typename ReshapeLayerFunction, typename TargetInfo>
678std::unique_ptr<IFunction> create_reshape_layer(ReshapeLayerNode &node)
679{
680 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
681
682 // Extract IO and info
683 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
684 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
685 ARM_COMPUTE_ERROR_ON(input == nullptr);
686 ARM_COMPUTE_ERROR_ON(output == nullptr);
687
688 // Create and configure function
689 auto func = support::cpp14::make_unique<ReshapeLayerFunction>();
690 func->configure(input, output);
691
692 // Log info
693 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
694 << " Target " << TargetInfo::TargetType
695 << " Data Type: " << input->info()->data_type()
696 << " Input shape: " << input->info()->tensor_shape()
697 << " Output shape: " << output->info()->tensor_shape()
698 << std::endl);
699
700 return std::move(func);
701}
702
703/** Create a backend resize layer function
704 *
705 * @tparam ResizeLayerFunction Backend resize function
706 * @tparam TargetInfo Target-specific information
707 *
708 * @param[in] node Node to create the backend function for
709 *
710 * @return Backend resize layer function
711 */
712template <typename ResizeLayerFunction, typename TargetInfo>
713std::unique_ptr<IFunction> create_resize_layer(ResizeLayerNode &node)
714{
715 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
716
717 // Extract IO and info
718 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
719 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
720 ARM_COMPUTE_ERROR_ON(input == nullptr);
721 ARM_COMPUTE_ERROR_ON(output == nullptr);
722 const InterpolationPolicy policy = node.policy();
723
724 // Create and configure function
725 auto func = support::cpp14::make_unique<ResizeLayerFunction>();
726 func->configure(input, output, policy, BorderMode::CONSTANT);
727
728 // Log info
729 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
730 << " Target " << TargetInfo::TargetType
731 << " Data Type: " << input->info()->data_type()
732 << " Input shape: " << input->info()->tensor_shape()
733 << " Output shape: " << output->info()->tensor_shape()
734 << " Interpolation: " << policy
735 << std::endl);
736
737 return std::move(func);
738}
739
740/** Create a backend softmax layer function
741 *
742 * @tparam SoftmaxLayerFunction Backend softmax function
743 * @tparam TargetInfo Target-specific information
744 *
745 * @param[in] node Node to create the backend function for
746 * @param[in] ctx Graph context
747 *
748 * @return Backend softmax layer function
749 */
750template <typename SoftmaxLayerFunction, typename TargetInfo>
751std::unique_ptr<IFunction> create_softmax_layer(SoftmaxLayerNode &node, GraphContext &ctx)
752{
753 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
754
755 // Extract IO and info
756 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
757 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
758 const float beta = node.beta();
759 ARM_COMPUTE_ERROR_ON(input == nullptr);
760 ARM_COMPUTE_ERROR_ON(output == nullptr);
761
762 // Create and configure function
763 auto func = support::cpp14::make_unique<SoftmaxLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
764 func->configure(input, output, beta);
765
766 // Log info
767 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
768 << " Target " << TargetInfo::TargetType
769 << " Data Type: " << input->info()->data_type()
770 << " Input shape: " << input->info()->tensor_shape()
771 << " Output shape: " << output->info()->tensor_shape()
772 << std::endl);
773
774 return std::move(func);
775}
776} // namespace detail
777} // namespace backends
778} // namespace graph
779} // namespace arm_compute
780
781#endif /* __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__ */