blob: df130682397c3cab3a66a23a9a789172ae005853 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +00002 * Copyright (c) 2017-2018 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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <set>
40#include <string>
41#include <tuple>
42
43using namespace arm_compute;
Michalis Spyroue74b2012018-04-18 09:49:16 +010044using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000046namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047{
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000048// Internal window config info
49using CLPoolingConfig = std::pair<unsigned int, BorderSize>; //num_elems_processed_per_iteration, border_size
50
Michalis Spyroue74b2012018-04-18 09:49:16 +010051void auto_init(const ITensorInfo *input, ITensorInfo *output, PoolingLayerInfo pool_info)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000052{
Michalis Spyroue74b2012018-04-18 09:49:16 +010053 TensorShape out_shape = compute_pool_shape(*input, pool_info);
54 auto_init_if_empty(*output, input->clone()->set_tensor_shape(out_shape));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055}
56
Georgios Pinitas631c41a2017-12-06 11:53:03 +000057Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
Georgios Pinitas3faea252017-10-30 14:13:50 +000058{
59 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michalis Spyroue74b2012018-04-18 09:49:16 +010060 DataLayout data_layout = input->data_layout();
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010061 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michalis Spyroue74b2012018-04-18 09:49:16 +010062 switch(data_layout)
63 {
64 case DataLayout::NCHW:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010065 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Michalis Spyroue74b2012018-04-18 09:49:16 +010066 break;
67 case DataLayout::NHWC:
68 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
69 break;
70 default:
71 ARM_COMPUTE_ERROR("Data layout not supported");
72 }
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000073 ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(input->data_type()) && pool_info.pool_type() == PoolingType::L2),
74 "Unsupported combination of parameters!");
Georgios Pinitas3faea252017-10-30 14:13:50 +000075
Georgios Pinitas3faea252017-10-30 14:13:50 +000076 // Checks performed when output is configured
77 if(output->total_size() != 0)
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyroue74b2012018-04-18 09:49:16 +010080 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010081 TensorInfo out_info(TensorInfo(compute_pool_shape(*input, pool_info), 1, output->data_type()));
Michalis Spyroue74b2012018-04-18 09:49:16 +010082 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
Georgios Pinitas3faea252017-10-30 14:13:50 +000083 }
84
Georgios Pinitas631c41a2017-12-06 11:53:03 +000085 return Status{};
Georgios Pinitas3faea252017-10-30 14:13:50 +000086}
87
Georgios Pinitas631c41a2017-12-06 11:53:03 +000088std::tuple<Status, Window, CLPoolingConfig> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000089{
Michalis Spyroue74b2012018-04-18 09:49:16 +010090 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
91
92 // Get data layout
93 const DataLayout data_layout = input->data_layout();
94 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
95 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
96
Giorgio Arena9f26b3e2017-11-28 14:35:00 +000097 int pool_stride_x = 0;
98 int pool_stride_y = 0;
99 unsigned int pooled_w = 0;
100 unsigned int pooled_h = 0;
Michalis Spyroue74b2012018-04-18 09:49:16 +0100101 int pool_size_x = pool_info.is_global_pooling() ? input->dimension(idx_width) : pool_info.pool_size().width;
102 int pool_size_y = pool_info.is_global_pooling() ? input->dimension(idx_height) : pool_info.pool_size().height;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000103 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000104 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100105 const int pool_pad_right = pad_stride_info.pad_right();
106 const int pool_pad_top = pad_stride_info.pad_top();
107 const int pool_pad_left = pad_stride_info.pad_left();
108 const int pool_pad_bottom = pad_stride_info.pad_bottom();
109 BorderSize border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000110
Michalis Spyroue74b2012018-04-18 09:49:16 +0100111 auto_init(input, output, pool_info);
112 pooled_w = output->tensor_shape()[idx_width];
113 pooled_h = output->tensor_shape()[idx_height];
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000114
Michalis Spyroue74b2012018-04-18 09:49:16 +0100115 const DataType data_type = input->data_type();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000116
Michalis Spyroue74b2012018-04-18 09:49:16 +0100117 const int input_width = input->dimension(idx_width);
118 const int input_height = input->dimension(idx_height);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000119
Michalis Spyroue74b2012018-04-18 09:49:16 +0100120 unsigned int num_elems_processed_per_iteration = 0;
121 bool window_changed = false;
122 Window win{};
123 switch(data_layout)
124 {
125 case DataLayout::NCHW:
126 {
127 // Change the number of elements processed per iteration
128 // for pooling 3x3 with stride less equal than 3
129 const bool can_optimize = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3) && !is_data_type_quantized(data_type);
130 num_elems_processed_per_iteration = can_optimize ? 4 : 1;
131 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 +0000132
Michalis Spyroue74b2012018-04-18 09:49:16 +0100133 // Number of iterations in X dimension
134 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 +0000135
Michalis Spyroue74b2012018-04-18 09:49:16 +0100136 // Upper limit for the number of right/bottom border elements that are accessed
137 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;
138 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 +0000139
Michalis Spyroue74b2012018-04-18 09:49:16 +0100140 border_size.right = std::max(upper_bound_w, pool_pad_right);
141 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000142
Michalis Spyroue74b2012018-04-18 09:49:16 +0100143 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000144
Michalis Spyroue74b2012018-04-18 09:49:16 +0100145 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 +0100146 pool_stride_x, pool_stride_y);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100147 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
148 window_changed = update_window_and_padding(win, input_access, output_access);
149 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
150 break;
151 }
152 case DataLayout::NHWC:
153 {
154 num_elems_processed_per_iteration = 8;
155 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000156
Georgios Pinitase2220552018-07-20 13:23:44 +0100157 AccessWindowStatic input_access(input,
158 0, -1,
159 ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100160 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
161 window_changed = update_window_and_padding(win, input_access, output_access);
162 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
163 break;
164 }
165 default:
166 ARM_COMPUTE_ERROR("Not implemented");
167 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000168
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000169 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000170 return std::make_tuple(err, win, CLPoolingConfig(num_elems_processed_per_iteration, border_size));
171}
172} // namespace
173
174CLPoolingLayerKernel::CLPoolingLayerKernel()
175 : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0), _num_elems_processed_per_iteration(1)
176{
177}
178
179BorderSize CLPoolingLayerKernel::border_size() const
180{
181 return _border_size;
182}
183
184void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info)
185{
Michalis Spyroue74b2012018-04-18 09:49:16 +0100186 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
187
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000188 int pool_stride_x = 0;
189 int pool_stride_y = 0;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000190 const PoolingType pool_type = pool_info.pool_type();
Michalis Spyroue74b2012018-04-18 09:49:16 +0100191 DataLayout data_layout = input->info()->data_layout();
192 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);
195 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;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000197 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
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000203 // Check output dimensions
Michalis Spyroue74b2012018-04-18 09:49:16 +0100204 auto_init(input->info(), output->info(), pool_info);
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000205 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000206
207 // Set instance variables
208 _input = input;
209 _output = output;
210 _pool_info = pool_info;
211
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100212 const DataType data_type = input->info()->data_type();
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000213
214 // Set build options
215 CLBuildOptions build_opts;
216 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
217 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000218 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
Michalis Spyroue74b2012018-04-18 09:49:16 +0100219 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
220 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
221 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
222 build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
223 build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
224 build_opts.add_option_if(data_type == DataType::F16, "-DFP16");
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000225
226 // Create kernel
Michalis Spyroue74b2012018-04-18 09:49:16 +0100227 switch(data_layout)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000228 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100229 case DataLayout::NCHW:
230 {
231 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left)));
232 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top)));
233 if(pool_type != PoolingType::MAX)
234 {
235 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
236 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000237
Michalis Spyroue74b2012018-04-18 09:49:16 +0100238 if((pool_size_x == 3) && (pool_size_y == 3) && !is_data_type_quantized_asymmetric(data_type))
239 {
240 // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where
241 // each thread computes 4 output elements
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100242 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 +0000243
Michalis Spyroue74b2012018-04-18 09:49:16 +0100244 std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_")
245 + support::cpp11::to_string(pool_size_x);
246 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
247 }
248 else // Run general case
249 {
250 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nchw" : "pooling_layer_MxN_nchw";
251 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
252 }
253 break;
254 }
255 case DataLayout::NHWC:
256 {
257 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
258 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
259 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
260 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nhwc" : "pooling_layer_MxN_nhwc";
261 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
262 break;
263 }
264 default:
265 ARM_COMPUTE_ERROR("Not implemented");
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000266 }
267
268 // Configure kernel window
269 auto win_config = validate_and_configure_window(input->info(), output->info(), pool_info);
270
271 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100272 ICLKernel::configure_internal(std::get<1>(win_config));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000273
Michalis Spyroue74b2012018-04-18 09:49:16 +0100274 if(data_layout == DataLayout::NCHW)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000275 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100276 CLPoolingConfig pooling_config = std::get<2>(win_config);
277 _num_elems_processed_per_iteration = pooling_config.first;
278 _border_size = pooling_config.second;
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000279 }
Michalis Spyroue74b2012018-04-18 09:49:16 +0100280 else
281 {
282 _border_size = BorderSize(1, 0, 0, 0);
283 _num_elems_processed_per_iteration = 8;
284 }
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000285
286 // Set config_id for enabling LWS tuning
287 _config_id = "pooling_layer_";
288 _config_id += lower_string(string_from_data_type(data_type));
289 _config_id += "_";
Michalis Spyroue74b2012018-04-18 09:49:16 +0100290 _config_id += lower_string(string_from_data_layout(data_layout));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000291 _config_id += "_";
Michalis Spyroue74b2012018-04-18 09:49:16 +0100292 _config_id += support::cpp11::to_string(output->info()->dimension(idx_width));
293 _config_id += "_";
294 _config_id += support::cpp11::to_string(output->info()->dimension(idx_height));
295 _config_id += "_";
296 _config_id += support::cpp11::to_string(output->info()->dimension(idx_channel));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100297 _config_id += "_";
298 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000299}
300
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000301Status CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000302{
303 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info));
304 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), pool_info)));
305
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000306 return Status{};
Giorgio Arena9f26b3e2017-11-28 14:35:00 +0000307}
308
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
310{
311 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
312 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
313
Georgios Pinitas15997872018-02-19 13:58:22 +0000314 unsigned int pool_stride_x = 0;
315 unsigned int pool_stride_y = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
317
Michalis Spyroue74b2012018-04-18 09:49:16 +0100318 switch(_input->info()->data_layout())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 {
Michalis Spyroue74b2012018-04-18 09:49:16 +0100320 case DataLayout::NCHW:
321 {
322 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
323 Window slice = window_collapsed.first_slice_window_3D();
324 do
325 {
326 // Upsample input by pool size
327 Window in_slice(slice);
328 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - _pool_info.pad_stride_info().pad_left(),
329 (in_slice.x().end() - _pool_info.pad_stride_info().pad_left()) * pool_stride_x,
330 pool_stride_x * _num_elems_processed_per_iteration));
331 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - _pool_info.pad_stride_info().pad_top(),
332 (in_slice.y().end() - _pool_info.pad_stride_info().pad_top()) * pool_stride_y,
333 pool_stride_y));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334
Michalis Spyroue74b2012018-04-18 09:49:16 +0100335 // Set inputs
336 unsigned int idx = 0;
337 add_3D_tensor_argument(idx, _input, in_slice);
338 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100339 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100340 }
341 while(window_collapsed.slide_window_slice_3D(slice));
342 break;
343 }
344 case DataLayout::NHWC:
345 {
346 Window slice = window.first_slice_window_3D();
347
348 Window in_slice = window.first_slice_window_3D();
349 in_slice.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration));
350 in_slice.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
351 in_slice.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
352 do
353 {
354 // Set inputs
355 unsigned int idx = 0;
356 add_3D_tensor_argument(idx, _input, in_slice);
357 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100358 enqueue(queue, *this, slice, lws_hint());
Michalis Spyroue74b2012018-04-18 09:49:16 +0100359 }
360 while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(in_slice));
361 break;
362 }
363 default:
364 ARM_COMPUTE_ERROR("Not implemented");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100366}