blob: e43d9769447abc06938c5e5084c3ecab66f24bab [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2017-2021 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"
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000030#include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
31#include "arm_compute/runtime/NEON/functions/NEFFTConvolutionLayer.h"
32#include "arm_compute/runtime/NEON/functions/NEGEMMConv2d.h"
33#include "arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h"
34#include "arm_compute/runtime/NEON/functions/NEWinogradConvolutionLayer.h"
35
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <cmath>
37#include <tuple>
Georgios Pinitasd8734b52017-12-22 15:27:52 +000038#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010040namespace arm_compute
41{
Anthony Barbier61b4fca2018-03-29 11:31:55 +010042NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) //NOLINT
43 : _memory_manager(std::move(memory_manager)),
44 _function()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045{
46}
47
Alex Gilday7da29b62018-03-23 14:16:00 +000048void 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 +010049 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000051 // Perform validate step
52 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010053 ARM_COMPUTE_UNUSED(num_groups);
Giorgio Arenaa3221e62018-05-03 15:57:48 +010054 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 +020055 enable_fast_math, num_groups));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000057 const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, num_groups);
Georgios Pinitas09050282020-05-14 10:03:56 +010058 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 +010059 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000060 case ConvolutionMethod::WINOGRAD:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000062 auto f = std::make_unique<NEWinogradConvolutionLayer>(_memory_manager);
Giorgio Arenaa3221e62018-05-03 15:57:48 +010063 f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000064 _function = std::move(f);
65 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000067 case ConvolutionMethod::GEMM:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000069 auto f = std::make_unique<NEGEMMConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000070 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000071 _function = std::move(f);
72 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000074 case ConvolutionMethod::GEMM_CONV2D:
75 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000076 auto f = std::make_unique<NEGEMMConv2d>(_memory_manager);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000077 f->configure(input, weights, biases, output, info);
78 _function = std::move(f);
79 break;
80 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000081 case ConvolutionMethod::DIRECT:
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010082 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000083 auto f = std::make_unique<NEDirectConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000084 f->configure(input, weights, biases, output, conv_info, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000085 _function = std::move(f);
86 break;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010087 }
giuros01154bc1c2019-03-26 17:44:40 +000088 case ConvolutionMethod::FFT:
89 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000090 auto f = std::make_unique<NEFFTConvolutionLayer>(_memory_manager);
giuros01154bc1c2019-03-26 17:44:40 +000091 f->configure(input, weights, biases, output, conv_info, act_info);
92 _function = std::move(f);
93 break;
94 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000095 default:
96 ARM_COMPUTE_ERROR("Not supported.");
97 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099}
100
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000101Status 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 +0100102 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 +0000103{
Sheri Zhangac6499a2021-02-10 15:32:38 +0000104 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1), "Grouping (num_groups != 1) is not supported on Neon");
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100105
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000106 const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, num_groups);
Georgios Pinitas09050282020-05-14 10:03:56 +0100107 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 +0000108 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000109 case ConvolutionMethod::WINOGRAD:
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100110 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 +0000111 break;
112 case ConvolutionMethod::GEMM:
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100113 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 +0000114 break;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000115 case ConvolutionMethod::GEMM_CONV2D:
116 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMConv2d::validate(input, weights, biases, output, info));
117 break;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000118 case ConvolutionMethod::DIRECT:
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100119 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info));
Georgios Pinitas49f83492019-06-27 17:08:07 +0100120 break;
giuros01154bc1c2019-03-26 17:44:40 +0000121 case ConvolutionMethod::FFT:
giuros01154bc1c2019-03-26 17:44:40 +0000122 ARM_COMPUTE_RETURN_ON_ERROR(NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info));
123 break;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000124 default:
125 ARM_COMPUTE_ERROR("Not supported.");
126 break;
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000127 }
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000128
129 return Status{};
130}
131
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000132ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights,
133 const ITensorInfo *output, const PadStrideInfo &conv_info,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100134 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000135{
Gian Marco Iodicea8aef292018-05-14 14:21:39 +0100136 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000137 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000138
Giorgio Arenae0837712018-06-12 11:30:50 +0100139 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
140 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
141 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
142
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000143 const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, 1);
144
Giorgio Arenae0837712018-06-12 11:30:50 +0100145 /* Input spatial dims, kernel size, IFM/OFM, conv info*/
146 using ConvolutionConfiguration = std::tuple<Size2D, Size2D, Size2D, PadStrideInfo>;
147 using ConfigurationMethod = std::pair<ConvolutionConfiguration, ConvolutionMethod>;
148
149 const std::vector<ConfigurationMethod> known_configs =
150 {
151 // Alexnet
152 ConfigurationMethod(ConvolutionConfiguration(Size2D(27U, 27U), Size2D(5U, 5U), Size2D(48U, 128U), PadStrideInfo(1U, 1U, 2U, 2U)), ConvolutionMethod::GEMM),
153 // VGG16 / VGG19
154 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)), ConvolutionMethod::GEMM),
155 // Mobilenet 224
156 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM),
157 // Mobilenet 160
158 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM)
159 };
160
161 const auto find_config = [&](ConfigurationMethod c)
162 {
163 const ConvolutionConfiguration config = c.first;
164 const PadStrideInfo info = std::get<3>(config);
165
166 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))
167 && 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()
168 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
169 };
170
171 std::vector<ConfigurationMethod>::const_iterator found;
172 if((found = std::find_if(known_configs.begin(), known_configs.end(), find_config)) != known_configs.end())
173 {
174 return (*found).second;
175 }
176
giuros01154bc1c2019-03-26 17:44:40 +0000177 if(dilation != Size2D(1U, 1U))
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000178 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100179 return ConvolutionMethod::GEMM;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000180 }
giuros01154bc1c2019-03-26 17:44:40 +0000181 else
182 {
Michalis Spyrou26dcbc72019-06-04 17:23:04 +0100183 // SRGAN
Manuel Bottini6e10aa32020-04-30 13:28:23 +0100184 // Output might not be initialized when it is an internal tensor of the layer using the convolution
185 if(input->total_size() > 1e7 && (weights->dimension(idx_h) > 7)
Michalis Spyrou26dcbc72019-06-04 17:23:04 +0100186 && (NEDirectConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
187 {
188 return ConvolutionMethod::DIRECT;
189 }
giuros01154bc1c2019-03-26 17:44:40 +0000190 if((weights->dimension(idx_h) > 7) && (input->dimension(idx_c) > output->dimension(idx_c)) && (NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
191 {
192 return ConvolutionMethod::FFT;
193 }
194 if(input->dimension(idx_c) < 16)
195 {
196 return ConvolutionMethod::GEMM;
197 }
SiCong Li6b6a16f2020-05-28 08:55:51 +0100198
199#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
200 // This heuristics only applies to F16 data type on A55r1
201 if(NEScheduler::get().cpu_info().get_cpu_model() == CPUModel::A55r1 && enable_fast_math && input->data_type() == DataType::F16)
202 {
203 // Exclude known bad winograd configs (and defaults to GEMM)
204 const std::vector<ConvolutionConfiguration> known_bad_winograd_f16_with_fastmath_configs =
205 {
206 // Squeezenet_V1_1 fire2 and fire3
207 ConvolutionConfiguration(Size2D(56U, 56U), Size2D(3U, 3U), Size2D(16U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)),
208 // Squeezenet_V1_1 fire6 and fire7
209 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(48U, 192U), PadStrideInfo(1U, 1U, 1U, 1U)),
210 // Squeezenet_V1_1 fire8 and fire9
211 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(64U, 256U), PadStrideInfo(1U, 1U, 1U, 1U)),
212 };
213 const auto find_conv_config = [&](ConvolutionConfiguration c)
214 {
215 const PadStrideInfo info = std::get<3>(c);
216
217 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))
218 && 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()
219 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
220 };
221
222 bool found_bad = std::find_if(known_bad_winograd_f16_with_fastmath_configs.begin(), known_bad_winograd_f16_with_fastmath_configs.end(),
223 find_conv_config)
224 != known_bad_winograd_f16_with_fastmath_configs.end();
225 if(found_bad)
226 {
227 return ConvolutionMethod::GEMM;
228 }
229 }
230#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000231 // For 1x1 convolutions run the default GEMM
232 if(weights->dimension(idx_w) == 1 && weights->dimension(idx_h) == 1)
233 {
234 return ConvolutionMethod::GEMM;
235 }
236
237 if(bool(NEWinogradConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)))
238 {
239 return ConvolutionMethod::WINOGRAD;
240 }
241 if(bool(NEGEMMConv2d::validate(input, weights, nullptr, output, info)))
242 {
243 return ConvolutionMethod::GEMM_CONV2D;
244 }
245 return ConvolutionMethod::GEMM;
giuros01154bc1c2019-03-26 17:44:40 +0000246 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000247}
248
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249void NEConvolutionLayer::run()
250{
Georgios Pinitas72219332018-06-05 14:56:06 +0100251 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000252 _function->run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253}
Georgios Pinitas72219332018-06-05 14:56:06 +0100254
255void NEConvolutionLayer::prepare()
256{
257 _function->prepare();
258}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100259} // namespace arm_compute