blob: fe15d4cec1ee2b47bc36c74925e920fe6bf443b3 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Matthew Bentham7d9a78e2023-05-31 13:18:33 +00002 * Copyright (c) 2018-2021,2023 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026#include "arm_compute/graph/backends/FunctionHelpers.h"
27#include "arm_compute/graph/backends/Utils.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010028#include "arm_compute/graph/Graph.h"
29#include "arm_compute/graph/GraphContext.h"
30#include "arm_compute/graph/Logger.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031#include "arm_compute/graph/nodes/Nodes.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "arm_compute/graph/TypePrinter.h"
Isabella Gottardi7234ed82018-11-27 08:51:10 +000033#include "arm_compute/runtime/CPP/CPPFunctions.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000034#include "arm_compute/runtime/NEON/NEFunctions.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000035
36using namespace arm_compute::utils::cast;
37
38namespace arm_compute
39{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010040namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000041{
42namespace backends
43{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010044/** Target specific information structure used to pass information to the layer templates */
45struct NETargetInfo
Georgios Pinitasd8734b52017-12-22 15:27:52 +000046{
giuros01acce5042019-02-21 17:32:34 +000047 using TensorType = arm_compute::ITensor;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010048 using SrcTensorType = const arm_compute::ITensor;
giuros01acce5042019-02-21 17:32:34 +000049 using TensorConcreteType = arm_compute::Tensor;
Georgios Pinitasda2491f2018-06-01 17:49:09 +010050 static Target TargetType;
51};
Georgios Pinitasd8734b52017-12-22 15:27:52 +000052
Georgios Pinitasda2491f2018-06-01 17:49:09 +010053Target NETargetInfo::TargetType = Target::NEON;
54
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000055/** Collection of CPU convolution functions */
Georgios Pinitasda2491f2018-06-01 17:49:09 +010056struct NEConvolutionLayerFunctions
Georgios Pinitasd8734b52017-12-22 15:27:52 +000057{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010058 using GenericConvolutionLayer = NEConvolutionLayer;
59 using GEMMConvolutionLayer = NEGEMMConvolutionLayer;
60 using DirectConvolutionLayer = NEDirectConvolutionLayer;
61 using WinogradConvolutionLayer = NEWinogradConvolutionLayer;
62};
63
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000064/** Collection of CPU element-wise functions */
Georgios Pinitasda2491f2018-06-01 17:49:09 +010065struct NEEltwiseFunctions
66{
67 using Addition = NEArithmeticAddition;
68 using Subtraction = NEArithmeticSubtraction;
69 using Multiplication = NEPixelWiseMultiplication;
thecha01f8e35842020-07-28 17:28:17 +010070 using Maximum = NEElementwiseMax;
Alessandro Navone6413e492021-02-02 11:39:05 +000071 using Division = NEElementwiseDivision;
Georgios Pinitasda2491f2018-06-01 17:49:09 +010072};
73
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000074/** Collection of CPU unary element-wise functions */
Sheri Zhang16dddd22020-05-27 15:03:48 +010075struct NEUnaryEltwiseFunctions
76{
77 using Exp = NEExpLayer;
78};
79
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000080/** Function and tensor types to be used inside a fused convolution/batch normalization layer */
giuros01acce5042019-02-21 17:32:34 +000081struct NEFusedLayerTypes
82{
Manuel Bottinibffb41e2019-06-20 16:00:27 +010083 using ConvolutionLayer = NEConvolutionLayer;
84 using DepthwiseConvolutionLayer = NEDepthwiseConvolutionLayer;
85 using FuseBatchNormalization = NEFuseBatchNormalization;
giuros01acce5042019-02-21 17:32:34 +000086};
87
Georgios Pinitasda2491f2018-06-01 17:49:09 +010088namespace detail
89{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010090template <>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091std::unique_ptr<IFunction> create_normalization_layer<NENormalizationLayer, NETargetInfo>(NormalizationLayerNode &node,
92 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 Pinitas40f51a62020-11-21 03:04:18 +0000104 auto func = std::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 "
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 << node.name() << " Type: " << node.type() << " Target: " << NETargetInfo::TargetType
110 << " Data Type: " << input->info()->data_type() << " Input shape: "
111 << input->info()->tensor_shape() << " Output shape: " << output->info()->tensor_shape()
112 << " Normalization info: " << norm_info.type() << std::endl);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000113
Paolo Tricerri8562a4e2023-09-24 13:52:43 +0000114 return func;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000115}
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100116} // namespace detail
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000117
118std::unique_ptr<IFunction> NEFunctionFactory::create(INode *node, GraphContext &ctx)
119{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 if (node == nullptr)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000121 {
122 return nullptr;
123 }
124
125 NodeType type = node->type();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 switch (type)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000127 {
128 case NodeType::ActivationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129 return detail::create_activation_layer<NEActivationLayer, NETargetInfo>(
130 *polymorphic_downcast<ActivationLayerNode *>(node));
thecha01e8f05da2020-08-24 17:21:41 +0100131 case NodeType::ArgMinMaxLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 return detail::create_arg_min_max_layer<NEArgMinMaxLayer, NETargetInfo>(
133 *polymorphic_downcast<ArgMinMaxLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000134 case NodeType::BatchNormalizationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 return detail::create_batch_normalization_layer<NEBatchNormalizationLayer, NETargetInfo>(
136 *polymorphic_downcast<BatchNormalizationLayerNode *>(node));
Georgios Pinitasf1adf112018-11-02 12:54:18 +0000137 case NodeType::ChannelShuffleLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 return detail::create_channel_shuffle_layer<NEChannelShuffleLayer, NETargetInfo>(
139 *polymorphic_downcast<ChannelShuffleLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000140 case NodeType::ConvolutionLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 return detail::create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(
142 *polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
thecha010a05e6a2020-08-28 18:40:38 +0100143 case NodeType::DepthToSpaceLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 return detail::create_depth_to_space_layer<NEDepthToSpaceLayer, NETargetInfo>(
145 *polymorphic_downcast<DepthToSpaceLayerNode *>(node));
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100146 case NodeType::DeconvolutionLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 return detail::create_deconvolution_layer<NEDeconvolutionLayer, NETargetInfo>(
148 *polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx);
Georgios Pinitase2220552018-07-20 13:23:44 +0100149 case NodeType::ConcatenateLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 return detail::create_concatenate_layer<NEConcatenateLayer, NETargetInfo>(
151 *polymorphic_downcast<ConcatenateLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000152 case NodeType::DepthwiseConvolutionLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 return detail::create_depthwise_convolution_layer<NEDepthwiseConvolutionLayer, NETargetInfo>(
154 *polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000155 case NodeType::DequantizationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 return detail::create_dequantization_layer<NEDequantizationLayer, NETargetInfo>(
157 *polymorphic_downcast<DequantizationLayerNode *>(node));
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000158 case NodeType::DetectionOutputLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 return detail::create_detection_output_layer<CPPDetectionOutputLayer, NETargetInfo>(
160 *polymorphic_downcast<DetectionOutputLayerNode *>(node));
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000161 case NodeType::DetectionPostProcessLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100162 return detail::create_detection_post_process_layer<NEDetectionPostProcessLayer, NETargetInfo>(
163 *polymorphic_downcast<DetectionPostProcessLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000164 case NodeType::EltwiseLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100165 return detail::create_eltwise_layer<NEEltwiseFunctions, NETargetInfo>(
166 *polymorphic_downcast<EltwiseLayerNode *>(node));
Sheri Zhang16dddd22020-05-27 15:03:48 +0100167 case NodeType::UnaryEltwiseLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 return detail::create_unary_eltwise_layer<NEUnaryEltwiseFunctions, NETargetInfo>(
169 *polymorphic_downcast<UnaryEltwiseLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000170 case NodeType::FlattenLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100171 return detail::create_flatten_layer<NEFlattenLayer, NETargetInfo>(
172 *polymorphic_downcast<FlattenLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000173 case NodeType::FullyConnectedLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100174 return detail::create_fully_connected_layer<NEFullyConnectedLayer, NETargetInfo>(
175 *polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
giuros01acce5042019-02-21 17:32:34 +0000176 case NodeType::FusedConvolutionBatchNormalizationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 return detail::create_fused_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(
178 *polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node), ctx);
Manuel Bottinicf3abde2019-07-29 16:59:41 +0100179 case NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100180 return detail::create_fused_depthwise_convolution_batch_normalization_layer<NEFusedLayerTypes,
181 NETargetInfo>(
182 *polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node), ctx);
thecha013603aff2020-09-01 14:52:38 +0100183 case NodeType::L2NormalizeLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 return detail::create_l2_normalize_layer<NEL2NormalizeLayer, NETargetInfo>(
185 *polymorphic_downcast<L2NormalizeLayerNode *>(node), ctx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000186 case NodeType::NormalizationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 return detail::create_normalization_layer<NENormalizationLayer, NETargetInfo>(
188 *polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
Georgios Pinitas07fbe372020-02-04 13:21:02 +0000189 case NodeType::PadLayer:
190 return detail::create_pad_layer<NEPadLayer, NETargetInfo>(*polymorphic_downcast<PadLayerNode *>(node));
Georgios Pinitas57c48242018-08-02 13:41:49 +0100191 case NodeType::PermuteLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 return detail::create_permute_layer<NEPermute, NETargetInfo>(
193 *polymorphic_downcast<PermuteLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000194 case NodeType::PoolingLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 return detail::create_pooling_layer<NEPoolingLayer, NETargetInfo>(
196 *polymorphic_downcast<PoolingLayerNode *>(node));
Georgios Pinitasf8c47492020-02-04 17:39:59 +0000197 case NodeType::PReluLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 return detail::create_prelu_layer<NEPReluLayer, NETargetInfo>(
199 *polymorphic_downcast<PReluLayerNode *>(node));
Giorgio Arena6e9d0e02020-01-03 15:02:04 +0000200 case NodeType::PrintLayer:
201 return detail::create_print_layer<NETargetInfo>(*polymorphic_downcast<PrintLayerNode *>(node));
Pablo Tello32521432018-11-15 14:43:10 +0000202 case NodeType::PriorBoxLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100203 return detail::create_priorbox_layer<NEPriorBoxLayer, NETargetInfo>(
204 *polymorphic_downcast<PriorBoxLayerNode *>(node));
Isabella Gottardi3db1ba92019-05-17 12:35:20 +0100205 case NodeType::QuantizationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 return detail::create_quantization_layer<NEQuantizationLayer, NETargetInfo>(
207 *polymorphic_downcast<QuantizationLayerNode *>(node));
thecha01d64444b2020-09-07 14:50:21 +0100208 case NodeType::ReductionOperationLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 return detail::create_reduction_operation_layer<NEReductionOperation, NETargetInfo>(
210 *polymorphic_downcast<ReductionLayerNode *>(node), ctx);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100211 case NodeType::ReorgLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100212 return detail::create_reorg_layer<NEReorgLayer, NETargetInfo>(
213 *polymorphic_downcast<ReorgLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000214 case NodeType::ReshapeLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100215 return detail::create_reshape_layer<NEReshapeLayer, NETargetInfo>(
216 *polymorphic_downcast<ReshapeLayerNode *>(node));
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100217 case NodeType::ResizeLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100218 return detail::create_resize_layer<NEScale, NETargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
Georgios Pinitas0b1c2db2020-12-04 15:51:34 +0000219 case NodeType::SliceLayer:
220 return detail::create_slice_layer<NESlice, NETargetInfo>(*polymorphic_downcast<SliceLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000221 case NodeType::SoftmaxLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100222 return detail::create_softmax_layer<NESoftmaxLayer, NETargetInfo>(
223 *polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
Michele Di Giorgioec699752019-03-22 15:25:32 +0000224 case NodeType::StackLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225 return detail::create_stack_layer<NEStackLayer, NETargetInfo>(
226 *polymorphic_downcast<StackLayerNode *>(node));
thecha012bfadd92020-08-12 17:25:51 +0100227 case NodeType::StridedSliceLayer:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100228 return detail::create_strided_slice_layer<NEStridedSlice, NETargetInfo>(
229 *polymorphic_downcast<StridedSliceLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000230 default:
231 return nullptr;
232 }
233}
234} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100235} // namespace graph
Pablo Tello32521432018-11-15 14:43:10 +0000236} // namespace arm_compute