blob: d00a4af4fec52c6328c52893c962acd3da75e6d0 [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);
Georgios Pinitas99089ce2019-02-06 14:16:18 +0000141 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000142 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w)
143 || (output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h));
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100144 }
145
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000146 return Status{};
147}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000149Status validate_arguments_pool_info(const unsigned int pool_size_x, const unsigned int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000150{
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000151 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_x == 0);
152 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_y == 0);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000153
154 return Status{};
155}
156
157std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &num_elems_processed_per_iteration,
158 BorderSize &border_size,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000159 unsigned int pooled_w, unsigned int pooled_h, int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000160{
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +0100161 // Output auto inizialitation if not yet initialized
162 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_pool_shape(*input, pool_info)));
163
Michalis Spyrou57dac842018-03-01 16:03:50 +0000164 DataLayout data_layout = input->data_layout();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000165 unsigned int num_elems_read_per_iteration = 0;
166 unsigned int num_elems_horizontal_window = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000167 int pool_stride_x = 0;
168 int pool_stride_y = 0;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000169 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
170 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
171 const int input_width = input->dimension(idx_width);
172 const int input_height = input->dimension(idx_height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000173 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
174 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000175 const int pool_pad_right = pad_stride_info.pad_right();
176 const int pool_pad_top = pad_stride_info.pad_top();
177 const int pool_pad_left = pad_stride_info.pad_left();
178 const int pool_pad_bottom = pad_stride_info.pad_bottom();
179 const bool is_square = pool_size_x == pool_size_y;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000180
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000181 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +0000182 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
183 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000184 pool_size_x,
185 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000186 pad_stride_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000188 //If it's not squared and optimized will be executed the MxN
189 num_elems_read_per_iteration = 1;
190 num_elems_processed_per_iteration = 1;
191 num_elems_horizontal_window = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192
Michalis Spyrou57dac842018-03-01 16:03:50 +0000193 const bool is_nhwc = data_layout == DataLayout::NHWC;
194
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000195 if(is_square)
196 {
197 switch(input->data_type())
198 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000199 case DataType::QASYMM8:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000200 if(is_nhwc)
201 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100202 num_elems_processed_per_iteration = 16;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000203 break;
204 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000205 switch(pool_size_x)
206 {
207 case 2:
208 num_elems_read_per_iteration = 16;
209 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
210 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
211 break;
212 case 3:
213 num_elems_read_per_iteration = 16;
214 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
215 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
216 break;
217 default:
218 break;
219 }
220 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000221#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
222 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000223 if(is_nhwc)
224 {
225 num_elems_processed_per_iteration = 8;
226 break;
227 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000228 switch(pool_size_x)
229 {
230 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000231 case 3:
232 num_elems_read_per_iteration = 4;
233 num_elems_processed_per_iteration = 1;
234 num_elems_horizontal_window = 1;
235 break;
236 default:
237 break;
238 }
239 break;
240#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
241 case DataType::F32:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000242 if(is_nhwc)
243 {
Georgios Pinitas64f1a902018-09-18 13:42:51 +0100244 num_elems_processed_per_iteration = 4;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000245 break;
246 }
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000247 switch(pool_size_x)
248 {
249 case 2:
250 num_elems_read_per_iteration = 2;
251 break;
252 case 3:
253 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
254 break;
255 case 7:
256 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
257 break;
258 default:
259 break;
260 }
261 num_elems_processed_per_iteration = 1;
262 num_elems_horizontal_window = 1;
263 break;
264 default:
265 ARM_COMPUTE_ERROR("Element size not supported");
266 break;
267 }
268 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000269 else
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000270 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000271 if(is_nhwc)
272 {
Michalis Spyrouced25572018-10-01 16:26:20 +0100273 num_elems_processed_per_iteration = 16 / input->element_size();
Michalis Spyrou57dac842018-03-01 16:03:50 +0000274 }
275 }
276
277 bool window_changed = false;
278 Window win{};
279 if(data_layout == DataLayout::NCHW)
280 {
281 // Number of iterations in X dimension
282 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
283
284 // Upper limit for the number of right/bottom border elements that are accessed
285 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;
286 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height;
287
288 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
289 border_size.right = std::max(upper_bound_w, pool_pad_right);
290 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
291
292 TensorShape output_shape{ input->tensor_shape() };
293 output_shape.set(0, pooled_w);
294 output_shape.set(1, pooled_h);
295 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
296
297 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
298 AccessWindowStatic input_access(input, -pool_pad_left, -pool_pad_top, input_width + border_size.right, input_height + border_size.bottom);
299
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000300 AccessWindowHorizontal output_access(output, 0, num_elems_horizontal_window);
301 window_changed = update_window_and_padding(win, input_access, output_access);
302 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
303 }
304 else
305 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000306 TensorShape output_shape{ input->tensor_shape() };
307 output_shape.set(1, pooled_w);
308 output_shape.set(2, pooled_h);
309 TensorInfo output_info(input->clone()->set_tensor_shape(output_shape));
310
311 win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration));
312 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
313
314 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
315 window_changed = update_window_and_padding(win, input_access, output_access);
316 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000317 }
318
319 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
320 return std::make_pair(err, win);
321}
322} // namespace
323
324NEPoolingLayerKernel::NEPoolingLayerKernel()
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000325 : _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 +0000326{
327}
328
329BorderSize NEPoolingLayerKernel::border_size() const
330{
331 return _border_size;
332}
333
334void NEPoolingLayerKernel::configure(const ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info)
335{
336 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
337
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000338 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000339 const bool is_global_pooling = pool_info.is_global_pooling();
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000340 const int pool_stride_x = pad_stride_info.stride().first;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000341
342 // Get data layout
343 const DataLayout data_layout = input->info()->data_layout();
344 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
345 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000346
347 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000348 const Size2D pool_size(
349 is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size().width,
350 is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size().height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000351
352 // Validate pool info before calling scaled_dimensions
Pablo Tello77e6c552018-12-04 15:33:49 +0000353 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_pool_info(pool_size.x(), pool_size.y()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000354
355 // Check output dimensions
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000356 unsigned int pooled_w, 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
643 // Store result
644 if(pool_stride_x == 1)
645 {
646 const uint8x8x2_t res = { { lower_res, upper_res } };
647 vst2_u8(reinterpret_cast<uint8_t *>(output.ptr()), res);
648 }
649 else
650 {
651 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), lower_res);
652 }
653 },
654 input, output);
655}
656
Pablo Tello77e6c552018-12-04 15:33:49 +0000657void NEPoolingLayerKernel::pooling3_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100658{
Pablo Tello77e6c552018-12-04 15:33:49 +0000659 ARM_COMPUTE_UNUSED(pooling_type);
660 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000661#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100662 Iterator input(_input, window_input);
663 Iterator output(_output, window);
664
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000665 constexpr const int pool_size = 3;
666 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
667 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
668 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
669 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
670 int pool_stride_x = 0;
671 int pool_stride_y = 0;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100672 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000673 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
674 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100675
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000676 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
677 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));
678 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 +0100679
680 execute_window_loop(window, [&](const Coordinates & id)
681 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100682 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
683 float16x4_t middle_data = vld1_f16(reinterpret_cast<const float16_t *>(input_middle_ptr + input.offset()));
684 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
685 float16x4_t res = {};
686
687 // Get power of 2 in case of l2 pooling
688 if(pooling_type == PoolingType::L2)
689 {
690 top_data = vmul_f16(top_data, top_data);
691 middle_data = vmul_f16(middle_data, middle_data);
692 bottom_data = vmul_f16(bottom_data, bottom_data);
693 }
694
695 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100696 {
697 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000698 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 +0100699 const float16x4_t scale_v = vdup_n_f16(scale);
700 // Perform pooling
701 const float16x4_t sum_data = vadd_f16(vadd_f16(top_data, bottom_data), middle_data);
702 res = vpadd_f16(vset_lane_f16(0.f, sum_data, 3), sum_data);
703 res = vmul_f16(vpadd_f16(res, res), scale_v);
704 }
705 else
706 {
707 const float16x4_t max_data = vmax_f16(vmax_f16(top_data, bottom_data), middle_data);
708 res = vpmax_f16(vset_lane_f16(-std::numeric_limits<float>::max(), max_data, 3), max_data);
709 res = vpmax_f16(res, res);
710 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100711
712 // Calculate square-root in case of l2 pooling
713 if(pooling_type == PoolingType::L2)
714 {
715 res = vinv_f16(vinvsqrt_f16(res));
716 }
717
Pablo Tello0c34fe22017-06-26 17:17:42 +0100718 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
719 },
720 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000721#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100722 ARM_COMPUTE_UNUSED(window_input);
723 ARM_COMPUTE_UNUSED(window);
724 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000725#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100726}
727
Pablo Tello77e6c552018-12-04 15:33:49 +0000728void NEPoolingLayerKernel::pooling2_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100729{
Pablo Tello77e6c552018-12-04 15:33:49 +0000730 ARM_COMPUTE_UNUSED(pooling_type);
731 ARM_COMPUTE_UNUSED(exclude_padding);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000732#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello0c34fe22017-06-26 17:17:42 +0100733 Iterator input(_input, window_input);
734 Iterator output(_output, window);
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000735 constexpr int pool_size = 2;
736 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
737 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
738 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
739 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
740 int pool_stride_x, pool_stride_y = 0;
741 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
742 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
743 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100744
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000745 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
746 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 +0100747
748 execute_window_loop(window, [&](const Coordinates & id)
749 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100750 float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset()));
751 float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset()));
752 float16x4_t res = {};
Pablo Tello0c34fe22017-06-26 17:17:42 +0100753
Georgios Pinitascdf51452017-08-31 14:21:36 +0100754 // Get power of 2 in case of l2 pooling
755 if(pooling_type == PoolingType::L2)
756 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100757 top_data = vmul_f16(top_data, top_data);
758 bottom_data = vmul_f16(bottom_data, bottom_data);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100759 }
760
761 if(pooling_type != PoolingType::MAX)
Pablo Tello0c34fe22017-06-26 17:17:42 +0100762 {
Pablo Tello77e6c552018-12-04 15:33:49 +0000763 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 +0100764 const float16x4_t scale_v = vdup_n_f16(scale);
765
766 const float16x4_t sum_data = vadd_f16(top_data, bottom_data);
767 res = vmul_f16(vpadd_f16(sum_data, sum_data), scale_v);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100768 }
769 else
770 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100771 const float16x4_t max_data = vmax_f16(top_data, bottom_data);
772 res = vpmax_f16(max_data, max_data);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100773 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100774
775 // Calculate square-root in case of l2 pooling
776 if(pooling_type == PoolingType::L2)
777 {
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100778 res = vinv_f16(vinvsqrt_f16(res));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100779 }
780
781 // Store result
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100782 *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100783 },
784 input, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000785#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100786 ARM_COMPUTE_UNUSED(window_input);
787 ARM_COMPUTE_UNUSED(window);
788 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000789#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello0c34fe22017-06-26 17:17:42 +0100790}
791
Pablo Tello77e6c552018-12-04 15:33:49 +0000792void NEPoolingLayerKernel::pooling3_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +0000793{
794 Iterator input(_input, window_input);
795 Iterator output(_output, window);
796
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000797 constexpr int pool_size = 3;
798 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
799 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
800 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
801 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
802 int pool_stride_x = 0;
803 int pool_stride_y = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +0000804 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000805 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
806 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +0000807
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000808 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
809 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));
810 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 +0000811
812 execute_window_loop(window, [&](const Coordinates & id)
813 {
814 const auto top_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_top_ptr + input.offset()));
815 const auto middle_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_middle_ptr + input.offset()));
816 const auto bottom_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_bottom_ptr + input.offset()));
817
818 if(pooling_type == PoolingType::AVG)
819 {
820 // Convert data to u16
821 const uint16x8x2_t top_data_u16 = { { vmovl_u8(vget_low_u8(top_data)), vmovl_u8(vget_high_u8(top_data)) } };
822 const uint16x8x2_t middle_data_u16 = { { vmovl_u8(vget_low_u8(middle_data)), vmovl_u8(vget_high_u8(middle_data)) } };
823 const uint16x8x2_t bottom_data_u16 = { { vmovl_u8(vget_low_u8(bottom_data)), vmovl_u8(vget_high_u8(bottom_data)) } };
824
825 // Calculate row sums
826 const uint16x8x2_t vrsum =
827 {
828 {
829 vaddq_u16(vaddq_u16(top_data_u16.val[0], bottom_data_u16.val[0]), middle_data_u16.val[0]),
830 vaddq_u16(vaddq_u16(top_data_u16.val[1], bottom_data_u16.val[1]), middle_data_u16.val[1]),
831 }
832 };
833 const uint16x8x2_t vrsum_shifted_1 =
834 {
835 {
836 vextq_u16(vrsum.val[0], vrsum.val[1], 1),
837 vextq_u16(vrsum.val[1], vrsum.val[1], 1)
838 }
839 };
840 const uint16x8x2_t vrsum_shifted_2 =
841 {
842 {
843 vextq_u16(vrsum.val[0], vrsum.val[1], 2),
844 vextq_u16(vrsum.val[1], vrsum.val[1], 2)
845 }
846 };
847 // Calculate final sum
848 uint16x8x2_t final_sum =
849 {
850 {
851 vaddq_u16(vaddq_u16(vrsum.val[0], vrsum_shifted_1.val[0]), vrsum_shifted_2.val[0]),
852 vaddq_u16(vaddq_u16(vrsum.val[1], vrsum_shifted_1.val[1]), vrsum_shifted_2.val[1]),
853 }
854 };
855 if(pool_stride_x == 2)
856 {
857 uint16x8_t res =
858 {
859 vgetq_lane_u16(final_sum.val[0], 0),
860 vgetq_lane_u16(final_sum.val[0], 2),
861 vgetq_lane_u16(final_sum.val[0], 4),
862 vgetq_lane_u16(final_sum.val[0], 6),
863 vgetq_lane_u16(final_sum.val[1], 0),
864 vgetq_lane_u16(final_sum.val[1], 2),
865 vgetq_lane_u16(final_sum.val[1], 4),
866 vgetq_lane_u16(final_sum.val[1], 6),
867 };
868
Pablo Tello77e6c552018-12-04 15:33:49 +0000869 scale_vector_s16x8(exclude_padding, res, id, 0, 1,
870 pool_size, upper_bound_w, upper_bound_h,
871 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000872 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), vmovn_u16(res));
873 }
874 else
875 {
876 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000877 scale_vector_s16x8(exclude_padding, final_sum.val[0], id, 0, 1,
878 pool_size, upper_bound_w, upper_bound_h,
879 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000880 // Scale lower result
Pablo Tello77e6c552018-12-04 15:33:49 +0000881 scale_vector_s16x8(exclude_padding, final_sum.val[1], id, 8, 1,
882 pool_size, upper_bound_w, upper_bound_h,
883 pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
Georgios Pinitas55186712018-01-08 17:37:12 +0000884 const uint8x16_t res = vcombine_u8(vmovn_u16(final_sum.val[0]), vmovn_u16(final_sum.val[1]));
885 vst1q_u8(reinterpret_cast<uint8_t *>(output.ptr()), res);
886 }
887 }
888 else
889 {
890 const uint8x16_t max_data = vmaxq_u8(vmaxq_u8(top_data, bottom_data), middle_data);
891 const uint8x16_t max_data_shift1 = vextq_u8(max_data, max_data, 1);
892 const uint8x16_t max_data_shift2 = vextq_u8(max_data, max_data, 2);
893 const uint8x16_t final_max = vmaxq_u8(vmaxq_u8(max_data, max_data_shift1), max_data_shift2);
894
895 if(pool_stride_x == 2)
896 {
897 const uint8x8x2_t table = { { vget_low_u8(final_max), vget_high_u8(final_max) } };
898 static const uint8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 };
899 const uint8x8_t res = vtbl2_u8(table, lookup_val);
900 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), res);
901 }
902 else
903 {
904 vst1q_u8(reinterpret_cast<uint8_t *>(output.ptr()), final_max);
905 }
906 }
907 },
908 input, output);
909}
910
Pablo Tello77e6c552018-12-04 15:33:49 +0000911void NEPoolingLayerKernel::poolingMxN_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100912{
Pablo Tello77e6c552018-12-04 15:33:49 +0000913 ARM_COMPUTE_UNUSED(pooling_type);
914 ARM_COMPUTE_UNUSED(exclude_padding);
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000915#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
916 Iterator input(_input, window_input);
917 Iterator output(_output, window);
918
919 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
920 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height;
921 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
922 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
923 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
924 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
925 int pool_stride_x = 0;
926 int pool_stride_y = 0;
927 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
928 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
929 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
930
931 execute_window_loop(window, [&](const Coordinates & id)
932 {
933 float16_t res = 0.0f;
934 float16x8_t vres = vdupq_n_f16(0.0f);
935
936 if(pooling_type != PoolingType::MAX)
937 {
938 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +0000939 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 +0000940
941 // Perform pooling
942
943 for(int y = 0; y < pool_size_y; ++y)
944 {
945 int x = 0;
946 for(; x <= (pool_size_x - 8); x += 8)
947 {
948 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
949 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
950
951 // Get power of 2 in case of l2 pooling and accumulate
952 if(pooling_type == PoolingType::L2)
953 {
954 vres = vaddq_f16(vres, vmulq_f16(data, data));
955 }
956 else
957 {
958 vres = vaddq_f16(vres, data);
959 }
960 }
961
962 // Leftover for loop
963 for(; x < pool_size_x; ++x)
964 {
965 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()));
966
967 // Get power of 2 in case of l2 pooling
968 if(pooling_type == PoolingType::L2)
969 {
970 data *= data;
971 }
972
973 res += data;
974 }
975 }
976
977 // Reduction
978 float16x4_t tmp = vpadd_f16(vget_high_f16(vres), vget_low_f16(vres));
979 res += vget_lane_f16(tmp, 0);
980 res += vget_lane_f16(tmp, 1);
981 res += vget_lane_f16(tmp, 2);
982 res += vget_lane_f16(tmp, 3);
983
984 // Divide by scale
985 res *= scale;
986 }
987 else
988 {
989 float16x8_t vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
990 res = std::numeric_limits<float>::lowest();
991
992 for(int y = 0; y < pool_size_y; ++y)
993 {
994 int x = 0;
995 for(; x <= (pool_size_x - 8); x += 8)
996 {
997 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
998 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
999 vres = vmaxq_f16(vres, data);
1000 }
1001
1002 // Leftover for loop
1003 for(; x < pool_size_x; ++x)
1004 {
1005 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()));
1006 res = std::max(res, data);
1007 }
1008 }
1009
1010 float16x4_t tmp = vpmax_f16(vget_high_f16(vres), vget_low_f16(vres));
1011 res = std::max(res, vget_lane_f16(tmp, 0));
1012 res = std::max(res, vget_lane_f16(tmp, 1));
1013 res = std::max(res, vget_lane_f16(tmp, 2));
1014 res = std::max(res, vget_lane_f16(tmp, 3));
1015 }
1016
1017 // Calculate square-root in case of l2 pooling
1018 if(pooling_type == PoolingType::L2)
1019 {
1020 res = std::sqrt(res);
1021 }
1022
1023 // Store result
1024 *(reinterpret_cast<float16_t *>(output.ptr())) = res;
1025 },
1026 input, output);
1027
1028#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1029 ARM_COMPUTE_UNUSED(window_input);
1030 ARM_COMPUTE_UNUSED(window);
1031 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1032#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1033}
1034
Pablo Tello77e6c552018-12-04 15:33:49 +00001035void NEPoolingLayerKernel::poolingMxN_f16_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001036{
Pablo Tello77e6c552018-12-04 15:33:49 +00001037 ARM_COMPUTE_UNUSED(pooling_type);
1038 ARM_COMPUTE_UNUSED(exclude_padding);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001039#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1040 Iterator input(_input, window_input);
1041 Iterator output(_output, window);
1042
1043 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1044 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1045 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1046 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1047 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1048 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1049 int pool_stride_x = 0;
1050 int pool_stride_y = 0;
1051 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1052 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1053 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1054
1055 float16x8_t vres;
1056
1057 execute_window_loop(window, [&](const Coordinates & id)
1058 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001059 const int idx_width = id.y() * pool_stride_x;
1060 const int idx_height = id.z() * pool_stride_y;
1061 const int pool_limit_y = pool_pad_top - idx_height;
1062 const int pool_limit_x = pool_pad_left - idx_width;
1063
1064 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1065 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1066 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1067 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1068
Michalis Spyrou57dac842018-03-01 16:03:50 +00001069 if(pooling_type != PoolingType::MAX)
1070 {
1071 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001072 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,
1073 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001074 const float16x8_t scale_v = vdupq_n_f16(scale);
1075
1076 // Perform pooling
1077 vres = vdupq_n_f16(0.0f);
Michalis Spyrouced25572018-10-01 16:26:20 +01001078 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001079 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001080 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001081 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001082 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1083 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1084
1085 // Get power of 2 in case of l2 pooling and accumulate
1086 if(pooling_type == PoolingType::L2)
1087 {
1088 vres = vaddq_f16(vres, vmulq_f16(data, data));
1089 }
1090 else
1091 {
1092 vres = vaddq_f16(vres, data);
1093 }
1094 }
1095 }
1096 // Divide by scale
1097 vres = vmulq_f16(vres, scale_v);
1098 }
1099 else
1100 {
1101 vres = vdupq_n_f16(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001102
1103 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001104 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001105 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001106 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001107 const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1108 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1109 vres = vmaxq_f16(vres, data);
1110 }
1111 }
1112 }
1113
1114 // Calculate square-root in case of l2 pooling
1115 if(pooling_type == PoolingType::L2)
1116 {
1117 float16x8_t sqrt_reciprocal = vrsqrteq_f16(vres);
1118 vres = vmulq_f16(vres, vmulq_f16(vrsqrtsq_f16(vmulq_f16(vres, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal));
1119 }
1120
1121 // Store result
1122 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vres);
1123 },
1124 input, output);
1125
1126#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1127 ARM_COMPUTE_UNUSED(window_input);
1128 ARM_COMPUTE_UNUSED(window);
1129 ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a");
1130#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1131}
1132
Pablo Tello77e6c552018-12-04 15:33:49 +00001133void NEPoolingLayerKernel::poolingMxN_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001134{
1135 Iterator input(_input, window_input);
1136 Iterator output(_output, window);
1137
1138 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
1139 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 +00001140 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1141 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1142 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1143 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1144 int pool_stride_x = 0;
1145 int pool_stride_y = 0;
Gian Marco Iodice16824302017-09-28 15:41:37 +01001146 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001147 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1148 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Gian Marco Iodice16824302017-09-28 15:41:37 +01001149
1150 execute_window_loop(window, [&](const Coordinates & id)
1151 {
1152 float res = 0.0f;
1153
1154 if(pooling_type != PoolingType::MAX)
1155 {
1156 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001157 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 +01001158
1159 // Perform pooling
1160 float32x4_t vres = vdupq_n_f32(0.0f);
1161
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001162 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001163 {
1164 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001165 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001166 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001167 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1168 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001169
1170 // Get power of 2 in case of l2 pooling and accumulate
1171 if(pooling_type == PoolingType::L2)
1172 {
1173 vres = vmlaq_f32(vres, data, data);
1174 }
1175 else
1176 {
1177 vres = vaddq_f32(vres, data);
1178 }
1179 }
1180
1181 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001182 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001183 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001184 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 +01001185
1186 // Get power of 2 in case of l2 pooling
1187 if(pooling_type == PoolingType::L2)
1188 {
1189 data *= data;
1190 }
1191
1192 res += data;
1193 }
1194 }
1195
1196#if defined(__aarch64__)
1197 // Reduction operation available on 64 bit architectures only
1198 res += vaddvq_f32(vres);
1199#else // __aarch64__
1200 // Reduction
1201 float32x2_t tmp = vpadd_f32(vget_high_f32(vres), vget_low_f32(vres));
1202 tmp = vpadd_f32(tmp, tmp);
1203
1204 res += vget_lane_f32(tmp, 0);
1205#endif // __aarch64__
1206 // Divide by scale
1207 res *= scale;
1208 }
1209 else
1210 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001211 float32x4_t vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
1212 res = std::numeric_limits<float>::lowest();
Gian Marco Iodice16824302017-09-28 15:41:37 +01001213
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001214 for(int y = 0; y < pool_size_y; ++y)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001215 {
1216 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001217 for(; x <= (pool_size_x - 4); x += 4)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001218 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001219 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1220 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Gian Marco Iodice16824302017-09-28 15:41:37 +01001221 vres = vmaxq_f32(vres, data);
1222 }
1223
1224 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001225 for(; x < pool_size_x; ++x)
Gian Marco Iodice16824302017-09-28 15:41:37 +01001226 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001227 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 +01001228 res = std::max(res, data);
1229 }
1230 }
1231
1232#if defined(__aarch64__)
1233 // Reduction operation available on 64 bit architectures only
1234 res = std::max(vmaxvq_f32(vres), res);
1235#else // __aarch64__
1236 float32x2_t tmp = vpmax_f32(vget_high_f32(vres), vget_low_f32(vres));
1237 tmp = vpmax_f32(tmp, tmp);
1238
1239 res = std::max(res, vget_lane_f32(tmp, 0));
1240#endif // __aarch64__
1241 }
1242
1243 // Calculate square-root in case of l2 pooling
1244 if(pooling_type == PoolingType::L2)
1245 {
1246 res = std::sqrt(res);
1247 }
1248
1249 // Store result
1250 *(reinterpret_cast<float *>(output.ptr())) = res;
1251 },
1252 input, output);
1253}
1254
Pablo Tello77e6c552018-12-04 15:33:49 +00001255void NEPoolingLayerKernel::pooling2_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1256{
1257 Iterator input(_input, window_input);
1258 Iterator output(_output, window);
1259
1260 constexpr int pool_size = 2;
1261 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1262 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1263 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1264 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1265 int pool_stride_x = 0;
1266 int pool_stride_y = 0;
1267 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1268 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1269 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1270
1271 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1272 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));
1273
1274 execute_window_loop(window, [&](const Coordinates & id)
1275 {
1276 float32x2_t top_data = vld1_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1277 float32x2_t bottom_data = vld1_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1278 float32x2_t res = {};
1279 float final_res = 0;
1280
1281 // Get power of 2 in case of l2 pooling
1282 if(pooling_type == PoolingType::L2)
1283 {
1284 top_data = vmul_f32(top_data, top_data);
1285 bottom_data = vmul_f32(bottom_data, bottom_data);
1286 }
1287
1288 if(pooling_type != PoolingType::MAX)
1289 {
1290 // Calculate scale
1291 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);
1292 const float32x2_t scale_v = vdup_n_f32(scale);
1293
1294 // Perform pooling
1295 const float32x2_t sum_data = vadd_f32(top_data, bottom_data);
1296 res = vmul_f32(vpadd_f32(sum_data, sum_data), scale_v);
1297 }
1298 else
1299 {
1300 const float32x2_t max_data = vmax_f32(top_data, bottom_data);
1301 res = vpmax_f32(max_data, max_data);
1302 }
1303 final_res = vget_lane_f32(res, 0);
1304
1305 // Calculate square-root in case of l2 pooling
1306 if(pooling_type == PoolingType::L2)
1307 {
1308 final_res = sqrt(final_res);
1309 }
1310
1311 // Store result
1312 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1313 },
1314 input, output);
1315}
1316
1317void NEPoolingLayerKernel::pooling3_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1318{
1319 Iterator input(_input, window_input);
1320 Iterator output(_output, window);
1321
1322 constexpr const int pool_size = 3;
1323 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1324 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1325 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1326 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1327 int pool_stride_x = 0;
1328 int pool_stride_y = 0;
1329 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1330 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1331 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1332
1333 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
1334 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));
1335 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));
1336
1337 execute_window_loop(window, [&](const Coordinates & id)
1338 {
1339 float32x4_t top_data = vld1q_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
1340 float32x4_t middle_data = vld1q_f32(reinterpret_cast<const float *>(input_middle_ptr + input.offset()));
1341 float32x4_t bottom_data = vld1q_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
1342 float32x2_t res = {};
1343 float final_res = 0;
1344
1345 // Get power of 2 in case of l2 pooling
1346 if(pooling_type == PoolingType::L2)
1347 {
1348 top_data = vmulq_f32(top_data, top_data);
1349 middle_data = vmulq_f32(middle_data, middle_data);
1350 bottom_data = vmulq_f32(bottom_data, bottom_data);
1351 }
1352
1353 if(pooling_type != PoolingType::MAX)
1354 {
1355 // Calculate scale
1356 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);
1357 const float32x2_t scale_v = vdup_n_f32(scale);
1358
1359 // Perform pooling
1360 const float32x4_t sum_data = vaddq_f32(vaddq_f32(top_data, bottom_data), middle_data);
1361 res = vpadd_f32(vget_high_f32(vsetq_lane_f32(0.f, sum_data, 3)), vget_low_f32(sum_data));
1362 res = vmul_f32(vpadd_f32(res, res), scale_v);
1363 }
1364 else
1365 {
1366 const float32x4_t max_data = vmaxq_f32(vmaxq_f32(top_data, bottom_data), middle_data);
1367 res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data, 3)), vget_low_f32(max_data));
1368 res = vpmax_f32(res, res);
1369 }
1370 final_res = vget_lane_f32(res, 0);
1371
1372 // Calculate square-root in case of l2 pooling
1373 if(pooling_type == PoolingType::L2)
1374 {
1375 final_res = sqrt(final_res);
1376 }
1377
1378 // Store result
1379 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1380 },
1381 input, output);
1382}
1383
1384void NEPoolingLayerKernel::pooling7_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
1385{
1386 Iterator input(_input, window_input);
1387 Iterator output(_output, window);
1388
1389 constexpr const int pool_size = 7;
1390 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1391 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1392 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1393 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1394 int pool_stride_x = 0;
1395 int pool_stride_y = 0;
1396 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1397 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1398 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
1399
1400 std::array<const uint8_t *, pool_size> input_ptrs{ {} };
1401 for(int i = 0; i < pool_size; ++i)
1402 {
1403 input_ptrs[i] = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + i));
1404 }
1405
1406 execute_window_loop(window, [&](const Coordinates & id)
1407 {
1408 float32x2_t res = {};
1409 float final_res = 0.f;
1410 if(pooling_type != PoolingType::MAX)
1411 {
1412 // Calculate scale
1413 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);
1414 const float32x2_t scale_v = vdup_n_f32(scale);
1415
1416 // Perform pooling
1417 float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1418 // Get power of 2 in case of l2 pooling
1419 if(pooling_type == PoolingType::L2)
1420 {
1421 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1422 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1423 }
1424 float32x4_t sum_data = vaddq_f32(data.val[0], vsetq_lane_f32(0.f, data.val[1], 3));
1425 for(int i = 1; i < pool_size; ++i)
1426 {
1427 data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1428 // Get power of 2 in case of l2 pooling
1429 if(pooling_type == PoolingType::L2)
1430 {
1431 data.val[0] = vmulq_f32(data.val[0], data.val[0]);
1432 data.val[1] = vmulq_f32(data.val[1], data.val[1]);
1433 }
1434 sum_data = vaddq_f32(sum_data, data.val[0]);
1435 sum_data = vaddq_f32(sum_data, vsetq_lane_f32(0.f, data.val[1], 3));
1436 }
1437 res = vpadd_f32(vget_high_f32(sum_data), vget_low_f32(sum_data));
1438 res = vmul_f32(vpadd_f32(res, res), scale_v);
1439 }
1440 else
1441 {
1442 float32x4x2_t max_data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
1443 for(int i = 1; i < pool_size; ++i)
1444 {
1445 const float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
1446 max_data = vmax2q_f32(max_data, data);
1447 }
1448 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]));
1449 res = vpmax_f32(res, vpmax_f32(vget_high_f32(max_data.val[0]), vget_low_f32(max_data.val[0])));
1450 res = vpmax_f32(res, res);
1451 }
1452 final_res = vget_lane_f32(res, 0);
1453
1454 // Calculate square-root in case of l2 pooling
1455 if(pooling_type == PoolingType::L2)
1456 {
1457 final_res = sqrt(final_res);
1458 }
1459
1460 // Store result
1461 *(reinterpret_cast<float *>(output.ptr())) = final_res;
1462 },
1463 input, output);
1464}
1465
1466void NEPoolingLayerKernel::poolingMxN_f32_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001467{
1468 Iterator input(_input, window_input);
1469 Iterator output(_output, window);
1470
1471 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1472 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1473 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1474 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1475 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1476 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1477 int pool_stride_x = 0;
1478 int pool_stride_y = 0;
1479 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1480 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1481 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1482
1483 float32x4_t vres;
1484
1485 execute_window_loop(window, [&](const Coordinates & id)
1486 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001487 const int idx_width = id.y() * pool_stride_x;
1488 const int idx_height = id.z() * pool_stride_y;
1489 const int pool_limit_y = pool_pad_top - idx_height;
1490 const int pool_limit_x = pool_pad_left - idx_width;
1491
1492 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1493 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1494 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1495 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1496
Michalis Spyrou57dac842018-03-01 16:03:50 +00001497 if(pooling_type != PoolingType::MAX)
1498 {
1499 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001500 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,
1501 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001502 const float32x4_t scale_v = vdupq_n_f32(scale);
1503
1504 // Perform pooling
1505 vres = vdupq_n_f32(0.0f);
1506
Michalis Spyrouced25572018-10-01 16:26:20 +01001507 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001508 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001509 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001510 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001511 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1512 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1513
1514 // Get power of 2 in case of l2 pooling and accumulate
1515 if(pooling_type == PoolingType::L2)
1516 {
1517 vres = vmlaq_f32(vres, data, data);
1518 }
1519 else
1520 {
1521 vres = vaddq_f32(vres, data);
1522 }
1523 }
1524 }
1525 // Divide by scale
1526 vres = vmulq_f32(vres, scale_v);
1527 }
1528 else
1529 {
1530 vres = vdupq_n_f32(std::numeric_limits<float>::lowest());
Michalis Spyrouced25572018-10-01 16:26:20 +01001531 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001532 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001533 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001534 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001535 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1536 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1537 vres = vmaxq_f32(vres, data);
1538 }
1539 }
1540 }
1541
1542 // Calculate square-root in case of l2 pooling
1543 if(pooling_type == PoolingType::L2)
1544 {
Georgios Pinitas027ce5b2018-11-08 18:55:36 +00001545 vres = vmulq_f32(vres, vinvsqrtq_f32(vres));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001546 }
1547
1548 // Store result
1549 vst1q_f32(reinterpret_cast<float *>(output.ptr()), vres);
1550 },
1551 input, output);
1552}
1553
Pablo Tello77e6c552018-12-04 15:33:49 +00001554void NEPoolingLayerKernel::poolingMxN_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Georgios Pinitas55186712018-01-08 17:37:12 +00001555{
1556 Iterator input(_input, window_input);
1557 Iterator output(_output, window);
1558
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001559 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width;
1560 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 +00001561 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1562 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1563 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1564 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1565 int pool_stride_x = 0;
1566 int pool_stride_y = 0;
Georgios Pinitas55186712018-01-08 17:37:12 +00001567 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001568 const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right);
1569 const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom);
Georgios Pinitas55186712018-01-08 17:37:12 +00001570
1571 execute_window_loop(window, [&](const Coordinates & id)
1572 {
1573 uint8_t res = 0;
1574
1575 if(pooling_type != PoolingType::MAX)
1576 {
1577 uint32x4_t vres = vdupq_n_u32(0);
1578 uint32_t sres = 0;
1579
1580 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001581 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 +00001582
1583 // Perform pooling
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001584 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001585 {
1586 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001587 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001588 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001589 const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1590 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001591
1592 const uint16x8_t data_u16 = vmovl_u8(data);
1593 vres = vaddq_u32(vres, vaddl_u16(vget_high_u16(data_u16), vget_low_u16(data_u16)));
1594 }
1595
1596 // Leftover for loop
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001597 for(; x < pool_size_x; ++x)
Georgios Pinitas55186712018-01-08 17:37:12 +00001598 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001599 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 +00001600 sres += data;
1601 }
1602 }
1603
1604 // Reduction
1605 const auto tmp = vpadd_u32(vget_high_u32(vres), vget_low_u32(vres));
1606 sres += vget_lane_u32(tmp, 0) + vget_lane_u32(tmp, 1);
1607
1608 // Divide by scale
1609 res = static_cast<uint8_t>(support::cpp11::round(sres * scale));
1610 }
1611 else
1612 {
1613 uint8x8_t vres = vdup_n_u8(0);
1614 res = 0;
1615
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001616 for(int y = 0; y < pool_size_y; ++y)
Georgios Pinitas55186712018-01-08 17:37:12 +00001617 {
1618 int x = 0;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001619 for(; x <= (pool_size_x - 8); x += 8)
Georgios Pinitas55186712018-01-08 17:37:12 +00001620 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +00001621 const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() +
1622 (y - pool_pad_top) * _input->info()->strides_in_bytes().y()));
Georgios Pinitas55186712018-01-08 17:37:12 +00001623 vres = vmax_u8(vres, data);
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 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 +00001630 res = std::max(res, data);
1631 }
1632 }
1633
1634 // Reduce max
1635 vres = vpmax_u8(vres, vres);
1636 vres = vpmax_u8(vres, vres);
1637 vres = vpmax_u8(vres, vres);
1638
1639 // Get max value
1640 res = std::max(res, vget_lane_u8(vres, 0));
1641 }
1642
1643 // Store result
1644 *(reinterpret_cast<uint8_t *>(output.ptr())) = res;
1645 },
1646 input, output);
1647}
1648
Pablo Tello77e6c552018-12-04 15:33:49 +00001649void NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001650{
1651 Iterator input(_input, window_input);
1652 Iterator output(_output, window);
1653
1654 const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width;
1655 const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height;
1656 const int pool_pad_right = _pool_info.pad_stride_info().pad_right();
1657 const int pool_pad_top = _pool_info.pad_stride_info().pad_top();
1658 const int pool_pad_left = _pool_info.pad_stride_info().pad_left();
1659 const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom();
1660 int pool_stride_x = 0;
1661 int pool_stride_y = 0;
1662 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
1663 const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right);
1664 const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom);
1665
Georgios Pinitas283fc602018-11-09 10:46:43 +00001666 const float32x4_t half_scale_v = vdupq_n_f32(0.5f);
1667
Michalis Spyrou57dac842018-03-01 16:03:50 +00001668 execute_window_loop(window, [&](const Coordinates & id)
1669 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001670 const int idx_width = id.y() * pool_stride_x;
1671 const int idx_height = id.z() * pool_stride_y;
1672 const int pool_limit_y = pool_pad_top - idx_height;
1673 const int pool_limit_x = pool_pad_left - idx_width;
1674
1675 const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y);
1676 const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y);
1677 const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x);
1678 const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x);
1679
Michalis Spyrou57dac842018-03-01 16:03:50 +00001680 if(pooling_type != PoolingType::MAX)
1681 {
1682 uint32x4_t vres1 = vdupq_n_u32(0);
1683 uint32x4_t vres2 = vdupq_n_u32(0);
Michalis Spyrouced25572018-10-01 16:26:20 +01001684 uint32x4_t vres3 = vdupq_n_u32(0);
1685 uint32x4_t vres4 = vdupq_n_u32(0);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001686
1687 // Calculate scale
Pablo Tello77e6c552018-12-04 15:33:49 +00001688 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,
1689 pool_stride_y);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001690 const float32x4_t scale_v = vdupq_n_f32(scale);
1691
1692 // Perform pooling
Michalis Spyrouced25572018-10-01 16:26:20 +01001693 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001694 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001695 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001696 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001697 const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1698 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001699
Michalis Spyrouced25572018-10-01 16:26:20 +01001700 const uint16x8_t data_u16 = vmovl_u8(vget_low_u8(data));
1701 const uint16x8_t data2_u16 = vmovl_u8(vget_high_u8(data));
1702 vres1 = vaddq_u32(vres1, vmovl_u16(vget_low_u16(data_u16)));
1703 vres2 = vaddq_u32(vres2, vmovl_u16(vget_high_u16(data_u16)));
1704 vres3 = vaddq_u32(vres3, vmovl_u16(vget_low_u16(data2_u16)));
1705 vres4 = vaddq_u32(vres4, vmovl_u16(vget_high_u16(data2_u16)));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001706 }
1707 }
Georgios Pinitas283fc602018-11-09 10:46:43 +00001708 // Divide by scale and add 0.5f to round to nearest instead of rounding towards zero
1709 vres1 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres1), scale_v));
1710 vres2 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres2), scale_v));
1711 vres3 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres3), scale_v));
1712 vres4 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres4), scale_v));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001713
Michalis Spyrouced25572018-10-01 16:26:20 +01001714 uint8x8_t res1 = vmovn_u16(vcombine_u16(vmovn_u32(vres1), vmovn_u32(vres2)));
1715 uint8x8_t res2 = vmovn_u16(vcombine_u16(vmovn_u32(vres3), vmovn_u32(vres4)));
Michalis Spyrou57dac842018-03-01 16:03:50 +00001716
1717 // Store result
Michalis Spyrouced25572018-10-01 16:26:20 +01001718 vst1_u8(output.ptr(), res1);
1719 vst1_u8(output.ptr() + 8, res2);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001720 }
1721 else
1722 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001723 uint8x16_t vres = vdupq_n_u8(0);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001724
Michalis Spyrouced25572018-10-01 16:26:20 +01001725 for(int y = pool_start_y; y < pool_end_y; ++y)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001726 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001727 for(int x = pool_start_x; x < pool_end_x; ++x)
Michalis Spyrou57dac842018-03-01 16:03:50 +00001728 {
Michalis Spyrouced25572018-10-01 16:26:20 +01001729 const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() +
1730 (y - pool_pad_top) * _input->info()->strides_in_bytes().z()));
1731 vres = vmaxq_u8(vres, data);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001732 }
1733 }
1734
1735 // Store result
Michalis Spyrouced25572018-10-01 16:26:20 +01001736 vst1q_u8(output.ptr(), vres);
Michalis Spyrou57dac842018-03-01 16:03:50 +00001737 }
1738 },
1739 input, output);
1740}
1741
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001742Status NEPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
1743{
1744 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
1745
1746 unsigned int pooled_w = 0;
1747 unsigned int pooled_h = 0;
1748 unsigned int num_elems_processed_per_iteration = 0;
1749 BorderSize border_size(0);
1750
Michalis Spyrou57dac842018-03-01 16:03:50 +00001751 const bool is_global_pooling = pool_info.is_global_pooling();
1752 unsigned int pool_size_x = 0;
1753 unsigned int pool_size_y = 0;
1754
1755 // Get data layout
1756 const DataLayout data_layout = input->data_layout();
1757 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1758 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1759
1760 pool_size_x = is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size().width;
1761 pool_size_y = is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size().height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001762
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001763 // Validate pool info before calling scaled_dimensions
1764 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_pool_info(pool_size_x, pool_size_y));
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001765
1766 // Check output dimensions
Michalis Spyrou57dac842018-03-01 16:03:50 +00001767 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width),
1768 input->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001769 pool_size_x,
1770 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001771 pool_info.pad_stride_info());
1772
Georgios Pinitas13d96e02018-08-23 11:20:23 +01001773 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, pooled_w, pooled_h));
Isabella Gottardi7567f5f2018-01-30 15:26:00 +00001774 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,
1775 pool_size_x, pool_size_y)
1776 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +00001777
1778 return Status{};
1779}
1780
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001781void NEPoolingLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001782{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001783 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001784 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1785 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1786 ARM_COMPUTE_ERROR_ON(_func == nullptr);
1787
Pablo Tello77e6c552018-12-04 15:33:49 +00001788 const unsigned int pool_stride_x = _pool_info.pad_stride_info().stride().first;
1789 const unsigned int pool_stride_y = _pool_info.pad_stride_info().stride().second;
1790 const unsigned int pool_size = _pool_info.pool_size().width;
1791 const bool exclude_padding = _pool_info.exclude_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001792
Michalis Spyrou57dac842018-03-01 16:03:50 +00001793 Window window_input(window);
1794 if(_input->info()->data_layout() == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001795 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001796 // Set step for input in x and y direction for the input
1797 unsigned int window_x_inc = 0;
1798 switch(_input->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +01001799 {
Michalis Spyrou57dac842018-03-01 16:03:50 +00001800 case DataType::QASYMM8:
1801 {
1802 window_x_inc = pool_stride_x;
1803 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
1804 {
1805 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
1806 }
1807 break;
1808 }
Pablo Tello77e6c552018-12-04 15:33:49 +00001809
Georgios Pinitas13d96e02018-08-23 11:20:23 +01001810 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +00001811 case DataType::F32:
1812 {
1813 window_x_inc = pool_stride_x;
1814 break;
1815 }
1816 default:
1817 {
1818 ARM_COMPUTE_ERROR("Not supported");
1819 }
Georgios Pinitas55186712018-01-08 17:37:12 +00001820 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001821 window_input.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
1822 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 +01001823 }
Michalis Spyrou57dac842018-03-01 16:03:50 +00001824 else
1825 {
Georgios Pinitascac13b12018-04-27 19:07:19 +01001826 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 +00001827 window_input.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x));
1828 window_input.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y));
1829 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001830
1831 // Run function
Pablo Tello77e6c552018-12-04 15:33:49 +00001832 (this->*_func)(window_input, window, _pool_info.pool_type(), exclude_padding);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001833}