blob: 1755e9a774a8e94f9fd4f9346f97101f0b2c7682 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham92046462020-03-07 22:15:55 +00002 * 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"
Matthew Bentham92046462020-03-07 22:15:55 +000030#include "support/MemorySupport.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010031
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <cmath>
33#include <tuple>
Georgios Pinitasd8734b52017-12-22 15:27:52 +000034#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010036namespace arm_compute
37{
Anthony Barbier61b4fca2018-03-29 11:31:55 +010038NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) //NOLINT
39 : _memory_manager(std::move(memory_manager)),
40 _function()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
42}
43
Alex Gilday7da29b62018-03-23 14:16:00 +000044void 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 +010045 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000047 // Perform validate step
48 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010049 ARM_COMPUTE_UNUSED(num_groups);
Giorgio Arenaa3221e62018-05-03 15:57:48 +010050 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,
51 enable_fast_math));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052
Andrew Mundy4d9379a2018-03-15 16:47:03 +000053 switch(NEConvolutionLayer::get_convolution_method(input->info(), weights->info(), output->info(), conv_info, weights_info, dilation, act_info))
Moritz Pflanzer80373f62017-09-15 10:42:58 +010054 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000055 case ConvolutionMethod::WINOGRAD:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 {
Georgios Pinitas9fb11592018-04-26 20:34:58 +010057 auto f = arm_compute::support::cpp14::make_unique<NEWinogradConvolutionLayer>(_memory_manager);
Giorgio Arenaa3221e62018-05-03 15:57:48 +010058 f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000059 _function = std::move(f);
60 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000062 case ConvolutionMethod::GEMM:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000064 auto f = arm_compute::support::cpp14::make_unique<NEGEMMConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000065 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000066 _function = std::move(f);
67 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000069 case ConvolutionMethod::DIRECT:
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010070 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000071 auto f = arm_compute::support::cpp14::make_unique<NEDirectConvolutionLayer>(_memory_manager);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000072 f->configure(input, weights, biases, output, conv_info, act_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000073 _function = std::move(f);
74 break;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010075 }
giuros01154bc1c2019-03-26 17:44:40 +000076 case ConvolutionMethod::FFT:
77 {
78 auto f = arm_compute::support::cpp14::make_unique<NEFFTConvolutionLayer>(_memory_manager);
79 f->configure(input, weights, biases, output, conv_info, act_info);
80 _function = std::move(f);
81 break;
82 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000083 default:
84 ARM_COMPUTE_ERROR("Not supported.");
85 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087}
88
Giorgio Arena7c23ad02017-11-30 15:08:38 +000089Status 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 +010090 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 +000091{
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010092 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1), "Grouping (num_groups != 1) is not supported on NEON");
93
Andrew Mundy4d9379a2018-03-15 16:47:03 +000094 switch(NEConvolutionLayer::get_convolution_method(input, weights, output, conv_info, weights_info, dilation, act_info))
Giorgio Arena7c23ad02017-11-30 15:08:38 +000095 {
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000096 case ConvolutionMethod::WINOGRAD:
97 //Validate Winograd
Isabella Gottardi96b86a92018-05-14 15:52:07 +010098 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 +000099 break;
100 case ConvolutionMethod::GEMM:
101 //Validate Gemm-based Convolution
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100102 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 +0000103 break;
104 case ConvolutionMethod::DIRECT:
105 //Validate Gemm-based Convolution
Isabella Gottardi96b86a92018-05-14 15:52:07 +0100106 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayer::validate(input, weights, biases, output, conv_info, act_info));
Georgios Pinitas49f83492019-06-27 17:08:07 +0100107 break;
giuros01154bc1c2019-03-26 17:44:40 +0000108 case ConvolutionMethod::FFT:
109 // Validate FFT-based convolution layer
110 ARM_COMPUTE_RETURN_ON_ERROR(NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info));
111 break;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000112 default:
113 ARM_COMPUTE_ERROR("Not supported.");
114 break;
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000115 }
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000116
117 return Status{};
118}
119
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000120ConvolutionMethod NEConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights,
121 const ITensorInfo *output, const PadStrideInfo &conv_info,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100122 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000123{
Gian Marco Iodicea8aef292018-05-14 14:21:39 +0100124 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000125 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000126
Giorgio Arenae0837712018-06-12 11:30:50 +0100127 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
128 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
129 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
130
131 /* Input spatial dims, kernel size, IFM/OFM, conv info*/
132 using ConvolutionConfiguration = std::tuple<Size2D, Size2D, Size2D, PadStrideInfo>;
133 using ConfigurationMethod = std::pair<ConvolutionConfiguration, ConvolutionMethod>;
134
135 const std::vector<ConfigurationMethod> known_configs =
136 {
137 // Alexnet
138 ConfigurationMethod(ConvolutionConfiguration(Size2D(27U, 27U), Size2D(5U, 5U), Size2D(48U, 128U), PadStrideInfo(1U, 1U, 2U, 2U)), ConvolutionMethod::GEMM),
139 // VGG16 / VGG19
140 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)), ConvolutionMethod::GEMM),
141 // Mobilenet 224
142 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM),
143 // Mobilenet 160
144 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM)
145 };
146
147 const auto find_config = [&](ConfigurationMethod c)
148 {
149 const ConvolutionConfiguration config = c.first;
150 const PadStrideInfo info = std::get<3>(config);
151
152 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))
153 && 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()
154 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
155 };
156
157 std::vector<ConfigurationMethod>::const_iterator found;
158 if((found = std::find_if(known_configs.begin(), known_configs.end(), find_config)) != known_configs.end())
159 {
160 return (*found).second;
161 }
162
giuros01154bc1c2019-03-26 17:44:40 +0000163 if(dilation != Size2D(1U, 1U))
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000164 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100165 return ConvolutionMethod::GEMM;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000166 }
giuros01154bc1c2019-03-26 17:44:40 +0000167 else
168 {
Michalis Spyrou26dcbc72019-06-04 17:23:04 +0100169 // SRGAN
170 if((input->dimension(idx_h) > 720U) && (output->dimension(idx_h) > 720U) && (weights->dimension(idx_h) == 9)
171 && (NEDirectConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
172 {
173 return ConvolutionMethod::DIRECT;
174 }
giuros01154bc1c2019-03-26 17:44:40 +0000175 if((weights->dimension(idx_h) > 7) && (input->dimension(idx_c) > output->dimension(idx_c)) && (NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
176 {
177 return ConvolutionMethod::FFT;
178 }
179 if(input->dimension(idx_c) < 16)
180 {
181 return ConvolutionMethod::GEMM;
182 }
183 return bool(NEWinogradConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
184 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000185}
186
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187void NEConvolutionLayer::run()
188{
Georgios Pinitas72219332018-06-05 14:56:06 +0100189 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000190 _function->run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191}
Georgios Pinitas72219332018-06-05 14:56:06 +0100192
193void NEConvolutionLayer::prepare()
194{
195 _function->prepare();
196}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100197} // namespace arm_compute