blob: d6a3fadd339f7b38f5d9605cce7b20c680ab3ab8 [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()));
Michele Di Giorgio2c877192020-02-18 19:06:27 +0000139 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(input->data_type()) && !pool_info.exclude_padding && (pool_info.pool_type == PoolingType::AVG) && pool_info.pad_stride_info.has_padding()
140 && (input->data_layout() == DataLayout::NHWC),
141 "exclude_padding equal false is not supported for AVG Pooling with padding on quantized types");
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000142
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000143 if(output->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100144 {
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000145 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000146 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
147 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w)
148 || (output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h));
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100149 }
150
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000151 return Status{};
152}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000154Status validate_arguments_pool_info(const unsigned int pool_size_x, const unsigned int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000155{
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000156 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_x == 0);
157 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_y == 0);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000158
159 return Status{};
160}
161
162std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &num_elems_processed_per_iteration,
163 BorderSize &border_size,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000164 unsigned int pooled_w, unsigned int pooled_h, int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000165{
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +0100166 // Output auto inizialitation if not yet initialized
167 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_pool_shape(*input, pool_info)));
168
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000169 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : pool_info.data_layout;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000170 unsigned int num_elems_read_per_iteration = 0;
171 unsigned int num_elems_horizontal_window = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000172 int pool_stride_x = 0;
173 int pool_stride_y = 0;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000174 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
175 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
176 const int input_width = input->dimension(idx_width);
177 const int input_height = input->dimension(idx_height);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000178 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000179 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000180 const int pool_pad_right = pad_stride_info.pad_right();
181 const int pool_pad_top = pad_stride_info.pad_top();
182 const int pool_pad_left = pad_stride_info.pad_left();
183 const int pool_pad_bottom = pad_stride_info.pad_bottom();
184 const bool is_square = pool_size_x == pool_size_y;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000185
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000186 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +0000187 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
188 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000189 pool_size_x,
190 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000191 pad_stride_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000193 //If it's not squared and optimized will be executed the MxN
194 num_elems_read_per_iteration = 1;
195 num_elems_processed_per_iteration = 1;
196 num_elems_horizontal_window = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
Michalis Spyrou57dac842018-03-01 16:03:50 +0000198 const bool is_nhwc = data_layout == DataLayout::NHWC;
199
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000200 if(is_square)
201 {
202 switch(input->data_type())
203 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000204 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000205 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000206 if(is_nhwc)
207 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100208 num_elems_processed_per_iteration = 16;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000209 break;
210 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000211 switch(pool_size_x)
212 {
213 case 2:
214 num_elems_read_per_iteration = 16;
215 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
216 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
217 break;
218 case 3:
219 num_elems_read_per_iteration = 16;
220 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
221 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
222 break;
223 default:
224 break;
225 }
226 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000227#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
228 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000229 if(is_nhwc)
230 {
231 num_elems_processed_per_iteration = 8;
232 break;
233 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000234 switch(pool_size_x)
235 {
236 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000237 case 3:
238 num_elems_read_per_iteration = 4;
239 num_elems_processed_per_iteration = 1;
240 num_elems_horizontal_window = 1;
241 break;
242 default:
243 break;
244 }
245 break;
246#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
247 case DataType::F32:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000248 if(is_nhwc)
249 {
Georgios Pinitas64f1a902018-09-18 13:42:51 +0100250 num_elems_processed_per_iteration = 4;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000251 break;
252 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000253 switch(pool_size_x)
254 {
255 case 2:
256 num_elems_read_per_iteration = 2;
257 break;
258 case 3:
259 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
260 break;
261 case 7:
262 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
263 break;
264 default:
265 break;
266 }
267 num_elems_processed_per_iteration = 1;
268 num_elems_horizontal_window = 1;
269 break;
270 default:
271 ARM_COMPUTE_ERROR("Element size not supported");
272 break;
273 }
274 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000275 else
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000276 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000277 if(is_nhwc)
278 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100279 num_elems_processed_per_iteration = 16 / input->element_size();
Michalis Spyrou57dac842018-03-01 16:03:50 +0000280 }
281 }
282
283 bool window_changed = false;
284 Window win{};
285 if(data_layout == DataLayout::NCHW)
286 {
287 // Number of iterations in X dimension
288 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
289
290 // Upper limit for the number of right/bottom border elements that are accessed
291 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;
292 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height;
293
294 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
295 border_size.right = std::max(upper_bound_w, pool_pad_right);
296 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
297
298 TensorShape output_shape{ input->tensor_shape() };
299 output_shape.set(0, pooled_w);
300 output_shape.set(1, pooled_h);
301 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
302
303 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
304 AccessWindowStatic input_access(input, -pool_pad_left, -pool_pad_top, input_width + border_size.right, input_height + border_size.bottom);
305
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000306 AccessWindowHorizontal output_access(output, 0, num_elems_horizontal_window);
307 window_changed = update_window_and_padding(win, input_access, output_access);
308 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
309 }
310 else
311 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000312 TensorShape output_shape{ input->tensor_shape() };
313 output_shape.set(1, pooled_w);
314 output_shape.set(2, pooled_h);
315 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
316
317 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
318 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
319
320 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
321 window_changed = update_window_and_padding(win, input_access, output_access);
322 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000323 }
324
325 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
326 return std::make_pair(err, win);
327}
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000328
329template <typename T>
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000330inline T vcvtq_q32_f32(float32x4_t values);
331
332template <>
333inline uint32x4_t vcvtq_q32_f32(float32x4_t values)
334{
335 return vcvtq_u32_f32(values);
336}
337
338template <>
339inline int32x4_t vcvtq_q32_f32(float32x4_t values)
340{
341 return vcvtq_s32_f32(values);
342}
343
344template <typename T>
345inline float32x4_t vcvtq_f32_q32(T values);
346
347template <>
348inline float32x4_t vcvtq_f32_q32(uint32x4_t values)
349{
350 return vcvtq_f32_u32(values);
351}
352
353template <>
354inline float32x4_t vcvtq_f32_q32(int32x4_t values)
355{
356 return vcvtq_f32_s32(values);
357}
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000358
359template <typename Tout>
360inline Tout vrequantize_pooling_with_scale(const float32x4x4_t &acc, const float quant_rescale, const float scale_pooling, const int32_t new_offset);
361
362template <>
363inline uint8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc, const float quant_rescale, const float scale_pooling, const int32_t new_offset)
364{
365 const float new_scale = quant_rescale / scale_pooling;
366 return vquantize(acc, UniformQuantizationInfo(new_scale, new_offset));
367}
368
369template <>
370inline int8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc, const float quant_rescale, const float scale_pooling, const int32_t new_offset)
371{
372 const float new_scale = quant_rescale / scale_pooling;
373 return vquantize_signed(acc, UniformQuantizationInfo(new_scale, new_offset));
374}
375
376template <typename Tin, typename Tout>
377inline Tout vrequantize_pooling(Tin vec1, Tin vec2, const UniformQuantizationInfo &requant_qinfo);
378
379template <>
380inline uint8x16_t vrequantize_pooling(uint8x8_t vec1, uint8x8_t vec2, const UniformQuantizationInfo &requant_qinfo)
381{
382 const float32x4x4_t acc =
383 {
384 {
385 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec1))))),
386 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec1))))),
387 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec2))))),
388 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec2))))),
389 }
390 };
391 return vquantize(acc, requant_qinfo);
392}
393
394template <>
395inline int8x16_t vrequantize_pooling(int8x8_t vec1, int8x8_t vec2, const UniformQuantizationInfo &requant_qinfo)
396{
397 const float32x4x4_t acc =
398 {
399 {
400 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec1))))),
401 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec1))))),
402 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec2))))),
403 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec2))))),
404 }
405 };
406 return vquantize_signed(acc, requant_qinfo);
407}
408
409template <typename T>
410inline T vrequantize_pooling(T &vec, const UniformQuantizationInfo &requant_qinfo);
411
412template <>
413inline uint8x8_t vrequantize_pooling(uint8x8_t &vec, const UniformQuantizationInfo &requant_qinfo)
414{
415 const float32x4x2_t acc =
416 {
417 {
418 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec))))),
419 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec))))),
420 }
421 };
422 return vquantize(acc, requant_qinfo);
423}
424
425template <>
426inline int8x8_t vrequantize_pooling(int8x8_t &vec, const UniformQuantizationInfo &requant_qinfo)
427{
428 const float32x4x2_t acc =
429 {
430 {
431 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec))))),
432 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec))))),
433 }
434 };
435 return vquantize_signed(acc, requant_qinfo);
436}
437
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000438} // namespace
439
440NEPoolingLayerKernel::NEPoolingLayerKernel()
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000441 : _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 +0000442{
443}
444
445BorderSize NEPoolingLayerKernel::border_size() const
446{
447 return _border_size;
448}
449
450void NEPoolingLayerKernel::configure(const ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info)
451{
452 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
453
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000454 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
455 const bool is_global_pooling = pool_info.is_global_pooling;
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000456 const int pool_stride_x = pad_stride_info.stride().first;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000457
458 // Get data layout
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000459 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->info()->data_layout() : pool_info.data_layout;
460 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
461 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000462
463 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000464 const Size2D pool_size(
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000465 is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size.width,
466 is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size.height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000467
468 // Validate pool info before calling scaled_dimensions
Pablo Tello77e6c552018-12-04 15:33:49 +0000469 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_pool_info(pool_size.x(), pool_size.y()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000470
471 // Check output dimensions
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100472 unsigned int pooled_w;
473 unsigned int pooled_h;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000474 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(idx_width),
475 input->info()->dimension(idx_height),
Pablo Tello77e6c552018-12-04 15:33:49 +0000476 pool_size.x(),
477 pool_size.y(),
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000478 pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000479
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000480 // Perform validation step
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100481 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, pooled_w, pooled_h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100482
483 // Set instance variables
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000484 _input = input;
485 _output = output;
486 _pool_info = pool_info;
487 _data_layout = input->info()->data_layout();
488 _is_square = (pool_size.x() == pool_size.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100489
Georgios Pinitas55186712018-01-08 17:37:12 +0000490 // Get data type
491 const DataType data_type = input->info()->data_type();
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000492 const bool is_nchw = _data_layout == DataLayout::NCHW;
Georgios Pinitas55186712018-01-08 17:37:12 +0000493
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100494 if(data_type == DataType::QASYMM8)
Georgios Pinitas55186712018-01-08 17:37:12 +0000495 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000496 if(pool_size.x() == 2 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000497 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000498 if(is_nchw)
Michalis Spyroubbd9fb92017-06-22 12:57:51 +0100499 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000500 _func = &NEPoolingLayerKernel::pooling2_q8_nchw<uint8_t>;
Pablo Tello77e6c552018-12-04 15:33:49 +0000501 }
502 else
503 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000504 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<uint8_t>;
Georgios Pinitas55186712018-01-08 17:37:12 +0000505 }
506 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000507 else if(pool_size.x() == 3 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000508 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000509 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000510 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000511 _func = &NEPoolingLayerKernel::pooling3_q8_nchw<uint8_t>;
Pablo Tello77e6c552018-12-04 15:33:49 +0000512 }
513 else
514 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000515 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<uint8_t>;
Georgios Pinitas55186712018-01-08 17:37:12 +0000516 }
517 }
518 else
519 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000520 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000521 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000522 _func = &NEPoolingLayerKernel::poolingMxN_q8_nchw<uint8_t>;
Pablo Tello77e6c552018-12-04 15:33:49 +0000523 }
524 else
525 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000526 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<uint8_t>;
527 }
528 }
529 }
530 else if(data_type == DataType::QASYMM8_SIGNED)
531 {
532 if(pool_size.x() == 2 && pool_stride_x < 3 && _is_square)
533 {
534 if(is_nchw)
535 {
536 _func = &NEPoolingLayerKernel::pooling2_q8_nchw<int8_t>;
537 }
538 else
539 {
540 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<int8_t>;
541 }
542 }
543 else if(pool_size.x() == 3 && pool_stride_x < 3 && _is_square)
544 {
545 if(is_nchw)
546 {
547 _func = &NEPoolingLayerKernel::pooling3_q8_nchw<int8_t>;
548 }
549 else
550 {
551 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<int8_t>;
552 }
553 }
554 else
555 {
556 if(is_nchw)
557 {
558 _func = &NEPoolingLayerKernel::poolingMxN_q8_nchw<int8_t>;
559 }
560 else
561 {
562 _func = &NEPoolingLayerKernel::poolingMxN_q8_nhwc<int8_t>;
Georgios Pinitas55186712018-01-08 17:37:12 +0000563 }
564 }
565 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000566 else if(data_type == DataType::F16)
567 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000568 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000569 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000570 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000571 {
572 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000573 {
574 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000575 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000576 _func = &NEPoolingLayerKernel::pooling2_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000577 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000578 else
579 {
580 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
581 }
582 }
583 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000584 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000585 {
586 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000587 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000588 _func = &NEPoolingLayerKernel::pooling3_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000589 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000590 else
591 {
592 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
593 }
594 }
595 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000596 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000597 {
598 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000599 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000600 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
601 }
602 else
603 {
604 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000605 }
606 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000607 }
608 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000609 }
610 }
611 else
612 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000613 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000614 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000615 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
616 }
617 else
618 {
619 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000620 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000621 }
622 }
623 else if(data_type == DataType::F32)
624 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000625 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000626 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000627 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000628 {
629 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000630 {
631 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000632 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000633 _func = &NEPoolingLayerKernel::pooling2_f32_nchw;
634 }
635 else
636 {
637 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000638 }
639 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000640 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000641 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000642 {
643 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000644 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000645 _func = &NEPoolingLayerKernel::pooling3_f32_nchw;
646 }
647 else
648 {
649 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000650 }
651 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000652 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000653 case 7:
Pablo Tello77e6c552018-12-04 15:33:49 +0000654 {
655 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000656 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000657 _func = &NEPoolingLayerKernel::pooling7_f32_nchw;
658 }
659 else
660 {
661 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000662 }
663 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000664 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000665 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000666 {
667 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000668 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000669 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
670 }
671 else
672 {
673 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000674 }
675 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000676 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000677 }
678 }
679 else
680 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000681 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000682 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000683 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
684 }
685 else
686 {
687 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000688 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000689 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100690 }
691
692 // Configure kernel window
Pablo Tello77e6c552018-12-04 15:33:49 +0000693 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 +0000694 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
695 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100696}
697
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000698template <typename T>
699void NEPoolingLayerKernel::pooling2_q8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000700{
701 Iterator input(_input, window_input);
702 Iterator output(_output, window);
703
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000704 /** NEON vector types */
705 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
706 using q8x16_t = typename wrapper::traits::neon_vector<T, 16>::type;
707 using q8x8x2_t = typename std::conditional<std::is_same<T, uint8_t>::value, uint8x8x2_t, int8x8x2_t>::type;
708 using q16_t = typename wrapper::traits::promote_t<T>;
709 using q16x4_t = typename wrapper::traits::neon_vector<q16_t, 4>::type;
710 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
711 using q16x8x2_t = typename wrapper::traits::neon_vector<q16_t, 16>::type;
712
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000713 constexpr int pool_size = 2;
714 int pool_stride_x = 0;
715 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000716 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
717 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
718 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
719 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
720 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000721 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
722 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000723
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000724 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))));
725 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 +0000726
727 const int scale_step_x = (pool_stride_x == 1) ? 2 : 1;
728
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100729 const UniformQuantizationInfo input_qinfo = _input->info()->quantization_info().uniform();
730 const UniformQuantizationInfo output_qinfo = _output->info()->quantization_info().uniform();
731 const bool have_different_qinfo = input_qinfo != output_qinfo;
732
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000733 const float requant_scale = output_qinfo.scale / input_qinfo.scale;
734 const int32_t requant_offset = output_qinfo.offset - static_cast<int32_t>(static_cast<float>(input_qinfo.offset) / requant_scale);
735 const UniformQuantizationInfo requant_qinfo = UniformQuantizationInfo(requant_scale, requant_offset);
736
Georgios Pinitas55186712018-01-08 17:37:12 +0000737 execute_window_loop(window, [&](const Coordinates & id)
738 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000739 const auto top_data = wrapper::vloadq(input_top_ptr + input.offset());
740 const auto bottom_data = wrapper::vloadq(input_bottom_ptr + input.offset());
741 q8x8_t lower_res = {};
742 q8x8_t upper_res = {};
Georgios Pinitas55186712018-01-08 17:37:12 +0000743
744 if(pooling_type != PoolingType::MAX)
745 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000746 const q16x8x2_t top_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(top_data)), wrapper::vmovl(wrapper::vgethigh(top_data)) } };
747 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 +0000748
749 // Add rows
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000750 const q16x8x2_t vrsum =
Georgios Pinitas55186712018-01-08 17:37:12 +0000751 {
752 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000753 wrapper::vadd(top_data_q16.val[0], bottom_data_q16.val[0]),
754 wrapper::vadd(top_data_q16.val[1], bottom_data_q16.val[1]),
Georgios Pinitas55186712018-01-08 17:37:12 +0000755 }
756 };
757
758 // Pair-wise add row data
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000759 const q16x4_t vpsum_1 = wrapper::vpadd(wrapper::vgetlow(vrsum.val[0]), wrapper::vgethigh(vrsum.val[0]));
760 const q16x4_t vpsum_2 = wrapper::vpadd(wrapper::vgetlow(vrsum.val[1]), wrapper::vgethigh(vrsum.val[1]));
Georgios Pinitas55186712018-01-08 17:37:12 +0000761
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000762 q16x8_t res_lower = wrapper::vcombine(vpsum_1, vpsum_2);
Georgios Pinitas55186712018-01-08 17:37:12 +0000763
764 // Scale lower result
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000765 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, res_lower, id, 0, scale_step_x,
766 pool_size, upper_bound_w, upper_bound_h,
767 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
768 lower_res = wrapper::vmovn(res_lower);
Georgios Pinitas55186712018-01-08 17:37:12 +0000769
770 // Compute upper result for stride_x == 1
771 if(pool_stride_x == 1)
772 {
773 // Shifted row sum
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000774 const q16x8x2_t vrsum_shifted =
Georgios Pinitas55186712018-01-08 17:37:12 +0000775 {
776 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000777 wrapper::vext_1(vrsum.val[0], vrsum.val[1]),
778 wrapper::vext_1(vrsum.val[1], vrsum.val[1])
Georgios Pinitas55186712018-01-08 17:37:12 +0000779 }
780 };
781
782 // Pair-wise add shifted row
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000783 q16x8_t res_upper = wrapper::vcombine(
784 wrapper::vpadd(wrapper::vgetlow(vrsum_shifted.val[0]), wrapper::vgethigh(vrsum_shifted.val[0])),
785 wrapper::vpadd(wrapper::vgetlow(vrsum_shifted.val[1]), wrapper::vgethigh(vrsum_shifted.val[1])));
Georgios Pinitas55186712018-01-08 17:37:12 +0000786
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000787 // Scale upper result
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000788 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, res_upper, id, 1, 2,
789 pool_size, upper_bound_w, upper_bound_h,
790 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
791 upper_res = wrapper::vmovn(res_upper);
Georgios Pinitas55186712018-01-08 17:37:12 +0000792 }
793 }
794 else
795 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000796 const q8x16_t max_data = wrapper::vmax(top_data, bottom_data);
797 lower_res = wrapper::vpmax(wrapper::vgetlow(max_data), wrapper::vgethigh(max_data));
Georgios Pinitas55186712018-01-08 17:37:12 +0000798 if(pool_stride_x == 1)
799 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000800 const q8x16_t max_data_shifted = wrapper::vext_1(max_data, max_data);
801 upper_res = wrapper::vpmax(wrapper::vgetlow(max_data_shifted), wrapper::vgethigh(max_data_shifted));
Georgios Pinitas55186712018-01-08 17:37:12 +0000802 }
803 }
804
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100805 if(have_different_qinfo)
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100806 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000807 const auto requantized_output = vrequantize_pooling<q8x8_t, q8x16_t>(lower_res, upper_res, requant_qinfo);
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000808 lower_res = wrapper::vgetlow(requantized_output);
809 upper_res = wrapper::vgethigh(requantized_output);
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100810 }
811
Georgios Pinitas55186712018-01-08 17:37:12 +0000812 // Store result
813 if(pool_stride_x == 1)
814 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000815 const q8x8x2_t res = { { lower_res, upper_res } };
816 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), res);
Georgios Pinitas55186712018-01-08 17:37:12 +0000817 }
818 else
819 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000820 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), lower_res);
Georgios Pinitas55186712018-01-08 17:37:12 +0000821 }
822 },
823 input, output);
824}
825
Pablo Tello77e6c552018-12-04 15:33:49 +0000826void NEPoolingLayerKernel::pooling3_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100827{
Pablo Tello77e6c552018-12-04 15:33:49 +0000828 ARM_COMPUTE_UNUSED(pooling_type);
829 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000830#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100831 Iterator input(_input, window_input);
832 Iterator output(_output, window);
833
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000834 constexpr const int pool_size = 3;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000835 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
836 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
837 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
838 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000839 int pool_stride_x = 0;
840 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000841 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000842 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
843 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100844
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000845 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
846 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));
847 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 +0100848
849 execute_window_loop(window, [&](const Coordinates & id)
850 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100851 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
852 float16x4_t middle_data = vld1_f16(reinterpret_cast<const float16_t *>(input_middle_ptr + input.offset()));
853 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
854 float16x4_t res = {};
855
856 // Get power of 2 in case of l2 pooling
857 if(pooling_type == PoolingType::L2)
858 {
859 top_data = vmul_f16(top_data, top_data);
860 middle_data = vmul_f16(middle_data, middle_data);
861 bottom_data = vmul_f16(bottom_data, bottom_data);
862 }
863
864 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100865 {
866 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000867 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 +0100868 const float16x4_t scale_v = vdup_n_f16(scale);
869 // Perform pooling
870 const float16x4_t sum_data = vadd_f16(vadd_f16(top_data, bottom_data), middle_data);
871 res = vpadd_f16(vset_lane_f16(0.f, sum_data, 3), sum_data);
872 res = vmul_f16(vpadd_f16(res, res), scale_v);
873 }
874 else
875 {
876 const float16x4_t max_data = vmax_f16(vmax_f16(top_data, bottom_data), middle_data);
877 res = vpmax_f16(vset_lane_f16(-std::numeric_limits<float>::max(), max_data, 3), max_data);
878 res = vpmax_f16(res, res);
879 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100880
881 // Calculate square-root in case of l2 pooling
882 if(pooling_type == PoolingType::L2)
883 {
884 res = vinv_f16(vinvsqrt_f16(res));
885 }
886
Pablo Tello0c34fe22017-06-26 17:17:42 +0100887 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
888 },
889 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000890#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100891 ARM_COMPUTE_UNUSED(window_input);
892 ARM_COMPUTE_UNUSED(window);
893 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000894#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100895}
896
Pablo Tello77e6c552018-12-04 15:33:49 +0000897void NEPoolingLayerKernel::pooling2_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100898{
Pablo Tello77e6c552018-12-04 15:33:49 +0000899 ARM_COMPUTE_UNUSED(pooling_type);
900 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000901#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100902 Iterator input(_input, window_input);
903 Iterator output(_output, window);
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000904 constexpr int pool_size = 2;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000905 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
906 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
907 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
908 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000909 int pool_stride_x, pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000910 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000911 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
912 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100913
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000914 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
915 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 +0100916
917 execute_window_loop(window, [&](const Coordinates & id)
918 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100919 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
920 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
921 float16x4_t res = {};
Pablo Tello0c34fe22017-06-26 17:17:42 +0100922
Georgios Pinitascdf51452017-08-31 14:21:36 +0100923 // Get power of 2 in case of l2 pooling
924 if(pooling_type == PoolingType::L2)
925 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100926 top_data = vmul_f16(top_data, top_data);
927 bottom_data = vmul_f16(bottom_data, bottom_data);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100928 }
929
930 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100931 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000932 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 +0100933 const float16x4_t scale_v = vdup_n_f16(scale);
934
935 const float16x4_t sum_data = vadd_f16(top_data, bottom_data);
936 res = vmul_f16(vpadd_f16(sum_data, sum_data), scale_v);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100937 }
938 else
939 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100940 const float16x4_t max_data = vmax_f16(top_data, bottom_data);
941 res = vpmax_f16(max_data, max_data);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100942 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100943
944 // Calculate square-root in case of l2 pooling
945 if(pooling_type == PoolingType::L2)
946 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100947 res = vinv_f16(vinvsqrt_f16(res));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100948 }
949
950 // Store result
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100951 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100952 },
953 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000954#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100955 ARM_COMPUTE_UNUSED(window_input);
956 ARM_COMPUTE_UNUSED(window);
957 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000958#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100959}
960
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000961template <typename T>
962void NEPoolingLayerKernel::pooling3_q8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000963{
964 Iterator input(_input, window_input);
965 Iterator output(_output, window);
966
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000967 /** NEON vector types */
968 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
969 using q8x16_t = typename wrapper::traits::neon_vector<T, 16>::type;
970 using q8x8x2_t = typename std::conditional<std::is_same<T, uint8_t>::value, uint8x8x2_t, int8x8x2_t>::type;
971 using q16_t = typename wrapper::traits::promote_t<T>;
972 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
973 using q16x8x2_t = typename wrapper::traits::neon_vector<q16_t, 16>::type;
974
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000975 constexpr int pool_size = 3;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000976 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
977 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
978 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
979 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000980 int pool_stride_x = 0;
981 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000982 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000983 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
984 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000985
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100986 const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform();
987 const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform();
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100988
Manuel Bottinicf4737a2020-02-06 11:58:51 +0000989 const float requant_scale = output_qinfo.scale / input_qinfo.scale;
990 const int32_t requant_offset = output_qinfo.offset - static_cast<int32_t>(static_cast<float>(input_qinfo.offset) / requant_scale);
991 const UniformQuantizationInfo requant_qinfo = UniformQuantizationInfo(requant_scale, requant_offset);
992
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000993 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))));
994 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)));
995 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 +0000996
997 execute_window_loop(window, [&](const Coordinates & id)
998 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000999 const auto top_data = wrapper::vloadq(input_top_ptr + input.offset());
1000 const auto middle_data = wrapper::vloadq(input_middle_ptr + input.offset());
1001 const auto bottom_data = wrapper::vloadq(input_bottom_ptr + input.offset());
1002 q8x8_t fres = {};
1003 q8x16_t fqres = {};
Georgios Pinitas55186712018-01-08 17:37:12 +00001004
1005 if(pooling_type == PoolingType::AVG)
1006 {
1007 // Convert data to u16
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001008 const q16x8x2_t top_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(top_data)), wrapper::vmovl(wrapper::vgethigh(top_data)) } };
1009 const q16x8x2_t middle_data_q16 = { { wrapper::vmovl(wrapper::vgetlow(middle_data)), wrapper::vmovl(wrapper::vgethigh(middle_data)) } };
1010 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 +00001011
1012 // Calculate row sums
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001013 const q16x8x2_t vrsum =
Georgios Pinitas55186712018-01-08 17:37:12 +00001014 {
1015 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001016 wrapper::vadd(wrapper::vadd(top_data_q16.val[0], bottom_data_q16.val[0]), middle_data_q16.val[0]),
1017 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 +00001018 }
1019 };
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001020 const q16x8x2_t vrsum_shifted_1 =
Georgios Pinitas55186712018-01-08 17:37:12 +00001021 {
1022 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001023 wrapper::vext_1(vrsum.val[0], vrsum.val[1]),
1024 wrapper::vext_1(vrsum.val[1], vrsum.val[1])
Georgios Pinitas55186712018-01-08 17:37:12 +00001025 }
1026 };
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001027 const q16x8x2_t vrsum_shifted_2 =
Georgios Pinitas55186712018-01-08 17:37:12 +00001028 {
1029 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001030 wrapper::vext_2(vrsum.val[0], vrsum.val[1]),
1031 wrapper::vext_2(vrsum.val[1], vrsum.val[1])
Georgios Pinitas55186712018-01-08 17:37:12 +00001032 }
1033 };
1034 // Calculate final sum
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001035 q16x8x2_t final_sum =
Georgios Pinitas55186712018-01-08 17:37:12 +00001036 {
1037 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001038 wrapper::vadd(wrapper::vadd(vrsum.val[0], vrsum_shifted_1.val[0]), vrsum_shifted_2.val[0]),
1039 wrapper::vadd(wrapper::vadd(vrsum.val[1], vrsum_shifted_1.val[1]), vrsum_shifted_2.val[1]),
Georgios Pinitas55186712018-01-08 17:37:12 +00001040 }
1041 };
1042 if(pool_stride_x == 2)
1043 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001044 q16x8_t res =
Georgios Pinitas55186712018-01-08 17:37:12 +00001045 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001046 wrapper::vgetlane(final_sum.val[0], 0),
1047 wrapper::vgetlane(final_sum.val[0], 2),
1048 wrapper::vgetlane(final_sum.val[0], 4),
1049 wrapper::vgetlane(final_sum.val[0], 6),
1050 wrapper::vgetlane(final_sum.val[1], 0),
1051 wrapper::vgetlane(final_sum.val[1], 2),
1052 wrapper::vgetlane(final_sum.val[1], 4),
1053 wrapper::vgetlane(final_sum.val[1], 6),
Georgios Pinitas55186712018-01-08 17:37:12 +00001054 };
1055
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001056 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, res, id, 0, 1,
1057 pool_size, upper_bound_w, upper_bound_h,
1058 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
1059 fres = wrapper::vmovn(res);
Georgios Pinitas55186712018-01-08 17:37:12 +00001060 }
1061 else
1062 {
1063 // Scale lower result
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001064 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, final_sum.val[0], id, 0, 1,
1065 pool_size, upper_bound_w, upper_bound_h,
1066 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +00001067 // Scale lower result
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001068 scale_vector_q16x8<q16_t, q16x8_t>(exclude_padding, final_sum.val[1], id, 8, 1,
1069 pool_size, upper_bound_w, upper_bound_h,
1070 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
1071 fqres = wrapper::vcombine(wrapper::vmovn(final_sum.val[0]), wrapper::vmovn(final_sum.val[1]));
Georgios Pinitas55186712018-01-08 17:37:12 +00001072 }
1073 }
1074 else
1075 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001076 const q8x16_t max_data = wrapper::vmax(wrapper::vmax(top_data, bottom_data), middle_data);
1077 const q8x16_t max_data_shift1 = wrapper::vext_1(max_data, max_data);
1078 const q8x16_t max_data_shift2 = wrapper::vext_2(max_data, max_data);
1079 const q8x16_t final_max = wrapper::vmax(wrapper::vmax(max_data, max_data_shift1), max_data_shift2);
Georgios Pinitas55186712018-01-08 17:37:12 +00001080
1081 if(pool_stride_x == 2)
1082 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001083 const q8x8x2_t table = { { wrapper::vgetlow(final_max), wrapper::vgethigh(final_max) } };
1084 static const q8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 };
1085 fres = wrapper::vtbl(table, lookup_val);
Georgios Pinitas55186712018-01-08 17:37:12 +00001086 }
1087 else
1088 {
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001089 fqres = final_max;
Georgios Pinitas55186712018-01-08 17:37:12 +00001090 }
1091 }
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001092
1093 // Store result
1094 if(pool_stride_x == 1)
1095 {
1096 if(input_qinfo != output_qinfo)
1097 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001098 fqres = vrequantize_pooling<q8x8_t, q8x16_t>(wrapper::vgetlow(fqres), wrapper::vgethigh(fqres), requant_qinfo);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001099 }
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001100 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), fqres);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001101 }
1102 else
1103 {
1104 if(input_qinfo != output_qinfo)
1105 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001106 fres = vrequantize_pooling<q8x8_t>(fres, requant_qinfo);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001107 }
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001108 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), fres);
Georgios Pinitasd66094e2019-04-15 15:44:17 +01001109 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001110 },
1111 input, output);
1112}
1113
Pablo Tello77e6c552018-12-04 15:33:49 +00001114void NEPoolingLayerKernel::poolingMxN_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001115{
Pablo Tello77e6c552018-12-04 15:33:49 +00001116 ARM_COMPUTE_UNUSED(pooling_type);
1117 ARM_COMPUTE_UNUSED(exclude_padding);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001118#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1119 Iterator input(_input, window_input);
1120 Iterator output(_output, window);
1121
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001122 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().x() : _pool_info.pool_size.width;
1123 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.height;
1124 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1125 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1126 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1127 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001128 int pool_stride_x = 0;
1129 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001130 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001131 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1132 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1133
1134 execute_window_loop(window, [&](const Coordinates & id)
1135 {
1136 float16_t res = 0.0f;
1137 float16x8_t vres = vdupq_n_f16(0.0f);
1138
1139 if(pooling_type != PoolingType::MAX)
1140 {
1141 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001142 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 +00001143
1144 // Perform pooling
1145
1146 for(int y = 0; y < pool_size_y; ++y)
1147 {
1148 int x = 0;
1149 for(; x <= (pool_size_x - 8); x += 8)
1150 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001151 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()) +
1152 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001153
1154 // Get power of 2 in case of l2 pooling and accumulate
1155 if(pooling_type == PoolingType::L2)
1156 {
1157 vres = vaddq_f16(vres, vmulq_f16(data, data));
1158 }
1159 else
1160 {
1161 vres = vaddq_f16(vres, data);
1162 }
1163 }
1164
1165 // Leftover for loop
1166 for(; x < pool_size_x; ++x)
1167 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001168 float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x())
1169 + (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001170
1171 // Get power of 2 in case of l2 pooling
1172 if(pooling_type == PoolingType::L2)
1173 {
1174 data *= data;
1175 }
1176
1177 res += data;
1178 }
1179 }
1180
1181 // Reduction
1182 float16x4_t tmp = vpadd_f16(vget_high_f16(vres), vget_low_f16(vres));
1183 res += vget_lane_f16(tmp, 0);
1184 res += vget_lane_f16(tmp, 1);
1185 res += vget_lane_f16(tmp, 2);
1186 res += vget_lane_f16(tmp, 3);
1187
1188 // Divide by scale
1189 res *= scale;
1190 }
1191 else
1192 {
1193 float16x8_t vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
1194 res = std::numeric_limits<float>::lowest();
1195
1196 for(int y = 0; y < pool_size_y; ++y)
1197 {
1198 int x = 0;
1199 for(; x <= (pool_size_x - 8); x += 8)
1200 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001201 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()) +
1202 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001203 vres = vmaxq_f16(vres, data);
1204 }
1205
1206 // Leftover for loop
1207 for(; x < pool_size_x; ++x)
1208 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001209 const float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x())
1210 + (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().y())));
1211 res = std::max(res, data);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001212 }
1213 }
1214
1215 float16x4_t tmp = vpmax_f16(vget_high_f16(vres), vget_low_f16(vres));
1216 res = std::max(res, vget_lane_f16(tmp, 0));
1217 res = std::max(res, vget_lane_f16(tmp, 1));
1218 res = std::max(res, vget_lane_f16(tmp, 2));
1219 res = std::max(res, vget_lane_f16(tmp, 3));
1220 }
1221
1222 // Calculate square-root in case of l2 pooling
1223 if(pooling_type == PoolingType::L2)
1224 {
1225 res = std::sqrt(res);
1226 }
1227
1228 // Store result
1229 *(reinterpret_cast<float16_t *>(output.ptr())) = res;
1230 },
1231 input, output);
1232
1233#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1234 ARM_COMPUTE_UNUSED(window_input);
1235 ARM_COMPUTE_UNUSED(window);
1236 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1237#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1238}
1239
Pablo Tello77e6c552018-12-04 15:33:49 +00001240void NEPoolingLayerKernel::poolingMxN_f16_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001241{
Pablo Tello77e6c552018-12-04 15:33:49 +00001242 ARM_COMPUTE_UNUSED(pooling_type);
1243 ARM_COMPUTE_UNUSED(exclude_padding);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001244#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1245 Iterator input(_input, window_input);
1246 Iterator output(_output, window);
1247
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001248 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.width;
1249 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().z() : _pool_info.pool_size.height;
1250 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1251 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1252 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1253 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001254 int pool_stride_x = 0;
1255 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001256 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001257 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1258 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1259
1260 float16x8_t vres;
1261
1262 execute_window_loop(window, [&](const Coordinates & id)
1263 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001264 const int idx_width = id.y() * pool_stride_x;
1265 const int idx_height = id.z() * pool_stride_y;
1266 const int pool_limit_y = pool_pad_top - idx_height;
1267 const int pool_limit_x = pool_pad_left - idx_width;
1268
1269 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1270 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1271 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1272 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1273
Michalis Spyrou57dac842018-03-01 16:03:50 +00001274 if(pooling_type != PoolingType::MAX)
1275 {
1276 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001277 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,
1278 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001279 const float16x8_t scale_v = vdupq_n_f16(scale);
1280
1281 // Perform pooling
1282 vres = vdupq_n_f16(0.0f);
Michalis Spyrouced25572018-10-01 16:26:20 +01001283 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001284 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001285 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001286 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001287 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()) +
1288 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001289
1290 // Get power of 2 in case of l2 pooling and accumulate
1291 if(pooling_type == PoolingType::L2)
1292 {
1293 vres = vaddq_f16(vres, vmulq_f16(data, data));
1294 }
1295 else
1296 {
1297 vres = vaddq_f16(vres, data);
1298 }
1299 }
1300 }
1301 // Divide by scale
1302 vres = vmulq_f16(vres, scale_v);
1303 }
1304 else
1305 {
1306 vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001307
1308 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001309 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001310 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001311 {
Georgios Pinitas0922dbb2019-11-25 18:39:27 +00001312 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()) +
1313 (y - pool_pad_top) * static_cast<int>(_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001314 vres = vmaxq_f16(vres, data);
1315 }
1316 }
1317 }
1318
1319 // Calculate square-root in case of l2 pooling
1320 if(pooling_type == PoolingType::L2)
1321 {
1322 float16x8_t sqrt_reciprocal = vrsqrteq_f16(vres);
1323 vres = vmulq_f16(vres, vmulq_f16(vrsqrtsq_f16(vmulq_f16(vres, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal));
1324 }
1325
1326 // Store result
1327 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vres);
1328 },
1329 input, output);
1330
1331#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1332 ARM_COMPUTE_UNUSED(window_input);
1333 ARM_COMPUTE_UNUSED(window);
1334 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1335#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1336}
1337
Pablo Tello77e6c552018-12-04 15:33:49 +00001338void NEPoolingLayerKernel::poolingMxN_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001339{
1340 Iterator input(_input, window_input);
1341 Iterator output(_output, window);
1342
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001343 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().x() : _pool_info.pool_size.width;
1344 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.height;
1345 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1346 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1347 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1348 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001349 int pool_stride_x = 0;
1350 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001351 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001352 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1353 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Gian Marco Iodice16824302017-09-28 15:41:37 +01001354
1355 execute_window_loop(window, [&](const Coordinates & id)
1356 {
1357 float res = 0.0f;
1358
1359 if(pooling_type != PoolingType::MAX)
1360 {
1361 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001362 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 +01001363
1364 // Perform pooling
1365 float32x4_t vres = vdupq_n_f32(0.0f);
1366
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001367 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001368 {
1369 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001370 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001371 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001372 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>
1373 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001374
1375 // Get power of 2 in case of l2 pooling and accumulate
1376 if(pooling_type == PoolingType::L2)
1377 {
1378 vres = vmlaq_f32(vres, data, data);
1379 }
1380 else
1381 {
1382 vres = vaddq_f32(vres, data);
1383 }
1384 }
1385
1386 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001387 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001388 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001389 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>
1390 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001391
1392 // Get power of 2 in case of l2 pooling
1393 if(pooling_type == PoolingType::L2)
1394 {
1395 data *= data;
1396 }
1397
1398 res += data;
1399 }
1400 }
1401
1402#if defined(__aarch64__)
1403 // Reduction operation available on 64 bit architectures only
1404 res += vaddvq_f32(vres);
1405#else // __aarch64__
1406 // Reduction
1407 float32x2_t tmp = vpadd_f32(vget_high_f32(vres), vget_low_f32(vres));
1408 tmp = vpadd_f32(tmp, tmp);
1409
1410 res += vget_lane_f32(tmp, 0);
1411#endif // __aarch64__
1412 // Divide by scale
1413 res *= scale;
1414 }
1415 else
1416 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001417 float32x4_t vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
1418 res = std::numeric_limits<float>::lowest();
Gian Marco Iodice16824302017-09-28 15:41:37 +01001419
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001420 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001421 {
1422 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001423 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001424 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001425 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>
1426 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001427 vres = vmaxq_f32(vres, data);
1428 }
1429
1430 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001431 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001432 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001433 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>
1434 (_input->info()->strides_in_bytes().y())));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001435 res = std::max(res, data);
1436 }
1437 }
1438
1439#if defined(__aarch64__)
1440 // Reduction operation available on 64 bit architectures only
1441 res = std::max(vmaxvq_f32(vres), res);
1442#else // __aarch64__
1443 float32x2_t tmp = vpmax_f32(vget_high_f32(vres), vget_low_f32(vres));
1444 tmp = vpmax_f32(tmp, tmp);
1445
1446 res = std::max(res, vget_lane_f32(tmp, 0));
1447#endif // __aarch64__
1448 }
1449
1450 // Calculate square-root in case of l2 pooling
1451 if(pooling_type == PoolingType::L2)
1452 {
1453 res = std::sqrt(res);
1454 }
1455
1456 // Store result
1457 *(reinterpret_cast<float *>(output.ptr())) = res;
1458 },
1459 input, output);
1460}
1461
Pablo Tello77e6c552018-12-04 15:33:49 +00001462void NEPoolingLayerKernel::pooling2_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1463{
1464 Iterator input(_input, window_input);
1465 Iterator output(_output, window);
1466
1467 constexpr int pool_size = 2;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001468 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1469 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1470 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1471 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Pablo Tello77e6c552018-12-04 15:33:49 +00001472 int pool_stride_x = 0;
1473 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001474 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Pablo Tello77e6c552018-12-04 15:33:49 +00001475 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1476 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1477
1478 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1479 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));
1480
1481 execute_window_loop(window, [&](const Coordinates & id)
1482 {
1483 float32x2_t top_data = vld1_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1484 float32x2_t bottom_data = vld1_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1485 float32x2_t res = {};
1486 float final_res = 0;
1487
1488 // Get power of 2 in case of l2 pooling
1489 if(pooling_type == PoolingType::L2)
1490 {
1491 top_data = vmul_f32(top_data, top_data);
1492 bottom_data = vmul_f32(bottom_data, bottom_data);
1493 }
1494
1495 if(pooling_type != PoolingType::MAX)
1496 {
1497 // Calculate scale
1498 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);
1499 const float32x2_t scale_v = vdup_n_f32(scale);
1500
1501 // Perform pooling
1502 const float32x2_t sum_data = vadd_f32(top_data, bottom_data);
1503 res = vmul_f32(vpadd_f32(sum_data, sum_data), scale_v);
1504 }
1505 else
1506 {
1507 const float32x2_t max_data = vmax_f32(top_data, bottom_data);
1508 res = vpmax_f32(max_data, max_data);
1509 }
1510 final_res = vget_lane_f32(res, 0);
1511
1512 // Calculate square-root in case of l2 pooling
1513 if(pooling_type == PoolingType::L2)
1514 {
1515 final_res = sqrt(final_res);
1516 }
1517
1518 // Store result
1519 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1520 },
1521 input, output);
1522}
1523
1524void NEPoolingLayerKernel::pooling3_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1525{
1526 Iterator input(_input, window_input);
1527 Iterator output(_output, window);
1528
1529 constexpr const int pool_size = 3;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001530 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1531 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1532 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1533 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Pablo Tello77e6c552018-12-04 15:33:49 +00001534 int pool_stride_x = 0;
1535 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001536 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Pablo Tello77e6c552018-12-04 15:33:49 +00001537 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1538 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1539
1540 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1541 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));
1542 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));
1543
1544 execute_window_loop(window, [&](const Coordinates & id)
1545 {
1546 float32x4_t top_data = vld1q_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1547 float32x4_t middle_data = vld1q_f32(reinterpret_cast<const float *>(input_middle_ptr + input.offset()));
1548 float32x4_t bottom_data = vld1q_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1549 float32x2_t res = {};
1550 float final_res = 0;
1551
1552 // Get power of 2 in case of l2 pooling
1553 if(pooling_type == PoolingType::L2)
1554 {
1555 top_data = vmulq_f32(top_data, top_data);
1556 middle_data = vmulq_f32(middle_data, middle_data);
1557 bottom_data = vmulq_f32(bottom_data, bottom_data);
1558 }
1559
1560 if(pooling_type != PoolingType::MAX)
1561 {
1562 // Calculate scale
1563 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);
1564 const float32x2_t scale_v = vdup_n_f32(scale);
1565
1566 // Perform pooling
1567 const float32x4_t sum_data = vaddq_f32(vaddq_f32(top_data, bottom_data), middle_data);
1568 res = vpadd_f32(vget_high_f32(vsetq_lane_f32(0.f, sum_data, 3)), vget_low_f32(sum_data));
1569 res = vmul_f32(vpadd_f32(res, res), scale_v);
1570 }
1571 else
1572 {
1573 const float32x4_t max_data = vmaxq_f32(vmaxq_f32(top_data, bottom_data), middle_data);
1574 res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data, 3)), vget_low_f32(max_data));
1575 res = vpmax_f32(res, res);
1576 }
1577 final_res = vget_lane_f32(res, 0);
1578
1579 // Calculate square-root in case of l2 pooling
1580 if(pooling_type == PoolingType::L2)
1581 {
1582 final_res = sqrt(final_res);
1583 }
1584
1585 // Store result
1586 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1587 },
1588 input, output);
1589}
1590
1591void NEPoolingLayerKernel::pooling7_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1592{
1593 Iterator input(_input, window_input);
1594 Iterator output(_output, window);
1595
1596 constexpr const int pool_size = 7;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001597 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1598 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1599 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1600 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Pablo Tello77e6c552018-12-04 15:33:49 +00001601 int pool_stride_x = 0;
1602 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001603 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Pablo Tello77e6c552018-12-04 15:33:49 +00001604 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1605 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1606
1607 std::array<const uint8_t *, pool_size> input_ptrs{ {} };
1608 for(int i = 0; i < pool_size; ++i)
1609 {
1610 input_ptrs[i] = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + i));
1611 }
1612
1613 execute_window_loop(window, [&](const Coordinates & id)
1614 {
1615 float32x2_t res = {};
1616 float final_res = 0.f;
1617 if(pooling_type != PoolingType::MAX)
1618 {
1619 // Calculate scale
1620 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);
1621 const float32x2_t scale_v = vdup_n_f32(scale);
1622
1623 // Perform pooling
1624 float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1625 // Get power of 2 in case of l2 pooling
1626 if(pooling_type == PoolingType::L2)
1627 {
1628 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1629 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1630 }
1631 float32x4_t sum_data = vaddq_f32(data.val[0], vsetq_lane_f32(0.f, data.val[1], 3));
1632 for(int i = 1; i < pool_size; ++i)
1633 {
1634 data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1635 // Get power of 2 in case of l2 pooling
1636 if(pooling_type == PoolingType::L2)
1637 {
1638 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1639 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1640 }
1641 sum_data = vaddq_f32(sum_data, data.val[0]);
1642 sum_data = vaddq_f32(sum_data, vsetq_lane_f32(0.f, data.val[1], 3));
1643 }
1644 res = vpadd_f32(vget_high_f32(sum_data), vget_low_f32(sum_data));
1645 res = vmul_f32(vpadd_f32(res, res), scale_v);
1646 }
1647 else
1648 {
1649 float32x4x2_t max_data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1650 for(int i = 1; i < pool_size; ++i)
1651 {
1652 const float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1653 max_data = vmax2q_f32(max_data, data);
1654 }
1655 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]));
1656 res = vpmax_f32(res, vpmax_f32(vget_high_f32(max_data.val[0]), vget_low_f32(max_data.val[0])));
1657 res = vpmax_f32(res, res);
1658 }
1659 final_res = vget_lane_f32(res, 0);
1660
1661 // Calculate square-root in case of l2 pooling
1662 if(pooling_type == PoolingType::L2)
1663 {
1664 final_res = sqrt(final_res);
1665 }
1666
1667 // Store result
1668 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1669 },
1670 input, output);
1671}
1672
1673void NEPoolingLayerKernel::poolingMxN_f32_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001674{
1675 Iterator input(_input, window_input);
1676 Iterator output(_output, window);
1677
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001678 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.width;
1679 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().z() : _pool_info.pool_size.height;
1680 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1681 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1682 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1683 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001684 int pool_stride_x = 0;
1685 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001686 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001687 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1688 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1689
1690 float32x4_t vres;
1691
1692 execute_window_loop(window, [&](const Coordinates & id)
1693 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001694 const int idx_width = id.y() * pool_stride_x;
1695 const int idx_height = id.z() * pool_stride_y;
1696 const int pool_limit_y = pool_pad_top - idx_height;
1697 const int pool_limit_x = pool_pad_left - idx_width;
1698
1699 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1700 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1701 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1702 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1703
Michalis Spyrou57dac842018-03-01 16:03:50 +00001704 if(pooling_type != PoolingType::MAX)
1705 {
1706 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001707 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,
1708 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001709 const float32x4_t scale_v = vdupq_n_f32(scale);
1710
1711 // Perform pooling
1712 vres = vdupq_n_f32(0.0f);
1713
Michalis Spyrouced25572018-10-01 16:26:20 +01001714 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001715 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001716 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001717 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001718 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>
1719 (_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001720
1721 // Get power of 2 in case of l2 pooling and accumulate
1722 if(pooling_type == PoolingType::L2)
1723 {
1724 vres = vmlaq_f32(vres, data, data);
1725 }
1726 else
1727 {
1728 vres = vaddq_f32(vres, data);
1729 }
1730 }
1731 }
1732 // Divide by scale
1733 vres = vmulq_f32(vres, scale_v);
1734 }
1735 else
1736 {
1737 vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001738 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001739 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001740 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001741 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +01001742 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>
1743 (_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001744 vres = vmaxq_f32(vres, data);
1745 }
1746 }
1747 }
1748
1749 // Calculate square-root in case of l2 pooling
1750 if(pooling_type == PoolingType::L2)
1751 {
Georgios Pinitas27f223d2019-12-16 19:23:02 +00001752 float32x4_t l2_res = { static_cast<float>(sqrt(vgetq_lane_f32(vres, 0))),
1753 static_cast<float>(sqrt(vgetq_lane_f32(vres, 1))),
1754 static_cast<float>(sqrt(vgetq_lane_f32(vres, 2))),
1755 static_cast<float>(sqrt(vgetq_lane_f32(vres, 3)))
1756 };
1757 vres = l2_res;
Michalis Spyrou57dac842018-03-01 16:03:50 +00001758 }
1759
1760 // Store result
1761 vst1q_f32(reinterpret_cast<float *>(output.ptr()), vres);
1762 },
1763 input, output);
1764}
1765
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001766template <typename T>
1767void NEPoolingLayerKernel::poolingMxN_q8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +00001768{
1769 Iterator input(_input, window_input);
1770 Iterator output(_output, window);
1771
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001772 /** NEON vector types */
1773 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
1774 using q16_t = typename wrapper::traits::promote_t<T>;
1775 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
1776 using q32_t = typename wrapper::traits::promote_t<q16_t>;
1777 using q32x4_t = typename wrapper::traits::neon_vector<q32_t, 4>::type;
1778
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001779 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().x() : _pool_info.pool_size.width;
1780 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.height;
1781 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1782 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1783 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1784 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001785 int pool_stride_x = 0;
1786 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001787 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001788 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1789 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +00001790
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001791 const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform();
1792 const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform();
1793
Georgios Pinitas55186712018-01-08 17:37:12 +00001794 execute_window_loop(window, [&](const Coordinates & id)
1795 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001796 T res = std::numeric_limits<T>::min();
Georgios Pinitas55186712018-01-08 17:37:12 +00001797
1798 if(pooling_type != PoolingType::MAX)
1799 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001800 q32x4_t vres = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1801 q32_t sres = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +00001802
1803 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001804 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 +00001805
1806 // Perform pooling
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001807 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001808 {
1809 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001810 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001811 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001812 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>
1813 (_input->info()->strides_in_bytes().y())));
Georgios Pinitas55186712018-01-08 17:37:12 +00001814
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001815 const q16x8_t data_q16 = wrapper::vmovl(data);
1816 vres = wrapper::vadd(vres, wrapper::vaddl(wrapper::vgethigh(data_q16), wrapper::vgetlow(data_q16)));
Georgios Pinitas55186712018-01-08 17:37:12 +00001817 }
1818
1819 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001820 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001821 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001822 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>
1823 (_input->info()->strides_in_bytes().y())));
Georgios Pinitas55186712018-01-08 17:37:12 +00001824 sres += data;
1825 }
1826 }
1827
1828 // Reduction
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001829 const auto tmp = wrapper::vpadd(wrapper::vgethigh(vres), wrapper::vgetlow(vres));
1830 sres += wrapper::vgetlane(tmp, 0) + wrapper::vgetlane(tmp, 1);
Georgios Pinitas55186712018-01-08 17:37:12 +00001831
1832 // Divide by scale
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001833 res = static_cast<T>(support::cpp11::round(sres * scale));
Georgios Pinitas55186712018-01-08 17:37:12 +00001834 }
1835 else
1836 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001837 q8x8_t vres = wrapper::vdup_n(std::numeric_limits<T>::min(), wrapper::traits::vector_64_tag{});
Georgios Pinitas55186712018-01-08 17:37:12 +00001838
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001839 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001840 {
1841 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001842 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001843 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001844 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>
1845 (_input->info()->strides_in_bytes().y())));
1846 vres = wrapper::vmax(vres, data);
Georgios Pinitas55186712018-01-08 17:37:12 +00001847 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001848 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001849 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001850 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001851 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>
1852 (_input->info()->strides_in_bytes().y())));
1853 res = std::max(res, data);
Georgios Pinitas55186712018-01-08 17:37:12 +00001854 }
1855 }
1856
1857 // Reduce max
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001858 vres = wrapper::vpmax(vres, vres);
1859 vres = wrapper::vpmax(vres, vres);
1860 vres = wrapper::vpmax(vres, vres);
Georgios Pinitas55186712018-01-08 17:37:12 +00001861
1862 // Get max value
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001863 res = std::max(res, wrapper::vgetlane(vres, 0));
Georgios Pinitas55186712018-01-08 17:37:12 +00001864 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001865 // Store result
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001866 res = (input_qinfo != output_qinfo) ? Qasymm8QuantizationHelper<T>::quantize(Qasymm8QuantizationHelper<T>::dequantize(res, input_qinfo), output_qinfo) : res;
1867 *(reinterpret_cast<T *>(output.ptr())) = res;
Georgios Pinitas55186712018-01-08 17:37:12 +00001868 },
1869 input, output);
1870}
1871
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001872template <typename T>
1873void NEPoolingLayerKernel::poolingMxN_q8_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001874{
1875 Iterator input(_input, window_input);
1876 Iterator output(_output, window);
1877
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001878 using q8x8_t = typename wrapper::traits::neon_vector<T, 8>::type;
1879 using q8x16_t = typename wrapper::traits::neon_vector<T, 16>::type;
1880 using q16_t = typename wrapper::traits::promote_t<T>;
1881 using q16x8_t = typename wrapper::traits::neon_vector<q16_t, 8>::type;
1882 using q32_t = typename wrapper::traits::promote_t<q16_t>;
1883 using q32x4_t = typename wrapper::traits::neon_vector<q32_t, 4>::type;
1884
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001885 const int pool_size_x = _pool_info.is_global_pooling ? _input->info()->tensor_shape().y() : _pool_info.pool_size.width;
1886 const int pool_size_y = _pool_info.is_global_pooling ? _input->info()->tensor_shape().z() : _pool_info.pool_size.height;
1887 const int pool_pad_right = _pool_info.pad_stride_info.pad_right();
1888 const int pool_pad_top = _pool_info.pad_stride_info.pad_top();
1889 const int pool_pad_left = _pool_info.pad_stride_info.pad_left();
1890 const int pool_pad_bottom = _pool_info.pad_stride_info.pad_bottom();
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001891
1892 int pool_stride_x = 0;
1893 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001894 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
Michalis Spyrou57dac842018-03-01 16:03:50 +00001895 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1896 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1897
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001898 const float32x4_t half_scale_v = vdupq_n_f32(0.5f);
1899 const UniformQuantizationInfo input_qinfo = _input->info()->quantization_info().uniform();
1900 const UniformQuantizationInfo output_qinfo = _output->info()->quantization_info().uniform();
Georgios Pinitas283fc602018-11-09 10:46:43 +00001901
Michele Di Giorgio82fa5502020-02-19 15:55:01 +00001902 const float quant_rescale = output_qinfo.scale / input_qinfo.scale;
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001903 // "new_offset" doesn't have to consider the "half_scale_v" in its computation
1904 // With a requantization performed in a single step there won't be uncertainties introduced
Michele Di Giorgio82fa5502020-02-19 15:55:01 +00001905 const int32_t new_offset = output_qinfo.offset - static_cast<int32_t>(static_cast<float>(input_qinfo.offset) / quant_rescale);
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001906
1907 const float requant_scale = output_qinfo.scale / input_qinfo.scale;
1908 const int32_t requant_offset = output_qinfo.offset - static_cast<int32_t>(static_cast<float>(input_qinfo.offset) / requant_scale);
1909 const UniformQuantizationInfo requant_qinfo = UniformQuantizationInfo(requant_scale, requant_offset);
1910
Michalis Spyrou57dac842018-03-01 16:03:50 +00001911 execute_window_loop(window, [&](const Coordinates & id)
1912 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001913 const int idx_width = id.y() * pool_stride_x;
1914 const int idx_height = id.z() * pool_stride_y;
1915 const int pool_limit_y = pool_pad_top - idx_height;
1916 const int pool_limit_x = pool_pad_left - idx_width;
1917
1918 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1919 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1920 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1921 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1922
Michalis Spyrou57dac842018-03-01 16:03:50 +00001923 if(pooling_type != PoolingType::MAX)
1924 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001925 q32x4_t vres1 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1926 q32x4_t vres2 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1927 q32x4_t vres3 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
1928 q32x4_t vres4 = wrapper::vdup_n(static_cast<q32_t>(0.f), wrapper::traits::vector_128_tag{});
Michalis Spyrou57dac842018-03-01 16:03:50 +00001929
1930 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001931 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,
1932 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001933
1934 // Perform pooling
Michalis Spyrouced25572018-10-01 16:26:20 +01001935 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001936 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001937 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001938 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001939 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>
1940 (_input->info()->strides_in_bytes().z())));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001941
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001942 const q16x8_t data_q16 = wrapper::vmovl(wrapper::vgetlow(data));
1943 const q16x8_t data2_q16 = wrapper::vmovl(wrapper::vgethigh(data));
1944 vres1 = wrapper::vadd(vres1, wrapper::vmovl(wrapper::vgetlow(data_q16)));
1945 vres2 = wrapper::vadd(vres2, wrapper::vmovl(wrapper::vgethigh(data_q16)));
1946 vres3 = wrapper::vadd(vres3, wrapper::vmovl(wrapper::vgetlow(data2_q16)));
1947 vres4 = wrapper::vadd(vres4, wrapper::vmovl(wrapper::vgethigh(data2_q16)));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001948 }
1949 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001950
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001951 if(input_qinfo != output_qinfo)
1952 {
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001953 const float32x4x4_t vres =
1954 {
1955 {
1956 vcvtq_f32_q32(vres1),
1957 vcvtq_f32_q32(vres2),
1958 vcvtq_f32_q32(vres3),
1959 vcvtq_f32_q32(vres4),
1960 }
1961 };
1962 const auto requantized_output = vrequantize_pooling_with_scale<q8x16_t>(vres, quant_rescale, scale, new_offset);
1963 // Store result
1964 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), wrapper::vgetlow(requantized_output));
1965 wrapper::vstore(reinterpret_cast<T *>(output.ptr()) + 8, wrapper::vgethigh(requantized_output));
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001966 }
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001967 else
1968 {
1969 const float32x4_t scale_v = vdupq_n_f32(scale);
1970 // Divide by scale and add 0.5f to round to nearest instead of rounding towards zero
1971 vres1 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres1), scale_v));
1972 vres2 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres2), scale_v));
1973 vres3 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres3), scale_v));
1974 vres4 = vcvtq_q32_f32<q32x4_t>(wrapper::vmla(half_scale_v, vcvtq_f32_q32(vres4), scale_v));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001975
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001976 const q8x8_t res1 = wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(vres1), wrapper::vmovn(vres2)));
1977 const q8x8_t res2 = wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(vres3), wrapper::vmovn(vres4)));
1978 // Store result
1979 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), res1);
1980 wrapper::vstore(reinterpret_cast<T *>(output.ptr()) + 8, res2);
1981 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001982 }
1983 else
1984 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001985 q8x16_t vres = wrapper::vdup_n(std::numeric_limits<T>::min(), wrapper::traits::vector_128_tag{});
Michalis Spyrou57dac842018-03-01 16:03:50 +00001986
Michalis Spyrouced25572018-10-01 16:26:20 +01001987 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001988 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001989 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001990 {
Manuel Bottinib4bb8272019-12-18 18:01:27 +00001991 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>
1992 (_input->info()->strides_in_bytes().z())));
1993 vres = wrapper::vmax(vres, data);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001994 }
1995 }
1996
1997 // Store result
Manuel Bottinicf4737a2020-02-06 11:58:51 +00001998 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 +00001999 }
2000 },
2001 input, output);
2002}
2003
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002004Status NEPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
2005{
2006 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
2007
2008 unsigned int pooled_w = 0;
2009 unsigned int pooled_h = 0;
2010 unsigned int num_elems_processed_per_iteration = 0;
2011 BorderSize border_size(0);
2012
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002013 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +00002014 unsigned int pool_size_x = 0;
2015 unsigned int pool_size_y = 0;
2016
2017 // Get data layout
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00002018 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : pool_info.data_layout;
2019 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
2020 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou57dac842018-03-01 16:03:50 +00002021
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002022 pool_size_x = is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size.width;
2023 pool_size_y = is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size.height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002024
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00002025 // Validate pool info before calling scaled_dimensions
2026 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_pool_info(pool_size_x, pool_size_y));
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002027
2028 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +00002029 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
2030 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00002031 pool_size_x,
2032 pool_size_y,
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002033 pool_info.pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002034
Georgios Pinitas13d96e02018-08-23 11:20:23 +01002035 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, pooled_w, pooled_h));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00002036 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,
2037 pool_size_x, pool_size_y)
2038 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +00002039
2040 return Status{};
2041}
2042
Moritz Pflanzerc186b572017-09-07 09:48:04 +01002043void NEPoolingLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002044{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01002045 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002046 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
2047 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
2048 ARM_COMPUTE_ERROR_ON(_func == nullptr);
2049
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002050 const unsigned int pool_stride_x = _pool_info.pad_stride_info.stride().first;
2051 const unsigned int pool_stride_y = _pool_info.pad_stride_info.stride().second;
2052 const unsigned int pool_size = _pool_info.pool_size.width;
2053 const bool exclude_padding = _pool_info.exclude_padding;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002054
Michalis Spyrou57dac842018-03-01 16:03:50 +00002055 Window window_input(window);
Georgios Pinitas14d9d982019-12-13 12:33:09 +00002056 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002057 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00002058 // Set step for input in x and y direction for the input
2059 unsigned int window_x_inc = 0;
2060 switch(_input->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +01002061 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00002062 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +00002063 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +00002064 {
2065 window_x_inc = pool_stride_x;
2066 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
2067 {
2068 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
2069 }
2070 break;
2071 }
Pablo Tello77e6c552018-12-04 15:33:49 +00002072
Georgios Pinitas13d96e02018-08-23 11:20:23 +01002073 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +00002074 case DataType::F32:
2075 {
2076 window_x_inc = pool_stride_x;
2077 break;
2078 }
2079 default:
2080 {
2081 ARM_COMPUTE_ERROR("Not supported");
2082 }
Georgios Pinitas55186712018-01-08 17:37:12 +00002083 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00002084 window_input.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
2085 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 +01002086 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00002087 else
2088 {
Georgios Pinitascac13b12018-04-27 19:07:19 +01002089 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 +00002090 window_input.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
2091 window_input.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
2092 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002093
2094 // Run function
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00002095 (this->*_func)(window_input, window, _pool_info.pool_type, exclude_padding);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002096}
Manuel Bottinib4bb8272019-12-18 18:01:27 +00002097} // namespace arm_compute