blob: 0b90d9f2901575c1530d6c0f91008e5ea74220e3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas99089ce2019-02-06 14:16:18 +00002 * Copyright (c) 2017-2019 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/NEON/kernels/NEPoolingLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbiereaefd002018-07-20 17:49:35 +010027#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
Georgios Pinitas55186712018-01-08 17:37:12 +000031#include "arm_compute/core/NEON/NEAsymm.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/NEON/NEFixedPoint.h"
Georgios Pinitascdf51452017-08-31 14:21:36 +010033#include "arm_compute/core/NEON/NEMath.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/Window.h"
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +010038#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
Georgios Pinitas55186712018-01-08 17:37:12 +000040#include "support/ToolchainSupport.h"
41
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042#include <algorithm>
43#include <arm_neon.h>
Georgios Pinitascdf51452017-08-31 14:21:36 +010044#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045#include <limits>
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +010046#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047#include <string>
48#include <tuple>
49
50using namespace arm_compute;
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +010051using namespace misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052
53namespace
54{
Pablo Tello77e6c552018-12-04 15:33:49 +000055inline float calculate_avg_scale(bool exclude_padding, DataLayout data_layout, const Coordinates &id, const int pool_size_x, const int pool_size_y, const int upper_bound_w, const int upper_bound_h,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 const int pad_x, const int pad_y, const int stride_x, const int stride_y)
57{
Michalis Spyrou57dac842018-03-01 16:03:50 +000058 const unsigned int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
59 const unsigned int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
60
61 int start_x = id[idx_width] * stride_x - pad_x;
62 int start_y = id[idx_height] * stride_y - pad_y;
63
64 const int end_x = std::min(start_x + pool_size_x, upper_bound_w);
65 const int end_y = std::min(start_y + pool_size_y, upper_bound_h);
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000066 if(exclude_padding)
67 {
68 start_x = std::max(0, start_x);
69 start_y = std::max(0, start_y);
70 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 return 1.f / ((end_y - start_y) * (end_x - start_x));
72}
73
Pablo Tello77e6c552018-12-04 15:33:49 +000074inline void scale_vector_s16x8(bool exclude_padding, uint16x8_t &v, const Coordinates &id, int id_offset, int step,
Georgios Pinitas55186712018-01-08 17:37:12 +000075 const int pool_size, const int upper_bound_w, const int upper_bound_h,
76 const int pad_x, const int pad_y, const int stride_x, const int stride_y)
77{
78 int start_x = (id.x() + id_offset) * stride_x - pad_x;
79 int start_y = id.y() * stride_y - pad_y;
80 const int end_y = std::min(start_y + pool_size, upper_bound_h);
81 if(exclude_padding)
82 {
83 start_y = std::max(0, start_y);
84 }
85
86 std::array<uint16_t, 8> elems =
87 {
88 {
89 vgetq_lane_u16(v, 0),
90 vgetq_lane_u16(v, 1),
91 vgetq_lane_u16(v, 2),
92 vgetq_lane_u16(v, 3),
93 vgetq_lane_u16(v, 4),
94 vgetq_lane_u16(v, 5),
95 vgetq_lane_u16(v, 6),
96 vgetq_lane_u16(v, 7),
97 }
98 };
99
100 for(auto &el : elems)
101 {
102 int c_start_x = start_x;
103 const int end_x = std::min(c_start_x + pool_size, upper_bound_w);
104 if(exclude_padding)
105 {
106 c_start_x = std::max(0, c_start_x);
107 }
108 float scale = 1.f / ((end_y - start_y) * (end_x - c_start_x));
109 el *= scale;
110 start_x += step * stride_x;
111 }
112
113 v = vsetq_lane_u16(elems[0], v, 0);
114 v = vsetq_lane_u16(elems[1], v, 1);
115 v = vsetq_lane_u16(elems[2], v, 2);
116 v = vsetq_lane_u16(elems[3], v, 3);
117 v = vsetq_lane_u16(elems[4], v, 4);
118 v = vsetq_lane_u16(elems[5], v, 5);
119 v = vsetq_lane_u16(elems[6], v, 6);
120 v = vsetq_lane_u16(elems[7], v, 7);
121}
122
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100123Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &pooled_w, unsigned int pooled_h)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000125 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000127 int pool_stride_x = 0;
128 int pool_stride_y = 0;
129 PoolingType pool_type = pool_info.pool_type();
130 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100132
Anthony Barbiereaefd002018-07-20 17:49:35 +0100133 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100134 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas55186712018-01-08 17:37:12 +0000135 ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(input->data_type()));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000136
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000137 if(output->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100138 {
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000139 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000140 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
141 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w)
142 || (output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h));
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100143 }
144
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000145 return Status{};
146}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000148Status validate_arguments_pool_info(const unsigned int pool_size_x, const unsigned int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000149{
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000150 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_x == 0);
151 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_y == 0);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000152
153 return Status{};
154}
155
156std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &num_elems_processed_per_iteration,
157 BorderSize &border_size,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000158 unsigned int pooled_w, unsigned int pooled_h, int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000159{
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +0100160 // Output auto inizialitation if not yet initialized
161 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_pool_shape(*input, pool_info)));
162
Michalis Spyrou57dac842018-03-01 16:03:50 +0000163 DataLayout data_layout = input->data_layout();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000164 unsigned int num_elems_read_per_iteration = 0;
165 unsigned int num_elems_horizontal_window = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000166 int pool_stride_x = 0;
167 int pool_stride_y = 0;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000168 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
169 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
170 const int input_width = input->dimension(idx_width);
171 const int input_height = input->dimension(idx_height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000172 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
173 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000174 const int pool_pad_right = pad_stride_info.pad_right();
175 const int pool_pad_top = pad_stride_info.pad_top();
176 const int pool_pad_left = pad_stride_info.pad_left();
177 const int pool_pad_bottom = pad_stride_info.pad_bottom();
178 const bool is_square = pool_size_x == pool_size_y;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000179
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000180 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +0000181 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
182 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000183 pool_size_x,
184 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000185 pad_stride_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000187 //If it's not squared and optimized will be executed the MxN
188 num_elems_read_per_iteration = 1;
189 num_elems_processed_per_iteration = 1;
190 num_elems_horizontal_window = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191
Michalis Spyrou57dac842018-03-01 16:03:50 +0000192 const bool is_nhwc = data_layout == DataLayout::NHWC;
193
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000194 if(is_square)
195 {
196 switch(input->data_type())
197 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000198 case DataType::QASYMM8:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000199 if(is_nhwc)
200 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100201 num_elems_processed_per_iteration = 16;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000202 break;
203 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000204 switch(pool_size_x)
205 {
206 case 2:
207 num_elems_read_per_iteration = 16;
208 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
209 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
210 break;
211 case 3:
212 num_elems_read_per_iteration = 16;
213 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
214 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
215 break;
216 default:
217 break;
218 }
219 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000220#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
221 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000222 if(is_nhwc)
223 {
224 num_elems_processed_per_iteration = 8;
225 break;
226 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000227 switch(pool_size_x)
228 {
229 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000230 case 3:
231 num_elems_read_per_iteration = 4;
232 num_elems_processed_per_iteration = 1;
233 num_elems_horizontal_window = 1;
234 break;
235 default:
236 break;
237 }
238 break;
239#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
240 case DataType::F32:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000241 if(is_nhwc)
242 {
Georgios Pinitas64f1a902018-09-18 13:42:51 +0100243 num_elems_processed_per_iteration = 4;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000244 break;
245 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000246 switch(pool_size_x)
247 {
248 case 2:
249 num_elems_read_per_iteration = 2;
250 break;
251 case 3:
252 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
253 break;
254 case 7:
255 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
256 break;
257 default:
258 break;
259 }
260 num_elems_processed_per_iteration = 1;
261 num_elems_horizontal_window = 1;
262 break;
263 default:
264 ARM_COMPUTE_ERROR("Element size not supported");
265 break;
266 }
267 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000268 else
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000269 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000270 if(is_nhwc)
271 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100272 num_elems_processed_per_iteration = 16 / input->element_size();
Michalis Spyrou57dac842018-03-01 16:03:50 +0000273 }
274 }
275
276 bool window_changed = false;
277 Window win{};
278 if(data_layout == DataLayout::NCHW)
279 {
280 // Number of iterations in X dimension
281 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
282
283 // Upper limit for the number of right/bottom border elements that are accessed
284 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;
285 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height;
286
287 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
288 border_size.right = std::max(upper_bound_w, pool_pad_right);
289 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
290
291 TensorShape output_shape{ input->tensor_shape() };
292 output_shape.set(0, pooled_w);
293 output_shape.set(1, pooled_h);
294 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
295
296 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
297 AccessWindowStatic input_access(input, -pool_pad_left, -pool_pad_top, input_width + border_size.right, input_height + border_size.bottom);
298
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000299 AccessWindowHorizontal output_access(output, 0, num_elems_horizontal_window);
300 window_changed = update_window_and_padding(win, input_access, output_access);
301 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
302 }
303 else
304 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000305 TensorShape output_shape{ input->tensor_shape() };
306 output_shape.set(1, pooled_w);
307 output_shape.set(2, pooled_h);
308 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
309
310 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
311 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
312
313 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
314 window_changed = update_window_and_padding(win, input_access, output_access);
315 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000316 }
317
318 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
319 return std::make_pair(err, win);
320}
321} // namespace
322
323NEPoolingLayerKernel::NEPoolingLayerKernel()
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000324 : _func(nullptr), _input(nullptr), _output(nullptr), _pool_info(), _num_elems_processed_per_iteration(0), _border_size(0), _is_square(false)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000325{
326}
327
328BorderSize NEPoolingLayerKernel::border_size() const
329{
330 return _border_size;
331}
332
333void NEPoolingLayerKernel::configure(const ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info)
334{
335 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
336
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000337 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000338 const bool is_global_pooling = pool_info.is_global_pooling();
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000339 const int pool_stride_x = pad_stride_info.stride().first;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000340
341 // Get data layout
342 const DataLayout data_layout = input->info()->data_layout();
343 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
344 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000345
346 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000347 const Size2D pool_size(
348 is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size().width,
349 is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size().height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000350
351 // Validate pool info before calling scaled_dimensions
Pablo Tello77e6c552018-12-04 15:33:49 +0000352 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_pool_info(pool_size.x(), pool_size.y()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000353
354 // Check output dimensions
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000355 unsigned int pooled_w, pooled_h;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000356 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(idx_width),
357 input->info()->dimension(idx_height),
Pablo Tello77e6c552018-12-04 15:33:49 +0000358 pool_size.x(),
359 pool_size.y(),
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000360 pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000361
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000362 // Perform validation step
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100363 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, pooled_w, pooled_h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100364
365 // Set instance variables
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000366 _input = input;
367 _output = output;
368 _pool_info = pool_info;
Pablo Tello77e6c552018-12-04 15:33:49 +0000369 _is_square = (pool_size.x() == pool_size.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100370
Georgios Pinitas55186712018-01-08 17:37:12 +0000371 // Get data type
372 const DataType data_type = input->info()->data_type();
Michalis Spyrou57dac842018-03-01 16:03:50 +0000373 const bool is_nchw = data_layout == DataLayout::NCHW;
Georgios Pinitas55186712018-01-08 17:37:12 +0000374
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100375 if(data_type == DataType::QASYMM8)
Georgios Pinitas55186712018-01-08 17:37:12 +0000376 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000377 if(pool_size.x() == 2 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000378 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000379 if(is_nchw)
Michalis Spyroubbd9fb92017-06-22 12:57:51 +0100380 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000381 _func = &NEPoolingLayerKernel::pooling2_qasymm8_nchw;
382 }
383 else
384 {
385 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc;
Georgios Pinitas55186712018-01-08 17:37:12 +0000386 }
387 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000388 else if(pool_size.x() == 3 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000389 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000390 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000391 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000392 _func = &NEPoolingLayerKernel::pooling3_qasymm8_nchw;
393 }
394 else
395 {
396 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc;
Georgios Pinitas55186712018-01-08 17:37:12 +0000397 }
398 }
399 else
400 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000401 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000402 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000403 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nchw;
404 }
405 else
406 {
407 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc;
Georgios Pinitas55186712018-01-08 17:37:12 +0000408 }
409 }
410 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000411 else if(data_type == DataType::F16)
412 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000413 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000414 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000415 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000416 {
417 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000418 {
419 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000420 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000421 _func = &NEPoolingLayerKernel::pooling2_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000422 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000423 else
424 {
425 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
426 }
427 }
428 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000429 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000430 {
431 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000432 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000433 _func = &NEPoolingLayerKernel::pooling3_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000434 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000435 else
436 {
437 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
438 }
439 }
440 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000441 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000442 {
443 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000444 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000445 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
446 }
447 else
448 {
449 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000450 }
451 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000452 }
453 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000454 }
455 }
456 else
457 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000458 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000459 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000460 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
461 }
462 else
463 {
464 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000465 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000466 }
467 }
468 else if(data_type == DataType::F32)
469 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000470 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000471 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000472 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000473 {
474 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000475 {
476 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000477 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000478 _func = &NEPoolingLayerKernel::pooling2_f32_nchw;
479 }
480 else
481 {
482 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000483 }
484 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000485 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000486 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000487 {
488 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000489 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000490 _func = &NEPoolingLayerKernel::pooling3_f32_nchw;
491 }
492 else
493 {
494 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000495 }
496 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000497 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000498 case 7:
Pablo Tello77e6c552018-12-04 15:33:49 +0000499 {
500 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000501 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000502 _func = &NEPoolingLayerKernel::pooling7_f32_nchw;
503 }
504 else
505 {
506 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000507 }
508 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000509 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000510 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000511 {
512 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000513 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000514 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
515 }
516 else
517 {
518 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000519 }
520 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000521 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000522 }
523 }
524 else
525 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000526 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000527 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000528 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
529 }
530 else
531 {
532 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000533 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000534 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100535 }
536
537 // Configure kernel window
Pablo Tello77e6c552018-12-04 15:33:49 +0000538 auto win_config = validate_and_configure_window(input->info(), output->info(), pool_info, _num_elems_processed_per_iteration, _border_size, pooled_w, pooled_h, pool_size.x(), pool_size.y());
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000539 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
540 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100541}
542
Pablo Tello77e6c552018-12-04 15:33:49 +0000543void NEPoolingLayerKernel::pooling2_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000544{
545 Iterator input(_input, window_input);
546 Iterator output(_output, window);
547
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000548 constexpr int pool_size = 2;
549 int pool_stride_x = 0;
550 int pool_stride_y = 0;
551 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
552 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
553 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
554 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
Georgios Pinitas55186712018-01-08 17:37:12 +0000555 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000556 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
557 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000558
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000559 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
560 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
Georgios Pinitas55186712018-01-08 17:37:12 +0000561
562 const int scale_step_x = (pool_stride_x == 1) ? 2 : 1;
563
564 execute_window_loop(window, [&](const Coordinates & id)
565 {
566 const auto top_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_top_ptr + input.offset()));
567 const auto bottom_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_bottom_ptr + input.offset()));
568 uint8x8_t lower_res = {};
569 uint8x8_t upper_res = {};
570
571 if(pooling_type != PoolingType::MAX)
572 {
573 const uint16x8x2_t top_data_u16 = { { vmovl_u8(vget_low_u8(top_data)), vmovl_u8(vget_high_u8(top_data)) } };
574 const uint16x8x2_t bottom_data_u16 = { { vmovl_u8(vget_low_u8(bottom_data)), vmovl_u8(vget_high_u8(bottom_data)) } };
575
576 // Add rows
577 const uint16x8x2_t vrsum =
578 {
579 {
580 vaddq_u16(top_data_u16.val[0], bottom_data_u16.val[0]),
581 vaddq_u16(top_data_u16.val[1], bottom_data_u16.val[1]),
582 }
583 };
584
585 // Pair-wise add row data
586 const uint16x4x2_t vpsum =
587 {
588 {
589 vpadd_u16(vget_low_u16(vrsum.val[0]), vget_high_u16(vrsum.val[0])),
590 vpadd_u16(vget_low_u16(vrsum.val[1]), vget_high_u16(vrsum.val[1])),
591 }
592 };
593
594 uint16x8_t res_lower = vcombine_u16(vpsum.val[0], vpsum.val[1]);
595
596 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000597 scale_vector_s16x8(exclude_padding, res_lower, id, 0, scale_step_x,
598 pool_size, upper_bound_w, upper_bound_h,
599 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000600 lower_res = vmovn_u16(res_lower);
601
602 // Compute upper result for stride_x == 1
603 if(pool_stride_x == 1)
604 {
605 // Shifted row sum
606 const uint16x8x2_t vrsum_shifted =
607 {
608 {
609 vextq_u16(vrsum.val[0], vrsum.val[1], 1),
610 vextq_u16(vrsum.val[1], vrsum.val[1], 1)
611 }
612 };
613
614 // Pair-wise add shifted row
615 const uint16x4x2_t vpsum_shifted =
616 {
617 {
618 vpadd_u16(vget_low_u16(vrsum_shifted.val[0]), vget_high_u16(vrsum_shifted.val[0])),
619 vpadd_u16(vget_low_u16(vrsum_shifted.val[1]), vget_high_u16(vrsum_shifted.val[1])),
620 }
621 };
622 uint16x8_t res_upper = vcombine_u16(vpsum_shifted.val[0], vpsum_shifted.val[1]);
623
624 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000625 scale_vector_s16x8(exclude_padding, res_upper, id, 1, 2,
626 pool_size, upper_bound_w, upper_bound_h,
627 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000628 upper_res = vmovn_u16(res_upper);
629 }
630 }
631 else
632 {
633 const uint8x16_t max_data = vmaxq_u8(top_data, bottom_data);
634 lower_res = vpmax_u8(vget_low_u8(max_data), vget_high_u8(max_data));
635 if(pool_stride_x == 1)
636 {
637 const uint8x16_t max_data_shifted = vextq_u8(max_data, max_data, 1);
638 upper_res = vpmax_u8(vget_low_u8(max_data_shifted), vget_high_u8(max_data_shifted));
639 }
640 }
641
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100642 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
643 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
644 if(input_qinfo != output_qinfo)
645 {
646 const auto requantized_output = vquantize(vdequantize(vcombine_u8(lower_res, upper_res), input_qinfo), output_qinfo);
647 lower_res = vget_low_u8(requantized_output);
648 upper_res = vget_high_u8(requantized_output);
649 }
650
Georgios Pinitas55186712018-01-08 17:37:12 +0000651 // Store result
652 if(pool_stride_x == 1)
653 {
654 const uint8x8x2_t res = { { lower_res, upper_res } };
655 vst2_u8(reinterpret_cast<uint8_t *>(output.ptr()), res);
656 }
657 else
658 {
659 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), lower_res);
660 }
661 },
662 input, output);
663}
664
Pablo Tello77e6c552018-12-04 15:33:49 +0000665void NEPoolingLayerKernel::pooling3_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100666{
Pablo Tello77e6c552018-12-04 15:33:49 +0000667 ARM_COMPUTE_UNUSED(pooling_type);
668 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000669#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100670 Iterator input(_input, window_input);
671 Iterator output(_output, window);
672
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000673 constexpr const int pool_size = 3;
674 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
675 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
676 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
677 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
678 int pool_stride_x = 0;
679 int pool_stride_y = 0;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100680 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000681 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
682 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100683
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000684 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
685 const unsigned char *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
686 const unsigned char *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 2));
Pablo Tello0c34fe22017-06-26 17:17:42 +0100687
688 execute_window_loop(window, [&](const Coordinates & id)
689 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100690 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
691 float16x4_t middle_data = vld1_f16(reinterpret_cast<const float16_t *>(input_middle_ptr + input.offset()));
692 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
693 float16x4_t res = {};
694
695 // Get power of 2 in case of l2 pooling
696 if(pooling_type == PoolingType::L2)
697 {
698 top_data = vmul_f16(top_data, top_data);
699 middle_data = vmul_f16(middle_data, middle_data);
700 bottom_data = vmul_f16(bottom_data, bottom_data);
701 }
702
703 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100704 {
705 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000706 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100707 const float16x4_t scale_v = vdup_n_f16(scale);
708 // Perform pooling
709 const float16x4_t sum_data = vadd_f16(vadd_f16(top_data, bottom_data), middle_data);
710 res = vpadd_f16(vset_lane_f16(0.f, sum_data, 3), sum_data);
711 res = vmul_f16(vpadd_f16(res, res), scale_v);
712 }
713 else
714 {
715 const float16x4_t max_data = vmax_f16(vmax_f16(top_data, bottom_data), middle_data);
716 res = vpmax_f16(vset_lane_f16(-std::numeric_limits<float>::max(), max_data, 3), max_data);
717 res = vpmax_f16(res, res);
718 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100719
720 // Calculate square-root in case of l2 pooling
721 if(pooling_type == PoolingType::L2)
722 {
723 res = vinv_f16(vinvsqrt_f16(res));
724 }
725
Pablo Tello0c34fe22017-06-26 17:17:42 +0100726 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
727 },
728 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000729#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100730 ARM_COMPUTE_UNUSED(window_input);
731 ARM_COMPUTE_UNUSED(window);
732 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000733#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100734}
735
Pablo Tello77e6c552018-12-04 15:33:49 +0000736void NEPoolingLayerKernel::pooling2_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100737{
Pablo Tello77e6c552018-12-04 15:33:49 +0000738 ARM_COMPUTE_UNUSED(pooling_type);
739 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000740#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100741 Iterator input(_input, window_input);
742 Iterator output(_output, window);
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000743 constexpr int pool_size = 2;
744 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
745 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
746 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
747 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
748 int pool_stride_x, pool_stride_y = 0;
749 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
750 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
751 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100752
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000753 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
754 const unsigned char *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
Pablo Tello0c34fe22017-06-26 17:17:42 +0100755
756 execute_window_loop(window, [&](const Coordinates & id)
757 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100758 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
759 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
760 float16x4_t res = {};
Pablo Tello0c34fe22017-06-26 17:17:42 +0100761
Georgios Pinitascdf51452017-08-31 14:21:36 +0100762 // Get power of 2 in case of l2 pooling
763 if(pooling_type == PoolingType::L2)
764 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100765 top_data = vmul_f16(top_data, top_data);
766 bottom_data = vmul_f16(bottom_data, bottom_data);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100767 }
768
769 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100770 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000771 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100772 const float16x4_t scale_v = vdup_n_f16(scale);
773
774 const float16x4_t sum_data = vadd_f16(top_data, bottom_data);
775 res = vmul_f16(vpadd_f16(sum_data, sum_data), scale_v);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100776 }
777 else
778 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100779 const float16x4_t max_data = vmax_f16(top_data, bottom_data);
780 res = vpmax_f16(max_data, max_data);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100781 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100782
783 // Calculate square-root in case of l2 pooling
784 if(pooling_type == PoolingType::L2)
785 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100786 res = vinv_f16(vinvsqrt_f16(res));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100787 }
788
789 // Store result
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100790 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100791 },
792 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000793#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100794 ARM_COMPUTE_UNUSED(window_input);
795 ARM_COMPUTE_UNUSED(window);
796 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000797#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100798}
799
Pablo Tello77e6c552018-12-04 15:33:49 +0000800void NEPoolingLayerKernel::pooling3_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000801{
802 Iterator input(_input, window_input);
803 Iterator output(_output, window);
804
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000805 constexpr int pool_size = 3;
806 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
807 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
808 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
809 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
810 int pool_stride_x = 0;
811 int pool_stride_y = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +0000812 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000813 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
814 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000815
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100816 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
817 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
818
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000819 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
820 const uint8_t *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
821 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 2));
Georgios Pinitas55186712018-01-08 17:37:12 +0000822
823 execute_window_loop(window, [&](const Coordinates & id)
824 {
825 const auto top_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_top_ptr + input.offset()));
826 const auto middle_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_middle_ptr + input.offset()));
827 const auto bottom_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_bottom_ptr + input.offset()));
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100828 uint8x8_t fres = {};
829 uint8x16_t fqres = {};
Georgios Pinitas55186712018-01-08 17:37:12 +0000830
831 if(pooling_type == PoolingType::AVG)
832 {
833 // Convert data to u16
834 const uint16x8x2_t top_data_u16 = { { vmovl_u8(vget_low_u8(top_data)), vmovl_u8(vget_high_u8(top_data)) } };
835 const uint16x8x2_t middle_data_u16 = { { vmovl_u8(vget_low_u8(middle_data)), vmovl_u8(vget_high_u8(middle_data)) } };
836 const uint16x8x2_t bottom_data_u16 = { { vmovl_u8(vget_low_u8(bottom_data)), vmovl_u8(vget_high_u8(bottom_data)) } };
837
838 // Calculate row sums
839 const uint16x8x2_t vrsum =
840 {
841 {
842 vaddq_u16(vaddq_u16(top_data_u16.val[0], bottom_data_u16.val[0]), middle_data_u16.val[0]),
843 vaddq_u16(vaddq_u16(top_data_u16.val[1], bottom_data_u16.val[1]), middle_data_u16.val[1]),
844 }
845 };
846 const uint16x8x2_t vrsum_shifted_1 =
847 {
848 {
849 vextq_u16(vrsum.val[0], vrsum.val[1], 1),
850 vextq_u16(vrsum.val[1], vrsum.val[1], 1)
851 }
852 };
853 const uint16x8x2_t vrsum_shifted_2 =
854 {
855 {
856 vextq_u16(vrsum.val[0], vrsum.val[1], 2),
857 vextq_u16(vrsum.val[1], vrsum.val[1], 2)
858 }
859 };
860 // Calculate final sum
861 uint16x8x2_t final_sum =
862 {
863 {
864 vaddq_u16(vaddq_u16(vrsum.val[0], vrsum_shifted_1.val[0]), vrsum_shifted_2.val[0]),
865 vaddq_u16(vaddq_u16(vrsum.val[1], vrsum_shifted_1.val[1]), vrsum_shifted_2.val[1]),
866 }
867 };
868 if(pool_stride_x == 2)
869 {
870 uint16x8_t res =
871 {
872 vgetq_lane_u16(final_sum.val[0], 0),
873 vgetq_lane_u16(final_sum.val[0], 2),
874 vgetq_lane_u16(final_sum.val[0], 4),
875 vgetq_lane_u16(final_sum.val[0], 6),
876 vgetq_lane_u16(final_sum.val[1], 0),
877 vgetq_lane_u16(final_sum.val[1], 2),
878 vgetq_lane_u16(final_sum.val[1], 4),
879 vgetq_lane_u16(final_sum.val[1], 6),
880 };
881
Pablo Tello77e6c552018-12-04 15:33:49 +0000882 scale_vector_s16x8(exclude_padding, res, id, 0, 1,
883 pool_size, upper_bound_w, upper_bound_h,
884 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100885 fres = vmovn_u16(res);
Georgios Pinitas55186712018-01-08 17:37:12 +0000886 }
887 else
888 {
889 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000890 scale_vector_s16x8(exclude_padding, final_sum.val[0], id, 0, 1,
891 pool_size, upper_bound_w, upper_bound_h,
892 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000893 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000894 scale_vector_s16x8(exclude_padding, final_sum.val[1], id, 8, 1,
895 pool_size, upper_bound_w, upper_bound_h,
896 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100897 fqres = vcombine_u8(vmovn_u16(final_sum.val[0]), vmovn_u16(final_sum.val[1]));
Georgios Pinitas55186712018-01-08 17:37:12 +0000898 }
899 }
900 else
901 {
902 const uint8x16_t max_data = vmaxq_u8(vmaxq_u8(top_data, bottom_data), middle_data);
903 const uint8x16_t max_data_shift1 = vextq_u8(max_data, max_data, 1);
904 const uint8x16_t max_data_shift2 = vextq_u8(max_data, max_data, 2);
905 const uint8x16_t final_max = vmaxq_u8(vmaxq_u8(max_data, max_data_shift1), max_data_shift2);
906
907 if(pool_stride_x == 2)
908 {
909 const uint8x8x2_t table = { { vget_low_u8(final_max), vget_high_u8(final_max) } };
910 static const uint8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 };
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100911 fres = vtbl2_u8(table, lookup_val);
Georgios Pinitas55186712018-01-08 17:37:12 +0000912 }
913 else
914 {
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100915 fqres = final_max;
Georgios Pinitas55186712018-01-08 17:37:12 +0000916 }
917 }
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100918
919 // Store result
920 if(pool_stride_x == 1)
921 {
922 if(input_qinfo != output_qinfo)
923 {
924 fqres = vquantize(vdequantize(fqres, input_qinfo), output_qinfo);
925 }
926 vst1q_u8(reinterpret_cast<uint8_t *>(output.ptr()), fqres);
927 }
928 else
929 {
930 if(input_qinfo != output_qinfo)
931 {
932 fres = vquantize(vdequantize(fres, input_qinfo), output_qinfo);
933 }
934 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), fres);
935 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000936 },
937 input, output);
938}
939
Pablo Tello77e6c552018-12-04 15:33:49 +0000940void NEPoolingLayerKernel::poolingMxN_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100941{
Pablo Tello77e6c552018-12-04 15:33:49 +0000942 ARM_COMPUTE_UNUSED(pooling_type);
943 ARM_COMPUTE_UNUSED(exclude_padding);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000944#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
945 Iterator input(_input, window_input);
946 Iterator output(_output, window);
947
948 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
949 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height;
950 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
951 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
952 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
953 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
954 int pool_stride_x = 0;
955 int pool_stride_y = 0;
956 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
957 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
958 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
959
960 execute_window_loop(window, [&](const Coordinates & id)
961 {
962 float16_t res = 0.0f;
963 float16x8_t vres = vdupq_n_f16(0.0f);
964
965 if(pooling_type != PoolingType::MAX)
966 {
967 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000968 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000969
970 // Perform pooling
971
972 for(int y = 0; y < pool_size_y; ++y)
973 {
974 int x = 0;
975 for(; x <= (pool_size_x - 8); x += 8)
976 {
977 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
978 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
979
980 // Get power of 2 in case of l2 pooling and accumulate
981 if(pooling_type == PoolingType::L2)
982 {
983 vres = vaddq_f16(vres, vmulq_f16(data, data));
984 }
985 else
986 {
987 vres = vaddq_f16(vres, data);
988 }
989 }
990
991 // Leftover for loop
992 for(; x < pool_size_x; ++x)
993 {
994 float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
995
996 // Get power of 2 in case of l2 pooling
997 if(pooling_type == PoolingType::L2)
998 {
999 data *= data;
1000 }
1001
1002 res += data;
1003 }
1004 }
1005
1006 // Reduction
1007 float16x4_t tmp = vpadd_f16(vget_high_f16(vres), vget_low_f16(vres));
1008 res += vget_lane_f16(tmp, 0);
1009 res += vget_lane_f16(tmp, 1);
1010 res += vget_lane_f16(tmp, 2);
1011 res += vget_lane_f16(tmp, 3);
1012
1013 // Divide by scale
1014 res *= scale;
1015 }
1016 else
1017 {
1018 float16x8_t vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
1019 res = std::numeric_limits<float>::lowest();
1020
1021 for(int y = 0; y < pool_size_y; ++y)
1022 {
1023 int x = 0;
1024 for(; x <= (pool_size_x - 8); x += 8)
1025 {
1026 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1027 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
1028 vres = vmaxq_f16(vres, data);
1029 }
1030
1031 // Leftover for loop
1032 for(; x < pool_size_x; ++x)
1033 {
1034 const float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
1035 res = std::max(res, data);
1036 }
1037 }
1038
1039 float16x4_t tmp = vpmax_f16(vget_high_f16(vres), vget_low_f16(vres));
1040 res = std::max(res, vget_lane_f16(tmp, 0));
1041 res = std::max(res, vget_lane_f16(tmp, 1));
1042 res = std::max(res, vget_lane_f16(tmp, 2));
1043 res = std::max(res, vget_lane_f16(tmp, 3));
1044 }
1045
1046 // Calculate square-root in case of l2 pooling
1047 if(pooling_type == PoolingType::L2)
1048 {
1049 res = std::sqrt(res);
1050 }
1051
1052 // Store result
1053 *(reinterpret_cast<float16_t *>(output.ptr())) = res;
1054 },
1055 input, output);
1056
1057#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1058 ARM_COMPUTE_UNUSED(window_input);
1059 ARM_COMPUTE_UNUSED(window);
1060 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1061#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1062}
1063
Pablo Tello77e6c552018-12-04 15:33:49 +00001064void NEPoolingLayerKernel::poolingMxN_f16_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001065{
Pablo Tello77e6c552018-12-04 15:33:49 +00001066 ARM_COMPUTE_UNUSED(pooling_type);
1067 ARM_COMPUTE_UNUSED(exclude_padding);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001068#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1069 Iterator input(_input, window_input);
1070 Iterator output(_output, window);
1071
1072 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1073 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1074 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1075 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1076 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1077 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1078 int pool_stride_x = 0;
1079 int pool_stride_y = 0;
1080 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1081 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1082 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1083
1084 float16x8_t vres;
1085
1086 execute_window_loop(window, [&](const Coordinates & id)
1087 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001088 const int idx_width = id.y() * pool_stride_x;
1089 const int idx_height = id.z() * pool_stride_y;
1090 const int pool_limit_y = pool_pad_top - idx_height;
1091 const int pool_limit_x = pool_pad_left - idx_width;
1092
1093 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1094 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1095 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1096 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1097
Michalis Spyrou57dac842018-03-01 16:03:50 +00001098 if(pooling_type != PoolingType::MAX)
1099 {
1100 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001101 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x,
1102 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001103 const float16x8_t scale_v = vdupq_n_f16(scale);
1104
1105 // Perform pooling
1106 vres = vdupq_n_f16(0.0f);
Michalis Spyrouced25572018-10-01 16:26:20 +01001107 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001108 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001109 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001110 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001111 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1112 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1113
1114 // Get power of 2 in case of l2 pooling and accumulate
1115 if(pooling_type == PoolingType::L2)
1116 {
1117 vres = vaddq_f16(vres, vmulq_f16(data, data));
1118 }
1119 else
1120 {
1121 vres = vaddq_f16(vres, data);
1122 }
1123 }
1124 }
1125 // Divide by scale
1126 vres = vmulq_f16(vres, scale_v);
1127 }
1128 else
1129 {
1130 vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001131
1132 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001133 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001134 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001135 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001136 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1137 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1138 vres = vmaxq_f16(vres, data);
1139 }
1140 }
1141 }
1142
1143 // Calculate square-root in case of l2 pooling
1144 if(pooling_type == PoolingType::L2)
1145 {
1146 float16x8_t sqrt_reciprocal = vrsqrteq_f16(vres);
1147 vres = vmulq_f16(vres, vmulq_f16(vrsqrtsq_f16(vmulq_f16(vres, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal));
1148 }
1149
1150 // Store result
1151 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vres);
1152 },
1153 input, output);
1154
1155#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1156 ARM_COMPUTE_UNUSED(window_input);
1157 ARM_COMPUTE_UNUSED(window);
1158 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1159#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1160}
1161
Pablo Tello77e6c552018-12-04 15:33:49 +00001162void NEPoolingLayerKernel::poolingMxN_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001163{
1164 Iterator input(_input, window_input);
1165 Iterator output(_output, window);
1166
1167 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
1168 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height;
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001169 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1170 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1171 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1172 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1173 int pool_stride_x = 0;
1174 int pool_stride_y = 0;
Gian Marco Iodice16824302017-09-28 15:41:37 +01001175 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001176 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1177 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Gian Marco Iodice16824302017-09-28 15:41:37 +01001178
1179 execute_window_loop(window, [&](const Coordinates & id)
1180 {
1181 float res = 0.0f;
1182
1183 if(pooling_type != PoolingType::MAX)
1184 {
1185 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001186 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Gian Marco Iodice16824302017-09-28 15:41:37 +01001187
1188 // Perform pooling
1189 float32x4_t vres = vdupq_n_f32(0.0f);
1190
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001191 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001192 {
1193 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001194 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001195 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001196 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1197 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001198
1199 // Get power of 2 in case of l2 pooling and accumulate
1200 if(pooling_type == PoolingType::L2)
1201 {
1202 vres = vmlaq_f32(vres, data, data);
1203 }
1204 else
1205 {
1206 vres = vaddq_f32(vres, data);
1207 }
1208 }
1209
1210 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001211 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001212 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001213 float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001214
1215 // Get power of 2 in case of l2 pooling
1216 if(pooling_type == PoolingType::L2)
1217 {
1218 data *= data;
1219 }
1220
1221 res += data;
1222 }
1223 }
1224
1225#if defined(__aarch64__)
1226 // Reduction operation available on 64 bit architectures only
1227 res += vaddvq_f32(vres);
1228#else // __aarch64__
1229 // Reduction
1230 float32x2_t tmp = vpadd_f32(vget_high_f32(vres), vget_low_f32(vres));
1231 tmp = vpadd_f32(tmp, tmp);
1232
1233 res += vget_lane_f32(tmp, 0);
1234#endif // __aarch64__
1235 // Divide by scale
1236 res *= scale;
1237 }
1238 else
1239 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001240 float32x4_t vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
1241 res = std::numeric_limits<float>::lowest();
Gian Marco Iodice16824302017-09-28 15:41:37 +01001242
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001243 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001244 {
1245 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001246 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001247 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001248 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1249 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001250 vres = vmaxq_f32(vres, data);
1251 }
1252
1253 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001254 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001255 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001256 const float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001257 res = std::max(res, data);
1258 }
1259 }
1260
1261#if defined(__aarch64__)
1262 // Reduction operation available on 64 bit architectures only
1263 res = std::max(vmaxvq_f32(vres), res);
1264#else // __aarch64__
1265 float32x2_t tmp = vpmax_f32(vget_high_f32(vres), vget_low_f32(vres));
1266 tmp = vpmax_f32(tmp, tmp);
1267
1268 res = std::max(res, vget_lane_f32(tmp, 0));
1269#endif // __aarch64__
1270 }
1271
1272 // Calculate square-root in case of l2 pooling
1273 if(pooling_type == PoolingType::L2)
1274 {
1275 res = std::sqrt(res);
1276 }
1277
1278 // Store result
1279 *(reinterpret_cast<float *>(output.ptr())) = res;
1280 },
1281 input, output);
1282}
1283
Pablo Tello77e6c552018-12-04 15:33:49 +00001284void NEPoolingLayerKernel::pooling2_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1285{
1286 Iterator input(_input, window_input);
1287 Iterator output(_output, window);
1288
1289 constexpr int pool_size = 2;
1290 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1291 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1292 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1293 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1294 int pool_stride_x = 0;
1295 int pool_stride_y = 0;
1296 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1297 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1298 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1299
1300 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1301 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
1302
1303 execute_window_loop(window, [&](const Coordinates & id)
1304 {
1305 float32x2_t top_data = vld1_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1306 float32x2_t bottom_data = vld1_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1307 float32x2_t res = {};
1308 float final_res = 0;
1309
1310 // Get power of 2 in case of l2 pooling
1311 if(pooling_type == PoolingType::L2)
1312 {
1313 top_data = vmul_f32(top_data, top_data);
1314 bottom_data = vmul_f32(bottom_data, bottom_data);
1315 }
1316
1317 if(pooling_type != PoolingType::MAX)
1318 {
1319 // Calculate scale
1320 float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
1321 const float32x2_t scale_v = vdup_n_f32(scale);
1322
1323 // Perform pooling
1324 const float32x2_t sum_data = vadd_f32(top_data, bottom_data);
1325 res = vmul_f32(vpadd_f32(sum_data, sum_data), scale_v);
1326 }
1327 else
1328 {
1329 const float32x2_t max_data = vmax_f32(top_data, bottom_data);
1330 res = vpmax_f32(max_data, max_data);
1331 }
1332 final_res = vget_lane_f32(res, 0);
1333
1334 // Calculate square-root in case of l2 pooling
1335 if(pooling_type == PoolingType::L2)
1336 {
1337 final_res = sqrt(final_res);
1338 }
1339
1340 // Store result
1341 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1342 },
1343 input, output);
1344}
1345
1346void NEPoolingLayerKernel::pooling3_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1347{
1348 Iterator input(_input, window_input);
1349 Iterator output(_output, window);
1350
1351 constexpr const int pool_size = 3;
1352 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1353 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1354 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1355 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1356 int pool_stride_x = 0;
1357 int pool_stride_y = 0;
1358 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1359 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1360 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1361
1362 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1363 const uint8_t *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
1364 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 2));
1365
1366 execute_window_loop(window, [&](const Coordinates & id)
1367 {
1368 float32x4_t top_data = vld1q_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1369 float32x4_t middle_data = vld1q_f32(reinterpret_cast<const float *>(input_middle_ptr + input.offset()));
1370 float32x4_t bottom_data = vld1q_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1371 float32x2_t res = {};
1372 float final_res = 0;
1373
1374 // Get power of 2 in case of l2 pooling
1375 if(pooling_type == PoolingType::L2)
1376 {
1377 top_data = vmulq_f32(top_data, top_data);
1378 middle_data = vmulq_f32(middle_data, middle_data);
1379 bottom_data = vmulq_f32(bottom_data, bottom_data);
1380 }
1381
1382 if(pooling_type != PoolingType::MAX)
1383 {
1384 // Calculate scale
1385 float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
1386 const float32x2_t scale_v = vdup_n_f32(scale);
1387
1388 // Perform pooling
1389 const float32x4_t sum_data = vaddq_f32(vaddq_f32(top_data, bottom_data), middle_data);
1390 res = vpadd_f32(vget_high_f32(vsetq_lane_f32(0.f, sum_data, 3)), vget_low_f32(sum_data));
1391 res = vmul_f32(vpadd_f32(res, res), scale_v);
1392 }
1393 else
1394 {
1395 const float32x4_t max_data = vmaxq_f32(vmaxq_f32(top_data, bottom_data), middle_data);
1396 res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data, 3)), vget_low_f32(max_data));
1397 res = vpmax_f32(res, res);
1398 }
1399 final_res = vget_lane_f32(res, 0);
1400
1401 // Calculate square-root in case of l2 pooling
1402 if(pooling_type == PoolingType::L2)
1403 {
1404 final_res = sqrt(final_res);
1405 }
1406
1407 // Store result
1408 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1409 },
1410 input, output);
1411}
1412
1413void NEPoolingLayerKernel::pooling7_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1414{
1415 Iterator input(_input, window_input);
1416 Iterator output(_output, window);
1417
1418 constexpr const int pool_size = 7;
1419 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1420 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1421 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1422 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1423 int pool_stride_x = 0;
1424 int pool_stride_y = 0;
1425 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1426 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1427 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1428
1429 std::array<const uint8_t *, pool_size> input_ptrs{ {} };
1430 for(int i = 0; i < pool_size; ++i)
1431 {
1432 input_ptrs[i] = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + i));
1433 }
1434
1435 execute_window_loop(window, [&](const Coordinates & id)
1436 {
1437 float32x2_t res = {};
1438 float final_res = 0.f;
1439 if(pooling_type != PoolingType::MAX)
1440 {
1441 // Calculate scale
1442 float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
1443 const float32x2_t scale_v = vdup_n_f32(scale);
1444
1445 // Perform pooling
1446 float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1447 // Get power of 2 in case of l2 pooling
1448 if(pooling_type == PoolingType::L2)
1449 {
1450 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1451 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1452 }
1453 float32x4_t sum_data = vaddq_f32(data.val[0], vsetq_lane_f32(0.f, data.val[1], 3));
1454 for(int i = 1; i < pool_size; ++i)
1455 {
1456 data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1457 // Get power of 2 in case of l2 pooling
1458 if(pooling_type == PoolingType::L2)
1459 {
1460 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1461 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1462 }
1463 sum_data = vaddq_f32(sum_data, data.val[0]);
1464 sum_data = vaddq_f32(sum_data, vsetq_lane_f32(0.f, data.val[1], 3));
1465 }
1466 res = vpadd_f32(vget_high_f32(sum_data), vget_low_f32(sum_data));
1467 res = vmul_f32(vpadd_f32(res, res), scale_v);
1468 }
1469 else
1470 {
1471 float32x4x2_t max_data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1472 for(int i = 1; i < pool_size; ++i)
1473 {
1474 const float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1475 max_data = vmax2q_f32(max_data, data);
1476 }
1477 res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data.val[1], 3)), vget_low_f32(max_data.val[1]));
1478 res = vpmax_f32(res, vpmax_f32(vget_high_f32(max_data.val[0]), vget_low_f32(max_data.val[0])));
1479 res = vpmax_f32(res, res);
1480 }
1481 final_res = vget_lane_f32(res, 0);
1482
1483 // Calculate square-root in case of l2 pooling
1484 if(pooling_type == PoolingType::L2)
1485 {
1486 final_res = sqrt(final_res);
1487 }
1488
1489 // Store result
1490 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1491 },
1492 input, output);
1493}
1494
1495void NEPoolingLayerKernel::poolingMxN_f32_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001496{
1497 Iterator input(_input, window_input);
1498 Iterator output(_output, window);
1499
1500 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1501 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1502 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1503 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1504 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1505 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1506 int pool_stride_x = 0;
1507 int pool_stride_y = 0;
1508 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1509 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1510 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1511
1512 float32x4_t vres;
1513
1514 execute_window_loop(window, [&](const Coordinates & id)
1515 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001516 const int idx_width = id.y() * pool_stride_x;
1517 const int idx_height = id.z() * pool_stride_y;
1518 const int pool_limit_y = pool_pad_top - idx_height;
1519 const int pool_limit_x = pool_pad_left - idx_width;
1520
1521 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1522 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1523 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1524 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1525
Michalis Spyrou57dac842018-03-01 16:03:50 +00001526 if(pooling_type != PoolingType::MAX)
1527 {
1528 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001529 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x,
1530 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001531 const float32x4_t scale_v = vdupq_n_f32(scale);
1532
1533 // Perform pooling
1534 vres = vdupq_n_f32(0.0f);
1535
Michalis Spyrouced25572018-10-01 16:26:20 +01001536 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001537 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001538 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001539 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001540 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1541 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1542
1543 // Get power of 2 in case of l2 pooling and accumulate
1544 if(pooling_type == PoolingType::L2)
1545 {
1546 vres = vmlaq_f32(vres, data, data);
1547 }
1548 else
1549 {
1550 vres = vaddq_f32(vres, data);
1551 }
1552 }
1553 }
1554 // Divide by scale
1555 vres = vmulq_f32(vres, scale_v);
1556 }
1557 else
1558 {
1559 vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001560 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001561 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001562 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001563 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001564 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1565 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1566 vres = vmaxq_f32(vres, data);
1567 }
1568 }
1569 }
1570
1571 // Calculate square-root in case of l2 pooling
1572 if(pooling_type == PoolingType::L2)
1573 {
Georgios Pinitas027ce5b2018-11-08 18:55:36 +00001574 vres = vmulq_f32(vres, vinvsqrtq_f32(vres));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001575 }
1576
1577 // Store result
1578 vst1q_f32(reinterpret_cast<float *>(output.ptr()), vres);
1579 },
1580 input, output);
1581}
1582
Pablo Tello77e6c552018-12-04 15:33:49 +00001583void NEPoolingLayerKernel::poolingMxN_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +00001584{
1585 Iterator input(_input, window_input);
1586 Iterator output(_output, window);
1587
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001588 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
1589 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height;
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001590 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1591 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1592 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1593 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1594 int pool_stride_x = 0;
1595 int pool_stride_y = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +00001596 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001597 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1598 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +00001599
1600 execute_window_loop(window, [&](const Coordinates & id)
1601 {
1602 uint8_t res = 0;
1603
1604 if(pooling_type != PoolingType::MAX)
1605 {
1606 uint32x4_t vres = vdupq_n_u32(0);
1607 uint32_t sres = 0;
1608
1609 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001610 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +00001611
1612 // Perform pooling
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001613 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001614 {
1615 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001616 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001617 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001618 const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1619 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001620
1621 const uint16x8_t data_u16 = vmovl_u8(data);
1622 vres = vaddq_u32(vres, vaddl_u16(vget_high_u16(data_u16), vget_low_u16(data_u16)));
1623 }
1624
1625 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001626 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001627 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001628 uint8_t data = *(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001629 sres += data;
1630 }
1631 }
1632
1633 // Reduction
1634 const auto tmp = vpadd_u32(vget_high_u32(vres), vget_low_u32(vres));
1635 sres += vget_lane_u32(tmp, 0) + vget_lane_u32(tmp, 1);
1636
1637 // Divide by scale
1638 res = static_cast<uint8_t>(support::cpp11::round(sres * scale));
1639 }
1640 else
1641 {
1642 uint8x8_t vres = vdup_n_u8(0);
1643 res = 0;
1644
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001645 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001646 {
1647 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001648 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001649 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001650 const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1651 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001652 vres = vmax_u8(vres, data);
1653 }
1654
1655 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001656 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001657 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001658 const uint8_t data = *(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001659 res = std::max(res, data);
1660 }
1661 }
1662
1663 // Reduce max
1664 vres = vpmax_u8(vres, vres);
1665 vres = vpmax_u8(vres, vres);
1666 vres = vpmax_u8(vres, vres);
1667
1668 // Get max value
1669 res = std::max(res, vget_lane_u8(vres, 0));
1670 }
1671
1672 // Store result
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001673 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
1674 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
1675 res = (input_qinfo != output_qinfo) ? sqcvt_qasymm8_f32(scvt_f32_qasymm8(res, input_qinfo.scale, input_qinfo.offset), output_qinfo.scale,
1676 output_qinfo.offset) :
1677 res;
Georgios Pinitas55186712018-01-08 17:37:12 +00001678 *(reinterpret_cast<uint8_t *>(output.ptr())) = res;
1679 },
1680 input, output);
1681}
1682
Pablo Tello77e6c552018-12-04 15:33:49 +00001683void NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001684{
1685 Iterator input(_input, window_input);
1686 Iterator output(_output, window);
1687
1688 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1689 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1690 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1691 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1692 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1693 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1694 int pool_stride_x = 0;
1695 int pool_stride_y = 0;
1696 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1697 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1698 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1699
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001700 const float32x4_t half_scale_v = vdupq_n_f32(0.5f);
1701 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
1702 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
Georgios Pinitas283fc602018-11-09 10:46:43 +00001703
Michalis Spyrou57dac842018-03-01 16:03:50 +00001704 execute_window_loop(window, [&](const Coordinates & id)
1705 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001706 const int idx_width = id.y() * pool_stride_x;
1707 const int idx_height = id.z() * pool_stride_y;
1708 const int pool_limit_y = pool_pad_top - idx_height;
1709 const int pool_limit_x = pool_pad_left - idx_width;
1710
1711 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1712 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1713 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1714 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1715
Michalis Spyrou57dac842018-03-01 16:03:50 +00001716 if(pooling_type != PoolingType::MAX)
1717 {
1718 uint32x4_t vres1 = vdupq_n_u32(0);
1719 uint32x4_t vres2 = vdupq_n_u32(0);
Michalis Spyrouced25572018-10-01 16:26:20 +01001720 uint32x4_t vres3 = vdupq_n_u32(0);
1721 uint32x4_t vres4 = vdupq_n_u32(0);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001722
1723 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001724 const float scale = calculate_avg_scale(exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x,
1725 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001726 const float32x4_t scale_v = vdupq_n_f32(scale);
1727
1728 // Perform pooling
Michalis Spyrouced25572018-10-01 16:26:20 +01001729 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001730 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001731 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001732 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001733 const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1734 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001735
Michalis Spyrouced25572018-10-01 16:26:20 +01001736 const uint16x8_t data_u16 = vmovl_u8(vget_low_u8(data));
1737 const uint16x8_t data2_u16 = vmovl_u8(vget_high_u8(data));
1738 vres1 = vaddq_u32(vres1, vmovl_u16(vget_low_u16(data_u16)));
1739 vres2 = vaddq_u32(vres2, vmovl_u16(vget_high_u16(data_u16)));
1740 vres3 = vaddq_u32(vres3, vmovl_u16(vget_low_u16(data2_u16)));
1741 vres4 = vaddq_u32(vres4, vmovl_u16(vget_high_u16(data2_u16)));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001742 }
1743 }
Georgios Pinitas283fc602018-11-09 10:46:43 +00001744 // Divide by scale and add 0.5f to round to nearest instead of rounding towards zero
1745 vres1 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres1), scale_v));
1746 vres2 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres2), scale_v));
1747 vres3 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres3), scale_v));
1748 vres4 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres4), scale_v));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001749
Michalis Spyrouced25572018-10-01 16:26:20 +01001750 uint8x8_t res1 = vmovn_u16(vcombine_u16(vmovn_u32(vres1), vmovn_u32(vres2)));
1751 uint8x8_t res2 = vmovn_u16(vcombine_u16(vmovn_u32(vres3), vmovn_u32(vres4)));
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001752 if(input_qinfo != output_qinfo)
1753 {
1754 const auto requantized_output = vquantize(vdequantize(vcombine_u8(res1, res2), input_qinfo), output_qinfo);
1755 res1 = vget_low_u8(requantized_output);
1756 res2 = vget_high_u8(requantized_output);
1757 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001758
1759 // Store result
Michalis Spyrouced25572018-10-01 16:26:20 +01001760 vst1_u8(output.ptr(), res1);
1761 vst1_u8(output.ptr() + 8, res2);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001762 }
1763 else
1764 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001765 uint8x16_t vres = vdupq_n_u8(0);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001766
Michalis Spyrouced25572018-10-01 16:26:20 +01001767 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001768 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001769 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001770 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001771 const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1772 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1773 vres = vmaxq_u8(vres, data);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001774 }
1775 }
1776
1777 // Store result
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001778 vst1q_u8(output.ptr(), (input_qinfo != output_qinfo) ? vquantize(vdequantize(vres, input_qinfo), output_qinfo) : vres);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001779 }
1780 },
1781 input, output);
1782}
1783
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001784Status NEPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
1785{
1786 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
1787
1788 unsigned int pooled_w = 0;
1789 unsigned int pooled_h = 0;
1790 unsigned int num_elems_processed_per_iteration = 0;
1791 BorderSize border_size(0);
1792
Michalis Spyrou57dac842018-03-01 16:03:50 +00001793 const bool is_global_pooling = pool_info.is_global_pooling();
1794 unsigned int pool_size_x = 0;
1795 unsigned int pool_size_y = 0;
1796
1797 // Get data layout
1798 const DataLayout data_layout = input->data_layout();
1799 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1800 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1801
1802 pool_size_x = is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size().width;
1803 pool_size_y = is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size().height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001804
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001805 // Validate pool info before calling scaled_dimensions
1806 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_pool_info(pool_size_x, pool_size_y));
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001807
1808 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +00001809 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
1810 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001811 pool_size_x,
1812 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001813 pool_info.pad_stride_info());
1814
Georgios Pinitas13d96e02018-08-23 11:20:23 +01001815 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, pooled_w, pooled_h));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001816 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), pool_info, num_elems_processed_per_iteration, border_size, pooled_w, pooled_h,
1817 pool_size_x, pool_size_y)
1818 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001819
1820 return Status{};
1821}
1822
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001823void NEPoolingLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001824{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001825 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001826 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1827 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1828 ARM_COMPUTE_ERROR_ON(_func == nullptr);
1829
Pablo Tello77e6c552018-12-04 15:33:49 +00001830 const unsigned int pool_stride_x = _pool_info.pad_stride_info().stride().first;
1831 const unsigned int pool_stride_y = _pool_info.pad_stride_info().stride().second;
1832 const unsigned int pool_size = _pool_info.pool_size().width;
1833 const bool exclude_padding = _pool_info.exclude_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001834
Michalis Spyrou57dac842018-03-01 16:03:50 +00001835 Window window_input(window);
1836 if(_input->info()->data_layout() == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001837 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001838 // Set step for input in x and y direction for the input
1839 unsigned int window_x_inc = 0;
1840 switch(_input->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +01001841 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001842 case DataType::QASYMM8:
1843 {
1844 window_x_inc = pool_stride_x;
1845 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
1846 {
1847 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
1848 }
1849 break;
1850 }
Pablo Tello77e6c552018-12-04 15:33:49 +00001851
Georgios Pinitas13d96e02018-08-23 11:20:23 +01001852 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +00001853 case DataType::F32:
1854 {
1855 window_x_inc = pool_stride_x;
1856 break;
1857 }
1858 default:
1859 {
1860 ARM_COMPUTE_ERROR("Not supported");
1861 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001862 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001863 window_input.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
1864 window_input.set(Window::DimY, Window::Dimension(window.y().start() * pool_stride_y, window.y().end() * pool_stride_y, pool_stride_y));
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001865 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001866 else
1867 {
Georgios Pinitascac13b12018-04-27 19:07:19 +01001868 window_input.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), _num_elems_processed_per_iteration));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001869 window_input.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
1870 window_input.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
1871 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001872
1873 // Run function
Pablo Tello77e6c552018-12-04 15:33:49 +00001874 (this->*_func)(window_input, window, _pool_info.pool_type(), exclude_padding);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001875}