blob: 349e64640c01f94e42c3247d4e01e1721604b853 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/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
Manuel Bottinib4bb8272019-12-18 18:01:27 +000042#include "arm_compute/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043#include <algorithm>
44#include <arm_neon.h>
Georgios Pinitascdf51452017-08-31 14:21:36 +010045#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046#include <limits>
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +010047#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048#include <string>
49#include <tuple>
50
Manuel Bottinib4bb8272019-12-18 18:01:27 +000051namespace arm_compute
52{
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +010053using namespace misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
55namespace
56{
Pablo Tello77e6c552018-12-04 15:33:49 +000057inline 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 +010058 const int pad_x, const int pad_y, const int stride_x, const int stride_y)
59{
Michalis Spyrou57dac842018-03-01 16:03:50 +000060 const unsigned int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
61 const unsigned int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
62
63 int start_x = id[idx_width] * stride_x - pad_x;
64 int start_y = id[idx_height] * stride_y - pad_y;
65
66 const int end_x = std::min(start_x + pool_size_x, upper_bound_w);
67 const int end_y = std::min(start_y + pool_size_y, upper_bound_h);
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000068 if(exclude_padding)
69 {
70 start_x = std::max(0, start_x);
71 start_y = std::max(0, start_y);
72 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 return 1.f / ((end_y - start_y) * (end_x - start_x));
74}
75
Manuel Bottinib4bb8272019-12-18 18:01:27 +000076template <typename T, typename TVec>
77inline void scale_vector_q16x8(bool exclude_padding, TVec &v, const Coordinates &id, int id_offset, int step,
Georgios Pinitas55186712018-01-08 17:37:12 +000078 const int pool_size, const int upper_bound_w, const int upper_bound_h,
79 const int pad_x, const int pad_y, const int stride_x, const int stride_y)
80{
81 int start_x = (id.x() + id_offset) * stride_x - pad_x;
82 int start_y = id.y() * stride_y - pad_y;
83 const int end_y = std::min(start_y + pool_size, upper_bound_h);
84 if(exclude_padding)
85 {
86 start_y = std::max(0, start_y);
87 }
88
Manuel Bottinib4bb8272019-12-18 18:01:27 +000089 std::array<T, 8> elems =
Georgios Pinitas55186712018-01-08 17:37:12 +000090 {
91 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +000092 wrapper::vgetlane(v, 0),
93 wrapper::vgetlane(v, 1),
94 wrapper::vgetlane(v, 2),
95 wrapper::vgetlane(v, 3),
96 wrapper::vgetlane(v, 4),
97 wrapper::vgetlane(v, 5),
98 wrapper::vgetlane(v, 6),
99 wrapper::vgetlane(v, 7),
Georgios Pinitas55186712018-01-08 17:37:12 +0000100 }
101 };
102
103 for(auto &el : elems)
104 {
105 int c_start_x = start_x;
106 const int end_x = std::min(c_start_x + pool_size, upper_bound_w);
107 if(exclude_padding)
108 {
109 c_start_x = std::max(0, c_start_x);
110 }
111 float scale = 1.f / ((end_y - start_y) * (end_x - c_start_x));
112 el *= scale;
113 start_x += step * stride_x;
114 }
115
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000116 v = wrapper::vsetlane(elems[0], v, 0);
117 v = wrapper::vsetlane(elems[1], v, 1);
118 v = wrapper::vsetlane(elems[2], v, 2);
119 v = wrapper::vsetlane(elems[3], v, 3);
120 v = wrapper::vsetlane(elems[4], v, 4);
121 v = wrapper::vsetlane(elems[5], v, 5);
122 v = wrapper::vsetlane(elems[6], v, 6);
123 v = wrapper::vsetlane(elems[7], v, 7);
Georgios Pinitas55186712018-01-08 17:37:12 +0000124}
125
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100126Status 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 +0100127{
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000128 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000130 int pool_stride_x = 0;
131 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000132 PoolingType pool_type = pool_info.pool_type;
133 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100135
Anthony Barbiereaefd002018-07-20 17:49:35 +0100136 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000137 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Georgios Pinitas55186712018-01-08 17:37:12 +0000138 ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(input->data_type()));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000139
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000140 if(output->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100141 {
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000142 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000143 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
144 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w)
145 || (output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h));
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100146 }
147
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000148 return Status{};
149}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000151Status validate_arguments_pool_info(const unsigned int pool_size_x, const unsigned int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000152{
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000153 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_x == 0);
154 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_y == 0);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000155
156 return Status{};
157}
158
159std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &num_elems_processed_per_iteration,
160 BorderSize &border_size,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000161 unsigned int pooled_w, unsigned int pooled_h, int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000162{
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +0100163 // Output auto inizialitation if not yet initialized
164 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_pool_shape(*input, pool_info)));
165
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000166 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : pool_info.data_layout;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000167 unsigned int num_elems_read_per_iteration = 0;
168 unsigned int num_elems_horizontal_window = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000169 int pool_stride_x = 0;
170 int pool_stride_y = 0;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000171 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
172 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
173 const int input_width = input->dimension(idx_width);
174 const int input_height = input->dimension(idx_height);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000175 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000176 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000177 const int pool_pad_right = pad_stride_info.pad_right();
178 const int pool_pad_top = pad_stride_info.pad_top();
179 const int pool_pad_left = pad_stride_info.pad_left();
180 const int pool_pad_bottom = pad_stride_info.pad_bottom();
181 const bool is_square = pool_size_x == pool_size_y;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000182
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000183 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +0000184 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
185 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000186 pool_size_x,
187 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000188 pad_stride_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000190 //If it's not squared and optimized will be executed the MxN
191 num_elems_read_per_iteration = 1;
192 num_elems_processed_per_iteration = 1;
193 num_elems_horizontal_window = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194
Michalis Spyrou57dac842018-03-01 16:03:50 +0000195 const bool is_nhwc = data_layout == DataLayout::NHWC;
196
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000197 if(is_square)
198 {
199 switch(input->data_type())
200 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000201 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000202 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000203 if(is_nhwc)
204 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100205 num_elems_processed_per_iteration = 16;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000206 break;
207 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000208 switch(pool_size_x)
209 {
210 case 2:
211 num_elems_read_per_iteration = 16;
212 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
213 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
214 break;
215 case 3:
216 num_elems_read_per_iteration = 16;
217 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
218 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
219 break;
220 default:
221 break;
222 }
223 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000224#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
225 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000226 if(is_nhwc)
227 {
228 num_elems_processed_per_iteration = 8;
229 break;
230 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000231 switch(pool_size_x)
232 {
233 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000234 case 3:
235 num_elems_read_per_iteration = 4;
236 num_elems_processed_per_iteration = 1;
237 num_elems_horizontal_window = 1;
238 break;
239 default:
240 break;
241 }
242 break;
243#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
244 case DataType::F32:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000245 if(is_nhwc)
246 {
Georgios Pinitas64f1a902018-09-18 13:42:51 +0100247 num_elems_processed_per_iteration = 4;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000248 break;
249 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000250 switch(pool_size_x)
251 {
252 case 2:
253 num_elems_read_per_iteration = 2;
254 break;
255 case 3:
256 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
257 break;
258 case 7:
259 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
260 break;
261 default:
262 break;
263 }
264 num_elems_processed_per_iteration = 1;
265 num_elems_horizontal_window = 1;
266 break;
267 default:
268 ARM_COMPUTE_ERROR("Element size not supported");
269 break;
270 }
271 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000272 else
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000273 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000274 if(is_nhwc)
275 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100276 num_elems_processed_per_iteration = 16 / input->element_size();
Michalis Spyrou57dac842018-03-01 16:03:50 +0000277 }
278 }
279
280 bool window_changed = false;
281 Window win{};
282 if(data_layout == DataLayout::NCHW)
283 {
284 // Number of iterations in X dimension
285 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
286
287 // Upper limit for the number of right/bottom border elements that are accessed
288 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;
289 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height;
290
291 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
292 border_size.right = std::max(upper_bound_w, pool_pad_right);
293 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
294
295 TensorShape output_shape{ input->tensor_shape() };
296 output_shape.set(0, pooled_w);
297 output_shape.set(1, pooled_h);
298 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
299
300 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
301 AccessWindowStatic input_access(input, -pool_pad_left, -pool_pad_top, input_width + border_size.right, input_height + border_size.bottom);
302
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000303 AccessWindowHorizontal output_access(output, 0, num_elems_horizontal_window);
304 window_changed = update_window_and_padding(win, input_access, output_access);
305 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
306 }
307 else
308 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000309 TensorShape output_shape{ input->tensor_shape() };
310 output_shape.set(1, pooled_w);
311 output_shape.set(2, pooled_h);
312 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
313
314 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
315 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
316
317 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
318 window_changed = update_window_and_padding(win, input_access, output_access);
319 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000320 }
321
322 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
323 return std::make_pair(err, win);
324}
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000325
326template <typename T>
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000327inline T vcvtq_q32_f32(float32x4_t values);
328
329template <>
330inline uint32x4_t vcvtq_q32_f32(float32x4_t values)
331{
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000332#ifdef __aarch64__
333 return vcvtnq_u32_f32(values);
334#else //__aarch64__
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000335 return vcvtq_u32_f32(values);
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000336#endif //__aarch64__
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000337}
338
339template <>
340inline int32x4_t vcvtq_q32_f32(float32x4_t values)
341{
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000342#ifdef __aarch64__
343 return vcvtnq_s32_f32(values);
344#else //__aarch64__
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000345 return vcvtq_s32_f32(values);
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000346#endif //__aarch64__
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000347}
348
349template <typename T>
350inline float32x4_t vcvtq_f32_q32(T values);
351
352template <>
353inline float32x4_t vcvtq_f32_q32(uint32x4_t values)
354{
355 return vcvtq_f32_u32(values);
356}
357
358template <>
359inline float32x4_t vcvtq_f32_q32(int32x4_t values)
360{
361 return vcvtq_f32_s32(values);
362}
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000363
364template <typename Tout>
365inline Tout vrequantize_pooling_with_scale(const float32x4x4_t &acc, const float quant_rescale, const float scale_pooling, const int32_t new_offset);
366
367template <>
368inline uint8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc, const float quant_rescale, const float scale_pooling, const int32_t new_offset)
369{
370 const float new_scale = quant_rescale / scale_pooling;
371 return vquantize(acc, UniformQuantizationInfo(new_scale, new_offset));
372}
373
374template <>
375inline int8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc, const float quant_rescale, const float scale_pooling, const int32_t new_offset)
376{
377 const float new_scale = quant_rescale / scale_pooling;
378 return vquantize_signed(acc, UniformQuantizationInfo(new_scale, new_offset));
379}
380
381template <typename Tin, typename Tout>
382inline Tout vrequantize_pooling(Tin vec1, Tin vec2, const UniformQuantizationInfo &requant_qinfo);
383
384template <>
385inline uint8x16_t vrequantize_pooling(uint8x8_t vec1, uint8x8_t vec2, const UniformQuantizationInfo &requant_qinfo)
386{
387 const float32x4x4_t acc =
388 {
389 {
390 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec1))))),
391 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec1))))),
392 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec2))))),
393 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec2))))),
394 }
395 };
396 return vquantize(acc, requant_qinfo);
397}
398
399template <>
400inline int8x16_t vrequantize_pooling(int8x8_t vec1, int8x8_t vec2, const UniformQuantizationInfo &requant_qinfo)
401{
402 const float32x4x4_t acc =
403 {
404 {
405 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec1))))),
406 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec1))))),
407 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec2))))),
408 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec2))))),
409 }
410 };
411 return vquantize_signed(acc, requant_qinfo);
412}
413
414template <typename T>
415inline T vrequantize_pooling(T &vec, const UniformQuantizationInfo &requant_qinfo);
416
417template <>
418inline uint8x8_t vrequantize_pooling(uint8x8_t &vec, const UniformQuantizationInfo &requant_qinfo)
419{
420 const float32x4x2_t acc =
421 {
422 {
423 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec))))),
424 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec))))),
425 }
426 };
427 return vquantize(acc, requant_qinfo);
428}
429
430template <>
431inline int8x8_t vrequantize_pooling(int8x8_t &vec, const UniformQuantizationInfo &requant_qinfo)
432{
433 const float32x4x2_t acc =
434 {
435 {
436 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec))))),
437 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec))))),
438 }
439 };
440 return vquantize_signed(acc, requant_qinfo);
441}
442
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000443} // namespace
444
445NEPoolingLayerKernel::NEPoolingLayerKernel()
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000446 : _func(nullptr), _input(nullptr), _output(nullptr), _pool_info(), _data_layout(DataLayout::UNKNOWN), _num_elems_processed_per_iteration(0), _border_size(0), _is_square(false)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000447{
448}
449
450BorderSize NEPoolingLayerKernel::border_size() const
451{
452 return _border_size;
453}
454
455void NEPoolingLayerKernel::configure(const ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info)
456{
457 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
458
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000459 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
460 const bool is_global_pooling = pool_info.is_global_pooling;
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000461 const int pool_stride_x = pad_stride_info.stride().first;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000462
463 // Get data layout
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000464 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->info()->data_layout() : pool_info.data_layout;
465 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
466 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000467
468 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000469 const Size2D pool_size(
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000470 is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size.width,
471 is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size.height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000472
473 // Validate pool info before calling scaled_dimensions
Pablo Tello77e6c552018-12-04 15:33:49 +0000474 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_pool_info(pool_size.x(), pool_size.y()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000475
476 // Check output dimensions
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100477 unsigned int pooled_w;
478 unsigned int pooled_h;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000479 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(idx_width),
480 input->info()->dimension(idx_height),
Pablo Tello77e6c552018-12-04 15:33:49 +0000481 pool_size.x(),
482 pool_size.y(),
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000483 pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000484
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000485 // Perform validation step
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100486 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, pooled_w, pooled_h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100487
488 // Set instance variables
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000489 _input = input;
490 _output = output;
491 _pool_info = pool_info;
492 _data_layout = input->info()->data_layout();
493 _is_square = (pool_size.x() == pool_size.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100494
Georgios Pinitas55186712018-01-08 17:37:12 +0000495 // Get data type
496 const DataType data_type = input->info()->data_type();
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000497 const bool is_nchw = _data_layout == DataLayout::NCHW;
Georgios Pinitas55186712018-01-08 17:37:12 +0000498
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100499 if(data_type == DataType::QASYMM8)
Georgios Pinitas55186712018-01-08 17:37:12 +0000500 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000501 if(pool_size.x() == 2 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000502 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000503 if(is_nchw)
Michalis Spyroubbd9fb92017-06-22 12:57:51 +0100504 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000505 _func = &NEPoolingLayerKernel::pooling2_q8_nchw<uint8_t>;
Pablo Tello77e6c552018-12-04 15:33:49 +0000506 }
507 else
508 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000509 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<uint8_t>;
Georgios Pinitas55186712018-01-08 17:37:12 +0000510 }
511 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000512 else if(pool_size.x() == 3 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000513 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000514 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000515 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000516 _func = &NEPoolingLayerKernel::pooling3_q8_nchw<uint8_t>;
Pablo Tello77e6c552018-12-04 15:33:49 +0000517 }
518 else
519 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000520 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<uint8_t>;
Georgios Pinitas55186712018-01-08 17:37:12 +0000521 }
522 }
523 else
524 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000525 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000526 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000527 _func = &NEPoolingLayerKernel::poolingMxN_q8_nchw<uint8_t>;
Pablo Tello77e6c552018-12-04 15:33:49 +0000528 }
529 else
530 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000531 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<uint8_t>;
532 }
533 }
534 }
535 else if(data_type == DataType::QASYMM8_SIGNED)
536 {
537 if(pool_size.x() == 2 && pool_stride_x < 3 && _is_square)
538 {
539 if(is_nchw)
540 {
541 _func = &NEPoolingLayerKernel::pooling2_q8_nchw<int8_t>;
542 }
543 else
544 {
545 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<int8_t>;
546 }
547 }
548 else if(pool_size.x() == 3 && pool_stride_x < 3 && _is_square)
549 {
550 if(is_nchw)
551 {
552 _func = &NEPoolingLayerKernel::pooling3_q8_nchw<int8_t>;
553 }
554 else
555 {
556 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<int8_t>;
557 }
558 }
559 else
560 {
561 if(is_nchw)
562 {
563 _func = &NEPoolingLayerKernel::poolingMxN_q8_nchw<int8_t>;
564 }
565 else
566 {
567 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<int8_t>;
Georgios Pinitas55186712018-01-08 17:37:12 +0000568 }
569 }
570 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000571 else if(data_type == DataType::F16)
572 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000573 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000574 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000575 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000576 {
577 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000578 {
579 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000580 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000581 _func = &NEPoolingLayerKernel::pooling2_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000582 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000583 else
584 {
585 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
586 }
587 }
588 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000589 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000590 {
591 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000592 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000593 _func = &NEPoolingLayerKernel::pooling3_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000594 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000595 else
596 {
597 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
598 }
599 }
600 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000601 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000602 {
603 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000604 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000605 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
606 }
607 else
608 {
609 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000610 }
611 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000612 }
613 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000614 }
615 }
616 else
617 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000618 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000619 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000620 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
621 }
622 else
623 {
624 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000625 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000626 }
627 }
628 else if(data_type == DataType::F32)
629 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000630 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000631 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000632 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000633 {
634 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000635 {
636 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000637 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000638 _func = &NEPoolingLayerKernel::pooling2_f32_nchw;
639 }
640 else
641 {
642 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000643 }
644 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000645 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000646 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000647 {
648 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000649 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000650 _func = &NEPoolingLayerKernel::pooling3_f32_nchw;
651 }
652 else
653 {
654 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000655 }
656 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000657 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000658 case 7:
Pablo Tello77e6c552018-12-04 15:33:49 +0000659 {
660 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000661 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000662 _func = &NEPoolingLayerKernel::pooling7_f32_nchw;
663 }
664 else
665 {
666 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000667 }
668 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000669 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000670 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000671 {
672 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000673 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000674 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
675 }
676 else
677 {
678 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000679 }
680 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000681 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000682 }
683 }
684 else
685 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000686 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000687 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000688 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
689 }
690 else
691 {
692 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000693 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000694 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100695 }
696
697 // Configure kernel window
Pablo Tello77e6c552018-12-04 15:33:49 +0000698 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 +0000699 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
700 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100701}
702
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000703template <typename T>
704void NEPoolingLayerKernel::pooling2_q8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000705{
706 Iterator input(_input, window_input);
707 Iterator output(_output, window);
708
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000709 /** NEON vector types */
710 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
711 using q8x16_t = typename wrapper::traits::neon_vector<T, 16>::type;
712 using q8x8x2_t = typename std::conditional<std::is_same<T, uint8_t>::value, uint8x8x2_t, int8x8x2_t>::type;
713 using q16_t = typename wrapper::traits::promote_t<T>;
714 using q16x4_t = typename wrapper::traits::neon_vector<q16_t, 4>::type;
715 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
716 using q16x8x2_t = typename wrapper::traits::neon_vector<q16_t, 16>::type;
717
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000718 constexpr int pool_size = 2;
719 int pool_stride_x = 0;
720 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000721 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
722 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
723 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
724 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
725 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000726 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
727 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000728
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000729 const T *const input_top_ptr = reinterpret_cast<const T *>(_input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))));
730 const T *const input_bottom_ptr = reinterpret_cast<const T *>(_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 +0000731
732 const int scale_step_x = (pool_stride_x == 1) ? 2 : 1;
733
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100734 const UniformQuantizationInfo input_qinfo = _input->info()->quantization_info().uniform();
735 const UniformQuantizationInfo output_qinfo = _output->info()->quantization_info().uniform();
736 const bool have_different_qinfo = input_qinfo != output_qinfo;
737
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000738 const float requant_scale = output_qinfo.scale / input_qinfo.scale;
739 const int32_t requant_offset = output_qinfo.offset - static_cast<int32_t>(static_cast<float>(input_qinfo.offset) / requant_scale);
740 const UniformQuantizationInfo requant_qinfo = UniformQuantizationInfo(requant_scale, requant_offset);
741
Georgios Pinitas55186712018-01-08 17:37:12 +0000742 execute_window_loop(window, [&](const Coordinates & id)
743 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000744 const auto top_data = wrapper::vloadq(input_top_ptr + input.offset());
745 const auto bottom_data = wrapper::vloadq(input_bottom_ptr + input.offset());
746 q8x8_t lower_res = {};
747 q8x8_t upper_res = {};
Georgios Pinitas55186712018-01-08 17:37:12 +0000748
749 if(pooling_type != PoolingType::MAX)
750 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000751 const q16x8x2_t top_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(top_data)), wrapper::vmovl(wrapper::vgethigh(top_data)) } };
752 const q16x8x2_t bottom_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(bottom_data)), wrapper::vmovl(wrapper::vgethigh(bottom_data)) } };
Georgios Pinitas55186712018-01-08 17:37:12 +0000753
754 // Add rows
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000755 const q16x8x2_t vrsum =
Georgios Pinitas55186712018-01-08 17:37:12 +0000756 {
757 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000758 wrapper::vadd(top_data_q16.val[0], bottom_data_q16.val[0]),
759 wrapper::vadd(top_data_q16.val[1], bottom_data_q16.val[1]),
Georgios Pinitas55186712018-01-08 17:37:12 +0000760 }
761 };
762
763 // Pair-wise add row data
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000764 const q16x4_t vpsum_1 = wrapper::vpadd(wrapper::vgetlow(vrsum.val[0]), wrapper::vgethigh(vrsum.val[0]));
765 const q16x4_t vpsum_2 = wrapper::vpadd(wrapper::vgetlow(vrsum.val[1]), wrapper::vgethigh(vrsum.val[1]));
Georgios Pinitas55186712018-01-08 17:37:12 +0000766
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000767 q16x8_t res_lower = wrapper::vcombine(vpsum_1, vpsum_2);
Georgios Pinitas55186712018-01-08 17:37:12 +0000768
769 // Scale lower result
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000770 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, res_lower, id, 0, scale_step_x,
771 pool_size, upper_bound_w, upper_bound_h,
772 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
773 lower_res = wrapper::vmovn(res_lower);
Georgios Pinitas55186712018-01-08 17:37:12 +0000774
775 // Compute upper result for stride_x == 1
776 if(pool_stride_x == 1)
777 {
778 // Shifted row sum
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000779 const q16x8x2_t vrsum_shifted =
Georgios Pinitas55186712018-01-08 17:37:12 +0000780 {
781 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000782 wrapper::vext_1(vrsum.val[0], vrsum.val[1]),
783 wrapper::vext_1(vrsum.val[1], vrsum.val[1])
Georgios Pinitas55186712018-01-08 17:37:12 +0000784 }
785 };
786
787 // Pair-wise add shifted row
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000788 q16x8_t res_upper = wrapper::vcombine(
789 wrapper::vpadd(wrapper::vgetlow(vrsum_shifted.val[0]), wrapper::vgethigh(vrsum_shifted.val[0])),
790 wrapper::vpadd(wrapper::vgetlow(vrsum_shifted.val[1]), wrapper::vgethigh(vrsum_shifted.val[1])));
Georgios Pinitas55186712018-01-08 17:37:12 +0000791
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000792 // Scale upper result
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000793 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, res_upper, id, 1, 2,
794 pool_size, upper_bound_w, upper_bound_h,
795 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
796 upper_res = wrapper::vmovn(res_upper);
Georgios Pinitas55186712018-01-08 17:37:12 +0000797 }
798 }
799 else
800 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000801 const q8x16_t max_data = wrapper::vmax(top_data, bottom_data);
802 lower_res = wrapper::vpmax(wrapper::vgetlow(max_data), wrapper::vgethigh(max_data));
Georgios Pinitas55186712018-01-08 17:37:12 +0000803 if(pool_stride_x == 1)
804 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000805 const q8x16_t max_data_shifted = wrapper::vext_1(max_data, max_data);
806 upper_res = wrapper::vpmax(wrapper::vgetlow(max_data_shifted), wrapper::vgethigh(max_data_shifted));
Georgios Pinitas55186712018-01-08 17:37:12 +0000807 }
808 }
809
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100810 if(have_different_qinfo)
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100811 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000812 const auto requantized_output = vrequantize_pooling<q8x8_t, q8x16_t>(lower_res, upper_res, requant_qinfo);
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000813 lower_res = wrapper::vgetlow(requantized_output);
814 upper_res = wrapper::vgethigh(requantized_output);
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100815 }
816
Georgios Pinitas55186712018-01-08 17:37:12 +0000817 // Store result
818 if(pool_stride_x == 1)
819 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000820 const q8x8x2_t res = { { lower_res, upper_res } };
821 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), res);
Georgios Pinitas55186712018-01-08 17:37:12 +0000822 }
823 else
824 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000825 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), lower_res);
Georgios Pinitas55186712018-01-08 17:37:12 +0000826 }
827 },
828 input, output);
829}
830
Pablo Tello77e6c552018-12-04 15:33:49 +0000831void NEPoolingLayerKernel::pooling3_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100832{
Pablo Tello77e6c552018-12-04 15:33:49 +0000833 ARM_COMPUTE_UNUSED(pooling_type);
834 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000835#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100836 Iterator input(_input, window_input);
837 Iterator output(_output, window);
838
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000839 constexpr const int pool_size = 3;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000840 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
841 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
842 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
843 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000844 int pool_stride_x = 0;
845 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000846 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000847 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
848 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100849
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000850 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
851 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));
852 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 +0100853
854 execute_window_loop(window, [&](const Coordinates & id)
855 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100856 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
857 float16x4_t middle_data = vld1_f16(reinterpret_cast<const float16_t *>(input_middle_ptr + input.offset()));
858 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
859 float16x4_t res = {};
860
861 // Get power of 2 in case of l2 pooling
862 if(pooling_type == PoolingType::L2)
863 {
864 top_data = vmul_f16(top_data, top_data);
865 middle_data = vmul_f16(middle_data, middle_data);
866 bottom_data = vmul_f16(bottom_data, bottom_data);
867 }
868
869 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100870 {
871 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000872 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 +0100873 const float16x4_t scale_v = vdup_n_f16(scale);
874 // Perform pooling
875 const float16x4_t sum_data = vadd_f16(vadd_f16(top_data, bottom_data), middle_data);
876 res = vpadd_f16(vset_lane_f16(0.f, sum_data, 3), sum_data);
877 res = vmul_f16(vpadd_f16(res, res), scale_v);
878 }
879 else
880 {
881 const float16x4_t max_data = vmax_f16(vmax_f16(top_data, bottom_data), middle_data);
882 res = vpmax_f16(vset_lane_f16(-std::numeric_limits<float>::max(), max_data, 3), max_data);
883 res = vpmax_f16(res, res);
884 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100885
886 // Calculate square-root in case of l2 pooling
887 if(pooling_type == PoolingType::L2)
888 {
889 res = vinv_f16(vinvsqrt_f16(res));
890 }
891
Pablo Tello0c34fe22017-06-26 17:17:42 +0100892 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
893 },
894 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000895#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100896 ARM_COMPUTE_UNUSED(window_input);
897 ARM_COMPUTE_UNUSED(window);
898 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000899#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100900}
901
Pablo Tello77e6c552018-12-04 15:33:49 +0000902void NEPoolingLayerKernel::pooling2_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100903{
Pablo Tello77e6c552018-12-04 15:33:49 +0000904 ARM_COMPUTE_UNUSED(pooling_type);
905 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000906#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100907 Iterator input(_input, window_input);
908 Iterator output(_output, window);
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000909 constexpr int pool_size = 2;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000910 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
911 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
912 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
913 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000914 int pool_stride_x, pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000915 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000916 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
917 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100918
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000919 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
920 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 +0100921
922 execute_window_loop(window, [&](const Coordinates & id)
923 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100924 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
925 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
926 float16x4_t res = {};
Pablo Tello0c34fe22017-06-26 17:17:42 +0100927
Georgios Pinitascdf51452017-08-31 14:21:36 +0100928 // Get power of 2 in case of l2 pooling
929 if(pooling_type == PoolingType::L2)
930 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100931 top_data = vmul_f16(top_data, top_data);
932 bottom_data = vmul_f16(bottom_data, bottom_data);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100933 }
934
935 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100936 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000937 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 +0100938 const float16x4_t scale_v = vdup_n_f16(scale);
939
940 const float16x4_t sum_data = vadd_f16(top_data, bottom_data);
941 res = vmul_f16(vpadd_f16(sum_data, sum_data), scale_v);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100942 }
943 else
944 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100945 const float16x4_t max_data = vmax_f16(top_data, bottom_data);
946 res = vpmax_f16(max_data, max_data);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100947 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100948
949 // Calculate square-root in case of l2 pooling
950 if(pooling_type == PoolingType::L2)
951 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100952 res = vinv_f16(vinvsqrt_f16(res));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100953 }
954
955 // Store result
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100956 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100957 },
958 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000959#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100960 ARM_COMPUTE_UNUSED(window_input);
961 ARM_COMPUTE_UNUSED(window);
962 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000963#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100964}
965
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000966template <typename T>
967void NEPoolingLayerKernel::pooling3_q8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000968{
969 Iterator input(_input, window_input);
970 Iterator output(_output, window);
971
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000972 /** NEON vector types */
973 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
974 using q8x16_t = typename wrapper::traits::neon_vector<T, 16>::type;
975 using q8x8x2_t = typename std::conditional<std::is_same<T, uint8_t>::value, uint8x8x2_t, int8x8x2_t>::type;
976 using q16_t = typename wrapper::traits::promote_t<T>;
977 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
978 using q16x8x2_t = typename wrapper::traits::neon_vector<q16_t, 16>::type;
979
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000980 constexpr int pool_size = 3;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000981 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
982 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
983 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
984 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000985 int pool_stride_x = 0;
986 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000987 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000988 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
989 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000990
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100991 const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform();
992 const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform();
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100993
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000994 const float requant_scale = output_qinfo.scale / input_qinfo.scale;
995 const int32_t requant_offset = output_qinfo.offset - static_cast<int32_t>(static_cast<float>(input_qinfo.offset) / requant_scale);
996 const UniformQuantizationInfo requant_qinfo = UniformQuantizationInfo(requant_scale, requant_offset);
997
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000998 const T *const input_top_ptr = reinterpret_cast<const T *>(_input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))));
999 const T *const input_middle_ptr = reinterpret_cast<const T *>(_input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1)));
1000 const T *const input_bottom_ptr = reinterpret_cast<const T *>(_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 +00001001
1002 execute_window_loop(window, [&](const Coordinates & id)
1003 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001004 const auto top_data = wrapper::vloadq(input_top_ptr + input.offset());
1005 const auto middle_data = wrapper::vloadq(input_middle_ptr + input.offset());
1006 const auto bottom_data = wrapper::vloadq(input_bottom_ptr + input.offset());
1007 q8x8_t fres = {};
1008 q8x16_t fqres = {};
Georgios Pinitas55186712018-01-08 17:37:12 +00001009
1010 if(pooling_type == PoolingType::AVG)
1011 {
1012 // Convert data to u16
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001013 const q16x8x2_t top_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(top_data)), wrapper::vmovl(wrapper::vgethigh(top_data)) } };
1014 const q16x8x2_t middle_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(middle_data)), wrapper::vmovl(wrapper::vgethigh(middle_data)) } };
1015 const q16x8x2_t bottom_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(bottom_data)), wrapper::vmovl(wrapper::vgethigh(bottom_data)) } };
Georgios Pinitas55186712018-01-08 17:37:12 +00001016
1017 // Calculate row sums
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001018 const q16x8x2_t vrsum =
Georgios Pinitas55186712018-01-08 17:37:12 +00001019 {
1020 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001021 wrapper::vadd(wrapper::vadd(top_data_q16.val[0], bottom_data_q16.val[0]), middle_data_q16.val[0]),
1022 wrapper::vadd(wrapper::vadd(top_data_q16.val[1], bottom_data_q16.val[1]), middle_data_q16.val[1]),
Georgios Pinitas55186712018-01-08 17:37:12 +00001023 }
1024 };
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001025 const q16x8x2_t vrsum_shifted_1 =
Georgios Pinitas55186712018-01-08 17:37:12 +00001026 {
1027 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001028 wrapper::vext_1(vrsum.val[0], vrsum.val[1]),
1029 wrapper::vext_1(vrsum.val[1], vrsum.val[1])
Georgios Pinitas55186712018-01-08 17:37:12 +00001030 }
1031 };
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001032 const q16x8x2_t vrsum_shifted_2 =
Georgios Pinitas55186712018-01-08 17:37:12 +00001033 {
1034 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001035 wrapper::vext_2(vrsum.val[0], vrsum.val[1]),
1036 wrapper::vext_2(vrsum.val[1], vrsum.val[1])
Georgios Pinitas55186712018-01-08 17:37:12 +00001037 }
1038 };
1039 // Calculate final sum
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001040 q16x8x2_t final_sum =
Georgios Pinitas55186712018-01-08 17:37:12 +00001041 {
1042 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001043 wrapper::vadd(wrapper::vadd(vrsum.val[0], vrsum_shifted_1.val[0]), vrsum_shifted_2.val[0]),
1044 wrapper::vadd(wrapper::vadd(vrsum.val[1], vrsum_shifted_1.val[1]), vrsum_shifted_2.val[1]),
Georgios Pinitas55186712018-01-08 17:37:12 +00001045 }
1046 };
1047 if(pool_stride_x == 2)
1048 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001049 q16x8_t res =
Georgios Pinitas55186712018-01-08 17:37:12 +00001050 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001051 wrapper::vgetlane(final_sum.val[0], 0),
1052 wrapper::vgetlane(final_sum.val[0], 2),
1053 wrapper::vgetlane(final_sum.val[0], 4),
1054 wrapper::vgetlane(final_sum.val[0], 6),
1055 wrapper::vgetlane(final_sum.val[1], 0),
1056 wrapper::vgetlane(final_sum.val[1], 2),
1057 wrapper::vgetlane(final_sum.val[1], 4),
1058 wrapper::vgetlane(final_sum.val[1], 6),
Georgios Pinitas55186712018-01-08 17:37:12 +00001059 };
1060
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001061 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, res, id, 0, 1,
1062 pool_size, upper_bound_w, upper_bound_h,
1063 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
1064 fres = wrapper::vmovn(res);
Georgios Pinitas55186712018-01-08 17:37:12 +00001065 }
1066 else
1067 {
1068 // Scale lower result
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001069 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, final_sum.val[0], id, 0, 1,
1070 pool_size, upper_bound_w, upper_bound_h,
1071 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +00001072 // Scale lower result
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001073 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, final_sum.val[1], id, 8, 1,
1074 pool_size, upper_bound_w, upper_bound_h,
1075 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
1076 fqres = wrapper::vcombine(wrapper::vmovn(final_sum.val[0]), wrapper::vmovn(final_sum.val[1]));
Georgios Pinitas55186712018-01-08 17:37:12 +00001077 }
1078 }
1079 else
1080 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001081 const q8x16_t max_data = wrapper::vmax(wrapper::vmax(top_data, bottom_data), middle_data);
1082 const q8x16_t max_data_shift1 = wrapper::vext_1(max_data, max_data);
1083 const q8x16_t max_data_shift2 = wrapper::vext_2(max_data, max_data);
1084 const q8x16_t final_max = wrapper::vmax(wrapper::vmax(max_data, max_data_shift1), max_data_shift2);
Georgios Pinitas55186712018-01-08 17:37:12 +00001085
1086 if(pool_stride_x == 2)
1087 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001088 const q8x8x2_t table = { { wrapper::vgetlow(final_max), wrapper::vgethigh(final_max) } };
1089 static const q8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 };
1090 fres = wrapper::vtbl(table, lookup_val);
Georgios Pinitas55186712018-01-08 17:37:12 +00001091 }
1092 else
1093 {
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001094 fqres = final_max;
Georgios Pinitas55186712018-01-08 17:37:12 +00001095 }
1096 }
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001097
1098 // Store result
1099 if(pool_stride_x == 1)
1100 {
1101 if(input_qinfo != output_qinfo)
1102 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001103 fqres = vrequantize_pooling<q8x8_t, q8x16_t>(wrapper::vgetlow(fqres), wrapper::vgethigh(fqres), requant_qinfo);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001104 }
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001105 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), fqres);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001106 }
1107 else
1108 {
1109 if(input_qinfo != output_qinfo)
1110 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001111 fres = vrequantize_pooling<q8x8_t>(fres, requant_qinfo);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001112 }
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001113 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), fres);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001114 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001115 },
1116 input, output);
1117}
1118
Pablo Tello77e6c552018-12-04 15:33:49 +00001119void NEPoolingLayerKernel::poolingMxN_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001120{
Pablo Tello77e6c552018-12-04 15:33:49 +00001121 ARM_COMPUTE_UNUSED(pooling_type);
1122 ARM_COMPUTE_UNUSED(exclude_padding);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001123#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1124 Iterator input(_input, window_input);
1125 Iterator output(_output, window);
1126
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001127 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().x() : _pool_info.pool_size.width;
1128 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.height;
1129 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1130 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1131 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1132 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001133 int pool_stride_x = 0;
1134 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001135 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001136 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1137 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1138
1139 execute_window_loop(window, [&](const Coordinates & id)
1140 {
1141 float16_t res = 0.0f;
1142 float16x8_t vres = vdupq_n_f16(0.0f);
1143
1144 if(pooling_type != PoolingType::MAX)
1145 {
1146 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001147 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 +00001148
1149 // Perform pooling
1150
1151 for(int y = 0; y < pool_size_y; ++y)
1152 {
1153 int x = 0;
1154 for(; x <= (pool_size_x - 8); x += 8)
1155 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001156 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) +
1157 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001158
1159 // Get power of 2 in case of l2 pooling and accumulate
1160 if(pooling_type == PoolingType::L2)
1161 {
1162 vres = vaddq_f16(vres, vmulq_f16(data, data));
1163 }
1164 else
1165 {
1166 vres = vaddq_f16(vres, data);
1167 }
1168 }
1169
1170 // Leftover for loop
1171 for(; x < pool_size_x; ++x)
1172 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001173 float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x())
1174 + (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001175
1176 // Get power of 2 in case of l2 pooling
1177 if(pooling_type == PoolingType::L2)
1178 {
1179 data *= data;
1180 }
1181
1182 res += data;
1183 }
1184 }
1185
1186 // Reduction
1187 float16x4_t tmp = vpadd_f16(vget_high_f16(vres), vget_low_f16(vres));
1188 res += vget_lane_f16(tmp, 0);
1189 res += vget_lane_f16(tmp, 1);
1190 res += vget_lane_f16(tmp, 2);
1191 res += vget_lane_f16(tmp, 3);
1192
1193 // Divide by scale
1194 res *= scale;
1195 }
1196 else
1197 {
1198 float16x8_t vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
1199 res = std::numeric_limits<float>::lowest();
1200
1201 for(int y = 0; y < pool_size_y; ++y)
1202 {
1203 int x = 0;
1204 for(; x <= (pool_size_x - 8); x += 8)
1205 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001206 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) +
1207 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001208 vres = vmaxq_f16(vres, data);
1209 }
1210
1211 // Leftover for loop
1212 for(; x < pool_size_x; ++x)
1213 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001214 const float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x())
1215 + (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
1216 res = std::max(res, data);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001217 }
1218 }
1219
1220 float16x4_t tmp = vpmax_f16(vget_high_f16(vres), vget_low_f16(vres));
1221 res = std::max(res, vget_lane_f16(tmp, 0));
1222 res = std::max(res, vget_lane_f16(tmp, 1));
1223 res = std::max(res, vget_lane_f16(tmp, 2));
1224 res = std::max(res, vget_lane_f16(tmp, 3));
1225 }
1226
1227 // Calculate square-root in case of l2 pooling
1228 if(pooling_type == PoolingType::L2)
1229 {
1230 res = std::sqrt(res);
1231 }
1232
1233 // Store result
1234 *(reinterpret_cast<float16_t *>(output.ptr())) = res;
1235 },
1236 input, output);
1237
1238#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1239 ARM_COMPUTE_UNUSED(window_input);
1240 ARM_COMPUTE_UNUSED(window);
1241 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1242#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1243}
1244
Pablo Tello77e6c552018-12-04 15:33:49 +00001245void NEPoolingLayerKernel::poolingMxN_f16_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001246{
Pablo Tello77e6c552018-12-04 15:33:49 +00001247 ARM_COMPUTE_UNUSED(pooling_type);
1248 ARM_COMPUTE_UNUSED(exclude_padding);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001249#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1250 Iterator input(_input, window_input);
1251 Iterator output(_output, window);
1252
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001253 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.width;
1254 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().z() : _pool_info.pool_size.height;
1255 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1256 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1257 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1258 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001259 int pool_stride_x = 0;
1260 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001261 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001262 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1263 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1264
1265 float16x8_t vres;
1266
1267 execute_window_loop(window, [&](const Coordinates & id)
1268 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001269 const int idx_width = id.y() * pool_stride_x;
1270 const int idx_height = id.z() * pool_stride_y;
1271 const int pool_limit_y = pool_pad_top - idx_height;
1272 const int pool_limit_x = pool_pad_left - idx_width;
1273
1274 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1275 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1276 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1277 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1278
Michalis Spyrou57dac842018-03-01 16:03:50 +00001279 if(pooling_type != PoolingType::MAX)
1280 {
1281 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001282 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,
1283 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001284 const float16x8_t scale_v = vdupq_n_f16(scale);
1285
1286 // Perform pooling
1287 vres = vdupq_n_f16(0.0f);
Michalis Spyrouced25572018-10-01 16:26:20 +01001288 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001289 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001290 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001291 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001292 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) +
1293 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001294
1295 // Get power of 2 in case of l2 pooling and accumulate
1296 if(pooling_type == PoolingType::L2)
1297 {
1298 vres = vaddq_f16(vres, vmulq_f16(data, data));
1299 }
1300 else
1301 {
1302 vres = vaddq_f16(vres, data);
1303 }
1304 }
1305 }
1306 // Divide by scale
1307 vres = vmulq_f16(vres, scale_v);
1308 }
1309 else
1310 {
1311 vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001312
1313 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001314 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001315 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001316 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001317 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) +
1318 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001319 vres = vmaxq_f16(vres, data);
1320 }
1321 }
1322 }
1323
1324 // Calculate square-root in case of l2 pooling
1325 if(pooling_type == PoolingType::L2)
1326 {
1327 float16x8_t sqrt_reciprocal = vrsqrteq_f16(vres);
1328 vres = vmulq_f16(vres, vmulq_f16(vrsqrtsq_f16(vmulq_f16(vres, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal));
1329 }
1330
1331 // Store result
1332 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vres);
1333 },
1334 input, output);
1335
1336#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1337 ARM_COMPUTE_UNUSED(window_input);
1338 ARM_COMPUTE_UNUSED(window);
1339 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1340#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1341}
1342
Pablo Tello77e6c552018-12-04 15:33:49 +00001343void NEPoolingLayerKernel::poolingMxN_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001344{
1345 Iterator input(_input, window_input);
1346 Iterator output(_output, window);
1347
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001348 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().x() : _pool_info.pool_size.width;
1349 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.height;
1350 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1351 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1352 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1353 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001354 int pool_stride_x = 0;
1355 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001356 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001357 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1358 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Gian Marco Iodice16824302017-09-28 15:41:37 +01001359
1360 execute_window_loop(window, [&](const Coordinates & id)
1361 {
1362 float res = 0.0f;
1363
1364 if(pooling_type != PoolingType::MAX)
1365 {
1366 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001367 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 +01001368
1369 // Perform pooling
1370 float32x4_t vres = vdupq_n_f32(0.0f);
1371
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001372 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001373 {
1374 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001375 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001376 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001377 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1378 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001379
1380 // Get power of 2 in case of l2 pooling and accumulate
1381 if(pooling_type == PoolingType::L2)
1382 {
1383 vres = vmlaq_f32(vres, data, data);
1384 }
1385 else
1386 {
1387 vres = vaddq_f32(vres, data);
1388 }
1389 }
1390
1391 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001392 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001393 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001394 float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1395 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001396
1397 // Get power of 2 in case of l2 pooling
1398 if(pooling_type == PoolingType::L2)
1399 {
1400 data *= data;
1401 }
1402
1403 res += data;
1404 }
1405 }
1406
1407#if defined(__aarch64__)
1408 // Reduction operation available on 64 bit architectures only
1409 res += vaddvq_f32(vres);
1410#else // __aarch64__
1411 // Reduction
1412 float32x2_t tmp = vpadd_f32(vget_high_f32(vres), vget_low_f32(vres));
1413 tmp = vpadd_f32(tmp, tmp);
1414
1415 res += vget_lane_f32(tmp, 0);
1416#endif // __aarch64__
1417 // Divide by scale
1418 res *= scale;
1419 }
1420 else
1421 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001422 float32x4_t vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
1423 res = std::numeric_limits<float>::lowest();
Gian Marco Iodice16824302017-09-28 15:41:37 +01001424
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001425 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001426 {
1427 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001428 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001429 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001430 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1431 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001432 vres = vmaxq_f32(vres, data);
1433 }
1434
1435 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001436 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001437 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001438 const float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1439 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001440 res = std::max(res, data);
1441 }
1442 }
1443
1444#if defined(__aarch64__)
1445 // Reduction operation available on 64 bit architectures only
1446 res = std::max(vmaxvq_f32(vres), res);
1447#else // __aarch64__
1448 float32x2_t tmp = vpmax_f32(vget_high_f32(vres), vget_low_f32(vres));
1449 tmp = vpmax_f32(tmp, tmp);
1450
1451 res = std::max(res, vget_lane_f32(tmp, 0));
1452#endif // __aarch64__
1453 }
1454
1455 // Calculate square-root in case of l2 pooling
1456 if(pooling_type == PoolingType::L2)
1457 {
1458 res = std::sqrt(res);
1459 }
1460
1461 // Store result
1462 *(reinterpret_cast<float *>(output.ptr())) = res;
1463 },
1464 input, output);
1465}
1466
Pablo Tello77e6c552018-12-04 15:33:49 +00001467void NEPoolingLayerKernel::pooling2_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1468{
1469 Iterator input(_input, window_input);
1470 Iterator output(_output, window);
1471
1472 constexpr int pool_size = 2;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001473 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1474 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1475 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1476 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Pablo Tello77e6c552018-12-04 15:33:49 +00001477 int pool_stride_x = 0;
1478 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001479 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Pablo Tello77e6c552018-12-04 15:33:49 +00001480 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1481 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1482
1483 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1484 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));
1485
1486 execute_window_loop(window, [&](const Coordinates & id)
1487 {
1488 float32x2_t top_data = vld1_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1489 float32x2_t bottom_data = vld1_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1490 float32x2_t res = {};
1491 float final_res = 0;
1492
1493 // Get power of 2 in case of l2 pooling
1494 if(pooling_type == PoolingType::L2)
1495 {
1496 top_data = vmul_f32(top_data, top_data);
1497 bottom_data = vmul_f32(bottom_data, bottom_data);
1498 }
1499
1500 if(pooling_type != PoolingType::MAX)
1501 {
1502 // Calculate scale
1503 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);
1504 const float32x2_t scale_v = vdup_n_f32(scale);
1505
1506 // Perform pooling
1507 const float32x2_t sum_data = vadd_f32(top_data, bottom_data);
1508 res = vmul_f32(vpadd_f32(sum_data, sum_data), scale_v);
1509 }
1510 else
1511 {
1512 const float32x2_t max_data = vmax_f32(top_data, bottom_data);
1513 res = vpmax_f32(max_data, max_data);
1514 }
1515 final_res = vget_lane_f32(res, 0);
1516
1517 // Calculate square-root in case of l2 pooling
1518 if(pooling_type == PoolingType::L2)
1519 {
1520 final_res = sqrt(final_res);
1521 }
1522
1523 // Store result
1524 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1525 },
1526 input, output);
1527}
1528
1529void NEPoolingLayerKernel::pooling3_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1530{
1531 Iterator input(_input, window_input);
1532 Iterator output(_output, window);
1533
1534 constexpr const int pool_size = 3;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001535 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1536 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1537 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1538 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Pablo Tello77e6c552018-12-04 15:33:49 +00001539 int pool_stride_x = 0;
1540 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001541 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Pablo Tello77e6c552018-12-04 15:33:49 +00001542 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1543 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1544
1545 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1546 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));
1547 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));
1548
1549 execute_window_loop(window, [&](const Coordinates & id)
1550 {
1551 float32x4_t top_data = vld1q_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1552 float32x4_t middle_data = vld1q_f32(reinterpret_cast<const float *>(input_middle_ptr + input.offset()));
1553 float32x4_t bottom_data = vld1q_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1554 float32x2_t res = {};
1555 float final_res = 0;
1556
1557 // Get power of 2 in case of l2 pooling
1558 if(pooling_type == PoolingType::L2)
1559 {
1560 top_data = vmulq_f32(top_data, top_data);
1561 middle_data = vmulq_f32(middle_data, middle_data);
1562 bottom_data = vmulq_f32(bottom_data, bottom_data);
1563 }
1564
1565 if(pooling_type != PoolingType::MAX)
1566 {
1567 // Calculate scale
1568 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);
1569 const float32x2_t scale_v = vdup_n_f32(scale);
1570
1571 // Perform pooling
1572 const float32x4_t sum_data = vaddq_f32(vaddq_f32(top_data, bottom_data), middle_data);
1573 res = vpadd_f32(vget_high_f32(vsetq_lane_f32(0.f, sum_data, 3)), vget_low_f32(sum_data));
1574 res = vmul_f32(vpadd_f32(res, res), scale_v);
1575 }
1576 else
1577 {
1578 const float32x4_t max_data = vmaxq_f32(vmaxq_f32(top_data, bottom_data), middle_data);
1579 res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data, 3)), vget_low_f32(max_data));
1580 res = vpmax_f32(res, res);
1581 }
1582 final_res = vget_lane_f32(res, 0);
1583
1584 // Calculate square-root in case of l2 pooling
1585 if(pooling_type == PoolingType::L2)
1586 {
1587 final_res = sqrt(final_res);
1588 }
1589
1590 // Store result
1591 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1592 },
1593 input, output);
1594}
1595
1596void NEPoolingLayerKernel::pooling7_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1597{
1598 Iterator input(_input, window_input);
1599 Iterator output(_output, window);
1600
1601 constexpr const int pool_size = 7;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001602 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1603 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1604 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1605 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Pablo Tello77e6c552018-12-04 15:33:49 +00001606 int pool_stride_x = 0;
1607 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001608 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Pablo Tello77e6c552018-12-04 15:33:49 +00001609 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1610 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1611
1612 std::array<const uint8_t *, pool_size> input_ptrs{ {} };
1613 for(int i = 0; i < pool_size; ++i)
1614 {
1615 input_ptrs[i] = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + i));
1616 }
1617
1618 execute_window_loop(window, [&](const Coordinates & id)
1619 {
1620 float32x2_t res = {};
1621 float final_res = 0.f;
1622 if(pooling_type != PoolingType::MAX)
1623 {
1624 // Calculate scale
1625 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);
1626 const float32x2_t scale_v = vdup_n_f32(scale);
1627
1628 // Perform pooling
1629 float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1630 // Get power of 2 in case of l2 pooling
1631 if(pooling_type == PoolingType::L2)
1632 {
1633 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1634 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1635 }
1636 float32x4_t sum_data = vaddq_f32(data.val[0], vsetq_lane_f32(0.f, data.val[1], 3));
1637 for(int i = 1; i < pool_size; ++i)
1638 {
1639 data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1640 // Get power of 2 in case of l2 pooling
1641 if(pooling_type == PoolingType::L2)
1642 {
1643 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1644 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1645 }
1646 sum_data = vaddq_f32(sum_data, data.val[0]);
1647 sum_data = vaddq_f32(sum_data, vsetq_lane_f32(0.f, data.val[1], 3));
1648 }
1649 res = vpadd_f32(vget_high_f32(sum_data), vget_low_f32(sum_data));
1650 res = vmul_f32(vpadd_f32(res, res), scale_v);
1651 }
1652 else
1653 {
1654 float32x4x2_t max_data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1655 for(int i = 1; i < pool_size; ++i)
1656 {
1657 const float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1658 max_data = vmax2q_f32(max_data, data);
1659 }
1660 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]));
1661 res = vpmax_f32(res, vpmax_f32(vget_high_f32(max_data.val[0]), vget_low_f32(max_data.val[0])));
1662 res = vpmax_f32(res, res);
1663 }
1664 final_res = vget_lane_f32(res, 0);
1665
1666 // Calculate square-root in case of l2 pooling
1667 if(pooling_type == PoolingType::L2)
1668 {
1669 final_res = sqrt(final_res);
1670 }
1671
1672 // Store result
1673 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1674 },
1675 input, output);
1676}
1677
1678void NEPoolingLayerKernel::poolingMxN_f32_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001679{
1680 Iterator input(_input, window_input);
1681 Iterator output(_output, window);
1682
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001683 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.width;
1684 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().z() : _pool_info.pool_size.height;
1685 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1686 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1687 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1688 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001689 int pool_stride_x = 0;
1690 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001691 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001692 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1693 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1694
1695 float32x4_t vres;
1696
1697 execute_window_loop(window, [&](const Coordinates & id)
1698 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001699 const int idx_width = id.y() * pool_stride_x;
1700 const int idx_height = id.z() * pool_stride_y;
1701 const int pool_limit_y = pool_pad_top - idx_height;
1702 const int pool_limit_x = pool_pad_left - idx_width;
1703
1704 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1705 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1706 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1707 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1708
Michalis Spyrou57dac842018-03-01 16:03:50 +00001709 if(pooling_type != PoolingType::MAX)
1710 {
1711 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001712 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,
1713 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001714 const float32x4_t scale_v = vdupq_n_f32(scale);
1715
1716 // Perform pooling
1717 vres = vdupq_n_f32(0.0f);
1718
Michalis Spyrouced25572018-10-01 16:26:20 +01001719 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001720 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001721 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001722 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001723 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int>
1724 (_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001725
1726 // Get power of 2 in case of l2 pooling and accumulate
1727 if(pooling_type == PoolingType::L2)
1728 {
1729 vres = vmlaq_f32(vres, data, data);
1730 }
1731 else
1732 {
1733 vres = vaddq_f32(vres, data);
1734 }
1735 }
1736 }
1737 // Divide by scale
1738 vres = vmulq_f32(vres, scale_v);
1739 }
1740 else
1741 {
1742 vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001743 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001744 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001745 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001746 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001747 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int>
1748 (_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001749 vres = vmaxq_f32(vres, data);
1750 }
1751 }
1752 }
1753
1754 // Calculate square-root in case of l2 pooling
1755 if(pooling_type == PoolingType::L2)
1756 {
Georgios Pinitas27f223d2019-12-16 19:23:02 +00001757 float32x4_t l2_res = { static_cast<float>(sqrt(vgetq_lane_f32(vres, 0))),
1758 static_cast<float>(sqrt(vgetq_lane_f32(vres, 1))),
1759 static_cast<float>(sqrt(vgetq_lane_f32(vres, 2))),
1760 static_cast<float>(sqrt(vgetq_lane_f32(vres, 3)))
1761 };
1762 vres = l2_res;
Michalis Spyrou57dac842018-03-01 16:03:50 +00001763 }
1764
1765 // Store result
1766 vst1q_f32(reinterpret_cast<float *>(output.ptr()), vres);
1767 },
1768 input, output);
1769}
1770
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001771template <typename T>
1772void NEPoolingLayerKernel::poolingMxN_q8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +00001773{
1774 Iterator input(_input, window_input);
1775 Iterator output(_output, window);
1776
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001777 /** NEON vector types */
1778 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
1779 using q16_t = typename wrapper::traits::promote_t<T>;
1780 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
1781 using q32_t = typename wrapper::traits::promote_t<q16_t>;
1782 using q32x4_t = typename wrapper::traits::neon_vector<q32_t, 4>::type;
1783
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001784 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().x() : _pool_info.pool_size.width;
1785 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.height;
1786 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1787 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1788 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1789 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001790 int pool_stride_x = 0;
1791 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001792 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001793 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1794 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +00001795
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001796 const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform();
1797 const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform();
1798
Georgios Pinitas55186712018-01-08 17:37:12 +00001799 execute_window_loop(window, [&](const Coordinates & id)
1800 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001801 T res = std::numeric_limits<T>::min();
Georgios Pinitas55186712018-01-08 17:37:12 +00001802
1803 if(pooling_type != PoolingType::MAX)
1804 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001805 q32x4_t vres = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1806 q32_t sres = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +00001807
1808 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001809 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 +00001810
1811 // Perform pooling
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001812 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001813 {
1814 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001815 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001816 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001817 const q8x8_t data = wrapper::vload(reinterpret_cast<const T *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1818 (_input->info()->strides_in_bytes().y())));
Georgios Pinitas55186712018-01-08 17:37:12 +00001819
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001820 const q16x8_t data_q16 = wrapper::vmovl(data);
1821 vres = wrapper::vadd(vres, wrapper::vaddl(wrapper::vgethigh(data_q16), wrapper::vgetlow(data_q16)));
Georgios Pinitas55186712018-01-08 17:37:12 +00001822 }
1823
1824 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001825 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001826 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001827 T data = *(reinterpret_cast<const T *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1828 (_input->info()->strides_in_bytes().y())));
Georgios Pinitas55186712018-01-08 17:37:12 +00001829 sres += data;
1830 }
1831 }
1832
1833 // Reduction
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001834 const auto tmp = wrapper::vpadd(wrapper::vgethigh(vres), wrapper::vgetlow(vres));
1835 sres += wrapper::vgetlane(tmp, 0) + wrapper::vgetlane(tmp, 1);
Georgios Pinitas55186712018-01-08 17:37:12 +00001836
1837 // Divide by scale
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001838 res = static_cast<T>(support::cpp11::round(sres * scale));
Georgios Pinitas55186712018-01-08 17:37:12 +00001839 }
1840 else
1841 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001842 q8x8_t vres = wrapper::vdup_n(std::numeric_limits<T>::min(), wrapper::traits::vector_64_tag{});
Georgios Pinitas55186712018-01-08 17:37:12 +00001843
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001844 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001845 {
1846 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001847 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001848 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001849 const q8x8_t data = wrapper::vload(reinterpret_cast<const T *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1850 (_input->info()->strides_in_bytes().y())));
1851 vres = wrapper::vmax(vres, data);
Georgios Pinitas55186712018-01-08 17:37:12 +00001852 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001853 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001854 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001855 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001856 const T data = *(reinterpret_cast<const T *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int>
1857 (_input->info()->strides_in_bytes().y())));
1858 res = std::max(res, data);
Georgios Pinitas55186712018-01-08 17:37:12 +00001859 }
1860 }
1861
1862 // Reduce max
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001863 vres = wrapper::vpmax(vres, vres);
1864 vres = wrapper::vpmax(vres, vres);
1865 vres = wrapper::vpmax(vres, vres);
Georgios Pinitas55186712018-01-08 17:37:12 +00001866
1867 // Get max value
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001868 res = std::max(res, wrapper::vgetlane(vres, 0));
Georgios Pinitas55186712018-01-08 17:37:12 +00001869 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001870 // Store result
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001871 res = (input_qinfo != output_qinfo) ? Qasymm8QuantizationHelper<T>::quantize(Qasymm8QuantizationHelper<T>::dequantize(res, input_qinfo), output_qinfo) : res;
1872 *(reinterpret_cast<T *>(output.ptr())) = res;
Georgios Pinitas55186712018-01-08 17:37:12 +00001873 },
1874 input, output);
1875}
1876
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001877template <typename T>
1878void NEPoolingLayerKernel::poolingMxN_q8_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001879{
1880 Iterator input(_input, window_input);
1881 Iterator output(_output, window);
1882
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001883 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
1884 using q8x16_t = typename wrapper::traits::neon_vector<T, 16>::type;
1885 using q16_t = typename wrapper::traits::promote_t<T>;
1886 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
1887 using q32_t = typename wrapper::traits::promote_t<q16_t>;
1888 using q32x4_t = typename wrapper::traits::neon_vector<q32_t, 4>::type;
1889
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001890 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.width;
1891 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().z() : _pool_info.pool_size.height;
1892 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1893 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1894 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1895 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001896
1897 int pool_stride_x = 0;
1898 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001899 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001900 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1901 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1902
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001903 const float32x4_t half_scale_v = vdupq_n_f32(0.5f);
1904 const UniformQuantizationInfo input_qinfo = _input->info()->quantization_info().uniform();
1905 const UniformQuantizationInfo output_qinfo = _output->info()->quantization_info().uniform();
Georgios Pinitas283fc602018-11-09 10:46:43 +00001906
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001907 const float quant_rescale = output_qinfo.scale / input_qinfo.scale;
1908 // "new_offset" doesn't have to consider the "half_scale_v" in its computation
1909 // With a requantization performed in a single step there won't be uncertainties introduced
1910 const int32_t new_offset = output_qinfo.offset - static_cast<int32_t>( static_cast<float>(input_qinfo.offset) / quant_rescale);
1911
1912 const float requant_scale = output_qinfo.scale / input_qinfo.scale;
1913 const int32_t requant_offset = output_qinfo.offset - static_cast<int32_t>(static_cast<float>(input_qinfo.offset) / requant_scale);
1914 const UniformQuantizationInfo requant_qinfo = UniformQuantizationInfo(requant_scale, requant_offset);
1915
Michalis Spyrou57dac842018-03-01 16:03:50 +00001916 execute_window_loop(window, [&](const Coordinates & id)
1917 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001918 const int idx_width = id.y() * pool_stride_x;
1919 const int idx_height = id.z() * pool_stride_y;
1920 const int pool_limit_y = pool_pad_top - idx_height;
1921 const int pool_limit_x = pool_pad_left - idx_width;
1922
1923 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1924 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1925 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1926 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1927
Michalis Spyrou57dac842018-03-01 16:03:50 +00001928 if(pooling_type != PoolingType::MAX)
1929 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001930 q32x4_t vres1 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1931 q32x4_t vres2 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1932 q32x4_t vres3 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1933 q32x4_t vres4 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
Michalis Spyrou57dac842018-03-01 16:03:50 +00001934
1935 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001936 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,
1937 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001938
1939 // Perform pooling
Michalis Spyrouced25572018-10-01 16:26:20 +01001940 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001941 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001942 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001943 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001944 const q8x16_t data = wrapper::vloadq(reinterpret_cast<const T *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int>
1945 (_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001946
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001947 const q16x8_t data_q16 = wrapper::vmovl(wrapper::vgetlow(data));
1948 const q16x8_t data2_q16 = wrapper::vmovl(wrapper::vgethigh(data));
1949 vres1 = wrapper::vadd(vres1, wrapper::vmovl(wrapper::vgetlow(data_q16)));
1950 vres2 = wrapper::vadd(vres2, wrapper::vmovl(wrapper::vgethigh(data_q16)));
1951 vres3 = wrapper::vadd(vres3, wrapper::vmovl(wrapper::vgetlow(data2_q16)));
1952 vres4 = wrapper::vadd(vres4, wrapper::vmovl(wrapper::vgethigh(data2_q16)));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001953 }
1954 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001955
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001956 if(input_qinfo != output_qinfo)
1957 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001958 const float32x4x4_t vres =
1959 {
1960 {
1961 vcvtq_f32_q32(vres1),
1962 vcvtq_f32_q32(vres2),
1963 vcvtq_f32_q32(vres3),
1964 vcvtq_f32_q32(vres4),
1965 }
1966 };
1967 const auto requantized_output = vrequantize_pooling_with_scale<q8x16_t>(vres, quant_rescale, scale, new_offset);
1968 // Store result
1969 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), wrapper::vgetlow(requantized_output));
1970 wrapper::vstore(reinterpret_cast<T *>(output.ptr()) + 8, wrapper::vgethigh(requantized_output));
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001971 }
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001972 else
1973 {
1974 const float32x4_t scale_v = vdupq_n_f32(scale);
1975 // Divide by scale and add 0.5f to round to nearest instead of rounding towards zero
1976 vres1 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres1), scale_v));
1977 vres2 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres2), scale_v));
1978 vres3 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres3), scale_v));
1979 vres4 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres4), scale_v));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001980
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001981 const q8x8_t res1 = wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(vres1), wrapper::vmovn(vres2)));
1982 const q8x8_t res2 = wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(vres3), wrapper::vmovn(vres4)));
1983 // Store result
1984 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), res1);
1985 wrapper::vstore(reinterpret_cast<T *>(output.ptr()) + 8, res2);
1986 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001987 }
1988 else
1989 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001990 q8x16_t vres = wrapper::vdup_n(std::numeric_limits<T>::min(), wrapper::traits::vector_128_tag{});
Michalis Spyrou57dac842018-03-01 16:03:50 +00001991
Michalis Spyrouced25572018-10-01 16:26:20 +01001992 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001993 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001994 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001995 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001996 const q8x16_t data = wrapper::vloadq(reinterpret_cast<const T *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int>
1997 (_input->info()->strides_in_bytes().z())));
1998 vres = wrapper::vmax(vres, data);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001999 }
2000 }
2001
2002 // Store result
Manuel Bottinicf4737a2020-02-06 11:58:51 +00002003 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), (input_qinfo != output_qinfo) ? vrequantize_pooling<q8x8_t, q8x16_t>(wrapper::vgetlow(vres), wrapper::vgethigh(vres), requant_qinfo) : vres);
Michalis Spyrou57dac842018-03-01 16:03:50 +00002004 }
2005 },
2006 input, output);
2007}
2008
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002009Status NEPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
2010{
2011 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
2012
2013 unsigned int pooled_w = 0;
2014 unsigned int pooled_h = 0;
2015 unsigned int num_elems_processed_per_iteration = 0;
2016 BorderSize border_size(0);
2017
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002018 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +00002019 unsigned int pool_size_x = 0;
2020 unsigned int pool_size_y = 0;
2021
2022 // Get data layout
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00002023 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : pool_info.data_layout;
2024 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
2025 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou57dac842018-03-01 16:03:50 +00002026
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002027 pool_size_x = is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size.width;
2028 pool_size_y = is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size.height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002029
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00002030 // Validate pool info before calling scaled_dimensions
2031 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_pool_info(pool_size_x, pool_size_y));
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002032
2033 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +00002034 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
2035 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00002036 pool_size_x,
2037 pool_size_y,
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002038 pool_info.pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002039
Georgios Pinitas13d96e02018-08-23 11:20:23 +01002040 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, pooled_w, pooled_h));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00002041 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,
2042 pool_size_x, pool_size_y)
2043 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002044
2045 return Status{};
2046}
2047
Moritz Pflanzerc186b572017-09-07 09:48:04 +01002048void NEPoolingLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002049{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01002050 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002051 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
2052 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
2053 ARM_COMPUTE_ERROR_ON(_func == nullptr);
2054
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002055 const unsigned int pool_stride_x = _pool_info.pad_stride_info.stride().first;
2056 const unsigned int pool_stride_y = _pool_info.pad_stride_info.stride().second;
2057 const unsigned int pool_size = _pool_info.pool_size.width;
2058 const bool exclude_padding = _pool_info.exclude_padding;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002059
Michalis Spyrou57dac842018-03-01 16:03:50 +00002060 Window window_input(window);
Georgios Pinitas14d9d982019-12-13 12:33:09 +00002061 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002062 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00002063 // Set step for input in x and y direction for the input
2064 unsigned int window_x_inc = 0;
2065 switch(_input->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +01002066 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00002067 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +00002068 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +00002069 {
2070 window_x_inc = pool_stride_x;
2071 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
2072 {
2073 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
2074 }
2075 break;
2076 }
Pablo Tello77e6c552018-12-04 15:33:49 +00002077
Georgios Pinitas13d96e02018-08-23 11:20:23 +01002078 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +00002079 case DataType::F32:
2080 {
2081 window_x_inc = pool_stride_x;
2082 break;
2083 }
2084 default:
2085 {
2086 ARM_COMPUTE_ERROR("Not supported");
2087 }
Georgios Pinitas55186712018-01-08 17:37:12 +00002088 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00002089 window_input.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
2090 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 +01002091 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00002092 else
2093 {
Georgios Pinitascac13b12018-04-27 19:07:19 +01002094 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 +00002095 window_input.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
2096 window_input.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
2097 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002098
2099 // Run function
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002100 (this->*_func)(window_input, window, _pool_info.pool_type, exclude_padding);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002101}
Manuel Bottinib4bb8272019-12-18 18:01:27 +00002102} // namespace arm_compute