blob: 41ab4d6922b4dbfc7a5871750ae518e41679c718 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Adnan AlSinan227db8d2023-02-14 14:24:09 +00002 * Copyright (c) 2017-2021, 2023 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClPool2dKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/TensorInfo.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000028#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Michalis Spyroue74b2012018-04-18 09:49:16 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Michele Di Giorgioe1314662021-02-01 17:09:32 +000035#include "support/Cast.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000037namespace arm_compute
38{
Michele Di Giorgioe1314662021-02-01 17:09:32 +000039namespace opencl
40{
41namespace kernels
42{
Michalis Spyroue74b2012018-04-18 09:49:16 +010043using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000045namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047Status validate_arguments(const ITensorInfo *src,
48 const ITensorInfo *dst,
49 const PoolingLayerInfo &pool_info,
50 const ITensorInfo *indices)
Georgios Pinitas3faea252017-10-30 14:13:50 +000051{
Michele Di Giorgioe1314662021-02-01 17:09:32 +000052 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
53 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
55 DataType::F16, DataType::F32);
56 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
57 (is_data_type_quantized_asymmetric(src->data_type()) && pool_info.pool_type == PoolingType::L2),
58 "Unsupported combination of parameters!");
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010059
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
61 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
62 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
63 const bool is_global_pooling = pool_info.is_global_pooling;
64 unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
65 unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
66 int output_width = 0;
67 int output_height = 0;
SiCongLic4270cf2021-12-22 11:22:40 +000068
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_region_entirely_outside_input(pool_info),
70 "Pooling region that is entirely outside input tensor is unsupported");
SiCongLic4270cf2021-12-22 11:22:40 +000071
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 std::tie(output_width, output_height) =
73 scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height], pool_size_x,
74 pool_size_y, pool_info.pad_stride_info);
75 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1),
76 "Calculated output dimension size is invalid");
Freddie Liardetafcbb8f2021-05-04 12:41:16 +010077
Sheri Zhang801bbcb2020-08-03 20:11:56 +010078 // Check indices
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 if (indices)
Sheri Zhang801bbcb2020-08-03 20:11:56 +010080 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +000081 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_info.pool_type != PoolingType::MAX,
83 "Pooling indices only supported for MAX pooling method");
84 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_info.pool_size != Size2D(2, 2)),
85 "Pooling indices only supported for pool size 2x2");
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010086
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 if (indices->total_size() != 0)
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010088 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +000089 TensorInfo idx_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, DataType::U32));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010090 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &idx_info);
91 }
Sheri Zhang801bbcb2020-08-03 20:11:56 +010092 }
Georgios Pinitas3faea252017-10-30 14:13:50 +000093
Michele Di Giorgioe1314662021-02-01 17:09:32 +000094 // Checks performed when dst is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 if (dst->total_size() != 0)
Georgios Pinitas3faea252017-10-30 14:13:50 +000096 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +000097 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
98 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
99 TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type()));
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
Georgios Pinitas3faea252017-10-30 14:13:50 +0000101 }
102
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000103 return Status{};
Georgios Pinitas3faea252017-10-30 14:13:50 +0000104}
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000105} // namespace
106
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100107ClPool2dKernel::ClPool2dKernel()
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000108{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100109 _type = CLKernelType::POOL;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000110}
111
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112void ClPool2dKernel::configure(const ClCompileContext &compile_context,
113 ITensorInfo *src,
114 ITensorInfo *dst,
115 const PoolingLayerInfo &pool_info,
116 ITensorInfo *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000117{
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000118 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Giorgio Arena8fce4962021-09-01 14:05:00 +0100119 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100120
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 auto padding_info = get_padding_info({src, dst, indices});
SiCong Li04a07062020-11-17 14:09:01 +0000122
Giorgio Arena8fce4962021-09-01 14:05:00 +0100123 // Auto init if empty
124 TensorShape out_shape = compute_pool_shape(*src, pool_info);
125 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 if (indices)
Giorgio Arena8fce4962021-09-01 14:05:00 +0100127 {
128 auto_init_if_empty(*indices, src->clone()->set_tensor_shape(out_shape).set_data_type(DataType::U32));
129 }
130
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000131 // Set instance variables
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 _pool_info = pool_info;
133 _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
134 _num_elems_processed_per_iteration =
135 (_data_layout == DataLayout::NCHW) ? 1 : ((dst->data_type() == DataType::F32) ? 2 : 4);
Giorgio Arena8fce4962021-09-01 14:05:00 +0100136 _num_elems_processed_per_iteration = adjust_vec_size(_num_elems_processed_per_iteration, dst->dimension(0));
137
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 int pool_stride_x = 0;
139 int pool_stride_y = 0;
140 const PoolingType pool_type = pool_info.pool_type;
141 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
142 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
143 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
144 const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
145 const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
146 const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
147 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
148 const bool exclude_padding = pool_info.exclude_padding;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000149 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 const int pool_pad_top = pad_stride_info.pad_top();
151 const int pool_pad_left = pad_stride_info.pad_left();
152 const DataType data_type = src->data_type();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000153
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100154 // Set build options
155 CLBuildOptions build_opts;
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100156 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000157 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
158 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000159 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100160 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
161 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
162 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
163 build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
164 build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
Giorgio Arena8fce4962021-09-01 14:05:00 +0100165 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
166 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 build_opts.add_option("-DMAX_WIDTH=" +
168 support::cpp11::to_string(src->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left)));
169 build_opts.add_option("-DMAX_HEIGHT=" +
170 support::cpp11::to_string(src->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top)));
Giorgio Arena8fce4962021-09-01 14:05:00 +0100171
172 // Tensor paddings are used to calculate the indicies for MAX pooling
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 if (pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices &&
174 is_data_type_float(data_type))
Giorgio Arena8fce4962021-09-01 14:05:00 +0100175 {
176 build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(src->tensor_shape().total_size_lower(3)));
177 }
178
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179 if (is_data_type_quantized_asymmetric(data_type))
Giorgio Arena8fce4962021-09-01 14:05:00 +0100180 {
181 build_opts.add_option("-DQUANTIZED");
182
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 if (src->quantization_info() != dst->quantization_info())
Giorgio Arena8fce4962021-09-01 14:05:00 +0100184 {
185 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
186 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
187
188 build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
189 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
190 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
191 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
192 }
193 }
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100194
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000195 // Set the initial value for the pooling operation accordingly with the data type
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 if (pool_type == PoolingType::MAX)
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000197 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 if (is_data_type_quantized(data_type))
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000199 {
200 PixelValue type_min{};
201 std::tie(type_min, std::ignore) = get_min_max(data_type);
202 build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>()));
203 }
204 else
205 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 std::string initial_value = pool_info.use_inf_as_limit
207 ? "(-INFINITY)"
208 : float_to_string_with_full_precision(std::numeric_limits<float>::lowest());
Adnan AlSinan227db8d2023-02-14 14:24:09 +0000209 build_opts.add_option("-DINITIAL_VALUE=" + initial_value);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000210 }
211 }
212 else
213 {
214 // Pool AVG and Pool L2 initial value
215 build_opts.add_option("-DINITIAL_VALUE=0");
216 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000217
218 // Create kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100219 switch (_data_layout)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000220 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100221 case DataLayout::NCHW:
222 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100223 const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision;
224 const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225 const auto acc_data_type = get_cl_type_from_data_type(
226 use_wider_accumulator ? DataType::F32
227 : (is_data_type_quantized(data_type) ? DataType::S32 : data_type));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100228 build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type);
229 build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION");
230
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100231 if (pool_type != PoolingType::MAX)
Michalis Spyroue74b2012018-04-18 09:49:16 +0100232 {
233 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
234 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000235
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100236 if (pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices &&
237 is_data_type_float(data_type))
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100238 {
239 // For max pooling with pool2x2, store indicies which will be used in max unpooling
Giorgio Arena8fce4962021-09-01 14:05:00 +0100240 std::string kernel_name = "pooling_layer_2_nchw_indices";
241 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100242 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100243 else // Run general case
244 {
Giorgio Arena8fce4962021-09-01 14:05:00 +0100245 std::string kernel_name = "pooling_layer_MxN_nchw";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100246 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100247 }
248 break;
249 }
250 case DataLayout::NHWC:
251 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100252 // Floating point mixed precision is support on F16 only
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100253 const auto use_fp_mixed_precision =
254 (data_type == DataType::F16) && pool_info.fp_mixed_precision && pool_type != PoolingType::MAX;
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100255
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100256 // Wider accumulation is required to avoid accuracy loss
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000257 // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation)
258 // Cast 2: Quantized (int8/uint8 src data and int32 accumulation )
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100259 DataType acc_data_type = data_type;
260
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100261 if (use_fp_mixed_precision)
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100262 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100263 acc_data_type = DataType::F32;
264 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265 else if (is_data_type_quantized(data_type) && pool_type != PoolingType::MAX)
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100266 {
267 acc_data_type = DataType::S32;
268 }
269
270 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type));
271 build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION");
272 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000273 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
274 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
275 build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height)));
276 build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel)));
277 build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100278 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" +
279 support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration));
280 if (pool_info.pool_size == Size2D(2, 2) && is_data_type_float(data_type))
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100281 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000282 build_opts.add_option_if(indices != nullptr && pool_type == PoolingType::MAX, "-DEXTRACT_MAX_INDEX");
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100283
284 std::string kernel_name = "pooling_layer_2x2_nhwc";
285 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100286 }
287 else
288 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100289 std::string kernel_name = is_data_type_quantized_asymmetric(data_type)
290 ? "pooling_layer_MxN_quantized_nhwc"
291 : "pooling_layer_MxN_nhwc";
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100292 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
293 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100294 break;
295 }
296 default:
297 ARM_COMPUTE_ERROR("Not implemented");
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000298 }
299
Giorgio Arena8fce4962021-09-01 14:05:00 +0100300 // Configure kernel window
301 Window win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
302 ICLKernel::configure_internal(win);
303
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000304 // Set config_id for enabling LWS tuning
305 _config_id = "pooling_layer_";
306 _config_id += lower_string(string_from_data_type(data_type));
307 _config_id += "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000308 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000309 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000310 _config_id += support::cpp11::to_string(dst->dimension(idx_width));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100311 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000312 _config_id += support::cpp11::to_string(dst->dimension(idx_height));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100313 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000314 _config_id += support::cpp11::to_string(dst->dimension(idx_channel));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100315 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000316 _config_id += lower_string(string_from_data_layout(src->data_layout()));
SiCong Li04a07062020-11-17 14:09:01 +0000317
Giorgio Arena8fce4962021-09-01 14:05:00 +0100318 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000319}
320
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100321Status ClPool2dKernel::validate(const ITensorInfo *src,
322 const ITensorInfo *dst,
323 const PoolingLayerInfo &pool_info,
324 const ITensorInfo *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000325{
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000326 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, indices));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000327 return Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000328}
329
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100330void ClPool2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331{
332 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000333 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100335 unsigned int pool_stride_x = 0;
336 unsigned int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000337 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100339 const auto src =
340 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
341 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0));
342 auto indices = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_1));
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000343
Georgios Pinitas89d71732018-10-29 20:07:15 +0000344 // Collapse window
345 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
346
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100347 switch (_data_layout)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100349 case DataLayout::NCHW:
350 {
Georgios Pinitas89d71732018-10-29 20:07:15 +0000351 Window slice = window_collapsed.first_slice_window_3D();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100352 do
353 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000354 // Set srcs
Michalis Spyroue74b2012018-04-18 09:49:16 +0100355 unsigned int idx = 0;
Giorgio Arena8fce4962021-09-01 14:05:00 +0100356 add_3D_tensor_argument(idx, src, slice);
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000357 add_3D_tensor_argument(idx, dst, slice);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100358 if (indices && is_data_type_float(src->info()->data_type()) && (_pool_info.pool_size == Size2D(2, 2)))
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100359 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000360 add_3D_tensor_argument(idx, indices, slice);
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100361 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100362 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100363 } while (window_collapsed.slide_window_slice_3D(slice));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100364 break;
365 }
366 case DataLayout::NHWC:
367 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000368 const size_t batch_size = dst->info()->tensor_shape().total_size_upper(3);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100369
Georgios Pinitas89d71732018-10-29 20:07:15 +0000370 Window slice = window_collapsed.first_slice_window_4D();
371 Window in_slice = window_collapsed.first_slice_window_4D();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100372 in_slice.set(Window::DimX,
373 Window::Dimension(0, src->info()->dimension(0), _num_elems_processed_per_iteration));
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000374 in_slice.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
375 in_slice.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100376 in_slice.set(3, Window::Dimension(0, batch_size, 1));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100377 do
378 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000379 // Set srcs
Michalis Spyroue74b2012018-04-18 09:49:16 +0100380 unsigned int idx = 0;
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000381 add_4D_tensor_argument(idx, src, in_slice);
382 add_4D_tensor_argument(idx, dst, slice);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100383 if (indices && is_data_type_float(src->info()->data_type()) &&
384 (_pool_info.pool_type == PoolingType::MAX) && (_pool_info.pool_size == Size2D(2, 2)))
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100385 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000386 add_4D_tensor_argument(idx, indices, slice);
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100387 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100388 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100389 } while (window.slide_window_slice_4D(slice) && window.slide_window_slice_4D(in_slice));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100390 break;
391 }
392 default:
393 ARM_COMPUTE_ERROR("Not implemented");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100395}
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000396} // namespace kernels
397} // namespace opencl
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000398} // namespace arm_compute