blob: 852de549fa8e268c940ee64c0289cb7ef818171d [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
giuros01acce5042019-02-21 17:32:34 +00002 * Copyright (c) 2018-2019 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;
50 using TensorConcreteType = arm_compute::Tensor;
Georgios Pinitasda2491f2018-06-01 17:49:09 +010051 static Target TargetType;
52};
Georgios Pinitasd8734b52017-12-22 15:27:52 +000053
Georgios Pinitasda2491f2018-06-01 17:49:09 +010054Target NETargetInfo::TargetType = Target::NEON;
55
56/** Collection of CL convolution functions */
57struct NEConvolutionLayerFunctions
Georgios Pinitasd8734b52017-12-22 15:27:52 +000058{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010059 using GenericConvolutionLayer = NEConvolutionLayer;
60 using GEMMConvolutionLayer = NEGEMMConvolutionLayer;
61 using DirectConvolutionLayer = NEDirectConvolutionLayer;
62 using WinogradConvolutionLayer = NEWinogradConvolutionLayer;
63};
64
65/** Collection of CL depthwise convolution functions */
66struct NEDepthwiseConvolutionLayerFunctions
67{
Georgios Pinitas30271c72019-06-24 14:56:34 +010068 using GenericDepthwiseConvolutionLayer = NEDepthwiseConvolutionLayer;
69 using OptimizedDepthwiseConvolutionLayer = NEDepthwiseConvolutionLayerOptimized;
Georgios Pinitasda2491f2018-06-01 17:49:09 +010070};
71
72/** Collection of CL element-wise functions */
73struct NEEltwiseFunctions
74{
75 using Addition = NEArithmeticAddition;
76 using Subtraction = NEArithmeticSubtraction;
77 using Multiplication = NEPixelWiseMultiplication;
78};
79
giuros01acce5042019-02-21 17:32:34 +000080/** Function and tensor types to be used inside a NEON fused convolution/batch normalization layer */
81struct 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{
Isabella Gottardi7234ed82018-11-27 08:51:10 +000090// Specialized functions
Georgios Pinitasda2491f2018-06-01 17:49:09 +010091template <>
92std::unique_ptr<IFunction> create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(ConvolutionLayerNode &node,
93 GraphContext &ctx)
94{
95 validate_node<NETargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000096
97 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +010098 NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0));
99 NETargetInfo::TensorType *weights = get_backing_tensor<NETargetInfo>(node.input(1));
100 NETargetInfo::TensorType *biases = get_backing_tensor<NETargetInfo>(node.input(2));
101 NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0));
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100102
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100103 const bool is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
104
105 if(is_quantized)
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100106 {
107 biases->info()->set_data_type(DataType::S32);
108 }
109
Georgios Pinitas08346e92018-10-16 19:10:46 +0100110 const PadStrideInfo conv_info = node.convolution_info();
111 const ConvolutionMethod conv_algorithm = node.convolution_method();
112 const ActivationLayerInfo fused_act = node.fused_activation();
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000113
114 // Create and configure function (we assume that functions have been validated before creation)
115 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, Target::NEON);
116 std::unique_ptr<IFunction> func;
117 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100118 if(conv_algorithm == ConvolutionMethod::Direct)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100120 std::tie(func, func_name) = create_named_memory_managed_function<NEDirectConvolutionLayer>(
Georgios Pinitas08346e92018-10-16 19:10:46 +0100121 std::string("DirectConvolutionLayer"), mm, input, weights, biases, output, conv_info, fused_act);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000122 }
123 else if(conv_algorithm == ConvolutionMethod::GEMM)
124 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100125 std::tie(func, func_name) = create_named_memory_managed_function<NEGEMMConvolutionLayer>(
Georgios Pinitas08346e92018-10-16 19:10:46 +0100126 std::string("GEMMConvolutionLayer"), mm, input, weights, biases, output, conv_info, WeightsInfo(), Size2D(1, 1), fused_act);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000127 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100128 else if(conv_algorithm == ConvolutionMethod::Winograd)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000129 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100130 std::tie(func, func_name) = create_named_memory_managed_function<NEWinogradConvolutionLayer>(
Georgios Pinitas08346e92018-10-16 19:10:46 +0100131 std::string("WinogradConvolutionLayer"), mm, input, weights, biases, output, conv_info, fused_act);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000132 }
133 else
134 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100135 std::tie(func, func_name) = create_named_memory_managed_function<NEConvolutionLayer>(
Georgios Pinitas08346e92018-10-16 19:10:46 +0100136 std::string("ConvolutionLayer"), mm, input, weights, biases, output, conv_info, WeightsInfo(), Size2D(1, 1), fused_act);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000137 }
138
139 // Log info
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100140 std::ostringstream qss;
141 if(is_quantized)
142 {
143 qss << " Input QuantInfo: " << input->info()->quantization_info()
144 << " Weights QuantInfo: " << weights->info()->quantization_info()
145 << " Output QuantInfo: " << output->info()->quantization_info();
146 }
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000147 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
148 << node.name()
149 << " Type: " << func_name
150 << " Target: " << NETargetInfo::TargetType
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000151 << " Data Type: " << input->info()->data_type()
Georgios Pinitasfd7e8532018-09-07 10:51:27 +0100152 << qss.str()
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000153 << " Input shape: " << input->info()->tensor_shape()
154 << " Weights shape: " << weights->info()->tensor_shape()
155 << " Output shape: " << output->info()->tensor_shape()
Georgios Pinitas08346e92018-10-16 19:10:46 +0100156 << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "")
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000157 << std::endl);
158 return func;
159}
160
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100161template <>
162std::unique_ptr<IFunction> create_normalization_layer<NENormalizationLayer, NETargetInfo>(NormalizationLayerNode &node, GraphContext &ctx)
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100163{
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100164 validate_node<NETargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100165
166 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100167 NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0));
168 NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000169 const NormalizationLayerInfo norm_info = node.normalization_info();
170 ARM_COMPUTE_ERROR_ON(input == nullptr);
171 ARM_COMPUTE_ERROR_ON(output == nullptr);
172
173 // Create and configure function
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100174 auto func = support::cpp14::make_unique<NENormalizationLayer>(get_memory_manager(ctx, NETargetInfo::TargetType));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000175 func->configure(input, output, norm_info);
176
177 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000178 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
179 << node.name()
180 << " Type: " << node.type()
181 << " Target: " << NETargetInfo::TargetType
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000182 << " Data Type: " << input->info()->data_type()
183 << " Input shape: " << input->info()->tensor_shape()
184 << " Output shape: " << output->info()->tensor_shape()
185 << " Normalization info: " << norm_info.type()
186 << std::endl);
187
188 return std::move(func);
189}
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100190} // namespace detail
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000191
192std::unique_ptr<IFunction> NEFunctionFactory::create(INode *node, GraphContext &ctx)
193{
194 if(node == nullptr)
195 {
196 return nullptr;
197 }
198
199 NodeType type = node->type();
200 switch(type)
201 {
202 case NodeType::ActivationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100203 return detail::create_activation_layer<NEActivationLayer, NETargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000204 case NodeType::BatchNormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100205 return detail::create_batch_normalization_layer<NEBatchNormalizationLayer, NETargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
Georgios Pinitasf1adf112018-11-02 12:54:18 +0000206 case NodeType::ChannelShuffleLayer:
207 return detail::create_channel_shuffle_layer<NEChannelShuffleLayer, NETargetInfo>(*polymorphic_downcast<ChannelShuffleLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000208 case NodeType::ConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100209 return detail::create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100210 case NodeType::DeconvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100211 return detail::create_deconvolution_layer<NEDeconvolutionLayer, NETargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx);
Georgios Pinitase2220552018-07-20 13:23:44 +0100212 case NodeType::ConcatenateLayer:
213 return detail::create_concatenate_layer<NEConcatenateLayer, NETargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000214 case NodeType::DepthwiseConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100215 return detail::create_depthwise_convolution_layer<NEDepthwiseConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000216 case NodeType::DetectionOutputLayer:
217 return detail::create_detection_output_layer<CPPDetectionOutputLayer, NETargetInfo>(*polymorphic_downcast<DetectionOutputLayerNode *>(node));
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000218 case NodeType::DetectionPostProcessLayer:
219 return detail::create_detection_post_process_layer<CPPDetectionPostProcessLayer, NETargetInfo>(*polymorphic_downcast<DetectionPostProcessLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000220 case NodeType::EltwiseLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100221 return detail::create_eltwise_layer<NEEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000222 case NodeType::FlattenLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100223 return detail::create_flatten_layer<NEFlattenLayer, NETargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000224 case NodeType::FullyConnectedLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100225 return detail::create_fully_connected_layer<NEFullyConnectedLayer, NETargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
giuros01acce5042019-02-21 17:32:34 +0000226 case NodeType::FusedConvolutionBatchNormalizationLayer:
227 return detail::create_fused_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node));
Manuel Bottinicf3abde2019-07-29 16:59:41 +0100228 case NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer:
229 return detail::create_fused_depthwise_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000230 case NodeType::NormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100231 return detail::create_normalization_layer<NENormalizationLayer, NETargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
Georgios Pinitas57c48242018-08-02 13:41:49 +0100232 case NodeType::PermuteLayer:
233 return detail::create_permute_layer<NEPermute, NETargetInfo>(*polymorphic_downcast<PermuteLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000234 case NodeType::PoolingLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100235 return detail::create_pooling_layer<NEPoolingLayer, NETargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
Pablo Tello32521432018-11-15 14:43:10 +0000236 case NodeType::PriorBoxLayer:
237 return detail::create_priorbox_layer<NEPriorBoxLayer, NETargetInfo>(*polymorphic_downcast<PriorBoxLayerNode *>(node));
Isabella Gottardi3db1ba92019-05-17 12:35:20 +0100238 case NodeType::QuantizationLayer:
239 return detail::create_quantization_layer<NEQuantizationLayer, NETargetInfo>(*polymorphic_downcast<QuantizationLayerNode *>(node));
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100240 case NodeType::ReorgLayer:
241 return detail::create_reorg_layer<NEReorgLayer, NETargetInfo>(*polymorphic_downcast<ReorgLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000242 case NodeType::ReshapeLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100243 return detail::create_reshape_layer<NEReshapeLayer, NETargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node));
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100244 case NodeType::ResizeLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100245 return detail::create_resize_layer<NEScale, NETargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000246 case NodeType::SoftmaxLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100247 return detail::create_softmax_layer<NESoftmaxLayer, NETargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
Michele Di Giorgioec699752019-03-22 15:25:32 +0000248 case NodeType::StackLayer:
249 return detail::create_stack_layer<NEStackLayer, NETargetInfo>(*polymorphic_downcast<StackLayerNode *>(node));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100250 case NodeType::UpsampleLayer:
251 return detail::create_upsample_layer<NEUpsampleLayer, NETargetInfo>(*polymorphic_downcast<UpsampleLayerNode *>(node), ctx);
Michalis Spyroue22aa132018-09-13 10:35:33 +0100252 case NodeType::YOLOLayer:
253 return detail::create_yolo_layer<NEYOLOLayer, NETargetInfo>(*polymorphic_downcast<YOLOLayerNode *>(node), ctx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000254 default:
255 return nullptr;
256 }
257}
258} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100259} // namespace graph
Pablo Tello32521432018-11-15 14:43:10 +0000260} // namespace arm_compute