blob: eb9475ccaa87deba4ff79c02b89aed340d562949 [file] [log] [blame]
Sheri Zhang06d1efd2021-07-28 11:20:04 +01001/*
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01002 * Copyright (c) 2021-2023 Arm Limited.
Sheri Zhang06d1efd2021-07-28 11:20:04 +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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/operators/ClConv2d.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010025
Sheri Zhang06d1efd2021-07-28 11:20:04 +010026#include "arm_compute/core/Validate.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010028#include "arm_compute/runtime/CL/CLScheduler.h"
29#include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010030#include "src/gpu/cl/operators/ClDirectConv2d.h"
31#include "src/gpu/cl/operators/ClGemmConv2d.h"
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +000032#include "src/gpu/cl/operators/ClIndirectConv2d.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010033#include "src/gpu/cl/operators/ClWinogradConv2d.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010034
ramelg012e53f172021-09-22 10:48:25 +010035#include "src/common/utils/Log.h"
36
Sheri Zhang06d1efd2021-07-28 11:20:04 +010037#include <memory>
38
Gian Marco Iodiceebbd5292021-08-17 16:25:37 +010039namespace
40{
41/** Get the suitable kernel size for using direct convolution method with NHWC data layout.
42 *
43 * @note Direct convolution should be executed when the kernel has the spatial dimensions greater than or equal to the value returned by this function
44 *
45 * @param[in] gpu_target GPU target
46 *
47 * @return the suitable kernel size for using direct convolution method with NHWC data layout
48 */
49size_t get_direct_conv_kernel_threshold_nhwc(arm_compute::GPUTarget gpu_target)
50{
51 switch(gpu_target)
52 {
53 case arm_compute::GPUTarget::G76:
54 case arm_compute::GPUTarget::G77:
55 case arm_compute::GPUTarget::G78:
56 return 5;
57 case arm_compute::GPUTarget::G71:
58 case arm_compute::GPUTarget::G72:
59 case arm_compute::GPUTarget::MIDGARD:
60 case arm_compute::GPUTarget::BIFROST:
61 return 7;
62 default:
63 return 5;
64 }
65}
66} // namespace
67
Sheri Zhang06d1efd2021-07-28 11:20:04 +010068namespace arm_compute
69{
70namespace opencl
71{
72using namespace arm_compute::misc::shape_calculator;
73
74ClConv2d::ClConv2d()
75 : _operator()
76{
77}
78
79ClConv2d::~ClConv2d() = default;
80
81void ClConv2d::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, const Conv2dInfo &conv2d_info,
82 const WeightsInfo &weights_info)
83{
84 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
85 ARM_COMPUTE_ERROR_THROW_ON(ClConv2d::validate(src, weights, ((biases != nullptr) ? biases : nullptr), dst, conv2d_info, weights_info));
ramelg012e53f172021-09-22 10:48:25 +010086 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, conv2d_info, weights_info);
Sheri Zhang06d1efd2021-07-28 11:20:04 +010087
88 switch(ClConv2d::get_convolution_method(src, weights, dst, conv2d_info, weights_info, CLScheduler::get().target()))
89 {
90 case ConvolutionMethod::WINOGRAD:
91 {
92 ARM_COMPUTE_ERROR_ON(conv2d_info.num_groups != 1);
93 auto f = std::make_unique<ClWinogradConv2d>();
94 f->configure(compile_context, src, weights, biases, dst, conv2d_info.conv_info, conv2d_info.act_info, conv2d_info.enable_fast_math);
95 _operator = std::move(f);
96 break;
97 }
98 case ConvolutionMethod::DIRECT:
99 {
100 ARM_COMPUTE_ERROR_ON(conv2d_info.num_groups != 1);
101 auto f = std::make_unique<ClDirectConv2d>();
102 f->configure(compile_context, src, weights, biases, dst, conv2d_info.conv_info, conv2d_info.act_info);
103 _operator = std::move(f);
104 break;
105 }
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000106 case ConvolutionMethod::INDIRECT:
107 {
108 ARM_COMPUTE_ERROR_ON(conv2d_info.num_groups != 1);
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000109 auto f = std::make_unique<ClIndirectConv2d>();
110 f->configure(compile_context, src, weights, biases, dst, conv2d_info.conv_info, conv2d_info.act_info);
111 _operator = std::move(f);
112 break;
113 }
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100114 case ConvolutionMethod::GEMM:
115 {
Georgios Pinitas19884632021-08-16 12:38:54 +0100116 auto f = std::make_unique<ClGemmConv2d>();
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100117 f->configure(compile_context, src, weights, biases, dst, conv2d_info, weights_info);
118 _operator = std::move(f);
119 break;
120 }
121 default:
122 ARM_COMPUTE_ERROR("Not supported.");
123 break;
124 }
125 _aux_mem = _operator->workspace();
126}
127
128Status ClConv2d::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const Conv2dInfo &conv2d_info,
129 const WeightsInfo &weights_info)
130{
131 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
132 ARM_COMPUTE_RETURN_ERROR_ON_MSG((conv2d_info.num_groups != 1) && (src->data_layout() != DataLayout::NCHW), "Grouping (num_groups != 1) with NHWC data layout is not supported");
133
134 const GPUTarget gpu_target = CLScheduler::get().target();
135
136 switch(ClConv2d::get_convolution_method(src, weights, dst, conv2d_info, weights_info, gpu_target))
137 {
138 case ConvolutionMethod::WINOGRAD:
139 {
140 //Validate Winograd
141 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv2d_info.num_groups != 1, "Grouping (num_groups != 1) with ClWinogradConv2d is not supported");
142 ARM_COMPUTE_RETURN_ON_ERROR(ClWinogradConv2d::validate(src, weights, biases, dst, conv2d_info.conv_info, conv2d_info.act_info, conv2d_info.enable_fast_math));
143 break;
144 }
145 case ConvolutionMethod::DIRECT:
146 {
147 // Validate direct convolution layer
148 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv2d_info.num_groups != 1, "Grouping (num_groups != 1) with ClDirectConv2d is not supported");
149 ARM_COMPUTE_RETURN_ON_ERROR(ClDirectConv2d::validate(src, weights, biases, dst, conv2d_info.conv_info, conv2d_info.act_info));
150 break;
151 }
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000152 case ConvolutionMethod::INDIRECT:
153 {
154 // Validate indirect convolution layer
155 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv2d_info.num_groups != 1, "Grouping (num_groups != 1) with ClIndirectConv2d is not supported");
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000156 ARM_COMPUTE_RETURN_ON_ERROR(ClIndirectConv2d::validate(src, weights, biases, dst, conv2d_info.conv_info, conv2d_info.act_info));
157 break;
158 }
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100159 case ConvolutionMethod::GEMM:
160 {
161 // Validate gemm-based convolution layer
Georgios Pinitas19884632021-08-16 12:38:54 +0100162 ARM_COMPUTE_RETURN_ON_ERROR(ClGemmConv2d::validate(src, weights, biases, dst, conv2d_info, weights_info));
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100163 break;
164 }
165 default:
166 ARM_COMPUTE_ERROR("Not supported.");
167 break;
168 }
169
170 return Status{};
171}
172
173ConvolutionMethod ClConv2d::get_convolution_method(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const Conv2dInfo &conv2d_info,
174 const WeightsInfo &weights_info, const GPUTarget gpu_target)
175{
176 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
177 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
178 ARM_COMPUTE_ERROR_ON_NULLPTR(weights);
179 ARM_COMPUTE_UNUSED(weights_info);
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100180
181 const PadStrideInfo conv_info = conv2d_info.conv_info;
182 const ActivationLayerInfo act_info = conv2d_info.act_info;
183 const Size2D dilation = conv2d_info.dilation;
184 bool enable_fast_math = conv2d_info.enable_fast_math;
185
186 const size_t idx_w = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
187 const size_t idx_h = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
188 const size_t idx_c = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::CHANNEL);
189
190 /* Input spatial dims, kernel size, IFM/OFM, conv info*/
191 using ConvolutionConfiguration = std::tuple<Size2D, Size2D, Size2D, PadStrideInfo, DataLayout>;
192 using ConfigurationMethod = std::pair<ConvolutionConfiguration, ConvolutionMethod>;
193
194 const std::vector<ConfigurationMethod> known_configs =
195 {
196 // Alexnet
197 ConfigurationMethod(ConvolutionConfiguration(Size2D(27U, 27U), Size2D(5U, 5U), Size2D(48U, 128U), PadStrideInfo(1U, 1U, 2U, 2U), DataLayout::NCHW), ConvolutionMethod::DIRECT),
198 // VGG16 / VGG19
199 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 64U), PadStrideInfo(1U, 1U, 1U, 1U), DataLayout::NCHW), ConvolutionMethod::DIRECT),
200 // Mobilenet 224
201 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NCHW), ConvolutionMethod::GEMM),
202 // Mobilenet 160
203 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NCHW), ConvolutionMethod::GEMM),
204 // Mobilenet 224
205 ConfigurationMethod(ConvolutionConfiguration(Size2D(224U, 224U), Size2D(3U, 3U), Size2D(3U, 32U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NHWC), ConvolutionMethod::GEMM),
206 // Mobilenet 160
207 ConfigurationMethod(ConvolutionConfiguration(Size2D(160U, 160U), Size2D(3U, 3U), Size2D(3U, 24U), PadStrideInfo(2U, 2U, 0U, 1U, 0U, 1U, DimensionRoundingType::FLOOR), DataLayout::NHWC), ConvolutionMethod::GEMM),
208 };
209
210 const auto find_config = [&](ConfigurationMethod c)
211 {
212 const ConvolutionConfiguration config = c.first;
213 const PadStrideInfo info = std::get<3>(config);
214 const DataLayout data_layout = std::get<4>(config);
215
216 return std::get<0>(config) == Size2D(src->dimension(idx_w), src->dimension(idx_h)) && std::get<1>(config) == Size2D(weights->dimension(idx_w), weights->dimension(idx_h))
217 && 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()
218 && info.pad_bottom() == conv_info.pad_bottom() && info.pad_left() == conv_info.pad_left() && info.stride() == conv_info.stride() && (data_layout == src->data_layout());
219 };
220
221 std::vector<ConfigurationMethod>::const_iterator found;
222 if((found = std::find_if(known_configs.begin(), known_configs.end(), find_config)) != known_configs.end())
223 {
224 return (*found).second;
225 }
226
227 if(dilation != Size2D(1U, 1U))
228 {
229 return ConvolutionMethod::GEMM;
230 }
231 else
232 {
233 if(src->data_layout() == DataLayout::NCHW)
234 {
235 // SRGAN
236 if((src->dimension(idx_h) > 720U) && (dst->dimension(idx_h) > 720U) && (weights->dimension(idx_h) == 9) && (conv_info.pad_top() < 3)
237 && (ClDirectConv2d::validate(src, weights, nullptr, dst, conv_info, act_info)))
238 {
239 return ConvolutionMethod::DIRECT;
240 }
241 if((weights->dimension(idx_h) > 5) && (src->dimension(idx_c) > dst->dimension(idx_c)) && (CLFFTConvolutionLayer::validate(src, weights, nullptr, dst, conv_info, act_info, enable_fast_math)))
242 {
243 return ConvolutionMethod::FFT;
244 }
245 if(src->dimension(idx_c) < 16)
246 {
247 return ConvolutionMethod::GEMM;
248 }
249 return bool(ClWinogradConv2d::validate(src, weights, nullptr, dst, conv_info, act_info, enable_fast_math)) ? ConvolutionMethod::WINOGRAD : ConvolutionMethod::GEMM;
250 }
251 else
252 {
Gian Marco Iodiceebbd5292021-08-17 16:25:37 +0100253 const bool is_direct_valid = bool(ClDirectConv2d::validate(src, weights, nullptr, dst, conv_info, act_info));
254 const bool is_wino_valid = bool(ClWinogradConv2d::validate(src, weights, nullptr, dst, conv_info, act_info, enable_fast_math));
255 const size_t kernel_sz_direct_conv_thr = get_direct_conv_kernel_threshold_nhwc(gpu_target);
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100256
257 // SRGAN case
258 if((src->dimension(idx_h) > 720U) && (dst->dimension(idx_h) > 720U) && (weights->dimension(idx_h) == 9) && (conv_info.pad_top() < 3)
259 && is_direct_valid)
260 {
261 return ConvolutionMethod::DIRECT;
262 }
263
264 // Floating-point case: GeMM/Direct/Winograd
265 if(is_data_type_float(src->data_type()))
266 {
Gian Marco Iodice0bae3ee2022-01-20 16:33:29 +0000267 // Get dst shape
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100268 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info);
269 const bool is_large_kernel_sz = (weights->dimension(idx_w) >= kernel_sz_direct_conv_thr) && (weights->dimension(idx_h) >= kernel_sz_direct_conv_thr);
270 const bool is_ifm_ge_8 = src->dimension(idx_c) >= 8;
271 const bool is_ifm_ge_16 = src->dimension(idx_c) >= 16;
272 const bool is_ofm_lte_8 = weights->dimension(3U) <= 8;
273 const bool is_ofm_lt_64 = weights->dimension(3U) < 64;
274 const bool workload_gte_8192 = (output_shape[0] * output_shape[1] * output_shape[2]) / 16 >= 8192;
275 const bool is_ifm_gt_ofm = src->dimension(idx_c) > weights->dimension(3U);
276 const bool is_m_one = output_shape[1] * output_shape[2] == 1;
277 const bool is_unit_stride = (conv2d_info.conv_info.stride().first == 1) && (conv2d_info.conv_info.stride().second == 1);
278 const int32_t kernel_sz = weights->dimension(idx_w) * weights->dimension(idx_h);
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100279
Gian Marco Iodice12571312022-08-25 12:25:44 +0100280 // Run Winograd if valid and IFM >= 8
281 if(is_wino_valid && is_ifm_ge_8)
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100282 {
Adnan AlSinane8712072022-07-21 16:34:49 +0100283 if(is_ofm_lte_8)
284 {
285 if(gpu_target == arm_compute::GPUTarget::G71 || gpu_target == arm_compute::GPUTarget::G72 || get_arch_from_target(gpu_target) == arm_compute::GPUTarget::MIDGARD)
286 {
287 return ConvolutionMethod::WINOGRAD;
288 }
289 }
290 else
291 {
292 return ConvolutionMethod::WINOGRAD;
293 }
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100294 }
Gian Marco Iodice78baa482021-12-01 09:26:14 +0000295
Gian Marco Iodice0bae3ee2022-01-20 16:33:29 +0000296 // Direct convolution case
Adnan AlSinancec1af52022-08-10 18:14:33 +0100297 if(is_direct_valid)
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100298 {
Adnan AlSinane8712072022-07-21 16:34:49 +0100299 if((gpu_target == arm_compute::GPUTarget::G71 || gpu_target == arm_compute::GPUTarget::G72 || get_arch_from_target(gpu_target) == arm_compute::GPUTarget::MIDGARD))
Gian Marco Iodice0bae3ee2022-01-20 16:33:29 +0000300 {
Gian Marco Iodice51d71192022-02-16 14:41:28 +0000301 if(is_large_kernel_sz && is_ifm_ge_16 && is_ifm_gt_ofm)
302 {
303 return ConvolutionMethod::DIRECT;
304 }
305 }
Adnan AlSinane8712072022-07-21 16:34:49 +0100306 else if(gpu_target == arm_compute::GPUTarget::G76)
Gian Marco Iodice51d71192022-02-16 14:41:28 +0000307 {
Adnan AlSinancec1af52022-08-10 18:14:33 +0100308 if((is_large_kernel_sz && workload_gte_8192 && is_ifm_ge_16) || (is_ofm_lte_8 && is_ifm_ge_16))
Gian Marco Iodice51d71192022-02-16 14:41:28 +0000309 {
310 return ConvolutionMethod::DIRECT;
311 }
Gian Marco Iodice0bae3ee2022-01-20 16:33:29 +0000312 }
Adnan AlSinane8712072022-07-21 16:34:49 +0100313 else
314 {
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000315 ConvolutionMethod preferred_conv_method = ConvolutionMethod::DIRECT;
316
317 const bool is_indirect_valid = bool(ClIndirectConv2d::validate(src, weights, nullptr, dst, conv_info, act_info));
318
319 // indirect conv2d should be called when:
320 // 1- When the kernel size is greater than 1x1 and less than or equal to 9x9 (81)
321 // 2- When the kernel size is odd
322 // 3- When the Gpu target is Arm Mali-G77
323 if(is_indirect_valid)
324 {
325 const bool is_kernel_sz_odd = kernel_sz % 2;
326 const bool is_g77 = gpu_target == GPUTarget::G77;
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100327 preferred_conv_method = (kernel_sz > 1) && (kernel_sz <= 81) && is_kernel_sz_odd && is_g77 ? ConvolutionMethod::INDIRECT : ConvolutionMethod::DIRECT;
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000328 }
329
330 // Direct/indirect convolution used for the first layer of the network
Gian Marco Iodice4478e1c2022-09-06 15:06:40 +0100331 if(workload_gte_8192 && !is_ifm_ge_16 && !is_unit_stride && is_ofm_lt_64)
332 {
333 // In general, the question we should ask for the first convolution layer of a model is:
334 // when the execution time of im2col + gemm < direct?. Since im2col does not depend on the OFM, it means that
335 // when OFM is big enough, the contribution of im2col is small and the GEMM approach is preferable.
336 // From internal experiments, the OFM threshold is 64 (is_ofm_lt_64)
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000337 return preferred_conv_method;
Gian Marco Iodice4478e1c2022-09-06 15:06:40 +0100338 }
339
340 if((is_large_kernel_sz || is_m_one) && workload_gte_8192 && is_ifm_ge_16)
341 {
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000342 return preferred_conv_method;
Gian Marco Iodice4478e1c2022-09-06 15:06:40 +0100343 }
344
345 // Direct convolution used for the last layer of the network
346 if(is_ofm_lte_8)
Adnan AlSinane8712072022-07-21 16:34:49 +0100347 {
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +0000348 return preferred_conv_method;
Adnan AlSinane8712072022-07-21 16:34:49 +0100349 }
350 }
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100351 }
352
353 // Default case
354 return ConvolutionMethod::GEMM;
355 }
356
357 // Generic case for quantized. Only GeMM
358 return ConvolutionMethod::GEMM;
359 }
360 }
361}
362
363void ClConv2d::run(ITensorPack &tensors)
364{
365 prepare(tensors);
366 _operator->run(tensors);
367}
368
369void ClConv2d::prepare(ITensorPack &tensors)
370{
371 _operator->prepare(tensors);
372}
373
374experimental::MemoryRequirements ClConv2d::workspace() const
375{
376 return _aux_mem;
377}
378} // namespace opencl
379} // namespace arm_compute