blob: 3878e0de58ac13939a2b55af63a50f22da3d3261 [file] [log] [blame]
Michalis Spyroub55f8e82021-07-22 11:23:11 +01001/*
2 * Copyright (c) 2017-2021 Arm Limited.
3 *
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 Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuConv2d.h"
Michalis Spyroub55f8e82021-07-22 11:23:11 +010025#include "arm_compute/runtime/NEON/NEScheduler.h"
26#include "arm_compute/runtime/NEON/functions/NEFFTConvolutionLayer.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010027#include "src/cpu/operators/CpuDirectConv2d.h"
28#include "src/cpu/operators/CpuGemm.h"
29#include "src/cpu/operators/CpuGemmConv2d.h"
30#include "src/cpu/operators/CpuGemmDirectConv2d.h"
31#include "src/cpu/operators/CpuWinogradConv2d.h"
Michalis Spyroub55f8e82021-07-22 11:23:11 +010032
33namespace arm_compute
34{
35namespace cpu
36{
37CpuConv2d::CpuConv2d()
38 : _function()
39{
40}
41
42CpuConv2d::~CpuConv2d() = default;
43
44void CpuConv2d::configure(ITensorInfo *input, ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
45 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
46{
47 // Perform validate step
48 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
49 ARM_COMPUTE_UNUSED(num_groups);
50 ARM_COMPUTE_ERROR_THROW_ON(CpuConv2d::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info,
51 enable_fast_math, num_groups));
52
53 const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, num_groups);
54 switch(CpuConv2d::get_convolution_method(input, weights, output, conv_info, weights_info, dilation, act_info, enable_fast_math))
55 {
56 case ConvolutionMethod::WINOGRAD:
57 {
58 auto f = std::make_unique<CpuWinogradConv2d>();
59 f->configure(input, weights, biases, output, conv_info, act_info, enable_fast_math);
60 _function = std::move(f);
61 break;
62 }
63 case ConvolutionMethod::GEMM:
64 {
Georgios Pinitas19884632021-08-16 12:38:54 +010065 auto f = std::make_unique<CpuGemmConv2d>();
Georgios Pinitasdf5bcb62021-08-17 16:30:12 +010066 f->configure(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math);
Michalis Spyroub55f8e82021-07-22 11:23:11 +010067 _function = std::move(f);
68 break;
69 }
70 case ConvolutionMethod::GEMM_CONV2D:
71 {
72 auto f = std::make_unique<CpuGemmDirectConv2d>();
73 f->configure(input, weights, biases, output, info);
74 _function = std::move(f);
75 break;
76 }
77 case ConvolutionMethod::DIRECT:
78 {
79 auto f = std::make_unique<CpuDirectConv2d>();
80 f->configure(input, weights, biases, output, conv_info, act_info);
81 _function = std::move(f);
82 break;
83 }
84 default:
85 ARM_COMPUTE_ERROR("Not supported.");
86 break;
87 }
88
89 _aux_mem = _function->workspace();
90}
91
92Status CpuConv2d::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
93 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
94{
95 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1), "Grouping (num_groups != 1) is not supported on Neon");
96
97 const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, num_groups);
98 switch(CpuConv2d::get_convolution_method(input, weights, output, conv_info, weights_info, dilation, act_info, enable_fast_math))
99 {
100 case ConvolutionMethod::WINOGRAD:
101 ARM_COMPUTE_RETURN_ON_ERROR(CpuWinogradConv2d::validate(input, weights, biases, output, conv_info, act_info, enable_fast_math));
102 break;
103 case ConvolutionMethod::GEMM:
Georgios Pinitas19884632021-08-16 12:38:54 +0100104 ARM_COMPUTE_RETURN_ON_ERROR(CpuGemmConv2d::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math));
Michalis Spyroub55f8e82021-07-22 11:23:11 +0100105 break;
106 case ConvolutionMethod::GEMM_CONV2D:
107 ARM_COMPUTE_RETURN_ON_ERROR(CpuGemmDirectConv2d::validate(input, weights, biases, output, info));
108 break;
109 case ConvolutionMethod::DIRECT:
110 ARM_COMPUTE_RETURN_ON_ERROR(CpuDirectConv2d::validate(input, weights, biases, output, conv_info, act_info));
111 break;
112 default:
113 ARM_COMPUTE_ERROR("Not supported.");
114 break;
115 }
116
117 return Status{};
118}
119
120ConvolutionMethod CpuConv2d::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights,
121 const ITensorInfo *output, const PadStrideInfo &conv_info,
122 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math)
123{
124 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, weights);
125 ARM_COMPUTE_UNUSED(weights_info);
126
127 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 const Conv2dInfo info(conv_info, dilation, act_info, enable_fast_math, 1);
132
133 /* Input spatial dims, kernel size, IFM/OFM, conv info*/
134 using ConvolutionConfiguration = std::tuple<Size2D, Size2D, Size2D, PadStrideInfo>;
135 using ConfigurationMethod = std::pair<ConvolutionConfiguration, ConvolutionMethod>;
136
137 const std::vector<ConfigurationMethod> known_configs =
138 {
139 // Alexnet
140 ConfigurationMethod(ConvolutionConfiguration(Size2D(27U, 27U), Size2D(5U, 5U), Size2D(48U, 128U), PadStrideInfo(1U, 1U, 2U, 2U)), ConvolutionMethod::GEMM),
141 // VGG16 / VGG19
142 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)), ConvolutionMethod::GEMM),
143 // Mobilenet 224
144 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM),
145 // Mobilenet 160
146 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR)), ConvolutionMethod::GEMM)
147 };
148
149 const auto find_config = [&](ConfigurationMethod c)
150 {
151 const ConvolutionConfiguration config = c.first;
152 const PadStrideInfo info = std::get<3>(config);
153
154 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))
155 && 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()
156 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
157 };
158
159 std::vector<ConfigurationMethod>::const_iterator found;
160 if((found = std::find_if(known_configs.begin(), known_configs.end(), find_config)) != known_configs.end())
161 {
162 return (*found).second;
163 }
164
165 if(dilation != Size2D(1U, 1U))
166 {
167 return ConvolutionMethod::GEMM;
168 }
169 else
170 {
171 // SRGAN
172 // Output might not be initialized when it is an internal tensor of the layer using the convolution
173 if(input->total_size() > 1e7 && (weights->dimension(idx_h) > 7)
174 && (CpuDirectConv2d::validate(input, weights, nullptr, output, conv_info, act_info)))
175 {
176 return ConvolutionMethod::DIRECT;
177 }
178 if((weights->dimension(idx_h) > 7) && (input->dimension(idx_c) > output->dimension(idx_c)) && (NEFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info)))
179 {
180 return ConvolutionMethod::FFT;
181 }
182 if(input->dimension(idx_c) < 16)
183 {
184 return ConvolutionMethod::GEMM;
185 }
186
187#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
188 // This heuristics only applies to F16 data type on A55r1
189 if(NEScheduler::get().cpu_info().get_cpu_model() == CPUModel::A55r1 && enable_fast_math && input->data_type() == DataType::F16)
190 {
191 // Exclude known bad winograd configs (and defaults to GEMM)
192 const std::vector<ConvolutionConfiguration> known_bad_winograd_f16_with_fastmath_configs =
193 {
194 // Squeezenet_V1_1 fire2 and fire3
195 ConvolutionConfiguration(Size2D(56U, 56U), Size2D(3U, 3U), Size2D(16U, 64U), PadStrideInfo(1U, 1U, 1U, 1U)),
196 // Squeezenet_V1_1 fire6 and fire7
197 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(48U, 192U), PadStrideInfo(1U, 1U, 1U, 1U)),
198 // Squeezenet_V1_1 fire8 and fire9
199 ConvolutionConfiguration(Size2D(14U, 14U), Size2D(3U, 3U), Size2D(64U, 256U), PadStrideInfo(1U, 1U, 1U, 1U)),
200 };
201 const auto find_conv_config = [&](ConvolutionConfiguration c)
202 {
203 const PadStrideInfo info = std::get<3>(c);
204
205 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))
206 && 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()
207 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride();
208 };
209
210 bool found_bad = std::find_if(known_bad_winograd_f16_with_fastmath_configs.begin(), known_bad_winograd_f16_with_fastmath_configs.end(),
211 find_conv_config)
212 != known_bad_winograd_f16_with_fastmath_configs.end();
213 if(found_bad)
214 {
215 return ConvolutionMethod::GEMM;
216 }
217 }
218#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
219 // For 1x1 convolutions run the default GEMM
220 if(weights->dimension(idx_w) == 1 && weights->dimension(idx_h) == 1)
221 {
222 return ConvolutionMethod::GEMM;
223 }
224
225 if(bool(CpuWinogradConv2d::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math)))
226 {
227 return ConvolutionMethod::WINOGRAD;
228 }
229 if(bool(CpuGemmDirectConv2d::validate(input, weights, nullptr, output, info)))
230 {
231 return ConvolutionMethod::GEMM_CONV2D;
232 }
233 return ConvolutionMethod::GEMM;
234 }
235}
236
237void CpuConv2d::run(ITensorPack &tensors)
238{
239 prepare(tensors);
240 _function->run(tensors);
241}
242
243void CpuConv2d::prepare(ITensorPack &tensors)
244{
245 _function->prepare(tensors);
246}
247
248experimental::MemoryRequirements CpuConv2d::workspace() const
249{
250 return _aux_mem;
251}
252} // namespace cpu
253} // namespace arm_compute