blob: ac2ffa19880cbf8f9b9e5fb8258fb9593ed77096 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas99089ce2019-02-06 14:16:18 +00002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/NEON/kernels/NEPoolingLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbiereaefd002018-07-20 17:49:35 +010027#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
Georgios Pinitas55186712018-01-08 17:37:12 +000031#include "arm_compute/core/NEON/NEAsymm.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/NEON/NEFixedPoint.h"
Georgios Pinitascdf51452017-08-31 14:21:36 +010033#include "arm_compute/core/NEON/NEMath.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/Window.h"
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +010038#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
Georgios Pinitas55186712018-01-08 17:37:12 +000040#include "support/ToolchainSupport.h"
41
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042#include <algorithm>
43#include <arm_neon.h>
Georgios Pinitascdf51452017-08-31 14:21:36 +010044#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045#include <limits>
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +010046#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047#include <string>
48#include <tuple>
49
50using namespace arm_compute;
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +010051using namespace misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052
53namespace
54{
Pablo Tello77e6c552018-12-04 15:33:49 +000055inline float calculate_avg_scale(bool exclude_padding, DataLayout data_layout, const Coordinates &id, const int pool_size_x, const int pool_size_y, const int upper_bound_w, const int upper_bound_h,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 const int pad_x, const int pad_y, const int stride_x, const int stride_y)
57{
Michalis Spyrou57dac842018-03-01 16:03:50 +000058 const unsigned int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
59 const unsigned int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
60
61 int start_x = id[idx_width] * stride_x - pad_x;
62 int start_y = id[idx_height] * stride_y - pad_y;
63
64 const int end_x = std::min(start_x + pool_size_x, upper_bound_w);
65 const int end_y = std::min(start_y + pool_size_y, upper_bound_h);
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000066 if(exclude_padding)
67 {
68 start_x = std::max(0, start_x);
69 start_y = std::max(0, start_y);
70 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 return 1.f / ((end_y - start_y) * (end_x - start_x));
72}
73
Pablo Tello77e6c552018-12-04 15:33:49 +000074inline void scale_vector_s16x8(bool exclude_padding, uint16x8_t &v, const Coordinates &id, int id_offset, int step,
Georgios Pinitas55186712018-01-08 17:37:12 +000075 const int pool_size, const int upper_bound_w, const int upper_bound_h,
76 const int pad_x, const int pad_y, const int stride_x, const int stride_y)
77{
78 int start_x = (id.x() + id_offset) * stride_x - pad_x;
79 int start_y = id.y() * stride_y - pad_y;
80 const int end_y = std::min(start_y + pool_size, upper_bound_h);
81 if(exclude_padding)
82 {
83 start_y = std::max(0, start_y);
84 }
85
86 std::array<uint16_t, 8> elems =
87 {
88 {
89 vgetq_lane_u16(v, 0),
90 vgetq_lane_u16(v, 1),
91 vgetq_lane_u16(v, 2),
92 vgetq_lane_u16(v, 3),
93 vgetq_lane_u16(v, 4),
94 vgetq_lane_u16(v, 5),
95 vgetq_lane_u16(v, 6),
96 vgetq_lane_u16(v, 7),
97 }
98 };
99
100 for(auto &el : elems)
101 {
102 int c_start_x = start_x;
103 const int end_x = std::min(c_start_x + pool_size, upper_bound_w);
104 if(exclude_padding)
105 {
106 c_start_x = std::max(0, c_start_x);
107 }
108 float scale = 1.f / ((end_y - start_y) * (end_x - c_start_x));
109 el *= scale;
110 start_x += step * stride_x;
111 }
112
113 v = vsetq_lane_u16(elems[0], v, 0);
114 v = vsetq_lane_u16(elems[1], v, 1);
115 v = vsetq_lane_u16(elems[2], v, 2);
116 v = vsetq_lane_u16(elems[3], v, 3);
117 v = vsetq_lane_u16(elems[4], v, 4);
118 v = vsetq_lane_u16(elems[5], v, 5);
119 v = vsetq_lane_u16(elems[6], v, 6);
120 v = vsetq_lane_u16(elems[7], v, 7);
121}
122
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100123Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &pooled_w, unsigned int pooled_h)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000125 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000127 int pool_stride_x = 0;
128 int pool_stride_y = 0;
129 PoolingType pool_type = pool_info.pool_type();
130 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100132
Anthony Barbiereaefd002018-07-20 17:49:35 +0100133 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100134 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas55186712018-01-08 17:37:12 +0000135 ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(input->data_type()));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000136
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000137 if(output->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100138 {
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000139 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000140 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
141 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w)
142 || (output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h));
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100143 }
144
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000145 return Status{};
146}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000148Status validate_arguments_pool_info(const unsigned int pool_size_x, const unsigned int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000149{
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000150 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_x == 0);
151 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_y == 0);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000152
153 return Status{};
154}
155
156std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &num_elems_processed_per_iteration,
157 BorderSize &border_size,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000158 unsigned int pooled_w, unsigned int pooled_h, int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000159{
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +0100160 // Output auto inizialitation if not yet initialized
161 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_pool_shape(*input, pool_info)));
162
Michalis Spyrou57dac842018-03-01 16:03:50 +0000163 DataLayout data_layout = input->data_layout();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000164 unsigned int num_elems_read_per_iteration = 0;
165 unsigned int num_elems_horizontal_window = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000166 int pool_stride_x = 0;
167 int pool_stride_y = 0;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000168 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
169 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
170 const int input_width = input->dimension(idx_width);
171 const int input_height = input->dimension(idx_height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000172 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
173 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000174 const int pool_pad_right = pad_stride_info.pad_right();
175 const int pool_pad_top = pad_stride_info.pad_top();
176 const int pool_pad_left = pad_stride_info.pad_left();
177 const int pool_pad_bottom = pad_stride_info.pad_bottom();
178 const bool is_square = pool_size_x == pool_size_y;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000179
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000180 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +0000181 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
182 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000183 pool_size_x,
184 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000185 pad_stride_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000187 //If it's not squared and optimized will be executed the MxN
188 num_elems_read_per_iteration = 1;
189 num_elems_processed_per_iteration = 1;
190 num_elems_horizontal_window = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191
Michalis Spyrou57dac842018-03-01 16:03:50 +0000192 const bool is_nhwc = data_layout == DataLayout::NHWC;
193
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000194 if(is_square)
195 {
196 switch(input->data_type())
197 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000198 case DataType::QASYMM8:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000199 if(is_nhwc)
200 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100201 num_elems_processed_per_iteration = 16;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000202 break;
203 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000204 switch(pool_size_x)
205 {
206 case 2:
207 num_elems_read_per_iteration = 16;
208 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
209 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
210 break;
211 case 3:
212 num_elems_read_per_iteration = 16;
213 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
214 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
215 break;
216 default:
217 break;
218 }
219 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000220#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
221 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000222 if(is_nhwc)
223 {
224 num_elems_processed_per_iteration = 8;
225 break;
226 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000227 switch(pool_size_x)
228 {
229 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000230 case 3:
231 num_elems_read_per_iteration = 4;
232 num_elems_processed_per_iteration = 1;
233 num_elems_horizontal_window = 1;
234 break;
235 default:
236 break;
237 }
238 break;
239#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
240 case DataType::F32:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000241 if(is_nhwc)
242 {
Georgios Pinitas64f1a902018-09-18 13:42:51 +0100243 num_elems_processed_per_iteration = 4;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000244 break;
245 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000246 switch(pool_size_x)
247 {
248 case 2:
249 num_elems_read_per_iteration = 2;
250 break;
251 case 3:
252 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
253 break;
254 case 7:
255 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
256 break;
257 default:
258 break;
259 }
260 num_elems_processed_per_iteration = 1;
261 num_elems_horizontal_window = 1;
262 break;
263 default:
264 ARM_COMPUTE_ERROR("Element size not supported");
265 break;
266 }
267 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000268 else
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000269 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000270 if(is_nhwc)
271 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100272 num_elems_processed_per_iteration = 16 / input->element_size();
Michalis Spyrou57dac842018-03-01 16:03:50 +0000273 }
274 }
275
276 bool window_changed = false;
277 Window win{};
278 if(data_layout == DataLayout::NCHW)
279 {
280 // Number of iterations in X dimension
281 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
282
283 // Upper limit for the number of right/bottom border elements that are accessed
284 const int upper_bound_w = ((num_iterations_x - 1) * num_elems_processed_per_iteration * pool_stride_x - pool_pad_left + num_elems_read_per_iteration) - input_width;
285 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height;
286
287 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
288 border_size.right = std::max(upper_bound_w, pool_pad_right);
289 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
290
291 TensorShape output_shape{ input->tensor_shape() };
292 output_shape.set(0, pooled_w);
293 output_shape.set(1, pooled_h);
294 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
295
296 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
297 AccessWindowStatic input_access(input, -pool_pad_left, -pool_pad_top, input_width + border_size.right, input_height + border_size.bottom);
298
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000299 AccessWindowHorizontal output_access(output, 0, num_elems_horizontal_window);
300 window_changed = update_window_and_padding(win, input_access, output_access);
301 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
302 }
303 else
304 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000305 TensorShape output_shape{ input->tensor_shape() };
306 output_shape.set(1, pooled_w);
307 output_shape.set(2, pooled_h);
308 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
309
310 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
311 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
312
313 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
314 window_changed = update_window_and_padding(win, input_access, output_access);
315 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000316 }
317
318 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
319 return std::make_pair(err, win);
320}
321} // namespace
322
323NEPoolingLayerKernel::NEPoolingLayerKernel()
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000324 : _func(nullptr), _input(nullptr), _output(nullptr), _pool_info(), _num_elems_processed_per_iteration(0), _border_size(0), _is_square(false)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000325{
326}
327
328BorderSize NEPoolingLayerKernel::border_size() const
329{
330 return _border_size;
331}
332
333void NEPoolingLayerKernel::configure(const ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info)
334{
335 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
336
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000337 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000338 const bool is_global_pooling = pool_info.is_global_pooling();
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000339 const int pool_stride_x = pad_stride_info.stride().first;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000340
341 // Get data layout
342 const DataLayout data_layout = input->info()->data_layout();
343 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
344 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000345
346 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000347 const Size2D pool_size(
348 is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size().width,
349 is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size().height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000350
351 // Validate pool info before calling scaled_dimensions
Pablo Tello77e6c552018-12-04 15:33:49 +0000352 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_pool_info(pool_size.x(), pool_size.y()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000353
354 // Check output dimensions
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100355 unsigned int pooled_w;
356 unsigned int pooled_h;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000357 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(idx_width),
358 input->info()->dimension(idx_height),
Pablo Tello77e6c552018-12-04 15:33:49 +0000359 pool_size.x(),
360 pool_size.y(),
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000361 pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000362
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000363 // Perform validation step
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100364 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, pooled_w, pooled_h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365
366 // Set instance variables
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000367 _input = input;
368 _output = output;
369 _pool_info = pool_info;
Pablo Tello77e6c552018-12-04 15:33:49 +0000370 _is_square = (pool_size.x() == pool_size.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100371
Georgios Pinitas55186712018-01-08 17:37:12 +0000372 // Get data type
373 const DataType data_type = input->info()->data_type();
Michalis Spyrou57dac842018-03-01 16:03:50 +0000374 const bool is_nchw = data_layout == DataLayout::NCHW;
Georgios Pinitas55186712018-01-08 17:37:12 +0000375
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100376 if(data_type == DataType::QASYMM8)
Georgios Pinitas55186712018-01-08 17:37:12 +0000377 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000378 if(pool_size.x() == 2 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000379 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000380 if(is_nchw)
Michalis Spyroubbd9fb92017-06-22 12:57:51 +0100381 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000382 _func = &NEPoolingLayerKernel::pooling2_qasymm8_nchw;
383 }
384 else
385 {
386 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc;
Georgios Pinitas55186712018-01-08 17:37:12 +0000387 }
388 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000389 else if(pool_size.x() == 3 && pool_stride_x < 3 && _is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000390 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000391 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000392 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000393 _func = &NEPoolingLayerKernel::pooling3_qasymm8_nchw;
394 }
395 else
396 {
397 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc;
Georgios Pinitas55186712018-01-08 17:37:12 +0000398 }
399 }
400 else
401 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000402 if(is_nchw)
Georgios Pinitas55186712018-01-08 17:37:12 +0000403 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000404 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nchw;
405 }
406 else
407 {
408 _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc;
Georgios Pinitas55186712018-01-08 17:37:12 +0000409 }
410 }
411 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000412 else if(data_type == DataType::F16)
413 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000414 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000415 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000416 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000417 {
418 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000419 {
420 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000421 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000422 _func = &NEPoolingLayerKernel::pooling2_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000423 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000424 else
425 {
426 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
427 }
428 }
429 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000430 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000431 {
432 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000433 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000434 _func = &NEPoolingLayerKernel::pooling3_f16_nchw;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000435 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000436 else
437 {
438 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
439 }
440 }
441 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000442 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000443 {
444 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000445 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000446 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
447 }
448 else
449 {
450 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000451 }
452 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000453 }
454 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000455 }
456 }
457 else
458 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000459 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000460 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000461 _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw;
462 }
463 else
464 {
465 _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000466 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000467 }
468 }
469 else if(data_type == DataType::F32)
470 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000471 if(_is_square)
Georgios Pinitas55186712018-01-08 17:37:12 +0000472 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000473 switch(pool_size.x())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000474 {
475 case 2:
Pablo Tello77e6c552018-12-04 15:33:49 +0000476 {
477 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000478 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000479 _func = &NEPoolingLayerKernel::pooling2_f32_nchw;
480 }
481 else
482 {
483 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000484 }
485 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000486 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000487 case 3:
Pablo Tello77e6c552018-12-04 15:33:49 +0000488 {
489 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000490 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000491 _func = &NEPoolingLayerKernel::pooling3_f32_nchw;
492 }
493 else
494 {
495 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000496 }
497 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000498 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000499 case 7:
Pablo Tello77e6c552018-12-04 15:33:49 +0000500 {
501 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000502 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000503 _func = &NEPoolingLayerKernel::pooling7_f32_nchw;
504 }
505 else
506 {
507 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000508 }
509 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000510 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000511 default:
Pablo Tello77e6c552018-12-04 15:33:49 +0000512 {
513 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000514 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000515 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
516 }
517 else
518 {
519 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000520 }
521 break;
Pablo Tello77e6c552018-12-04 15:33:49 +0000522 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000523 }
524 }
525 else
526 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000527 if(is_nchw)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000528 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000529 _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw;
530 }
531 else
532 {
533 _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000534 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000535 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100536 }
537
538 // Configure kernel window
Pablo Tello77e6c552018-12-04 15:33:49 +0000539 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 +0000540 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
541 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100542}
543
Pablo Tello77e6c552018-12-04 15:33:49 +0000544void NEPoolingLayerKernel::pooling2_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000545{
546 Iterator input(_input, window_input);
547 Iterator output(_output, window);
548
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000549 constexpr int pool_size = 2;
550 int pool_stride_x = 0;
551 int pool_stride_y = 0;
552 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
553 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
554 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
555 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
Georgios Pinitas55186712018-01-08 17:37:12 +0000556 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000557 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
558 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000559
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000560 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
561 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
Georgios Pinitas55186712018-01-08 17:37:12 +0000562
563 const int scale_step_x = (pool_stride_x == 1) ? 2 : 1;
564
565 execute_window_loop(window, [&](const Coordinates & id)
566 {
567 const auto top_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_top_ptr + input.offset()));
568 const auto bottom_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_bottom_ptr + input.offset()));
569 uint8x8_t lower_res = {};
570 uint8x8_t upper_res = {};
571
572 if(pooling_type != PoolingType::MAX)
573 {
574 const uint16x8x2_t top_data_u16 = { { vmovl_u8(vget_low_u8(top_data)), vmovl_u8(vget_high_u8(top_data)) } };
575 const uint16x8x2_t bottom_data_u16 = { { vmovl_u8(vget_low_u8(bottom_data)), vmovl_u8(vget_high_u8(bottom_data)) } };
576
577 // Add rows
578 const uint16x8x2_t vrsum =
579 {
580 {
581 vaddq_u16(top_data_u16.val[0], bottom_data_u16.val[0]),
582 vaddq_u16(top_data_u16.val[1], bottom_data_u16.val[1]),
583 }
584 };
585
586 // Pair-wise add row data
587 const uint16x4x2_t vpsum =
588 {
589 {
590 vpadd_u16(vget_low_u16(vrsum.val[0]), vget_high_u16(vrsum.val[0])),
591 vpadd_u16(vget_low_u16(vrsum.val[1]), vget_high_u16(vrsum.val[1])),
592 }
593 };
594
595 uint16x8_t res_lower = vcombine_u16(vpsum.val[0], vpsum.val[1]);
596
597 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000598 scale_vector_s16x8(exclude_padding, res_lower, id, 0, scale_step_x,
599 pool_size, upper_bound_w, upper_bound_h,
600 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000601 lower_res = vmovn_u16(res_lower);
602
603 // Compute upper result for stride_x == 1
604 if(pool_stride_x == 1)
605 {
606 // Shifted row sum
607 const uint16x8x2_t vrsum_shifted =
608 {
609 {
610 vextq_u16(vrsum.val[0], vrsum.val[1], 1),
611 vextq_u16(vrsum.val[1], vrsum.val[1], 1)
612 }
613 };
614
615 // Pair-wise add shifted row
616 const uint16x4x2_t vpsum_shifted =
617 {
618 {
619 vpadd_u16(vget_low_u16(vrsum_shifted.val[0]), vget_high_u16(vrsum_shifted.val[0])),
620 vpadd_u16(vget_low_u16(vrsum_shifted.val[1]), vget_high_u16(vrsum_shifted.val[1])),
621 }
622 };
623 uint16x8_t res_upper = vcombine_u16(vpsum_shifted.val[0], vpsum_shifted.val[1]);
624
625 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000626 scale_vector_s16x8(exclude_padding, res_upper, id, 1, 2,
627 pool_size, upper_bound_w, upper_bound_h,
628 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000629 upper_res = vmovn_u16(res_upper);
630 }
631 }
632 else
633 {
634 const uint8x16_t max_data = vmaxq_u8(top_data, bottom_data);
635 lower_res = vpmax_u8(vget_low_u8(max_data), vget_high_u8(max_data));
636 if(pool_stride_x == 1)
637 {
638 const uint8x16_t max_data_shifted = vextq_u8(max_data, max_data, 1);
639 upper_res = vpmax_u8(vget_low_u8(max_data_shifted), vget_high_u8(max_data_shifted));
640 }
641 }
642
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100643 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
644 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
645 if(input_qinfo != output_qinfo)
646 {
647 const auto requantized_output = vquantize(vdequantize(vcombine_u8(lower_res, upper_res), input_qinfo), output_qinfo);
648 lower_res = vget_low_u8(requantized_output);
649 upper_res = vget_high_u8(requantized_output);
650 }
651
Georgios Pinitas55186712018-01-08 17:37:12 +0000652 // Store result
653 if(pool_stride_x == 1)
654 {
655 const uint8x8x2_t res = { { lower_res, upper_res } };
656 vst2_u8(reinterpret_cast<uint8_t *>(output.ptr()), res);
657 }
658 else
659 {
660 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), lower_res);
661 }
662 },
663 input, output);
664}
665
Pablo Tello77e6c552018-12-04 15:33:49 +0000666void NEPoolingLayerKernel::pooling3_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100667{
Pablo Tello77e6c552018-12-04 15:33:49 +0000668 ARM_COMPUTE_UNUSED(pooling_type);
669 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000670#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100671 Iterator input(_input, window_input);
672 Iterator output(_output, window);
673
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000674 constexpr const int pool_size = 3;
675 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
676 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
677 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
678 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
679 int pool_stride_x = 0;
680 int pool_stride_y = 0;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100681 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000682 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
683 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100684
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000685 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
686 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));
687 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 +0100688
689 execute_window_loop(window, [&](const Coordinates & id)
690 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100691 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
692 float16x4_t middle_data = vld1_f16(reinterpret_cast<const float16_t *>(input_middle_ptr + input.offset()));
693 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
694 float16x4_t res = {};
695
696 // Get power of 2 in case of l2 pooling
697 if(pooling_type == PoolingType::L2)
698 {
699 top_data = vmul_f16(top_data, top_data);
700 middle_data = vmul_f16(middle_data, middle_data);
701 bottom_data = vmul_f16(bottom_data, bottom_data);
702 }
703
704 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100705 {
706 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000707 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 +0100708 const float16x4_t scale_v = vdup_n_f16(scale);
709 // Perform pooling
710 const float16x4_t sum_data = vadd_f16(vadd_f16(top_data, bottom_data), middle_data);
711 res = vpadd_f16(vset_lane_f16(0.f, sum_data, 3), sum_data);
712 res = vmul_f16(vpadd_f16(res, res), scale_v);
713 }
714 else
715 {
716 const float16x4_t max_data = vmax_f16(vmax_f16(top_data, bottom_data), middle_data);
717 res = vpmax_f16(vset_lane_f16(-std::numeric_limits<float>::max(), max_data, 3), max_data);
718 res = vpmax_f16(res, res);
719 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100720
721 // Calculate square-root in case of l2 pooling
722 if(pooling_type == PoolingType::L2)
723 {
724 res = vinv_f16(vinvsqrt_f16(res));
725 }
726
Pablo Tello0c34fe22017-06-26 17:17:42 +0100727 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
728 },
729 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000730#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100731 ARM_COMPUTE_UNUSED(window_input);
732 ARM_COMPUTE_UNUSED(window);
733 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000734#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100735}
736
Pablo Tello77e6c552018-12-04 15:33:49 +0000737void NEPoolingLayerKernel::pooling2_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100738{
Pablo Tello77e6c552018-12-04 15:33:49 +0000739 ARM_COMPUTE_UNUSED(pooling_type);
740 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000741#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100742 Iterator input(_input, window_input);
743 Iterator output(_output, window);
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000744 constexpr int pool_size = 2;
745 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
746 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
747 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
748 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
749 int pool_stride_x, pool_stride_y = 0;
750 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
751 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
752 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100753
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000754 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
755 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 +0100756
757 execute_window_loop(window, [&](const Coordinates & id)
758 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100759 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
760 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
761 float16x4_t res = {};
Pablo Tello0c34fe22017-06-26 17:17:42 +0100762
Georgios Pinitascdf51452017-08-31 14:21:36 +0100763 // Get power of 2 in case of l2 pooling
764 if(pooling_type == PoolingType::L2)
765 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100766 top_data = vmul_f16(top_data, top_data);
767 bottom_data = vmul_f16(bottom_data, bottom_data);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100768 }
769
770 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100771 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000772 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 +0100773 const float16x4_t scale_v = vdup_n_f16(scale);
774
775 const float16x4_t sum_data = vadd_f16(top_data, bottom_data);
776 res = vmul_f16(vpadd_f16(sum_data, sum_data), scale_v);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100777 }
778 else
779 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100780 const float16x4_t max_data = vmax_f16(top_data, bottom_data);
781 res = vpmax_f16(max_data, max_data);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100782 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100783
784 // Calculate square-root in case of l2 pooling
785 if(pooling_type == PoolingType::L2)
786 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100787 res = vinv_f16(vinvsqrt_f16(res));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100788 }
789
790 // Store result
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100791 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100792 },
793 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000794#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100795 ARM_COMPUTE_UNUSED(window_input);
796 ARM_COMPUTE_UNUSED(window);
797 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000798#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100799}
800
Pablo Tello77e6c552018-12-04 15:33:49 +0000801void NEPoolingLayerKernel::pooling3_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000802{
803 Iterator input(_input, window_input);
804 Iterator output(_output, window);
805
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000806 constexpr int pool_size = 3;
807 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
808 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
809 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
810 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
811 int pool_stride_x = 0;
812 int pool_stride_y = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +0000813 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000814 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
815 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000816
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100817 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
818 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
819
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000820 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
821 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));
822 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 2));
Georgios Pinitas55186712018-01-08 17:37:12 +0000823
824 execute_window_loop(window, [&](const Coordinates & id)
825 {
826 const auto top_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_top_ptr + input.offset()));
827 const auto middle_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_middle_ptr + input.offset()));
828 const auto bottom_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_bottom_ptr + input.offset()));
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100829 uint8x8_t fres = {};
830 uint8x16_t fqres = {};
Georgios Pinitas55186712018-01-08 17:37:12 +0000831
832 if(pooling_type == PoolingType::AVG)
833 {
834 // Convert data to u16
835 const uint16x8x2_t top_data_u16 = { { vmovl_u8(vget_low_u8(top_data)), vmovl_u8(vget_high_u8(top_data)) } };
836 const uint16x8x2_t middle_data_u16 = { { vmovl_u8(vget_low_u8(middle_data)), vmovl_u8(vget_high_u8(middle_data)) } };
837 const uint16x8x2_t bottom_data_u16 = { { vmovl_u8(vget_low_u8(bottom_data)), vmovl_u8(vget_high_u8(bottom_data)) } };
838
839 // Calculate row sums
840 const uint16x8x2_t vrsum =
841 {
842 {
843 vaddq_u16(vaddq_u16(top_data_u16.val[0], bottom_data_u16.val[0]), middle_data_u16.val[0]),
844 vaddq_u16(vaddq_u16(top_data_u16.val[1], bottom_data_u16.val[1]), middle_data_u16.val[1]),
845 }
846 };
847 const uint16x8x2_t vrsum_shifted_1 =
848 {
849 {
850 vextq_u16(vrsum.val[0], vrsum.val[1], 1),
851 vextq_u16(vrsum.val[1], vrsum.val[1], 1)
852 }
853 };
854 const uint16x8x2_t vrsum_shifted_2 =
855 {
856 {
857 vextq_u16(vrsum.val[0], vrsum.val[1], 2),
858 vextq_u16(vrsum.val[1], vrsum.val[1], 2)
859 }
860 };
861 // Calculate final sum
862 uint16x8x2_t final_sum =
863 {
864 {
865 vaddq_u16(vaddq_u16(vrsum.val[0], vrsum_shifted_1.val[0]), vrsum_shifted_2.val[0]),
866 vaddq_u16(vaddq_u16(vrsum.val[1], vrsum_shifted_1.val[1]), vrsum_shifted_2.val[1]),
867 }
868 };
869 if(pool_stride_x == 2)
870 {
871 uint16x8_t res =
872 {
873 vgetq_lane_u16(final_sum.val[0], 0),
874 vgetq_lane_u16(final_sum.val[0], 2),
875 vgetq_lane_u16(final_sum.val[0], 4),
876 vgetq_lane_u16(final_sum.val[0], 6),
877 vgetq_lane_u16(final_sum.val[1], 0),
878 vgetq_lane_u16(final_sum.val[1], 2),
879 vgetq_lane_u16(final_sum.val[1], 4),
880 vgetq_lane_u16(final_sum.val[1], 6),
881 };
882
Pablo Tello77e6c552018-12-04 15:33:49 +0000883 scale_vector_s16x8(exclude_padding, res, id, 0, 1,
884 pool_size, upper_bound_w, upper_bound_h,
885 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100886 fres = vmovn_u16(res);
Georgios Pinitas55186712018-01-08 17:37:12 +0000887 }
888 else
889 {
890 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000891 scale_vector_s16x8(exclude_padding, final_sum.val[0], id, 0, 1,
892 pool_size, upper_bound_w, upper_bound_h,
893 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000894 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000895 scale_vector_s16x8(exclude_padding, final_sum.val[1], id, 8, 1,
896 pool_size, upper_bound_w, upper_bound_h,
897 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100898 fqres = vcombine_u8(vmovn_u16(final_sum.val[0]), vmovn_u16(final_sum.val[1]));
Georgios Pinitas55186712018-01-08 17:37:12 +0000899 }
900 }
901 else
902 {
903 const uint8x16_t max_data = vmaxq_u8(vmaxq_u8(top_data, bottom_data), middle_data);
904 const uint8x16_t max_data_shift1 = vextq_u8(max_data, max_data, 1);
905 const uint8x16_t max_data_shift2 = vextq_u8(max_data, max_data, 2);
906 const uint8x16_t final_max = vmaxq_u8(vmaxq_u8(max_data, max_data_shift1), max_data_shift2);
907
908 if(pool_stride_x == 2)
909 {
910 const uint8x8x2_t table = { { vget_low_u8(final_max), vget_high_u8(final_max) } };
911 static const uint8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 };
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100912 fres = vtbl2_u8(table, lookup_val);
Georgios Pinitas55186712018-01-08 17:37:12 +0000913 }
914 else
915 {
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100916 fqres = final_max;
Georgios Pinitas55186712018-01-08 17:37:12 +0000917 }
918 }
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100919
920 // Store result
921 if(pool_stride_x == 1)
922 {
923 if(input_qinfo != output_qinfo)
924 {
925 fqres = vquantize(vdequantize(fqres, input_qinfo), output_qinfo);
926 }
927 vst1q_u8(reinterpret_cast<uint8_t *>(output.ptr()), fqres);
928 }
929 else
930 {
931 if(input_qinfo != output_qinfo)
932 {
933 fres = vquantize(vdequantize(fres, input_qinfo), output_qinfo);
934 }
935 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), fres);
936 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000937 },
938 input, output);
939}
940
Pablo Tello77e6c552018-12-04 15:33:49 +0000941void NEPoolingLayerKernel::poolingMxN_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100942{
Pablo Tello77e6c552018-12-04 15:33:49 +0000943 ARM_COMPUTE_UNUSED(pooling_type);
944 ARM_COMPUTE_UNUSED(exclude_padding);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000945#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
946 Iterator input(_input, window_input);
947 Iterator output(_output, window);
948
949 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
950 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height;
951 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
952 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
953 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
954 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
955 int pool_stride_x = 0;
956 int pool_stride_y = 0;
957 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
958 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
959 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
960
961 execute_window_loop(window, [&](const Coordinates & id)
962 {
963 float16_t res = 0.0f;
964 float16x8_t vres = vdupq_n_f16(0.0f);
965
966 if(pooling_type != PoolingType::MAX)
967 {
968 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000969 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 +0000970
971 // Perform pooling
972
973 for(int y = 0; y < pool_size_y; ++y)
974 {
975 int x = 0;
976 for(; x <= (pool_size_x - 8); x += 8)
977 {
978 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
979 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
980
981 // Get power of 2 in case of l2 pooling and accumulate
982 if(pooling_type == PoolingType::L2)
983 {
984 vres = vaddq_f16(vres, vmulq_f16(data, data));
985 }
986 else
987 {
988 vres = vaddq_f16(vres, data);
989 }
990 }
991
992 // Leftover for loop
993 for(; x < pool_size_x; ++x)
994 {
995 float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
996
997 // Get power of 2 in case of l2 pooling
998 if(pooling_type == PoolingType::L2)
999 {
1000 data *= data;
1001 }
1002
1003 res += data;
1004 }
1005 }
1006
1007 // Reduction
1008 float16x4_t tmp = vpadd_f16(vget_high_f16(vres), vget_low_f16(vres));
1009 res += vget_lane_f16(tmp, 0);
1010 res += vget_lane_f16(tmp, 1);
1011 res += vget_lane_f16(tmp, 2);
1012 res += vget_lane_f16(tmp, 3);
1013
1014 // Divide by scale
1015 res *= scale;
1016 }
1017 else
1018 {
1019 float16x8_t vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
1020 res = std::numeric_limits<float>::lowest();
1021
1022 for(int y = 0; y < pool_size_y; ++y)
1023 {
1024 int x = 0;
1025 for(; x <= (pool_size_x - 8); x += 8)
1026 {
1027 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1028 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
1029 vres = vmaxq_f16(vres, data);
1030 }
1031
1032 // Leftover for loop
1033 for(; x < pool_size_x; ++x)
1034 {
1035 const float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
1036 res = std::max(res, data);
1037 }
1038 }
1039
1040 float16x4_t tmp = vpmax_f16(vget_high_f16(vres), vget_low_f16(vres));
1041 res = std::max(res, vget_lane_f16(tmp, 0));
1042 res = std::max(res, vget_lane_f16(tmp, 1));
1043 res = std::max(res, vget_lane_f16(tmp, 2));
1044 res = std::max(res, vget_lane_f16(tmp, 3));
1045 }
1046
1047 // Calculate square-root in case of l2 pooling
1048 if(pooling_type == PoolingType::L2)
1049 {
1050 res = std::sqrt(res);
1051 }
1052
1053 // Store result
1054 *(reinterpret_cast<float16_t *>(output.ptr())) = res;
1055 },
1056 input, output);
1057
1058#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1059 ARM_COMPUTE_UNUSED(window_input);
1060 ARM_COMPUTE_UNUSED(window);
1061 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1062#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1063}
1064
Pablo Tello77e6c552018-12-04 15:33:49 +00001065void NEPoolingLayerKernel::poolingMxN_f16_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001066{
Pablo Tello77e6c552018-12-04 15:33:49 +00001067 ARM_COMPUTE_UNUSED(pooling_type);
1068 ARM_COMPUTE_UNUSED(exclude_padding);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001069#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1070 Iterator input(_input, window_input);
1071 Iterator output(_output, window);
1072
1073 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1074 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1075 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1076 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1077 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1078 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1079 int pool_stride_x = 0;
1080 int pool_stride_y = 0;
1081 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1082 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1083 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1084
1085 float16x8_t vres;
1086
1087 execute_window_loop(window, [&](const Coordinates & id)
1088 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001089 const int idx_width = id.y() * pool_stride_x;
1090 const int idx_height = id.z() * pool_stride_y;
1091 const int pool_limit_y = pool_pad_top - idx_height;
1092 const int pool_limit_x = pool_pad_left - idx_width;
1093
1094 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1095 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1096 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1097 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1098
Michalis Spyrou57dac842018-03-01 16:03:50 +00001099 if(pooling_type != PoolingType::MAX)
1100 {
1101 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001102 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,
1103 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001104 const float16x8_t scale_v = vdupq_n_f16(scale);
1105
1106 // Perform pooling
1107 vres = vdupq_n_f16(0.0f);
Michalis Spyrouced25572018-10-01 16:26:20 +01001108 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001109 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001110 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001111 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001112 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1113 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1114
1115 // Get power of 2 in case of l2 pooling and accumulate
1116 if(pooling_type == PoolingType::L2)
1117 {
1118 vres = vaddq_f16(vres, vmulq_f16(data, data));
1119 }
1120 else
1121 {
1122 vres = vaddq_f16(vres, data);
1123 }
1124 }
1125 }
1126 // Divide by scale
1127 vres = vmulq_f16(vres, scale_v);
1128 }
1129 else
1130 {
1131 vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001132
1133 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001134 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001135 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001136 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001137 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1138 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1139 vres = vmaxq_f16(vres, data);
1140 }
1141 }
1142 }
1143
1144 // Calculate square-root in case of l2 pooling
1145 if(pooling_type == PoolingType::L2)
1146 {
1147 float16x8_t sqrt_reciprocal = vrsqrteq_f16(vres);
1148 vres = vmulq_f16(vres, vmulq_f16(vrsqrtsq_f16(vmulq_f16(vres, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal));
1149 }
1150
1151 // Store result
1152 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vres);
1153 },
1154 input, output);
1155
1156#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1157 ARM_COMPUTE_UNUSED(window_input);
1158 ARM_COMPUTE_UNUSED(window);
1159 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1160#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1161}
1162
Pablo Tello77e6c552018-12-04 15:33:49 +00001163void NEPoolingLayerKernel::poolingMxN_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001164{
1165 Iterator input(_input, window_input);
1166 Iterator output(_output, window);
1167
1168 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
1169 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height;
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001170 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1171 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1172 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1173 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1174 int pool_stride_x = 0;
1175 int pool_stride_y = 0;
Gian Marco Iodice16824302017-09-28 15:41:37 +01001176 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001177 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1178 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Gian Marco Iodice16824302017-09-28 15:41:37 +01001179
1180 execute_window_loop(window, [&](const Coordinates & id)
1181 {
1182 float res = 0.0f;
1183
1184 if(pooling_type != PoolingType::MAX)
1185 {
1186 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001187 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 +01001188
1189 // Perform pooling
1190 float32x4_t vres = vdupq_n_f32(0.0f);
1191
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001192 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001193 {
1194 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001195 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001196 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001197 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1198 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001199
1200 // Get power of 2 in case of l2 pooling and accumulate
1201 if(pooling_type == PoolingType::L2)
1202 {
1203 vres = vmlaq_f32(vres, data, data);
1204 }
1205 else
1206 {
1207 vres = vaddq_f32(vres, data);
1208 }
1209 }
1210
1211 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001212 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001213 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001214 float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001215
1216 // Get power of 2 in case of l2 pooling
1217 if(pooling_type == PoolingType::L2)
1218 {
1219 data *= data;
1220 }
1221
1222 res += data;
1223 }
1224 }
1225
1226#if defined(__aarch64__)
1227 // Reduction operation available on 64 bit architectures only
1228 res += vaddvq_f32(vres);
1229#else // __aarch64__
1230 // Reduction
1231 float32x2_t tmp = vpadd_f32(vget_high_f32(vres), vget_low_f32(vres));
1232 tmp = vpadd_f32(tmp, tmp);
1233
1234 res += vget_lane_f32(tmp, 0);
1235#endif // __aarch64__
1236 // Divide by scale
1237 res *= scale;
1238 }
1239 else
1240 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001241 float32x4_t vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
1242 res = std::numeric_limits<float>::lowest();
Gian Marco Iodice16824302017-09-28 15:41:37 +01001243
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001244 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001245 {
1246 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001247 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001248 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001249 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1250 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001251 vres = vmaxq_f32(vres, data);
1252 }
1253
1254 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001255 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001256 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001257 const float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001258 res = std::max(res, data);
1259 }
1260 }
1261
1262#if defined(__aarch64__)
1263 // Reduction operation available on 64 bit architectures only
1264 res = std::max(vmaxvq_f32(vres), res);
1265#else // __aarch64__
1266 float32x2_t tmp = vpmax_f32(vget_high_f32(vres), vget_low_f32(vres));
1267 tmp = vpmax_f32(tmp, tmp);
1268
1269 res = std::max(res, vget_lane_f32(tmp, 0));
1270#endif // __aarch64__
1271 }
1272
1273 // Calculate square-root in case of l2 pooling
1274 if(pooling_type == PoolingType::L2)
1275 {
1276 res = std::sqrt(res);
1277 }
1278
1279 // Store result
1280 *(reinterpret_cast<float *>(output.ptr())) = res;
1281 },
1282 input, output);
1283}
1284
Pablo Tello77e6c552018-12-04 15:33:49 +00001285void NEPoolingLayerKernel::pooling2_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1286{
1287 Iterator input(_input, window_input);
1288 Iterator output(_output, window);
1289
1290 constexpr int pool_size = 2;
1291 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1292 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1293 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1294 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1295 int pool_stride_x = 0;
1296 int pool_stride_y = 0;
1297 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1298 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1299 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1300
1301 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1302 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));
1303
1304 execute_window_loop(window, [&](const Coordinates & id)
1305 {
1306 float32x2_t top_data = vld1_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1307 float32x2_t bottom_data = vld1_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1308 float32x2_t res = {};
1309 float final_res = 0;
1310
1311 // Get power of 2 in case of l2 pooling
1312 if(pooling_type == PoolingType::L2)
1313 {
1314 top_data = vmul_f32(top_data, top_data);
1315 bottom_data = vmul_f32(bottom_data, bottom_data);
1316 }
1317
1318 if(pooling_type != PoolingType::MAX)
1319 {
1320 // Calculate scale
1321 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);
1322 const float32x2_t scale_v = vdup_n_f32(scale);
1323
1324 // Perform pooling
1325 const float32x2_t sum_data = vadd_f32(top_data, bottom_data);
1326 res = vmul_f32(vpadd_f32(sum_data, sum_data), scale_v);
1327 }
1328 else
1329 {
1330 const float32x2_t max_data = vmax_f32(top_data, bottom_data);
1331 res = vpmax_f32(max_data, max_data);
1332 }
1333 final_res = vget_lane_f32(res, 0);
1334
1335 // Calculate square-root in case of l2 pooling
1336 if(pooling_type == PoolingType::L2)
1337 {
1338 final_res = sqrt(final_res);
1339 }
1340
1341 // Store result
1342 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1343 },
1344 input, output);
1345}
1346
1347void NEPoolingLayerKernel::pooling3_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1348{
1349 Iterator input(_input, window_input);
1350 Iterator output(_output, window);
1351
1352 constexpr const int pool_size = 3;
1353 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1354 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1355 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1356 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1357 int pool_stride_x = 0;
1358 int pool_stride_y = 0;
1359 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1360 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1361 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1362
1363 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1364 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));
1365 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));
1366
1367 execute_window_loop(window, [&](const Coordinates & id)
1368 {
1369 float32x4_t top_data = vld1q_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1370 float32x4_t middle_data = vld1q_f32(reinterpret_cast<const float *>(input_middle_ptr + input.offset()));
1371 float32x4_t bottom_data = vld1q_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1372 float32x2_t res = {};
1373 float final_res = 0;
1374
1375 // Get power of 2 in case of l2 pooling
1376 if(pooling_type == PoolingType::L2)
1377 {
1378 top_data = vmulq_f32(top_data, top_data);
1379 middle_data = vmulq_f32(middle_data, middle_data);
1380 bottom_data = vmulq_f32(bottom_data, bottom_data);
1381 }
1382
1383 if(pooling_type != PoolingType::MAX)
1384 {
1385 // Calculate scale
1386 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);
1387 const float32x2_t scale_v = vdup_n_f32(scale);
1388
1389 // Perform pooling
1390 const float32x4_t sum_data = vaddq_f32(vaddq_f32(top_data, bottom_data), middle_data);
1391 res = vpadd_f32(vget_high_f32(vsetq_lane_f32(0.f, sum_data, 3)), vget_low_f32(sum_data));
1392 res = vmul_f32(vpadd_f32(res, res), scale_v);
1393 }
1394 else
1395 {
1396 const float32x4_t max_data = vmaxq_f32(vmaxq_f32(top_data, bottom_data), middle_data);
1397 res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data, 3)), vget_low_f32(max_data));
1398 res = vpmax_f32(res, res);
1399 }
1400 final_res = vget_lane_f32(res, 0);
1401
1402 // Calculate square-root in case of l2 pooling
1403 if(pooling_type == PoolingType::L2)
1404 {
1405 final_res = sqrt(final_res);
1406 }
1407
1408 // Store result
1409 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1410 },
1411 input, output);
1412}
1413
1414void NEPoolingLayerKernel::pooling7_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1415{
1416 Iterator input(_input, window_input);
1417 Iterator output(_output, window);
1418
1419 constexpr const int pool_size = 7;
1420 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1421 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1422 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1423 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1424 int pool_stride_x = 0;
1425 int pool_stride_y = 0;
1426 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1427 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1428 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1429
1430 std::array<const uint8_t *, pool_size> input_ptrs{ {} };
1431 for(int i = 0; i < pool_size; ++i)
1432 {
1433 input_ptrs[i] = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + i));
1434 }
1435
1436 execute_window_loop(window, [&](const Coordinates & id)
1437 {
1438 float32x2_t res = {};
1439 float final_res = 0.f;
1440 if(pooling_type != PoolingType::MAX)
1441 {
1442 // Calculate scale
1443 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);
1444 const float32x2_t scale_v = vdup_n_f32(scale);
1445
1446 // Perform pooling
1447 float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1448 // Get power of 2 in case of l2 pooling
1449 if(pooling_type == PoolingType::L2)
1450 {
1451 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1452 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1453 }
1454 float32x4_t sum_data = vaddq_f32(data.val[0], vsetq_lane_f32(0.f, data.val[1], 3));
1455 for(int i = 1; i < pool_size; ++i)
1456 {
1457 data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1458 // Get power of 2 in case of l2 pooling
1459 if(pooling_type == PoolingType::L2)
1460 {
1461 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1462 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1463 }
1464 sum_data = vaddq_f32(sum_data, data.val[0]);
1465 sum_data = vaddq_f32(sum_data, vsetq_lane_f32(0.f, data.val[1], 3));
1466 }
1467 res = vpadd_f32(vget_high_f32(sum_data), vget_low_f32(sum_data));
1468 res = vmul_f32(vpadd_f32(res, res), scale_v);
1469 }
1470 else
1471 {
1472 float32x4x2_t max_data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1473 for(int i = 1; i < pool_size; ++i)
1474 {
1475 const float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1476 max_data = vmax2q_f32(max_data, data);
1477 }
1478 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]));
1479 res = vpmax_f32(res, vpmax_f32(vget_high_f32(max_data.val[0]), vget_low_f32(max_data.val[0])));
1480 res = vpmax_f32(res, res);
1481 }
1482 final_res = vget_lane_f32(res, 0);
1483
1484 // Calculate square-root in case of l2 pooling
1485 if(pooling_type == PoolingType::L2)
1486 {
1487 final_res = sqrt(final_res);
1488 }
1489
1490 // Store result
1491 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1492 },
1493 input, output);
1494}
1495
1496void NEPoolingLayerKernel::poolingMxN_f32_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001497{
1498 Iterator input(_input, window_input);
1499 Iterator output(_output, window);
1500
1501 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1502 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1503 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1504 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1505 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1506 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1507 int pool_stride_x = 0;
1508 int pool_stride_y = 0;
1509 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1510 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1511 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1512
1513 float32x4_t vres;
1514
1515 execute_window_loop(window, [&](const Coordinates & id)
1516 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001517 const int idx_width = id.y() * pool_stride_x;
1518 const int idx_height = id.z() * pool_stride_y;
1519 const int pool_limit_y = pool_pad_top - idx_height;
1520 const int pool_limit_x = pool_pad_left - idx_width;
1521
1522 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1523 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1524 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1525 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1526
Michalis Spyrou57dac842018-03-01 16:03:50 +00001527 if(pooling_type != PoolingType::MAX)
1528 {
1529 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001530 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,
1531 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001532 const float32x4_t scale_v = vdupq_n_f32(scale);
1533
1534 // Perform pooling
1535 vres = vdupq_n_f32(0.0f);
1536
Michalis Spyrouced25572018-10-01 16:26:20 +01001537 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001538 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001539 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001540 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001541 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1542 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1543
1544 // Get power of 2 in case of l2 pooling and accumulate
1545 if(pooling_type == PoolingType::L2)
1546 {
1547 vres = vmlaq_f32(vres, data, data);
1548 }
1549 else
1550 {
1551 vres = vaddq_f32(vres, data);
1552 }
1553 }
1554 }
1555 // Divide by scale
1556 vres = vmulq_f32(vres, scale_v);
1557 }
1558 else
1559 {
1560 vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001561 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001562 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001563 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001564 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001565 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1566 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1567 vres = vmaxq_f32(vres, data);
1568 }
1569 }
1570 }
1571
1572 // Calculate square-root in case of l2 pooling
1573 if(pooling_type == PoolingType::L2)
1574 {
Georgios Pinitas027ce5b2018-11-08 18:55:36 +00001575 vres = vmulq_f32(vres, vinvsqrtq_f32(vres));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001576 }
1577
1578 // Store result
1579 vst1q_f32(reinterpret_cast<float *>(output.ptr()), vres);
1580 },
1581 input, output);
1582}
1583
Pablo Tello77e6c552018-12-04 15:33:49 +00001584void NEPoolingLayerKernel::poolingMxN_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +00001585{
1586 Iterator input(_input, window_input);
1587 Iterator output(_output, window);
1588
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001589 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
1590 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height;
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001591 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1592 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1593 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1594 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1595 int pool_stride_x = 0;
1596 int pool_stride_y = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +00001597 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001598 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1599 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +00001600
1601 execute_window_loop(window, [&](const Coordinates & id)
1602 {
1603 uint8_t res = 0;
1604
1605 if(pooling_type != PoolingType::MAX)
1606 {
1607 uint32x4_t vres = vdupq_n_u32(0);
1608 uint32_t sres = 0;
1609
1610 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001611 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 +00001612
1613 // Perform pooling
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001614 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001615 {
1616 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001617 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001618 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001619 const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1620 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001621
1622 const uint16x8_t data_u16 = vmovl_u8(data);
1623 vres = vaddq_u32(vres, vaddl_u16(vget_high_u16(data_u16), vget_low_u16(data_u16)));
1624 }
1625
1626 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001627 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001628 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001629 uint8_t data = *(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001630 sres += data;
1631 }
1632 }
1633
1634 // Reduction
1635 const auto tmp = vpadd_u32(vget_high_u32(vres), vget_low_u32(vres));
1636 sres += vget_lane_u32(tmp, 0) + vget_lane_u32(tmp, 1);
1637
1638 // Divide by scale
1639 res = static_cast<uint8_t>(support::cpp11::round(sres * scale));
1640 }
1641 else
1642 {
1643 uint8x8_t vres = vdup_n_u8(0);
1644 res = 0;
1645
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001646 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001647 {
1648 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001649 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001650 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001651 const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1652 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001653 vres = vmax_u8(vres, data);
1654 }
1655
1656 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001657 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001658 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001659 const uint8_t data = *(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001660 res = std::max(res, data);
1661 }
1662 }
1663
1664 // Reduce max
1665 vres = vpmax_u8(vres, vres);
1666 vres = vpmax_u8(vres, vres);
1667 vres = vpmax_u8(vres, vres);
1668
1669 // Get max value
1670 res = std::max(res, vget_lane_u8(vres, 0));
1671 }
1672
1673 // Store result
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001674 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
1675 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
1676 res = (input_qinfo != output_qinfo) ? sqcvt_qasymm8_f32(scvt_f32_qasymm8(res, input_qinfo.scale, input_qinfo.offset), output_qinfo.scale,
1677 output_qinfo.offset) :
1678 res;
Georgios Pinitas55186712018-01-08 17:37:12 +00001679 *(reinterpret_cast<uint8_t *>(output.ptr())) = res;
1680 },
1681 input, output);
1682}
1683
Pablo Tello77e6c552018-12-04 15:33:49 +00001684void NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001685{
1686 Iterator input(_input, window_input);
1687 Iterator output(_output, window);
1688
1689 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1690 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1691 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1692 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1693 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1694 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1695 int pool_stride_x = 0;
1696 int pool_stride_y = 0;
1697 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1698 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1699 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1700
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001701 const float32x4_t half_scale_v = vdupq_n_f32(0.5f);
1702 const QuantizationInfo &input_qinfo = _input->info()->quantization_info();
1703 const QuantizationInfo &output_qinfo = _output->info()->quantization_info();
Georgios Pinitas283fc602018-11-09 10:46:43 +00001704
Michalis Spyrou57dac842018-03-01 16:03:50 +00001705 execute_window_loop(window, [&](const Coordinates & id)
1706 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001707 const int idx_width = id.y() * pool_stride_x;
1708 const int idx_height = id.z() * pool_stride_y;
1709 const int pool_limit_y = pool_pad_top - idx_height;
1710 const int pool_limit_x = pool_pad_left - idx_width;
1711
1712 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1713 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1714 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1715 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1716
Michalis Spyrou57dac842018-03-01 16:03:50 +00001717 if(pooling_type != PoolingType::MAX)
1718 {
1719 uint32x4_t vres1 = vdupq_n_u32(0);
1720 uint32x4_t vres2 = vdupq_n_u32(0);
Michalis Spyrouced25572018-10-01 16:26:20 +01001721 uint32x4_t vres3 = vdupq_n_u32(0);
1722 uint32x4_t vres4 = vdupq_n_u32(0);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001723
1724 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001725 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,
1726 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001727 const float32x4_t scale_v = vdupq_n_f32(scale);
1728
1729 // Perform pooling
Michalis Spyrouced25572018-10-01 16:26:20 +01001730 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001731 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001732 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001733 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001734 const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1735 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001736
Michalis Spyrouced25572018-10-01 16:26:20 +01001737 const uint16x8_t data_u16 = vmovl_u8(vget_low_u8(data));
1738 const uint16x8_t data2_u16 = vmovl_u8(vget_high_u8(data));
1739 vres1 = vaddq_u32(vres1, vmovl_u16(vget_low_u16(data_u16)));
1740 vres2 = vaddq_u32(vres2, vmovl_u16(vget_high_u16(data_u16)));
1741 vres3 = vaddq_u32(vres3, vmovl_u16(vget_low_u16(data2_u16)));
1742 vres4 = vaddq_u32(vres4, vmovl_u16(vget_high_u16(data2_u16)));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001743 }
1744 }
Georgios Pinitas283fc602018-11-09 10:46:43 +00001745 // Divide by scale and add 0.5f to round to nearest instead of rounding towards zero
1746 vres1 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres1), scale_v));
1747 vres2 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres2), scale_v));
1748 vres3 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres3), scale_v));
1749 vres4 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres4), scale_v));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001750
Michalis Spyrouced25572018-10-01 16:26:20 +01001751 uint8x8_t res1 = vmovn_u16(vcombine_u16(vmovn_u32(vres1), vmovn_u32(vres2)));
1752 uint8x8_t res2 = vmovn_u16(vcombine_u16(vmovn_u32(vres3), vmovn_u32(vres4)));
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001753 if(input_qinfo != output_qinfo)
1754 {
1755 const auto requantized_output = vquantize(vdequantize(vcombine_u8(res1, res2), input_qinfo), output_qinfo);
1756 res1 = vget_low_u8(requantized_output);
1757 res2 = vget_high_u8(requantized_output);
1758 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001759
1760 // Store result
Michalis Spyrouced25572018-10-01 16:26:20 +01001761 vst1_u8(output.ptr(), res1);
1762 vst1_u8(output.ptr() + 8, res2);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001763 }
1764 else
1765 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001766 uint8x16_t vres = vdupq_n_u8(0);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001767
Michalis Spyrouced25572018-10-01 16:26:20 +01001768 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001769 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001770 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001771 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001772 const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1773 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1774 vres = vmaxq_u8(vres, data);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001775 }
1776 }
1777
1778 // Store result
Pablo Telloa52e4cf2019-04-01 14:55:18 +01001779 vst1q_u8(output.ptr(), (input_qinfo != output_qinfo) ? vquantize(vdequantize(vres, input_qinfo), output_qinfo) : vres);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001780 }
1781 },
1782 input, output);
1783}
1784
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001785Status NEPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
1786{
1787 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
1788
1789 unsigned int pooled_w = 0;
1790 unsigned int pooled_h = 0;
1791 unsigned int num_elems_processed_per_iteration = 0;
1792 BorderSize border_size(0);
1793
Michalis Spyrou57dac842018-03-01 16:03:50 +00001794 const bool is_global_pooling = pool_info.is_global_pooling();
1795 unsigned int pool_size_x = 0;
1796 unsigned int pool_size_y = 0;
1797
1798 // Get data layout
1799 const DataLayout data_layout = input->data_layout();
1800 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1801 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1802
1803 pool_size_x = is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size().width;
1804 pool_size_y = is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size().height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001805
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001806 // Validate pool info before calling scaled_dimensions
1807 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_pool_info(pool_size_x, pool_size_y));
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001808
1809 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +00001810 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
1811 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001812 pool_size_x,
1813 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001814 pool_info.pad_stride_info());
1815
Georgios Pinitas13d96e02018-08-23 11:20:23 +01001816 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, pooled_w, pooled_h));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001817 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,
1818 pool_size_x, pool_size_y)
1819 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001820
1821 return Status{};
1822}
1823
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001824void NEPoolingLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001825{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001826 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001827 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1828 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1829 ARM_COMPUTE_ERROR_ON(_func == nullptr);
1830
Pablo Tello77e6c552018-12-04 15:33:49 +00001831 const unsigned int pool_stride_x = _pool_info.pad_stride_info().stride().first;
1832 const unsigned int pool_stride_y = _pool_info.pad_stride_info().stride().second;
1833 const unsigned int pool_size = _pool_info.pool_size().width;
1834 const bool exclude_padding = _pool_info.exclude_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001835
Michalis Spyrou57dac842018-03-01 16:03:50 +00001836 Window window_input(window);
1837 if(_input->info()->data_layout() == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001838 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001839 // Set step for input in x and y direction for the input
1840 unsigned int window_x_inc = 0;
1841 switch(_input->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +01001842 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001843 case DataType::QASYMM8:
1844 {
1845 window_x_inc = pool_stride_x;
1846 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
1847 {
1848 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
1849 }
1850 break;
1851 }
Pablo Tello77e6c552018-12-04 15:33:49 +00001852
Georgios Pinitas13d96e02018-08-23 11:20:23 +01001853 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +00001854 case DataType::F32:
1855 {
1856 window_x_inc = pool_stride_x;
1857 break;
1858 }
1859 default:
1860 {
1861 ARM_COMPUTE_ERROR("Not supported");
1862 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001863 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001864 window_input.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
1865 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 +01001866 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001867 else
1868 {
Georgios Pinitascac13b12018-04-27 19:07:19 +01001869 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 +00001870 window_input.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
1871 window_input.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
1872 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001873
1874 // Run function
Pablo Tello77e6c552018-12-04 15:33:49 +00001875 (this->*_func)(window_input, window, _pool_info.pool_type(), exclude_padding);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001876}