blob: 1771834aacba8f2eba88383453b664c734792a16 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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/core/CL/kernels/CLPoolingLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000030#include "arm_compute/core/CL/ICLKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/CL/ICLTensor.h"
32#include "arm_compute/core/CL/OpenCL.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/core/Window.h"
Michalis Spyroue74b2012018-04-18 09:49:16 +010037#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <set>
41#include <string>
42#include <tuple>
43
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000044namespace arm_compute
45{
Michalis Spyroue74b2012018-04-18 09:49:16 +010046using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000048namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049{
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000050// Internal window config info
51using CLPoolingConfig = std::pair<unsigned int, BorderSize>; //num_elems_processed_per_iteration, border_size
52
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010053void auto_init(const ITensorInfo *input, ITensorInfo *output, ITensorInfo *indices, PoolingLayerInfo pool_info)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000054{
Michalis Spyroue74b2012018-04-18 09:49:16 +010055 TensorShape out_shape = compute_pool_shape(*input, pool_info);
56 auto_init_if_empty(*output, input->clone()->set_tensor_shape(out_shape));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010057 if(indices)
58 {
59 auto_init_if_empty(*indices, input->clone()->set_tensor_shape(out_shape).set_data_type(DataType::U32));
60 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061}
62
morgolockcc1f6c92020-03-24 09:26:48 +000063Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Georgios Pinitas3faea252017-10-30 14:13:50 +000064{
65 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010066 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000067 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +000068 ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(input->data_type()) && pool_info.pool_type == PoolingType::L2),
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000069 "Unsupported combination of parameters!");
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +010070
Sheri Zhang801bbcb2020-08-03 20:11:56 +010071 // Check indices
72 if(indices)
73 {
Sheri Zhang801bbcb2020-08-03 20:11:56 +010074 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
75 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_info.pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
76 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 +010077
78 if(indices->total_size() != 0)
79 {
80 TensorInfo idx_info(TensorInfo(compute_pool_shape(*input, pool_info), 1, DataType::U32));
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &idx_info);
82 }
Sheri Zhang801bbcb2020-08-03 20:11:56 +010083 }
Georgios Pinitas3faea252017-10-30 14:13:50 +000084
Georgios Pinitas3faea252017-10-30 14:13:50 +000085 // Checks performed when output is configured
86 if(output->total_size() != 0)
87 {
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyroue74b2012018-04-18 09:49:16 +010089 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010090 TensorInfo out_info(TensorInfo(compute_pool_shape(*input, pool_info), 1, output->data_type()));
Michalis Spyroue74b2012018-04-18 09:49:16 +010091 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
Georgios Pinitas3faea252017-10-30 14:13:50 +000092 }
93
Georgios Pinitas631c41a2017-12-06 11:53:03 +000094 return Status{};
Georgios Pinitas3faea252017-10-30 14:13:50 +000095}
96
Sheri Zhang801bbcb2020-08-03 20:11:56 +010097std::tuple<Status, Window, CLPoolingConfig> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info, ITensorInfo *indices = nullptr)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000098{
Michalis Spyroue74b2012018-04-18 09:49:16 +010099 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
100
101 // Get data layout
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000102 const DataLayout data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : pool_info.data_layout;
Michalis Spyroue74b2012018-04-18 09:49:16 +0100103 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
104 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
105
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000106 int pool_stride_x = 0;
107 int pool_stride_y = 0;
108 unsigned int pooled_w = 0;
109 unsigned int pooled_h = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000110 int pool_size_x = pool_info.is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size.width;
111 int pool_size_y = pool_info.is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size.height;
112 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000113 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100114 const int pool_pad_right = pad_stride_info.pad_right();
115 const int pool_pad_top = pad_stride_info.pad_top();
116 const int pool_pad_left = pad_stride_info.pad_left();
117 const int pool_pad_bottom = pad_stride_info.pad_bottom();
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100118 BorderSize border_size = BorderSize();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000119
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100120 auto_init(input, output, indices, pool_info);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100121 pooled_w = output->tensor_shape()[idx_width];
122 pooled_h = output->tensor_shape()[idx_height];
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000123
Michalis Spyroue74b2012018-04-18 09:49:16 +0100124 const DataType data_type = input->data_type();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000125
Michalis Spyroue74b2012018-04-18 09:49:16 +0100126 const int input_width = input->dimension(idx_width);
127 const int input_height = input->dimension(idx_height);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000128
Michalis Spyroue74b2012018-04-18 09:49:16 +0100129 unsigned int num_elems_processed_per_iteration = 0;
130 bool window_changed = false;
131 Window win{};
132 switch(data_layout)
133 {
134 case DataLayout::NCHW:
135 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100136 // Initialize border size
137 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100138 // Change the number of elements processed per iteration
139 // for pooling 3x3 with stride less equal than 3
140 const bool can_optimize = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3) && !is_data_type_quantized(data_type);
141 num_elems_processed_per_iteration = can_optimize ? 4 : 1;
142 const unsigned int num_elems_read_per_iteration = (num_elems_processed_per_iteration - 1) * pool_stride_x + pool_size_x;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000143
Michalis Spyroue74b2012018-04-18 09:49:16 +0100144 // Number of iterations in X dimension
145 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000146
Michalis Spyroue74b2012018-04-18 09:49:16 +0100147 // Upper limit for the number of right/bottom border elements that are accessed
148 const int upper_bound_w = ((num_iterations_x - 1) * num_elems_processed_per_iteration * pool_stride_x - pool_pad_left + num_elems_read_per_iteration) - input_width;
149 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000150
Michalis Spyroue74b2012018-04-18 09:49:16 +0100151 border_size.right = std::max(upper_bound_w, pool_pad_right);
152 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000153
Michalis Spyroue74b2012018-04-18 09:49:16 +0100154 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000155
Michalis Spyroue74b2012018-04-18 09:49:16 +0100156 AccessWindowRectangle input_access(input, -pool_pad_left, -pool_pad_top, num_elems_read_per_iteration, pool_size_y,
Giorgio Arena3c520c52018-05-01 11:47:24 +0100157 pool_stride_x, pool_stride_y);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100158 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100159
160 // Update indices window
161 if(indices)
162 {
163 AccessWindowHorizontal indices_access(indices, 0, num_elems_processed_per_iteration);
164 window_changed = update_window_and_padding(win, input_access, output_access, indices_access);
165 indices_access.set_valid_region(win, ValidRegion(Coordinates(), indices->tensor_shape()));
166 }
167 else
168 {
169 window_changed = update_window_and_padding(win, input_access, output_access);
170 }
171
Michalis Spyroue74b2012018-04-18 09:49:16 +0100172 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
173 break;
174 }
175 case DataLayout::NHWC:
176 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100177 // Initialize border size
178 border_size = BorderSize();
179 num_elems_processed_per_iteration = adjust_vec_size(4, output->dimension(0));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100180 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000181
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100182 if(indices != nullptr)
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100183 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100184 indices->set_valid_region(ValidRegion(Coordinates(), indices->tensor_shape()));
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100185 }
186
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100187 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100188 break;
189 }
190 default:
191 ARM_COMPUTE_ERROR("Not implemented");
192 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000193
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000194 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000195 return std::make_tuple(err, win, CLPoolingConfig(num_elems_processed_per_iteration, border_size));
196}
197} // namespace
198
199CLPoolingLayerKernel::CLPoolingLayerKernel()
morgolockcc1f6c92020-03-24 09:26:48 +0000200 : _input(nullptr), _output(nullptr), _indices(nullptr), _pool_info(), _data_layout(DataLayout::UNKNOWN), _border_size(0), _num_elems_processed_per_iteration(1)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000201{
202}
203
204BorderSize CLPoolingLayerKernel::border_size() const
205{
206 return _border_size;
207}
208
morgolockcc1f6c92020-03-24 09:26:48 +0000209void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info, ICLTensor *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000210{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100211 configure(CLKernelLibrary::get().get_compile_context(), input, output, pool_info, indices);
212}
213
Manuel Bottini679fc962020-04-21 16:08:53 +0100214void CLPoolingLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info, ICLTensor *indices)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100215{
Michalis Spyroue74b2012018-04-18 09:49:16 +0100216 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
217
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000218 // Set instance variables
morgolockcc1f6c92020-03-24 09:26:48 +0000219 _input = input;
220 _output = output;
221 _pool_info = pool_info;
222 _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->info()->data_layout() : pool_info.data_layout;
223 _indices = indices;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000224 int pool_stride_x = 0;
225 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000226 const PoolingType pool_type = pool_info.pool_type;
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000227 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
228 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
229 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100230 const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000231 const int pool_size_x = pool_info.is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size.width;
232 const int pool_size_y = pool_info.is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size.height;
233 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
234 const bool exclude_padding = pool_info.exclude_padding;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000235 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Georgios Pinitas15997872018-02-19 13:58:22 +0000236 const int pool_pad_top = pad_stride_info.pad_top();
237 const int pool_pad_left = pad_stride_info.pad_left();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000238
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100239 // Set build options
240 CLBuildOptions build_opts;
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100241 const DataType data_type = input->info()->data_type();
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100242
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100243 // Configure kernel window
244 auto win_config = validate_and_configure_window(input->info(), output->info(), pool_info, (indices ? indices->info() : nullptr));
245
246 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
247 ICLKernel::configure_internal(std::get<1>(win_config));
248
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100249 CLPoolingConfig pooling_config = std::get<2>(win_config);
250 _num_elems_processed_per_iteration = pooling_config.first;
251 _border_size = pooling_config.second;
252
253 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration));
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100254
255 // Tensor paddings are used to calculate the indicies for MAX pooling
256 if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && _indices && is_data_type_float(data_type))
257 {
258 build_opts.add_option("-DPAD_TENSOR_LEFT=" + support::cpp11::to_string(input->info()->padding().left));
259 build_opts.add_option("-DPAD_TENSOR_RIGHT=" + support::cpp11::to_string(input->info()->padding().right));
260 build_opts.add_option("-DPAD_TENSOR_TOP=" + support::cpp11::to_string(input->info()->padding().top));
261 build_opts.add_option("-DPAD_TENSOR_BOTTOM=" + support::cpp11::to_string(input->info()->padding().bottom));
262 build_opts.add_option("-DTENSOR_CHANNEL=" + support::cpp11::to_string(input->info()->dimension(idx_channel)));
263 build_opts.add_option("-DTENSOR_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
264 build_opts.add_option("-DTENSOR_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
265 }
266
267 if(is_data_type_quantized_asymmetric(data_type) && input->info()->quantization_info() != output->info()->quantization_info())
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100268 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100269 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
270 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
271
272 build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
273 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
274 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
275 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100276 }
277
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000278 // Check output dimensions
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100279 auto_init(input->info(), output->info(), indices ? indices->info() : nullptr, pool_info);
280
morgolockcc1f6c92020-03-24 09:26:48 +0000281 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, (indices) ? indices->info() : nullptr));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000282
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000283 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
284 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000285 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100286 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
287 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
288 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
289 build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
290 build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100291
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000292 // Set the initial value for the pooling operation accordingly with the data type
293 if(pool_type == PoolingType::MAX)
294 {
295 if(is_data_type_quantized(data_type))
296 {
297 PixelValue type_min{};
298 std::tie(type_min, std::ignore) = get_min_max(data_type);
299 build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>()));
300 }
301 else
302 {
303 build_opts.add_option("-DINITIAL_VALUE=" + float_to_string_with_full_precision(std::numeric_limits<float>::lowest()));
304 }
305 }
306 else
307 {
308 // Pool AVG and Pool L2 initial value
309 build_opts.add_option("-DINITIAL_VALUE=0");
310 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000311
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100312 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left)));
313 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top)));
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100314
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000315 // Create kernel
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000316 switch(_data_layout)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000317 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100318 case DataLayout::NCHW:
319 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100320 const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision;
321 const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX);
322 const auto acc_data_type = get_cl_type_from_data_type(use_wider_accumulator ? DataType::F32 : data_type);
323 build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type);
324 build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION");
325
Michalis Spyroue74b2012018-04-18 09:49:16 +0100326 if(pool_type != PoolingType::MAX)
327 {
328 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
329 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000330
Michalis Spyroue74b2012018-04-18 09:49:16 +0100331 if((pool_size_x == 3) && (pool_size_y == 3) && !is_data_type_quantized_asymmetric(data_type))
332 {
333 // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where
334 // each thread computes 4 output elements
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100335 const bool is_pool3x3_stride_le3 = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000336
Michalis Spyroue74b2012018-04-18 09:49:16 +0100337 std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_")
338 + support::cpp11::to_string(pool_size_x);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100339 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100340 }
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100341 else if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && _indices && is_data_type_float(data_type))
342 {
343 // For max pooling with pool2x2, store indicies which will be used in max unpooling
344 if(data_type == DataType::F32)
345 {
346 std::string kernel_name = "pooling_layer_2_nchw_indices_fp32";
347 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
348 }
349 else if(data_type == DataType::F16)
350 {
351 std::string kernel_name = "pooling_layer_2_nchw_indices_fp16";
352 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
353 }
354 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100355 else // Run general case
356 {
357 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nchw" : "pooling_layer_MxN_nchw";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100358 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100359 }
360 break;
361 }
362 case DataLayout::NHWC:
363 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100364 // Floating point mixed precision is support on F16 only
365 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 +0100366
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100367 // Wider accumulation is required to avoid accuracy loss
368 // Case 1: Floating point mixed precision (fp16 input data and fp32 accumulation)
369 // Cast 2: Quantized (int8/uint8 input data and int32 accumulation )
370 DataType acc_data_type = data_type;
371
372 if(use_fp_mixed_precision)
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100373 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100374 acc_data_type = DataType::F32;
375 }
376 else if(is_data_type_quantized(data_type) && pool_type != PoolingType::MAX)
377 {
378 acc_data_type = DataType::S32;
379 }
380
381 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type));
382 build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION");
383 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
384 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
385 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
386 build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(output->info()->dimension(idx_height)));
387 build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(output->info()->dimension(idx_channel)));
388 build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(output->info()->dimension(idx_batch_size)));
389 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % _num_elems_processed_per_iteration));
390 if(pool_info.pool_size == Size2D(2, 2) && is_data_type_float(data_type))
391 {
392 build_opts.add_option_if(_indices != nullptr && pool_type == PoolingType::MAX, "-DEXTRACT_MAX_INDEX");
393
394 std::string kernel_name = "pooling_layer_2x2_nhwc";
395 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100396 }
397 else
398 {
399 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nhwc" : "pooling_layer_MxN_nhwc";
400 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
401 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100402 break;
403 }
404 default:
405 ARM_COMPUTE_ERROR("Not implemented");
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000406 }
407
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000408 // Set config_id for enabling LWS tuning
409 _config_id = "pooling_layer_";
410 _config_id += lower_string(string_from_data_type(data_type));
411 _config_id += "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000412 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000413 _config_id += "_";
Michalis Spyroue74b2012018-04-18 09:49:16 +0100414 _config_id += support::cpp11::to_string(output->info()->dimension(idx_width));
415 _config_id += "_";
416 _config_id += support::cpp11::to_string(output->info()->dimension(idx_height));
417 _config_id += "_";
418 _config_id += support::cpp11::to_string(output->info()->dimension(idx_channel));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100419 _config_id += "_";
420 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000421}
422
morgolockcc1f6c92020-03-24 09:26:48 +0000423Status CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000424{
morgolockcc1f6c92020-03-24 09:26:48 +0000425 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, indices));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000426 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), pool_info)));
427
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000428 return Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000429}
430
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100431void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
432{
433 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
434 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
435
Georgios Pinitas15997872018-02-19 13:58:22 +0000436 unsigned int pool_stride_x = 0;
437 unsigned int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000438 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439
Georgios Pinitas89d71732018-10-29 20:07:15 +0000440 // Collapse window
441 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
442
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000443 switch(_data_layout)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100444 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100445 case DataLayout::NCHW:
446 {
Georgios Pinitas89d71732018-10-29 20:07:15 +0000447 Window slice = window_collapsed.first_slice_window_3D();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100448 do
449 {
450 // Upsample input by pool size
451 Window in_slice(slice);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000452 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - _pool_info.pad_stride_info.pad_left(),
453 (in_slice.x().end() - _pool_info.pad_stride_info.pad_left()) * pool_stride_x,
Michalis Spyroue74b2012018-04-18 09:49:16 +0100454 pool_stride_x * _num_elems_processed_per_iteration));
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000455 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - _pool_info.pad_stride_info.pad_top(),
456 (in_slice.y().end() - _pool_info.pad_stride_info.pad_top()) * pool_stride_y,
Michalis Spyroue74b2012018-04-18 09:49:16 +0100457 pool_stride_y));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458
Michalis Spyroue74b2012018-04-18 09:49:16 +0100459 // Set inputs
460 unsigned int idx = 0;
461 add_3D_tensor_argument(idx, _input, in_slice);
462 add_3D_tensor_argument(idx, _output, slice);
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100463 if(_indices && is_data_type_float(_input->info()->data_type()) && (_pool_info.pool_size == Size2D(2, 2)))
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100464 {
465 add_3D_tensor_argument(idx, _indices, slice);
466 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100467 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100468 }
469 while(window_collapsed.slide_window_slice_3D(slice));
470 break;
471 }
472 case DataLayout::NHWC:
473 {
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100474 const size_t batch_size = _output->info()->tensor_shape().total_size_upper(3);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100475
Georgios Pinitas89d71732018-10-29 20:07:15 +0000476 Window slice = window_collapsed.first_slice_window_4D();
477 Window in_slice = window_collapsed.first_slice_window_4D();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100478 in_slice.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration));
479 in_slice.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
480 in_slice.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
Gian Marco Iodice7333e1f2020-10-08 10:25:49 +0100481 in_slice.set(3, Window::Dimension(0, batch_size, 1));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100482 do
483 {
484 // Set inputs
485 unsigned int idx = 0;
Georgios Pinitas89d71732018-10-29 20:07:15 +0000486 add_4D_tensor_argument(idx, _input, in_slice);
487 add_4D_tensor_argument(idx, _output, slice);
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100488 if(_indices && is_data_type_float(_input->info()->data_type()) && (_pool_info.pool_type == PoolingType::MAX) && (_pool_info.pool_size == Size2D(2, 2)))
489 {
490 add_4D_tensor_argument(idx, _indices, slice);
491 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100492 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100493 }
Georgios Pinitas89d71732018-10-29 20:07:15 +0000494 while(window.slide_window_slice_4D(slice) && window.slide_window_slice_4D(in_slice));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100495 break;
496 }
497 default:
498 ARM_COMPUTE_ERROR("Not implemented");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100499 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100500}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000501} // namespace arm_compute