blob: 978d3bc1a8ad34ac124dcce55d8b3ece9962e6e2 [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));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100527 const FullyConnectedLayerInfo fc_info = node.info();
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100528
529 // Create and configure function
530 auto func = support::cpp14::make_unique<FullyConnectedLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100531 func->configure(input, weights, biases, output, fc_info);
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
536 // Log info
537 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
538 << " Target " << TargetInfo::TargetType
539 << " Data Type: " << input->info()->data_type()
540 << " Input shape: " << input->info()->tensor_shape()
541 << " Weights shape: " << weights->info()->tensor_shape()
542 << " Output shape: " << output->info()->tensor_shape()
543 << std::endl);
544
545 return std::move(func);
546}
547
548/** Create a backend normalization layer function
549 *
550 * @tparam NormalizationLayerFunction Backend normalization function
551 * @tparam TargetInfo Target-specific information
552 *
553 * @param[in] node Node to create the backend function for
554 * @param[in] ctx Graph context
555 *
556 * @return Backend normalization layer function
557 */
558template <typename NormalizationLayerFunction, typename TargetInfo>
559std::unique_ptr<IFunction> create_normalization_layer(NormalizationLayerNode &node, GraphContext &ctx)
560{
561 ARM_COMPUTE_UNUSED(ctx);
562
563 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
564
565 // Extract IO and info
566 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
567 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
568 const NormalizationLayerInfo norm_info = node.normalization_info();
569 ARM_COMPUTE_ERROR_ON(input == nullptr);
570 ARM_COMPUTE_ERROR_ON(output == nullptr);
571
572 // Create and configure function
573 auto func = support::cpp14::make_unique<NormalizationLayerFunction>();
574 func->configure(input, output, norm_info);
575
576 // Log info
577 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
578 << " Target " << TargetInfo::TargetType
579 << " Data Type: " << input->info()->data_type()
580 << " Input shape: " << input->info()->tensor_shape()
581 << " Output shape: " << output->info()->tensor_shape()
582 << " Normalization info: " << norm_info.type()
583 << std::endl);
584
585 return std::move(func);
586}
587
588/** Create a backend pooling layer function
589 *
590 * @tparam PoolingLayerFunction Backend pooling function
591 * @tparam TargetInfo Target-specific information
592 *
593 * @param[in] node Node to create the backend function for
594 *
595 * @return Backend pooling layer function
596 */
597template <typename PoolingLayerFunction, typename TargetInfo>
598std::unique_ptr<IFunction> create_pooling_layer(PoolingLayerNode &node)
599{
600 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
601
602 // Extract IO and info
603 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
604 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
605 const PoolingLayerInfo pool_info = node.pooling_info();
606 ARM_COMPUTE_ERROR_ON(input == nullptr);
607 ARM_COMPUTE_ERROR_ON(output == nullptr);
608
609 // Create and configure function
610 auto func = support::cpp14::make_unique<PoolingLayerFunction>();
611 func->configure(input, output, pool_info);
612
613 // Log info
614 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
615 << " Target " << TargetInfo::TargetType
616 << " Data Type: " << input->info()->data_type()
617 << " Input shape: " << input->info()->tensor_shape()
618 << " Output shape: " << output->info()->tensor_shape()
619 << " Pooling info: " << pool_info.pool_type()
620 << std::endl);
621
622 return std::move(func);
623}
624
625/** Create a backend reshape layer function
626 *
627 * @tparam ReshapeLayerFunction Backend reshape function
628 * @tparam TargetInfo Target-specific information
629 *
630 * @param[in] node Node to create the backend function for
631 *
632 * @return Backend reshape layer function
633 */
634template <typename ReshapeLayerFunction, typename TargetInfo>
635std::unique_ptr<IFunction> create_reshape_layer(ReshapeLayerNode &node)
636{
637 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
638
639 // Extract IO and info
640 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
641 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
642 ARM_COMPUTE_ERROR_ON(input == nullptr);
643 ARM_COMPUTE_ERROR_ON(output == nullptr);
644
645 // Create and configure function
646 auto func = support::cpp14::make_unique<ReshapeLayerFunction>();
647 func->configure(input, output);
648
649 // Log info
650 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
651 << " Target " << TargetInfo::TargetType
652 << " Data Type: " << input->info()->data_type()
653 << " Input shape: " << input->info()->tensor_shape()
654 << " Output shape: " << output->info()->tensor_shape()
655 << std::endl);
656
657 return std::move(func);
658}
659
660/** Create a backend resize layer function
661 *
662 * @tparam ResizeLayerFunction Backend resize function
663 * @tparam TargetInfo Target-specific information
664 *
665 * @param[in] node Node to create the backend function for
666 *
667 * @return Backend resize layer function
668 */
669template <typename ResizeLayerFunction, typename TargetInfo>
670std::unique_ptr<IFunction> create_resize_layer(ResizeLayerNode &node)
671{
672 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
673
674 // Extract IO and info
675 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
676 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
677 ARM_COMPUTE_ERROR_ON(input == nullptr);
678 ARM_COMPUTE_ERROR_ON(output == nullptr);
679 const InterpolationPolicy policy = node.policy();
680
681 // Create and configure function
682 auto func = support::cpp14::make_unique<ResizeLayerFunction>();
683 func->configure(input, output, policy, BorderMode::CONSTANT);
684
685 // Log info
686 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
687 << " Target " << TargetInfo::TargetType
688 << " Data Type: " << input->info()->data_type()
689 << " Input shape: " << input->info()->tensor_shape()
690 << " Output shape: " << output->info()->tensor_shape()
691 << " Interpolation: " << policy
692 << std::endl);
693
694 return std::move(func);
695}
696
697/** Create a backend softmax layer function
698 *
699 * @tparam SoftmaxLayerFunction Backend softmax function
700 * @tparam TargetInfo Target-specific information
701 *
702 * @param[in] node Node to create the backend function for
703 * @param[in] ctx Graph context
704 *
705 * @return Backend softmax layer function
706 */
707template <typename SoftmaxLayerFunction, typename TargetInfo>
708std::unique_ptr<IFunction> create_softmax_layer(SoftmaxLayerNode &node, GraphContext &ctx)
709{
710 validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
711
712 // Extract IO and info
713 typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0));
714 typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0));
715 const float beta = node.beta();
716 ARM_COMPUTE_ERROR_ON(input == nullptr);
717 ARM_COMPUTE_ERROR_ON(output == nullptr);
718
719 // Create and configure function
720 auto func = support::cpp14::make_unique<SoftmaxLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType));
721 func->configure(input, output, beta);
722
723 // Log info
724 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
725 << " Target " << TargetInfo::TargetType
726 << " Data Type: " << input->info()->data_type()
727 << " Input shape: " << input->info()->tensor_shape()
728 << " Output shape: " << output->info()->tensor_shape()
729 << std::endl);
730
731 return std::move(func);
732}
733} // namespace detail
734} // namespace backends
735} // namespace graph
736} // namespace arm_compute
737
738#endif /* __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__ */