blob: a1afc585e0d952e6ccf2219a87e95020dc8602d5 [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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CL/CLValidate.h"
32#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
Michele Di Giorgioe1314662021-02-01 17:09:32 +000034#include "support/Cast.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000036namespace arm_compute
37{
Michele Di Giorgioe1314662021-02-01 17:09:32 +000038namespace opencl
39{
40namespace kernels
41{
Michalis Spyroue74b2012018-04-18 09:49:16 +010042using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000044namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045{
Michele Di Giorgioe1314662021-02-01 17:09:32 +000046Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Georgios Pinitas3faea252017-10-30 14:13:50 +000047{
Michele Di Giorgioe1314662021-02-01 17:09:32 +000048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
49 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(src->data_type()) && pool_info.pool_type == PoolingType::L2),
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000052 "Unsupported combination of parameters!");
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010053
Freddie Liardetafcbb8f2021-05-04 12:41:16 +010054 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
55 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
56 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
57 const bool is_global_pooling = pool_info.is_global_pooling;
58 unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
59 unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
60 int output_width = 0;
61 int output_height = 0;
SiCongLic4270cf2021-12-22 11:22:40 +000062
63 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_region_entirely_outside_input(pool_info), "Pooling region that is entirely outside input tensor is unsupported");
64
Freddie Liardetafcbb8f2021-05-04 12:41:16 +010065 std::tie(output_width, output_height) = scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
66 pool_size_x, pool_size_y, pool_info.pad_stride_info);
67 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1), "Calculated output dimension size is invalid");
68
Sheri Zhang801bbcb2020-08-03 20:11:56 +010069 // Check indices
70 if(indices)
71 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +000072 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
Sheri Zhang801bbcb2020-08-03 20:11:56 +010073 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_info.pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
74 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_info.pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010075
76 if(indices->total_size() != 0)
77 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +000078 TensorInfo idx_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, DataType::U32));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010079 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &idx_info);
80 }
Sheri Zhang801bbcb2020-08-03 20:11:56 +010081 }
Georgios Pinitas3faea252017-10-30 14:13:50 +000082
Michele Di Giorgioe1314662021-02-01 17:09:32 +000083 // Checks performed when dst is configured
84 if(dst->total_size() != 0)
Georgios Pinitas3faea252017-10-30 14:13:50 +000085 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +000086 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
87 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
88 TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type()));
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
Georgios Pinitas3faea252017-10-30 14:13:50 +000090 }
91
Georgios Pinitas631c41a2017-12-06 11:53:03 +000092 return Status{};
Georgios Pinitas3faea252017-10-30 14:13:50 +000093}
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000094} // namespace
95
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010096ClPool2dKernel::ClPool2dKernel()
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000097{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010098 _type = CLKernelType::POOL;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000099}
100
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100101void ClPool2dKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000102{
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000103 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Giorgio Arena8fce4962021-09-01 14:05:00 +0100104 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100105
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000106 auto padding_info = get_padding_info({ src, dst, indices });
SiCong Li04a07062020-11-17 14:09:01 +0000107
Giorgio Arena8fce4962021-09-01 14:05:00 +0100108 // Auto init if empty
109 TensorShape out_shape = compute_pool_shape(*src, pool_info);
110 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape));
111 if(indices)
112 {
113 auto_init_if_empty(*indices, src->clone()->set_tensor_shape(out_shape).set_data_type(DataType::U32));
114 }
115
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000116 // Set instance variables
Giorgio Arena8fce4962021-09-01 14:05:00 +0100117 _pool_info = pool_info;
118 _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
119 _num_elems_processed_per_iteration = (_data_layout == DataLayout::NCHW) ? 1 : ((dst->data_type() == DataType::F32) ? 2 : 4);
120 _num_elems_processed_per_iteration = adjust_vec_size(_num_elems_processed_per_iteration, dst->dimension(0));
121
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000122 int pool_stride_x = 0;
123 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000124 const PoolingType pool_type = pool_info.pool_type;
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000125 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
126 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
127 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100128 const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000129 const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
130 const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000131 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
132 const bool exclude_padding = pool_info.exclude_padding;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000133 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Giorgio Arena8fce4962021-09-01 14:05:00 +0100134 const int pool_pad_top = pad_stride_info.pad_top();
135 const int pool_pad_left = pad_stride_info.pad_left();
136 const DataType data_type = src->data_type();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000137
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100138 // Set build options
139 CLBuildOptions build_opts;
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100140 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000141 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
142 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000143 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100144 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
145 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
146 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
147 build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
148 build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
Giorgio Arena8fce4962021-09-01 14:05:00 +0100149 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
150 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
151 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left)));
152 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top)));
153
154 // Tensor paddings are used to calculate the indicies for MAX pooling
155 if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && is_data_type_float(data_type))
156 {
157 build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(src->tensor_shape().total_size_lower(3)));
158 }
159
160 if(is_data_type_quantized_asymmetric(data_type))
161 {
162 build_opts.add_option("-DQUANTIZED");
163
164 if(src->quantization_info() != dst->quantization_info())
165 {
166 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
167 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
168
169 build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
170 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
171 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
172 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
173 }
174 }
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100175
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000176 // Set the initial value for the pooling operation accordingly with the data type
177 if(pool_type == PoolingType::MAX)
178 {
179 if(is_data_type_quantized(data_type))
180 {
181 PixelValue type_min{};
182 std::tie(type_min, std::ignore) = get_min_max(data_type);
183 build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>()));
184 }
185 else
186 {
Adnan AlSinan227db8d2023-02-14 14:24:09 +0000187 std::string initial_value = pool_info.use_inf_as_limit ? "(-INFINITY)" : float_to_string_with_full_precision(std::numeric_limits<float>::lowest());
188 build_opts.add_option("-DINITIAL_VALUE=" + initial_value);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000189 }
190 }
191 else
192 {
193 // Pool AVG and Pool L2 initial value
194 build_opts.add_option("-DINITIAL_VALUE=0");
195 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000196
197 // Create kernel
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000198 switch(_data_layout)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000199 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100200 case DataLayout::NCHW:
201 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100202 const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision;
203 const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX);
Giorgio Arena8fce4962021-09-01 14:05:00 +0100204 const auto acc_data_type = get_cl_type_from_data_type(use_wider_accumulator ? DataType::F32 : (is_data_type_quantized(data_type) ? DataType::S32 : data_type));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100205 build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type);
206 build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION");
207
Michalis Spyroue74b2012018-04-18 09:49:16 +0100208 if(pool_type != PoolingType::MAX)
209 {
210 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
211 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000212
Giorgio Arena8fce4962021-09-01 14:05:00 +0100213 if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && is_data_type_float(data_type))
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100214 {
215 // For max pooling with pool2x2, store indicies which will be used in max unpooling
Giorgio Arena8fce4962021-09-01 14:05:00 +0100216 std::string kernel_name = "pooling_layer_2_nchw_indices";
217 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100218 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100219 else // Run general case
220 {
Giorgio Arena8fce4962021-09-01 14:05:00 +0100221 std::string kernel_name = "pooling_layer_MxN_nchw";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100222 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100223 }
224 break;
225 }
226 case DataLayout::NHWC:
227 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100228 // Floating point mixed precision is support on F16 only
229 const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision && pool_type != PoolingType::MAX;
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100230
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100231 // Wider accumulation is required to avoid accuracy loss
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000232 // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation)
233 // Cast 2: Quantized (int8/uint8 src data and int32 accumulation )
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100234 DataType acc_data_type = data_type;
235
236 if(use_fp_mixed_precision)
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100237 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100238 acc_data_type = DataType::F32;
239 }
240 else if(is_data_type_quantized(data_type) && pool_type != PoolingType::MAX)
241 {
242 acc_data_type = DataType::S32;
243 }
244
245 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type));
246 build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION");
247 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000248 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
249 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
250 build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height)));
251 build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel)));
252 build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size)));
253 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100254 if(pool_info.pool_size == Size2D(2, 2) && is_data_type_float(data_type))
255 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000256 build_opts.add_option_if(indices != nullptr && pool_type == PoolingType::MAX, "-DEXTRACT_MAX_INDEX");
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100257
258 std::string kernel_name = "pooling_layer_2x2_nhwc";
259 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100260 }
261 else
262 {
263 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nhwc" : "pooling_layer_MxN_nhwc";
264 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
265 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100266 break;
267 }
268 default:
269 ARM_COMPUTE_ERROR("Not implemented");
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000270 }
271
Giorgio Arena8fce4962021-09-01 14:05:00 +0100272 // Configure kernel window
273 Window win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
274 ICLKernel::configure_internal(win);
275
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000276 // Set config_id for enabling LWS tuning
277 _config_id = "pooling_layer_";
278 _config_id += lower_string(string_from_data_type(data_type));
279 _config_id += "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000280 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000281 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000282 _config_id += support::cpp11::to_string(dst->dimension(idx_width));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100283 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000284 _config_id += support::cpp11::to_string(dst->dimension(idx_height));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100285 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000286 _config_id += support::cpp11::to_string(dst->dimension(idx_channel));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100287 _config_id += "_";
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000288 _config_id += lower_string(string_from_data_layout(src->data_layout()));
SiCong Li04a07062020-11-17 14:09:01 +0000289
Giorgio Arena8fce4962021-09-01 14:05:00 +0100290 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000291}
292
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100293Status ClPool2dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000294{
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000295 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, indices));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000296 return Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000297}
298
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100299void ClPool2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300{
301 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000302 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303
Georgios Pinitas15997872018-02-19 13:58:22 +0000304 unsigned int pool_stride_x = 0;
305 unsigned int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000306 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000308 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
309 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0));
310 auto indices = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_1));
311
Georgios Pinitas89d71732018-10-29 20:07:15 +0000312 // Collapse window
313 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
314
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000315 switch(_data_layout)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100317 case DataLayout::NCHW:
318 {
Georgios Pinitas89d71732018-10-29 20:07:15 +0000319 Window slice = window_collapsed.first_slice_window_3D();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100320 do
321 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000322 // Set srcs
Michalis Spyroue74b2012018-04-18 09:49:16 +0100323 unsigned int idx = 0;
Giorgio Arena8fce4962021-09-01 14:05:00 +0100324 add_3D_tensor_argument(idx, src, slice);
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000325 add_3D_tensor_argument(idx, dst, slice);
326 if(indices && is_data_type_float(src->info()->data_type()) && (_pool_info.pool_size == Size2D(2, 2)))
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100327 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000328 add_3D_tensor_argument(idx, indices, slice);
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100329 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100330 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100331 }
332 while(window_collapsed.slide_window_slice_3D(slice));
333 break;
334 }
335 case DataLayout::NHWC:
336 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000337 const size_t batch_size = dst->info()->tensor_shape().total_size_upper(3);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100338
Georgios Pinitas89d71732018-10-29 20:07:15 +0000339 Window slice = window_collapsed.first_slice_window_4D();
340 Window in_slice = window_collapsed.first_slice_window_4D();
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000341 in_slice.set(Window::DimX, Window::Dimension(0, src->info()->dimension(0), _num_elems_processed_per_iteration));
342 in_slice.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
343 in_slice.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100344 in_slice.set(3, Window::Dimension(0, batch_size, 1));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100345 do
346 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000347 // Set srcs
Michalis Spyroue74b2012018-04-18 09:49:16 +0100348 unsigned int idx = 0;
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000349 add_4D_tensor_argument(idx, src, in_slice);
350 add_4D_tensor_argument(idx, dst, slice);
351 if(indices && is_data_type_float(src->info()->data_type()) && (_pool_info.pool_type == PoolingType::MAX) && (_pool_info.pool_size == Size2D(2, 2)))
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100352 {
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000353 add_4D_tensor_argument(idx, indices, slice);
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100354 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100355 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100356 }
Georgios Pinitas89d71732018-10-29 20:07:15 +0000357 while(window.slide_window_slice_4D(slice) && window.slide_window_slice_4D(in_slice));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100358 break;
359 }
360 default:
361 ARM_COMPUTE_ERROR("Not implemented");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100362 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100363}
Michele Di Giorgioe1314662021-02-01 17:09:32 +0000364} // namespace kernels
365} // namespace opencl
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000366} // namespace arm_compute