blob: dbbca4771bacc3279d1533e0fdaf43c941d71d4f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiocbbed282019-12-20 13:26:08 +00002 * 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
Michalis Spyroue74b2012018-04-18 09:49:16 +010053void auto_init(const ITensorInfo *input, ITensorInfo *output, 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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057}
58
morgolockcc1f6c92020-03-24 09:26:48 +000059Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Georgios Pinitas3faea252017-10-30 14:13:50 +000060{
61 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010062 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
morgolockcc1f6c92020-03-24 09:26:48 +000063 ARM_COMPUTE_RETURN_ERROR_ON_MSG(indices, "Indices not supported in the CL backend.");
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000064 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 +000065 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 +000066 "Unsupported combination of parameters!");
Michele Di Giorgio2c877192020-02-18 19:06:27 +000067 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(input->data_type()) && !pool_info.exclude_padding && (pool_info.pool_type == PoolingType::AVG) && pool_info.pad_stride_info.has_padding()
68 && (input->data_layout() == DataLayout::NHWC),
69 "exclude_padding equal false is not supported for AVG Pooling with padding on quantized types");
Georgios Pinitas3faea252017-10-30 14:13:50 +000070
Georgios Pinitas3faea252017-10-30 14:13:50 +000071 // Checks performed when output is configured
72 if(output->total_size() != 0)
73 {
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyroue74b2012018-04-18 09:49:16 +010075 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010076 TensorInfo out_info(TensorInfo(compute_pool_shape(*input, pool_info), 1, output->data_type()));
Michalis Spyroue74b2012018-04-18 09:49:16 +010077 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
Georgios Pinitas3faea252017-10-30 14:13:50 +000078 }
79
Georgios Pinitas631c41a2017-12-06 11:53:03 +000080 return Status{};
Georgios Pinitas3faea252017-10-30 14:13:50 +000081}
82
Georgios Pinitas631c41a2017-12-06 11:53:03 +000083std::tuple<Status, Window, CLPoolingConfig> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000084{
Michalis Spyroue74b2012018-04-18 09:49:16 +010085 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
86
87 // Get data layout
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000088 const DataLayout data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : pool_info.data_layout;
Michalis Spyroue74b2012018-04-18 09:49:16 +010089 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
90 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
91
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000092 int pool_stride_x = 0;
93 int pool_stride_y = 0;
94 unsigned int pooled_w = 0;
95 unsigned int pooled_h = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +000096 int pool_size_x = pool_info.is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size.width;
97 int pool_size_y = pool_info.is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size.height;
98 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000099 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100100 const int pool_pad_right = pad_stride_info.pad_right();
101 const int pool_pad_top = pad_stride_info.pad_top();
102 const int pool_pad_left = pad_stride_info.pad_left();
103 const int pool_pad_bottom = pad_stride_info.pad_bottom();
104 BorderSize border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000105
Michalis Spyroue74b2012018-04-18 09:49:16 +0100106 auto_init(input, output, pool_info);
107 pooled_w = output->tensor_shape()[idx_width];
108 pooled_h = output->tensor_shape()[idx_height];
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000109
Michalis Spyroue74b2012018-04-18 09:49:16 +0100110 const DataType data_type = input->data_type();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000111
Michalis Spyroue74b2012018-04-18 09:49:16 +0100112 const int input_width = input->dimension(idx_width);
113 const int input_height = input->dimension(idx_height);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000114
Michalis Spyroue74b2012018-04-18 09:49:16 +0100115 unsigned int num_elems_processed_per_iteration = 0;
116 bool window_changed = false;
117 Window win{};
118 switch(data_layout)
119 {
120 case DataLayout::NCHW:
121 {
122 // Change the number of elements processed per iteration
123 // for pooling 3x3 with stride less equal than 3
124 const bool can_optimize = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3) && !is_data_type_quantized(data_type);
125 num_elems_processed_per_iteration = can_optimize ? 4 : 1;
126 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 +0000127
Michalis Spyroue74b2012018-04-18 09:49:16 +0100128 // Number of iterations in X dimension
129 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 +0000130
Michalis Spyroue74b2012018-04-18 09:49:16 +0100131 // Upper limit for the number of right/bottom border elements that are accessed
132 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;
133 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 +0000134
Michalis Spyroue74b2012018-04-18 09:49:16 +0100135 border_size.right = std::max(upper_bound_w, pool_pad_right);
136 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000137
Michalis Spyroue74b2012018-04-18 09:49:16 +0100138 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000139
Michalis Spyroue74b2012018-04-18 09:49:16 +0100140 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 +0100141 pool_stride_x, pool_stride_y);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100142 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
143 window_changed = update_window_and_padding(win, input_access, output_access);
144 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
145 break;
146 }
147 case DataLayout::NHWC:
148 {
149 num_elems_processed_per_iteration = 8;
150 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000151
Georgios Pinitase2220552018-07-20 13:23:44 +0100152 AccessWindowStatic input_access(input,
153 0, -1,
154 ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100155 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
156 window_changed = update_window_and_padding(win, input_access, output_access);
157 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
158 break;
159 }
160 default:
161 ARM_COMPUTE_ERROR("Not implemented");
162 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000163
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000164 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000165 return std::make_tuple(err, win, CLPoolingConfig(num_elems_processed_per_iteration, border_size));
166}
167} // namespace
168
169CLPoolingLayerKernel::CLPoolingLayerKernel()
morgolockcc1f6c92020-03-24 09:26:48 +0000170 : _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 +0000171{
172}
173
174BorderSize CLPoolingLayerKernel::border_size() const
175{
176 return _border_size;
177}
178
morgolockcc1f6c92020-03-24 09:26:48 +0000179void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info, ICLTensor *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000180{
Michalis Spyroue74b2012018-04-18 09:49:16 +0100181 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
182
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000183 // Set instance variables
morgolockcc1f6c92020-03-24 09:26:48 +0000184 _input = input;
185 _output = output;
186 _pool_info = pool_info;
187 _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->info()->data_layout() : pool_info.data_layout;
188 _indices = indices;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000189 int pool_stride_x = 0;
190 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000191 const PoolingType pool_type = pool_info.pool_type;
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000192 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
193 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
194 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000195 const int pool_size_x = pool_info.is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size.width;
196 const int pool_size_y = pool_info.is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size.height;
197 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
198 const bool exclude_padding = pool_info.exclude_padding;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000199 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Georgios Pinitas15997872018-02-19 13:58:22 +0000200 const int pool_pad_top = pad_stride_info.pad_top();
201 const int pool_pad_left = pad_stride_info.pad_left();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000202
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100203 // Set build options
204 CLBuildOptions build_opts;
205
206 if(is_data_type_quantized_asymmetric(input->info()->data_type()) && input->info()->quantization_info() != output->info()->quantization_info())
207 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100208 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
209 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
210
211 build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
212 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
213 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
214 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100215 }
216
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000217 // Check output dimensions
Michalis Spyroue74b2012018-04-18 09:49:16 +0100218 auto_init(input->info(), output->info(), pool_info);
morgolockcc1f6c92020-03-24 09:26:48 +0000219 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, (indices) ? indices->info() : nullptr));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000220
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100221 const DataType data_type = input->info()->data_type();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000222
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000223 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
224 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000225 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100226 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
227 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
228 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
229 build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
230 build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100231
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000232 // Set the initial value for the pooling operation accordingly with the data type
233 if(pool_type == PoolingType::MAX)
234 {
235 if(is_data_type_quantized(data_type))
236 {
237 PixelValue type_min{};
238 std::tie(type_min, std::ignore) = get_min_max(data_type);
239 build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>()));
240 }
241 else
242 {
243 build_opts.add_option("-DINITIAL_VALUE=" + float_to_string_with_full_precision(std::numeric_limits<float>::lowest()));
244 }
245 }
246 else
247 {
248 // Pool AVG and Pool L2 initial value
249 build_opts.add_option("-DINITIAL_VALUE=0");
250 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000251
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000252 const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision;
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100253 const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX);
254 const auto acc_data_type = get_cl_type_from_data_type(use_wider_accumulator ? DataType::F32 : data_type);
255 build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type);
256 build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION");
257
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000258 // Create kernel
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000259 switch(_data_layout)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000260 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100261 case DataLayout::NCHW:
262 {
263 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left)));
264 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top)));
265 if(pool_type != PoolingType::MAX)
266 {
267 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
268 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000269
Michalis Spyroue74b2012018-04-18 09:49:16 +0100270 if((pool_size_x == 3) && (pool_size_y == 3) && !is_data_type_quantized_asymmetric(data_type))
271 {
272 // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where
273 // each thread computes 4 output elements
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100274 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 +0000275
Michalis Spyroue74b2012018-04-18 09:49:16 +0100276 std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_")
277 + support::cpp11::to_string(pool_size_x);
278 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
279 }
280 else // Run general case
281 {
282 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nchw" : "pooling_layer_MxN_nchw";
283 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
284 }
285 break;
286 }
287 case DataLayout::NHWC:
288 {
289 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
290 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
291 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
Georgios Pinitas89d71732018-10-29 20:07:15 +0000292 build_opts.add_option_if(output->info()->tensor_shape().total_size_upper(3) > 1,
293 "-DDST_DEPTH=" + support::cpp11::to_string(output->info()->dimension(idx_height)));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100294 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nhwc" : "pooling_layer_MxN_nhwc";
295 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
296 break;
297 }
298 default:
299 ARM_COMPUTE_ERROR("Not implemented");
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000300 }
301
302 // Configure kernel window
303 auto win_config = validate_and_configure_window(input->info(), output->info(), pool_info);
304
305 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100306 ICLKernel::configure_internal(std::get<1>(win_config));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000307
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000308 if(_data_layout == DataLayout::NCHW)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000309 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100310 CLPoolingConfig pooling_config = std::get<2>(win_config);
311 _num_elems_processed_per_iteration = pooling_config.first;
312 _border_size = pooling_config.second;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000313 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100314 else
315 {
316 _border_size = BorderSize(1, 0, 0, 0);
317 _num_elems_processed_per_iteration = 8;
318 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000319
320 // Set config_id for enabling LWS tuning
321 _config_id = "pooling_layer_";
322 _config_id += lower_string(string_from_data_type(data_type));
323 _config_id += "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000324 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000325 _config_id += "_";
Michalis Spyroue74b2012018-04-18 09:49:16 +0100326 _config_id += support::cpp11::to_string(output->info()->dimension(idx_width));
327 _config_id += "_";
328 _config_id += support::cpp11::to_string(output->info()->dimension(idx_height));
329 _config_id += "_";
330 _config_id += support::cpp11::to_string(output->info()->dimension(idx_channel));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100331 _config_id += "_";
332 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000333}
334
morgolockcc1f6c92020-03-24 09:26:48 +0000335Status CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000336{
morgolockcc1f6c92020-03-24 09:26:48 +0000337 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, indices));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000338 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), pool_info)));
339
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000340 return Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000341}
342
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
344{
345 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
346 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
347
Georgios Pinitas15997872018-02-19 13:58:22 +0000348 unsigned int pool_stride_x = 0;
349 unsigned int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000350 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351
Georgios Pinitas89d71732018-10-29 20:07:15 +0000352 // Collapse window
353 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
354
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000355 switch(_data_layout)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100357 case DataLayout::NCHW:
358 {
Georgios Pinitas89d71732018-10-29 20:07:15 +0000359 Window slice = window_collapsed.first_slice_window_3D();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100360 do
361 {
362 // Upsample input by pool size
363 Window in_slice(slice);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000364 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - _pool_info.pad_stride_info.pad_left(),
365 (in_slice.x().end() - _pool_info.pad_stride_info.pad_left()) * pool_stride_x,
Michalis Spyroue74b2012018-04-18 09:49:16 +0100366 pool_stride_x * _num_elems_processed_per_iteration));
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000367 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - _pool_info.pad_stride_info.pad_top(),
368 (in_slice.y().end() - _pool_info.pad_stride_info.pad_top()) * pool_stride_y,
Michalis Spyroue74b2012018-04-18 09:49:16 +0100369 pool_stride_y));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100370
Michalis Spyroue74b2012018-04-18 09:49:16 +0100371 // Set inputs
372 unsigned int idx = 0;
373 add_3D_tensor_argument(idx, _input, in_slice);
374 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100375 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100376 }
377 while(window_collapsed.slide_window_slice_3D(slice));
378 break;
379 }
380 case DataLayout::NHWC:
381 {
Georgios Pinitas89d71732018-10-29 20:07:15 +0000382 const size_t total_batches = _output->info()->tensor_shape().total_size_upper(3);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100383
Georgios Pinitas89d71732018-10-29 20:07:15 +0000384 Window slice = window_collapsed.first_slice_window_4D();
385 Window in_slice = window_collapsed.first_slice_window_4D();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100386 in_slice.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration));
387 in_slice.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
388 in_slice.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
Georgios Pinitas89d71732018-10-29 20:07:15 +0000389 in_slice.set(3, Window::Dimension(0, total_batches, 1));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100390 do
391 {
392 // Set inputs
393 unsigned int idx = 0;
Georgios Pinitas89d71732018-10-29 20:07:15 +0000394 add_4D_tensor_argument(idx, _input, in_slice);
395 add_4D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100396 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100397 }
Georgios Pinitas89d71732018-10-29 20:07:15 +0000398 while(window.slide_window_slice_4D(slice) && window.slide_window_slice_4D(in_slice));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100399 break;
400 }
401 default:
402 ARM_COMPUTE_ERROR("Not implemented");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100404}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000405} // namespace arm_compute