blob: 2ef2b9881f7a34acdc69d16d624ea8b8fc388ee3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/FixedPoint.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/NEFixedPoint.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37#include <algorithm>
38#include <arm_neon.h>
39#include <limits>
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +010040#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include <string>
42#include <tuple>
43
44using namespace arm_compute;
45
46namespace
47{
48inline float calculate_avg_scale(const Coordinates &id, const int pool_size, const int upper_bound_w, const int upper_bound_h,
49 const int pad_x, const int pad_y, const int stride_x, const int stride_y)
50{
51 int start_x = id.x() * stride_x - pad_x;
52 int start_y = id.y() * stride_y - pad_y;
53 int end_x = std::min(start_x + pool_size, upper_bound_w);
54 int end_y = std::min(start_y + pool_size, upper_bound_h);
55 return 1.f / ((end_y - start_y) * (end_x - start_x));
56}
57
58inline qint8_t calculate_avg_scale_q8(const Coordinates &id, int pool_size, int upper_bound_w, int upper_bound_h,
59 int pad_x, int pad_y, int stride_x, int stride_y, int fixed_point_position)
60{
61 static std::array<qint8_t, 10> scale_values_q8 =
62 { { 0x0, 0x0, 0x40, 0x2A, 0x20, 0x19, 0x15, 0x12, 0x10, 0xE } };
63 const int start_x = id.x() * stride_x - pad_x;
64 const int start_y = id.y() * stride_y - pad_y;
65 const int end_x = std::min(start_x + pool_size, upper_bound_w);
66 const int end_y = std::min(start_y + pool_size, upper_bound_h);
67 const int val = ((end_y - start_y) * (end_x - start_x));
68 return scale_values_q8[val] >> (7 - fixed_point_position);
69}
70} // namespace
71
72NEPoolingLayerKernel::NEPoolingLayerKernel()
73 : _func(nullptr), _input(nullptr), _output(nullptr), _pool_info(), _num_elems_processed_per_iteration(0), _border_size(0)
74{
75}
76
77BorderSize NEPoolingLayerKernel::border_size() const
78{
79 return _border_size;
80}
81
82void NEPoolingLayerKernel::configure(const ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info)
83{
Gian Marco Iodice4e288692017-06-27 11:41:59 +010084 int pool_pad_x = 0;
85 int pool_pad_y = 0;
86 int pool_stride_x = 0;
87 int pool_stride_y = 0;
88 unsigned int pooled_w = 0;
89 unsigned int pooled_h = 0;
90 PoolingType pool_type = pool_info.pool_type();
91 int pool_size = pool_info.pool_size();
92 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad();
94 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
95
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +010096 static const std::set<int> supported_pool_sizes = { 2, 3, 7 };
97 ARM_COMPUTE_UNUSED(supported_pool_sizes);
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::F32);
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100100 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100101 ARM_COMPUTE_ERROR_ON(supported_pool_sizes.find(pool_size) == supported_pool_sizes.end());
102 ARM_COMPUTE_ERROR_ON(7 == pool_size && input->info()->data_type() != DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 ARM_COMPUTE_ERROR_ON(pool_pad_x >= pool_size || pool_pad_y >= pool_size);
104 ARM_COMPUTE_ERROR_ON(input->info()->data_type() == DataType::QS8 && pool_type == PoolingType::AVG && input->info()->fixed_point_position() > 6);
105 ARM_COMPUTE_ERROR_ON(input->info()->data_type() == DataType::QS8 && pool_stride_x > 2);
106
107 // Check output dimensions
108 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100109 pool_size, pool_size, pool_info.pad_stride_info());
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100110
111 // Output auto initialization if not yet initialized
112 {
113 TensorShape output_shape{ input->info()->tensor_shape() };
114 output_shape.set(0, pooled_w);
115 output_shape.set(1, pooled_h);
116
117 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
118 }
119
120 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
121 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h));
123
124 unsigned int num_elems_read_per_iteration = 0;
125 unsigned int num_elems_processed_per_iteration = 0;
126 unsigned int num_elems_horizontal_window = 0;
127
128 // Select element size
129 switch(input->info()->data_type())
130 {
131 case DataType::QS8:
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100132 num_elems_read_per_iteration = 16;
133 switch(pool_size)
134 {
135 case 2:
136 num_elems_processed_per_iteration = 8;
137 break;
138 case 3:
139 num_elems_processed_per_iteration = 7;
140 break;
141 default:
142 ARM_COMPUTE_ERROR("Pooling size not supported");
143 }
144 num_elems_horizontal_window = 8;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 break;
146 case DataType::F32:
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100147 switch(pool_size)
148 {
149 case 2:
150 num_elems_read_per_iteration = 2;
151 break;
152 case 3:
153 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
154 break;
155 case 7:
156 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
157 break;
158 default:
159 ARM_COMPUTE_ERROR("Pooling size not supported");
160 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 num_elems_processed_per_iteration = 1;
162 num_elems_horizontal_window = 1;
163 break;
164 default:
165 ARM_COMPUTE_ERROR("Element size not supported");
166 break;
167 }
168
169 _num_elems_processed_per_iteration = num_elems_processed_per_iteration;
170 const int input_width = input->info()->dimension(0);
171 const int input_height = input->info()->dimension(1);
172 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elems_read_per_iteration) - input_width;
173 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
174
175 // Set instance variables
176 _input = input;
177 _output = output;
178 _pool_info = pool_info;
179 _border_size = BorderSize(pool_pad_y, pool_pad_x);
180 _border_size.right = std::max(upper_bound_w, pool_pad_x);
181 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
182
183 // Select appropriate function
184 switch(pool_size)
185 {
186 case 2:
187 if(input->info()->data_type() == DataType::QS8)
188 {
189 _func = (PoolingType::AVG == pool_type) ? &NEPoolingLayerKernel::pooling2_q8<PoolingType::AVG> : &NEPoolingLayerKernel::pooling2_q8<PoolingType::MAX>;
190 }
191 else if(input->info()->data_type() == DataType::F32)
192 {
193 _func = (PoolingType::AVG == pool_type) ? &NEPoolingLayerKernel::pooling2_f32<PoolingType::AVG> : &NEPoolingLayerKernel::pooling2_f32<PoolingType::MAX>;
194 }
195 break;
196 case 3:
197 if(input->info()->data_type() == DataType::QS8)
198 {
199 _func = (PoolingType::AVG == pool_type) ? &NEPoolingLayerKernel::pooling3_q8<PoolingType::AVG> : &NEPoolingLayerKernel::pooling3_q8<PoolingType::MAX>;
200 }
201 else if(input->info()->data_type() == DataType::F32)
202 {
203 _func = (PoolingType::AVG == pool_type) ? &NEPoolingLayerKernel::pooling3_f32<PoolingType::AVG> : &NEPoolingLayerKernel::pooling3_f32<PoolingType::MAX>;
204 }
205 break;
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100206 case 7:
207 _func = (PoolingType::AVG == pool_type) ? &NEPoolingLayerKernel::pooling7_f32<PoolingType::AVG> : &NEPoolingLayerKernel::pooling7_f32<PoolingType::MAX>;
208 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 default:
210 ARM_COMPUTE_ERROR("Unsupported pooling size");
211 break;
212 }
213
214 // Configure kernel window
215 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
216 AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right, input_height + _border_size.bottom);
217 AccessWindowHorizontal output_access(output->info(), 0, num_elems_horizontal_window);
218 update_window_and_padding(win, input_access, output_access);
219 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
220 INEKernel::configure(win);
221}
222
223template <PoolingType pooling_type>
224void NEPoolingLayerKernel::pooling2_q8(const Window &window_input, const Window &window)
225{
226 Iterator input(_input, window_input);
227 Iterator output(_output, window);
228
229 const int fixed_point_position = _input->info()->fixed_point_position();
230 constexpr int pool_size = 2;
231 int pool_pad_x = 0;
232 int pool_pad_y = 0;
233 int pool_stride_x = 0;
234 int pool_stride_y = 0;
235 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
236 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
237 const int upper_bound_w = _input->info()->dimension(0) + pool_pad_x;
238 const int upper_bound_h = _input->info()->dimension(1) + pool_pad_y;
239
240 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y)));
241 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y) + 1));
242
243 execute_window_loop(window, [&](const Coordinates & id)
244 {
245 const auto top_data = vld1q_qs8(reinterpret_cast<const qint8_t *>(input_top_ptr + input.offset()));
246 const auto bottom_data = vld1q_qs8(reinterpret_cast<const qint8_t *>(input_bottom_ptr + input.offset()));
247 qint8x8_t res = {};
248 if(pooling_type == PoolingType::AVG)
249 {
250 // Calculate scale
251 const qint8_t scale = calculate_avg_scale_q8(id, pool_size, upper_bound_w, upper_bound_h, pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y, fixed_point_position);
252 const qint8x8_t scale_vec = vdup_n_qs8(scale);
253
254 // Perform pooling
255 const qint8x16_t sum_data = vqaddq_qs8(top_data, bottom_data);
256 res = vqmul_qs8(vpadd_s8(vget_low_s8(sum_data), vget_high_s8(sum_data)), scale_vec, fixed_point_position);
257 }
258 else
259 {
260 const qint8x16_t max_data = vmaxq_s8(top_data, bottom_data);
261 res = vpmax_s8(vget_low_s8(max_data), vget_high_s8(max_data));
262 }
263 vst1_qs8(reinterpret_cast<qint8_t *>(output.ptr()), res);
264 },
265 input, output);
266}
267
268template <PoolingType pooling_type>
269void NEPoolingLayerKernel::pooling2_f32(const Window &window_input, const Window &window)
270{
271 Iterator input(_input, window_input);
272 Iterator output(_output, window);
273
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100274 constexpr int pool_size = 2;
275 int pool_pad_x = 0;
276 int pool_pad_y = 0;
277 int pool_stride_x = 0;
278 int pool_stride_y = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
280 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
281 const int upper_bound_w = _input->info()->dimension(0) + pool_pad_x;
282 const int upper_bound_h = _input->info()->dimension(1) + pool_pad_y;
283
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100284 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y)));
285 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y) + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286
287 execute_window_loop(window, [&](const Coordinates & id)
288 {
289 const float32x2_t top_data = vld1_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
290 const float32x2_t bottom_data = vld1_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
291 float32x2_t res = {};
292 if(pooling_type == PoolingType::AVG)
293 {
294 // Calculate scale
295 float scale = calculate_avg_scale(id, pool_size, upper_bound_w, upper_bound_h, pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y);
296 const float32x2_t scale_v = vdup_n_f32(scale);
297
298 // Perform pooling
299 const float32x2_t sum_data = vadd_f32(top_data, bottom_data);
300 res = vmul_f32(vpadd_f32(sum_data, sum_data), scale_v);
301 }
302 else
303 {
304 const float32x2_t max_data = vmax_f32(top_data, bottom_data);
305 res = vpmax_f32(max_data, max_data);
306 }
307 *(reinterpret_cast<float *>(output.ptr())) = vget_lane_f32(res, 0);
308 },
309 input, output);
310}
311
312template <PoolingType pooling_type>
313void NEPoolingLayerKernel::pooling3_q8(const Window &window_input, const Window &window)
314{
315 Iterator input(_input, window_input);
316 Iterator output(_output, window);
317
318 const int fixed_point_position = _input->info()->fixed_point_position();
319 constexpr int pool_size = 3;
320 int pool_pad_x = 0;
321 int pool_pad_y = 0;
322 int pool_stride_x = 0;
323 int pool_stride_y = 0;
324 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
325 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
326 const int upper_bound_w = _input->info()->dimension(0) + pool_pad_x;
327 const int upper_bound_h = _input->info()->dimension(1) + pool_pad_y;
328
329 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y)));
330 const uint8_t *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y) + 1));
331 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y) + 2));
332
333 execute_window_loop(window, [&](const Coordinates & id)
334 {
335 const auto top_data = vld1q_qs8(reinterpret_cast<const qint8_t *>(input_top_ptr + input.offset()));
336 const auto middle_data = vld1q_qs8(reinterpret_cast<const qint8_t *>(input_middle_ptr + input.offset()));
337 const auto bottom_data = vld1q_qs8(reinterpret_cast<const qint8_t *>(input_bottom_ptr + input.offset()));
338 qint8x8_t res = {};
339 if(pooling_type == PoolingType::AVG)
340 {
341 // Calculate scale
342 const qint8_t scale = calculate_avg_scale_q8(id, pool_size, upper_bound_w, upper_bound_h, pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y, fixed_point_position);
343 const qint8x8_t scale_vec = vdup_n_qs8(scale);
344
345 // Perform pooling for stride 2
346 const qint8x16_t sum_data = vqaddq_qs8(vqaddq_qs8(top_data, bottom_data), middle_data);
347 const qint8x16_t sum_data2 = vextq_s8(sum_data, sum_data, 1);
348 const qint8x16_t sum_data3 = vextq_s8(sum_data, sum_data, 2);
349 const qint8x16_t final_sum = vqaddq_qs8(vqaddq_qs8(sum_data, sum_data2), sum_data3);
350 if(pool_stride_x == 2)
351 {
352 const qint8x8x2_t table = { { vget_low_s8(final_sum), vget_high_s8(final_sum) } };
353 static const qint8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 };
354 res = vtbl2_s8(table, lookup_val);
355 }
356 else
357 {
358 res = vget_low_s8(final_sum);
359 }
360 res = vqmul_qs8(res, scale_vec, fixed_point_position);
361 }
362 else
363 {
364 const qint8x16_t max_data = vmaxq_s8(vmaxq_s8(top_data, bottom_data), middle_data);
365 const qint8x16_t max_data2 = vextq_s8(max_data, max_data, 1);
366 const qint8x16_t max_data3 = vextq_s8(max_data, max_data, 2);
367 const qint8x16_t final_max = vmaxq_s8(vmaxq_s8(max_data, max_data2), max_data3);
368
369 if(pool_stride_x == 2)
370 {
371 const qint8x8x2_t table = { { vget_low_s8(final_max), vget_high_s8(final_max) } };
372 static const qint8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 };
373 res = vtbl2_s8(table, lookup_val);
374 }
375 else
376 {
377 res = vget_low_s8(final_max);
378 }
379 }
380 vst1_qs8(reinterpret_cast<qint8_t *>(output.ptr()), res);
381 },
382 input, output);
383}
384
385template <PoolingType pooling_type>
386void NEPoolingLayerKernel::pooling3_f32(const Window &window_input, const Window &window)
387{
388 Iterator input(_input, window_input);
389 Iterator output(_output, window);
390
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100391 constexpr const int pool_size = 3;
392 int pool_pad_x = 0;
393 int pool_pad_y = 0;
394 int pool_stride_x = 0;
395 int pool_stride_y = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100396 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
397 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
398 const int upper_bound_w = _input->info()->dimension(0) + pool_pad_x;
399 const int upper_bound_h = _input->info()->dimension(1) + pool_pad_y;
400
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100401 const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y)));
402 const uint8_t *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y) + 1));
403 const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y) + 2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100404
405 execute_window_loop(window, [&](const Coordinates & id)
406 {
407 const float32x4_t top_data = vld1q_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset()));
408 const float32x4_t middle_data = vld1q_f32(reinterpret_cast<const float *>(input_middle_ptr + input.offset()));
409 const float32x4_t bottom_data = vld1q_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset()));
410 float32x2_t res = {};
411 if(pooling_type == PoolingType::AVG)
412 {
413 // Calculate scale
414 float scale = calculate_avg_scale(id, pool_size, upper_bound_w, upper_bound_h, pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y);
415 const float32x2_t scale_v = vdup_n_f32(scale);
416
417 // Perform pooling
418 const float32x4_t sum_data = vaddq_f32(vaddq_f32(top_data, bottom_data), middle_data);
419 res = vpadd_f32(vget_high_f32(vsetq_lane_f32(0.f, sum_data, 3)), vget_low_f32(sum_data));
420 res = vmul_f32(vpadd_f32(res, res), scale_v);
421 }
422 else
423 {
424 const float32x4_t max_data = vmaxq_f32(vmaxq_f32(top_data, bottom_data), middle_data);
425 res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data, 3)), vget_low_f32(max_data));
426 res = vpmax_f32(res, res);
427 }
428 *(reinterpret_cast<float *>(output.ptr())) = vget_lane_f32(res, 0);
429 },
430 input, output);
431}
432
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100433template <PoolingType pooling_type>
434void NEPoolingLayerKernel::pooling7_f32(const Window &window_input, const Window &window)
435{
436 Iterator input(_input, window_input);
437 Iterator output(_output, window);
438
439 constexpr const int pool_size = 7;
440 int pool_pad_x = 0;
441 int pool_pad_y = 0;
442 int pool_stride_x = 0;
443 int pool_stride_y = 0;
444 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
445 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
446 const int upper_bound_w = _input->info()->dimension(0) + pool_pad_x;
447 const int upper_bound_h = _input->info()->dimension(1) + pool_pad_y;
448
449 std::array<const uint8_t *, pool_size> input_ptrs{ {} };
450 for(int i = 0; i < pool_size; ++i)
451 {
452 input_ptrs[i] = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_x), -static_cast<int>(pool_pad_y) + i));
453 }
454
455 execute_window_loop(window, [&](const Coordinates & id)
456 {
457 float32x2_t res = {};
458 if(pooling_type == PoolingType::AVG)
459 {
460 // Calculate scale
461 float scale = calculate_avg_scale(id, pool_size, upper_bound_w, upper_bound_h, pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y);
462 const float32x2_t scale_v = vdup_n_f32(scale);
463
464 // Perform pooling
465 float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
466 float32x4_t sum_data = vaddq_f32(data.val[0], vsetq_lane_f32(0.f, data.val[1], 3));
467 for(int i = 1; i < pool_size; ++i)
468 {
469 data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
470 sum_data = vaddq_f32(sum_data, data.val[0]);
471 sum_data = vaddq_f32(sum_data, vsetq_lane_f32(0.f, data.val[1], 3));
472 }
473 res = vpadd_f32(vget_high_f32(sum_data), vget_low_f32(sum_data));
474 res = vmul_f32(vpadd_f32(res, res), scale_v);
475 }
476 else
477 {
478 float32x4x2_t max_data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset()));
479 for(int i = 1; i < pool_size; ++i)
480 {
481 const float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset()));
482 max_data = vmax2q_f32(max_data, data);
483 }
484 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]));
485 res = vpmax_f32(res, vpmax_f32(vget_high_f32(max_data.val[0]), vget_low_f32(max_data.val[0])));
486 res = vpmax_f32(res, res);
487 }
488 *(reinterpret_cast<float *>(output.ptr())) = vget_lane_f32(res, 0);
489 },
490 input, output);
491}
492
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100493void NEPoolingLayerKernel::run(const Window &window)
494{
495 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
496 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
497 ARM_COMPUTE_ERROR_ON(_func == nullptr);
498
499 unsigned int pool_stride_x, pool_stride_y = 0;
500 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
501
502 // Set step for input in x and y direction for the input
503 Window window_input(window);
504 unsigned int window_x_inc = 0;
505 if(_input->info()->data_type() == DataType::QS8)
506 {
507 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
508 }
509 else
510 {
511 window_x_inc = pool_stride_x;
512 }
513 window_input.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
514 window_input.set(Window::DimY, Window::Dimension(window.y().start() * pool_stride_y, window.y().end() * pool_stride_y, pool_stride_y));
515
516 // Run function
517 (this->*_func)(window_input, window);
518}