blob: 1a1519bb6d751b46f2f986f9fb495737ddfb7e60 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/backends/NEON/NEFunctionFactory.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
26#include "arm_compute/core/utils/misc/Cast.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/Graph.h"
28#include "arm_compute/graph/GraphContext.h"
29#include "arm_compute/graph/Logger.h"
30#include "arm_compute/graph/TypePrinter.h"
Georgios Pinitasda2491f2018-06-01 17:49:09 +010031#include "arm_compute/graph/backends/FunctionHelpers.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032#include "arm_compute/graph/backends/Utils.h"
33#include "arm_compute/graph/nodes/Nodes.h"
Isabella Gottardi7234ed82018-11-27 08:51:10 +000034#include "arm_compute/runtime/CPP/CPPFunctions.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000035#include "arm_compute/runtime/NEON/NEFunctions.h"
36#include "support/ToolchainSupport.h"
37
38using namespace arm_compute::utils::cast;
39
40namespace arm_compute
41{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010042namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000043{
44namespace backends
45{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010046/** Target specific information structure used to pass information to the layer templates */
47struct NETargetInfo
Georgios Pinitasd8734b52017-12-22 15:27:52 +000048{
giuros01acce5042019-02-21 17:32:34 +000049 using TensorType = arm_compute::ITensor;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010050 using SrcTensorType = const arm_compute::ITensor;
giuros01acce5042019-02-21 17:32:34 +000051 using TensorConcreteType = arm_compute::Tensor;
Georgios Pinitasda2491f2018-06-01 17:49:09 +010052 static Target TargetType;
53};
Georgios Pinitasd8734b52017-12-22 15:27:52 +000054
Georgios Pinitasda2491f2018-06-01 17:49:09 +010055Target NETargetInfo::TargetType = Target::NEON;
56
Sheri Zhang16dddd22020-05-27 15:03:48 +010057/** Collection of NEON convolution functions */
Georgios Pinitasda2491f2018-06-01 17:49:09 +010058struct NEConvolutionLayerFunctions
Georgios Pinitasd8734b52017-12-22 15:27:52 +000059{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010060 using GenericConvolutionLayer = NEConvolutionLayer;
61 using GEMMConvolutionLayer = NEGEMMConvolutionLayer;
62 using DirectConvolutionLayer = NEDirectConvolutionLayer;
63 using WinogradConvolutionLayer = NEWinogradConvolutionLayer;
64};
65
Sheri Zhang16dddd22020-05-27 15:03:48 +010066/** Collection of NEON element-wise functions */
Georgios Pinitasda2491f2018-06-01 17:49:09 +010067struct NEEltwiseFunctions
68{
69 using Addition = NEArithmeticAddition;
70 using Subtraction = NEArithmeticSubtraction;
71 using Multiplication = NEPixelWiseMultiplication;
thecha01f8e35842020-07-28 17:28:17 +010072 using Maximum = NEElementwiseMax;
Georgios Pinitasda2491f2018-06-01 17:49:09 +010073};
74
Sheri Zhang16dddd22020-05-27 15:03:48 +010075/** Collection of NEON unary element-wise functions */
76struct NEUnaryEltwiseFunctions
77{
78 using Exp = NEExpLayer;
79};
80
giuros01acce5042019-02-21 17:32:34 +000081/** Function and tensor types to be used inside a NEON fused convolution/batch normalization layer */
82struct NEFusedLayerTypes
83{
Manuel Bottinibffb41e2019-06-20 16:00:27 +010084 using ConvolutionLayer = NEConvolutionLayer;
85 using DepthwiseConvolutionLayer = NEDepthwiseConvolutionLayer;
86 using FuseBatchNormalization = NEFuseBatchNormalization;
giuros01acce5042019-02-21 17:32:34 +000087};
88
Georgios Pinitasda2491f2018-06-01 17:49:09 +010089namespace detail
90{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010091template <>
92std::unique_ptr<IFunction> create_normalization_layer<NENormalizationLayer, NETargetInfo>(NormalizationLayerNode &node, GraphContext &ctx)
Georgios Pinitas087eaf62018-05-16 15:52:35 +010093{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010094 validate_node<NETargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitas087eaf62018-05-16 15:52:35 +010095
96 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +010097 NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0));
98 NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0));
Georgios Pinitasd8734b52017-12-22 15:27:52 +000099 const NormalizationLayerInfo norm_info = node.normalization_info();
100 ARM_COMPUTE_ERROR_ON(input == nullptr);
101 ARM_COMPUTE_ERROR_ON(output == nullptr);
102
103 // Create and configure function
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100104 auto func = support::cpp14::make_unique<NENormalizationLayer>(get_memory_manager(ctx, NETargetInfo::TargetType));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000105 func->configure(input, output, norm_info);
106
107 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000108 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
109 << node.name()
110 << " Type: " << node.type()
111 << " Target: " << NETargetInfo::TargetType
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000112 << " Data Type: " << input->info()->data_type()
113 << " Input shape: " << input->info()->tensor_shape()
114 << " Output shape: " << output->info()->tensor_shape()
115 << " Normalization info: " << norm_info.type()
116 << std::endl);
117
Georgios Pinitas0b192e82020-02-20 17:09:28 +0000118 return RETURN_UNIQUE_PTR(func);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119}
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100120} // namespace detail
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000121
122std::unique_ptr<IFunction> NEFunctionFactory::create(INode *node, GraphContext &ctx)
123{
124 if(node == nullptr)
125 {
126 return nullptr;
127 }
128
129 NodeType type = node->type();
130 switch(type)
131 {
132 case NodeType::ActivationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100133 return detail::create_activation_layer<NEActivationLayer, NETargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000134 case NodeType::BatchNormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100135 return detail::create_batch_normalization_layer<NEBatchNormalizationLayer, NETargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
Georgios Pinitasf1adf112018-11-02 12:54:18 +0000136 case NodeType::ChannelShuffleLayer:
137 return detail::create_channel_shuffle_layer<NEChannelShuffleLayer, NETargetInfo>(*polymorphic_downcast<ChannelShuffleLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000138 case NodeType::ConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100139 return detail::create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100140 case NodeType::DeconvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100141 return detail::create_deconvolution_layer<NEDeconvolutionLayer, NETargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx);
Georgios Pinitase2220552018-07-20 13:23:44 +0100142 case NodeType::ConcatenateLayer:
143 return detail::create_concatenate_layer<NEConcatenateLayer, NETargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000144 case NodeType::DepthwiseConvolutionLayer:
Manuel Bottini05069f02019-09-26 17:18:26 +0100145 return detail::create_depthwise_convolution_layer<NEDepthwiseConvolutionLayer, NETargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000146 case NodeType::DequantizationLayer:
147 return detail::create_dequantization_layer<NEDequantizationLayer, NETargetInfo>(*polymorphic_downcast<DequantizationLayerNode *>(node));
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000148 case NodeType::DetectionOutputLayer:
149 return detail::create_detection_output_layer<CPPDetectionOutputLayer, NETargetInfo>(*polymorphic_downcast<DetectionOutputLayerNode *>(node));
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000150 case NodeType::DetectionPostProcessLayer:
Giuseppe Rossinid9853782019-10-25 11:11:44 +0100151 return detail::create_detection_post_process_layer<NEDetectionPostProcessLayer, NETargetInfo>(*polymorphic_downcast<DetectionPostProcessLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000152 case NodeType::EltwiseLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100153 return detail::create_eltwise_layer<NEEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
Sheri Zhang16dddd22020-05-27 15:03:48 +0100154 case NodeType::UnaryEltwiseLayer:
155 return detail::create_unary_eltwise_layer<NEUnaryEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<UnaryEltwiseLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000156 case NodeType::FlattenLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100157 return detail::create_flatten_layer<NEFlattenLayer, NETargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000158 case NodeType::FullyConnectedLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100159 return detail::create_fully_connected_layer<NEFullyConnectedLayer, NETargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
giuros01acce5042019-02-21 17:32:34 +0000160 case NodeType::FusedConvolutionBatchNormalizationLayer:
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +0000161 return detail::create_fused_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node), ctx);
Manuel Bottinicf3abde2019-07-29 16:59:41 +0100162 case NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer:
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +0000163 return detail::create_fused_depthwise_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node), ctx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000164 case NodeType::NormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100165 return detail::create_normalization_layer<NENormalizationLayer, NETargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
Georgios Pinitas07fbe372020-02-04 13:21:02 +0000166 case NodeType::PadLayer:
167 return detail::create_pad_layer<NEPadLayer, NETargetInfo>(*polymorphic_downcast<PadLayerNode *>(node));
Georgios Pinitas57c48242018-08-02 13:41:49 +0100168 case NodeType::PermuteLayer:
169 return detail::create_permute_layer<NEPermute, NETargetInfo>(*polymorphic_downcast<PermuteLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000170 case NodeType::PoolingLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100171 return detail::create_pooling_layer<NEPoolingLayer, NETargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
Georgios Pinitasf8c47492020-02-04 17:39:59 +0000172 case NodeType::PReluLayer:
173 return detail::create_prelu_layer<NEPReluLayer, NETargetInfo>(*polymorphic_downcast<PReluLayerNode *>(node));
Giorgio Arena6e9d0e02020-01-03 15:02:04 +0000174 case NodeType::PrintLayer:
175 return detail::create_print_layer<NETargetInfo>(*polymorphic_downcast<PrintLayerNode *>(node));
Pablo Tello32521432018-11-15 14:43:10 +0000176 case NodeType::PriorBoxLayer:
177 return detail::create_priorbox_layer<NEPriorBoxLayer, NETargetInfo>(*polymorphic_downcast<PriorBoxLayerNode *>(node));
Isabella Gottardi3db1ba92019-05-17 12:35:20 +0100178 case NodeType::QuantizationLayer:
179 return detail::create_quantization_layer<NEQuantizationLayer, NETargetInfo>(*polymorphic_downcast<QuantizationLayerNode *>(node));
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100180 case NodeType::ReorgLayer:
181 return detail::create_reorg_layer<NEReorgLayer, NETargetInfo>(*polymorphic_downcast<ReorgLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000182 case NodeType::ReshapeLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100183 return detail::create_reshape_layer<NEReshapeLayer, NETargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node));
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100184 case NodeType::ResizeLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100185 return detail::create_resize_layer<NEScale, NETargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000186 case NodeType::SoftmaxLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100187 return detail::create_softmax_layer<NESoftmaxLayer, NETargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
Michele Di Giorgioec699752019-03-22 15:25:32 +0000188 case NodeType::StackLayer:
189 return detail::create_stack_layer<NEStackLayer, NETargetInfo>(*polymorphic_downcast<StackLayerNode *>(node));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100190 case NodeType::UpsampleLayer:
191 return detail::create_upsample_layer<NEUpsampleLayer, NETargetInfo>(*polymorphic_downcast<UpsampleLayerNode *>(node), ctx);
Michalis Spyroue22aa132018-09-13 10:35:33 +0100192 case NodeType::YOLOLayer:
193 return detail::create_yolo_layer<NEYOLOLayer, NETargetInfo>(*polymorphic_downcast<YOLOLayerNode *>(node), ctx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000194 default:
195 return nullptr;
196 }
197}
198} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100199} // namespace graph
Pablo Tello32521432018-11-15 14:43:10 +0000200} // namespace arm_compute