blob: 901b1e880efec02990c1be8a8fd7f1914d7abd00 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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#include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010030#include "src/core/NEON/kernels/NECol2ImKernel.h"
31#include "src/core/NEON/kernels/NEConvertQuantizedSignednessKernel.h"
32#include "src/core/NEON/kernels/NECopyKernel.h"
33#include "src/core/NEON/kernels/NEDirectConvolutionLayerKernel.h"
34#include "src/core/NEON/kernels/NEDirectConvolutionLayerOutputStageKernel.h"
35#include "src/core/NEON/kernels/NEFFTDigitReverseKernel.h"
36#include "src/core/NEON/kernels/NEFFTRadixStageKernel.h"
37#include "src/core/NEON/kernels/NEFFTScaleKernel.h"
38#include "src/core/NEON/kernels/NEFillBorderKernel.h"
39#include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
40#include "src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
41#include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h"
42#include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionOutputStageKernel.h"
43#include "src/core/NEON/kernels/NEGEMMLowpReductionKernel.h"
44#include "src/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h"
45#include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
46#include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
47#include "src/core/NEON/kernels/NEIm2ColKernel.h"
48#include "src/core/NEON/kernels/NEPadLayerKernel.h"
49#include "src/core/NEON/kernels/NEReductionOperationKernel.h"
50#include "src/core/NEON/kernels/NEWeightsReshapeKernel.h"
Matthew Bentham92046462020-03-07 22:15:55 +000051#include "support/MemorySupport.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010052
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053#include <cmath>
54#include <tuple>
Georgios Pinitasd8734b52017-12-22 15:27:52 +000055#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010057namespace arm_compute
58{
Anthony Barbier61b4fca2018-03-29 11:31:55 +010059NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) //NOLINT
60 : _memory_manager(std::move(memory_manager)),
61 _function()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062{
63}
64
Alex Gilday7da29b62018-03-23 14:16:00 +000065void NEConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010066 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000068 // Perform validate step
69 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010070 ARM_COMPUTE_UNUSED(num_groups);
Giorgio Arenaa3221e62018-05-03 15:57:48 +010071 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info, dilation, act_info,
Alexander Jung187558f2020-08-05 16:26:41 +020072 enable_fast_math, num_groups));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
Georgios Pinitas09050282020-05-14 10:03:56 +010074 switch(NEConvolutionLayer::get_convolution_method(input->info(), weights->info(), output->info(), conv_info, weights_info, dilation, act_info, enable_fast_math))
Moritz Pflanzer80373f62017-09-15 10:42:58 +010075 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000076 case ConvolutionMethod::WINOGRAD:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 {
Georgios Pinitas9fb11592018-04-26 20:34:58 +010078 auto f = arm_compute::support::cpp14::make_unique<NEWinogradConvolutionLayer>(_memory_manager);
Giorgio Arenaa3221e62018-05-03 15:57:48 +010079 f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000080 _function = std::move(f);
81 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000083 case ConvolutionMethod::GEMM:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000085 auto f = arm_compute::support::cpp14::make_unique<NEGEMMConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000086 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000087 _function = std::move(f);
88 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000090 case ConvolutionMethod::DIRECT:
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010091 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000092 auto f = arm_compute::support::cpp14::make_unique<NEDirectConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000093 f->configure(input, weights, biases, output, conv_info, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000094 _function = std::move(f);
95 break;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010096 }
giuros01154bc1c2019-03-26 17:44:40 +000097 case ConvolutionMethod::FFT:
98 {
99 auto f = arm_compute::support::cpp14::make_unique<NEFFTConvolutionLayer>(_memory_manager);
100 f->configure(input, weights, biases, output, conv_info, act_info);
101 _function = std::move(f);
102 break;
103 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000104 default:
105 ARM_COMPUTE_ERROR("Not supported.");
106 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108}
109
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000110Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100111 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000112{
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100113 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1), "Grouping (num_groups != 1) is not supported on NEON");
114
Georgios Pinitas09050282020-05-14 10:03:56 +0100115 switch(NEConvolutionLayer::get_convolution_method(input, weights, output, conv_info, weights_info, dilation, act_info, enable_fast_math))
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000116 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000117 case ConvolutionMethod::WINOGRAD:
118 //Validate Winograd
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100119 ARM_COMPUTE_RETURN_ON_ERROR(NEWinogradConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000120 break;
121 case ConvolutionMethod::GEMM:
122 //Validate Gemm-based Convolution
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100123 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMConvolutionLayer::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000124 break;
125 case ConvolutionMethod::DIRECT:
Manuel Bottini6e10aa32020-04-30 13:28:23 +0100126 //Validate Direct Convolution
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100127 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info));
Georgios Pinitas49f83492019-06-27 17:08:07 +0100128 break;
giuros01154bc1c2019-03-26 17:44:40 +0000129 case ConvolutionMethod::FFT:
130 // Validate FFT-based convolution layer
131 ARM_COMPUTE_RETURN_ON_ERROR(NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info));
132 break;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000133 default:
134 ARM_COMPUTE_ERROR("Not supported.");
135 break;
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000136 }
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000137
138 return Status{};
139}
140
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000141ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights,
142 const ITensorInfo *output, const PadStrideInfo &conv_info,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100143 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000144{
Gian Marco Iodicea8aef292018-05-14 14:21:39 +0100145 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000146 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000147
Giorgio Arenae0837712018-06-12 11:30:50 +0100148 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
149 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
150 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
151
152 /* Input spatial dims, kernel size, IFM/OFM, conv info*/
153 using ConvolutionConfiguration = std::tuple<Size2D, Size2D, Size2D, PadStrideInfo>;
154 using ConfigurationMethod = std::pair<ConvolutionConfiguration, ConvolutionMethod>;
155
156 const std::vector<ConfigurationMethod> known_configs =
157 {
158 // Alexnet
159 ConfigurationMethod(ConvolutionConfiguration(Size2D(27U, 27U), Size2D(5U, 5U), Size2D(48U, 128U), PadStrideInfo(1U, 1U, 2U, 2U)), ConvolutionMethod::GEMM),
160 // VGG16 / VGG19
161 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)), ConvolutionMethod::GEMM),
162 // Mobilenet 224
163 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM),
164 // Mobilenet 160
165 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM)
166 };
167
168 const auto find_config = [&](ConfigurationMethod c)
169 {
170 const ConvolutionConfiguration config = c.first;
171 const PadStrideInfo info = std::get<3>(config);
172
173 return std::get<0>(config) == Size2D(input->dimension(idx_w), input->dimension(idx_h)) && std::get<1>(config) == Size2D(weights->dimension(idx_w), weights->dimension(idx_h))
174 && std::get<2>(config) == Size2D(weights->dimension(idx_c), weights->dimension(3)) && info.pad_top() == conv_info.pad_top() && info.pad_right() == conv_info.pad_right()
175 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
176 };
177
178 std::vector<ConfigurationMethod>::const_iterator found;
179 if((found = std::find_if(known_configs.begin(), known_configs.end(), find_config)) != known_configs.end())
180 {
181 return (*found).second;
182 }
183
giuros01154bc1c2019-03-26 17:44:40 +0000184 if(dilation != Size2D(1U, 1U))
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000185 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100186 return ConvolutionMethod::GEMM;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000187 }
giuros01154bc1c2019-03-26 17:44:40 +0000188 else
189 {
Michalis Spyrou26dcbc72019-06-04 17:23:04 +0100190 // SRGAN
Manuel Bottini6e10aa32020-04-30 13:28:23 +0100191 // Output might not be initialized when it is an internal tensor of the layer using the convolution
192 if(input->total_size() > 1e7 && (weights->dimension(idx_h) > 7)
Michalis Spyrou26dcbc72019-06-04 17:23:04 +0100193 && (NEDirectConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
194 {
195 return ConvolutionMethod::DIRECT;
196 }
giuros01154bc1c2019-03-26 17:44:40 +0000197 if((weights->dimension(idx_h) > 7) && (input->dimension(idx_c) > output->dimension(idx_c)) && (NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
198 {
199 return ConvolutionMethod::FFT;
200 }
201 if(input->dimension(idx_c) < 16)
202 {
203 return ConvolutionMethod::GEMM;
204 }
SiCong Li6b6a16f2020-05-28 08:55:51 +0100205
206#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
207 // This heuristics only applies to F16 data type on A55r1
208 if(NEScheduler::get().cpu_info().get_cpu_model() == CPUModel::A55r1 && enable_fast_math && input->data_type() == DataType::F16)
209 {
210 // Exclude known bad winograd configs (and defaults to GEMM)
211 const std::vector<ConvolutionConfiguration> known_bad_winograd_f16_with_fastmath_configs =
212 {
213 // Squeezenet_V1_1 fire2 and fire3
214 ConvolutionConfiguration(Size2D(56U, 56U), Size2D(3U, 3U), Size2D(16U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)),
215 // Squeezenet_V1_1 fire6 and fire7
216 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(48U, 192U), PadStrideInfo(1U, 1U, 1U, 1U)),
217 // Squeezenet_V1_1 fire8 and fire9
218 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(64U, 256U), PadStrideInfo(1U, 1U, 1U, 1U)),
219 };
220 const auto find_conv_config = [&](ConvolutionConfiguration c)
221 {
222 const PadStrideInfo info = std::get<3>(c);
223
224 return std::get<0>(c) == Size2D(input->dimension(idx_w), input->dimension(idx_h)) && std::get<1>(c) == Size2D(weights->dimension(idx_w), weights->dimension(idx_h))
225 && std::get<2>(c) == Size2D(weights->dimension(idx_c), weights->dimension(3)) && info.pad_top() == conv_info.pad_top() && info.pad_right() == conv_info.pad_right()
226 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
227 };
228
229 bool found_bad = std::find_if(known_bad_winograd_f16_with_fastmath_configs.begin(), known_bad_winograd_f16_with_fastmath_configs.end(),
230 find_conv_config)
231 != known_bad_winograd_f16_with_fastmath_configs.end();
232 if(found_bad)
233 {
234 return ConvolutionMethod::GEMM;
235 }
236 }
237#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
giuros01154bc1c2019-03-26 17:44:40 +0000238 return bool(NEWinogradConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
239 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000240}
241
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100242void NEConvolutionLayer::run()
243{
Georgios Pinitas72219332018-06-05 14:56:06 +0100244 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000245 _function->run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246}
Georgios Pinitas72219332018-06-05 14:56:06 +0100247
248void NEConvolutionLayer::prepare()
249{
250 _function->prepare();
251}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100252} // namespace arm_compute