blob: aaa37863cb4b0e00ca5c0e4fc9f01cc4a49f79a1 [file] [log] [blame]
Sheri Zhang79144a62021-02-08 17:43:04 +00001/*
Adnan AlSinan227db8d2023-02-14 14:24:09 +00002 * Copyright (c) 2021-2023 Arm Limited.
Sheri Zhang79144a62021-02-08 17:43:04 +00003 *
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/Helpers.h"
25#include "arm_compute/core/ITensor.h"
26#include "arm_compute/core/Types.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
Sheri Zhang79144a62021-02-08 17:43:04 +000028#include "src/core/helpers/WindowHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010030#include "src/cpu/kernels/pool2d/neon/list.h"
Sheri Zhang79144a62021-02-08 17:43:04 +000031
32namespace arm_compute
33{
34namespace cpu
35{
36namespace
37{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038void pooling2_f32_maxpool_indices(const ITensor *src,
39 ITensor *dst0,
40 ITensor *dst1,
41 PoolingLayerInfo &pool_info,
42 const Window &window_src,
43 const Window &window)
Sheri Zhang79144a62021-02-08 17:43:04 +000044{
45 const int window_start_x = window.x().start();
46 const int window_end_x = window.x().end();
47 const int window_step_x = 4;
48
49 Window window_out = window;
50 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
51
52 Iterator in(src, window_src);
53 Iterator out(dst0, window_out);
54 Iterator indices(dst1, window_out);
55
56 const int pool_pad_top = pool_info.pad_stride_info.pad_top();
57 const int pool_pad_left = pool_info.pad_stride_info.pad_left();
58
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 int pool_stride_x = 0;
60 int pool_stride_y = 0;
Sheri Zhang79144a62021-02-08 17:43:04 +000061 std::tie(pool_stride_x, pool_stride_y) = pool_info.pad_stride_info.stride();
62
63 float32x4_t vres;
64 float res;
65
Sang-Hoon Parkfe56edb2021-04-13 20:21:11 +010066 const int pad_right = src->info()->padding().right;
67 const int pad_left = src->info()->padding().left;
68 const int pad_horizontal = pad_right + pad_left;
69 const int in_stride_y = static_cast<int>(src->info()->strides_in_bytes().y());
70 const int in_stride_z = static_cast<int>(src->info()->strides_in_bytes().z());
Sheri Zhang79144a62021-02-08 17:43:04 +000071
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 execute_window_loop(
73 window_out,
74 [&](const Coordinates &id)
Sheri Zhang79144a62021-02-08 17:43:04 +000075 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 const int idx_width = id.y() * pool_stride_x;
77 const int idx_height = id.z() * pool_stride_y;
78 const int pool_limit_y = pool_pad_top - idx_height;
79 const int pool_limit_x = pool_pad_left - idx_width;
Sheri Zhang79144a62021-02-08 17:43:04 +000080
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 const int pool_start_y = std::max(0, window_src.z().start() + pool_limit_y);
82 const int pool_start_x = std::max(0, window_src.y().start() + pool_limit_x);
Sheri Zhang79144a62021-02-08 17:43:04 +000083
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010084 const int in_x0_offset =
85 (pool_start_x - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
86 (pool_start_y - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z());
87 const int in_x1_offset =
88 (pool_start_x + 1 - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
89 (pool_start_y - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z());
90 const int in_x2_offset =
91 (pool_start_x - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
92 (pool_start_y + 1 - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z());
93 const int in_x3_offset =
94 (pool_start_x + 1 - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
95 (pool_start_y + 1 - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z());
Sheri Zhang79144a62021-02-08 17:43:04 +000096
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097 int x_off = window_start_x;
98 for (; x_off <= (window_end_x - window_step_x); x_off += window_step_x)
99 {
100 const auto in_x0_ptr = reinterpret_cast<const float *>(in.ptr() + in_x0_offset);
101 const auto in_x1_ptr = reinterpret_cast<const float *>(in.ptr() + in_x1_offset);
102 const auto in_x2_ptr = reinterpret_cast<const float *>(in.ptr() + in_x2_offset);
103 const auto in_x3_ptr = reinterpret_cast<const float *>(in.ptr() + in_x3_offset);
104 const auto v_x0 = vld1q_f32(in_x0_ptr + x_off);
105 const auto v_x1 = vld1q_f32(in_x1_ptr + x_off);
106 const auto v_x2 = vld1q_f32(in_x2_ptr + x_off);
107 const auto v_x3 = vld1q_f32(in_x3_ptr + x_off);
108 vres = vmaxq_f32(vmaxq_f32(v_x2, v_x3), vmaxq_f32(v_x0, v_x1));
109 // Store result
110 vst1q_f32(reinterpret_cast<float *>(out.ptr()) + x_off, vres);
Sheri Zhang79144a62021-02-08 17:43:04 +0000111
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 const uint32_t offset_base = offset_no_padding<float>(in.offset(), id, *src->info(), pool_stride_x,
113 pool_stride_y, DataLayout::NHWC);
114 const uint32_t offset_x0 = offset_base / sizeof(float) + x_off;
115 const uint32_t offset_x1 = offset_x0 + in_stride_y / sizeof(float) - pad_horizontal;
116 const uint32_t offset_x2 =
117 offset_x0 + in_stride_z / sizeof(float) - pad_horizontal * src->info()->tensor_shape()[1];
118 const uint32_t offset_x3 = offset_x2 + in_stride_y / sizeof(float) - pad_horizontal;
119 const uint32x4_t voffset_x0 = {offset_x0, offset_x0 + 1, offset_x0 + 2, offset_x0 + 3};
120 const uint32x4_t voffset_x1 = {offset_x1, offset_x1 + 1, offset_x1 + 2, offset_x1 + 3};
121 const uint32x4_t voffset_x2 = {offset_x2, offset_x2 + 1, offset_x2 + 2, offset_x2 + 3};
122 const uint32x4_t voffset_x3 = {offset_x3, offset_x3 + 1, offset_x3 + 2, offset_x3 + 3};
123 const uint32x4_t tmp_indices0 = vbslq_u32(vcgeq_f32(v_x0, v_x1), voffset_x0, voffset_x1);
124 const uint32x4_t tmp_indices1 = vbslq_u32(vcgeq_f32(v_x2, v_x3), voffset_x2, voffset_x3);
125 const uint32x4_t tmp_indices2 =
126 vbslq_u32(vcgeq_f32(vmaxq_f32(v_x0, v_x1), vmaxq_f32(v_x2, v_x3)), tmp_indices0, tmp_indices1);
Sheri Zhang79144a62021-02-08 17:43:04 +0000127
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 // Store indices
129 vst1q_u32(reinterpret_cast<uint32_t *>(indices.ptr()) + x_off, tmp_indices2);
130 }
Sheri Zhang79144a62021-02-08 17:43:04 +0000131
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 // Left-overs loop
133 for (; x_off < window_end_x; ++x_off)
134 {
135 const auto x0 = *(reinterpret_cast<const float *>(in.ptr() + in_x0_offset) + x_off);
136 const auto x1 = *(reinterpret_cast<const float *>(in.ptr() + in_x1_offset) + x_off);
137 const auto x2 = *(reinterpret_cast<const float *>(in.ptr() + in_x2_offset) + x_off);
138 const auto x3 = *(reinterpret_cast<const float *>(in.ptr() + in_x3_offset) + x_off);
139 res = std::max(std::max(x2, x3), std::max(x0, x1));
140
141 // Store result
142 *(reinterpret_cast<float *>(out.ptr()) + x_off) = res;
143
144 const uint32_t offset_base = offset_no_padding<float>(in.offset(), id, *src->info(), pool_stride_x,
145 pool_stride_y, DataLayout::NHWC);
146 const uint32_t offset_x0 = offset_base / sizeof(float) + x_off;
147 const uint32_t offset_x1 = offset_x0 + in_stride_y / sizeof(float) - pad_horizontal;
148 const uint32_t offset_x2 =
149 offset_x0 + in_stride_z / sizeof(float) - pad_horizontal * src->info()->tensor_shape()[1];
150 const uint32_t offset_x3 = offset_x2 + in_stride_y / sizeof(float) - pad_horizontal;
151 const uint32_t tmp_idx0 = (x0 >= x1) ? offset_x0 : offset_x1;
152 const uint32_t tmp_idx1 = (x2 >= x3) ? offset_x2 : offset_x3;
153 const uint32_t tmp_idx2 = (std::max(x0, x1) >= std::max(x2, x3)) ? tmp_idx0 : tmp_idx1;
154
155 // Store indices
156 *(reinterpret_cast<uint32_t *>(indices.ptr()) + x_off) = tmp_idx2;
157 }
158 },
159 in, out, indices);
Sheri Zhang79144a62021-02-08 17:43:04 +0000160}
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000161} // namespace
162
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163void poolingMxN_fp32_neon_nhwc_kernel_indices(
164 const ITensor *src, ITensor *dst0, ITensor *dst1, const PoolingLayerInfo &pool_info, const Window &window)
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000165{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 const int window_start_x = window.x().start();
167 const int window_end_x = window.x().end();
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000168 constexpr int window_step_x = 4;
169
170 Window window_out = window;
171 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
172
173 Iterator out(dst0, window_out);
174 Iterator indices(dst1, window_out);
175
176 const int pool_size_x = pool_info.is_global_pooling ? src->info()->tensor_shape().y() : pool_info.pool_size.width;
177 const int pool_size_y = pool_info.is_global_pooling ? src->info()->tensor_shape().z() : pool_info.pool_size.height;
178
179 const int pool_pad_top = pool_info.pad_stride_info.pad_top();
180 const int pool_pad_left = pool_info.pad_stride_info.pad_left();
181
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100182 int pool_stride_x = 0;
183 int pool_stride_y = 0;
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000184 std::tie(pool_stride_x, pool_stride_y) = pool_info.pad_stride_info.stride();
185
186 const float min_value = get_initial_min<float>(pool_info.use_inf_as_limit);
187
188 float32x4_t vres;
189 uint32x4_t vidx;
190
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 constexpr int idx_width = 1;
192 constexpr int idx_height = 2;
193 constexpr int idx_batch = 3;
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000194
195 const int y_stride = static_cast<int>(src->info()->strides_in_bytes().y());
196 const int z_stride = static_cast<int>(src->info()->strides_in_bytes().z());
197 const int n_stride = static_cast<int>(src->info()->strides_in_bytes()[idx_batch]);
198
199 const int input_dim_w = src->info()->dimension(idx_width);
200 const int input_dim_h = src->info()->dimension(idx_height);
201
202 const uint8_t *in_ptr_start = src->buffer() + src->info()->offset_first_element_in_bytes();
203
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204 execute_window_loop(
205 window_out,
206 [&](const Coordinates &id)
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000207 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100208 const int idx_width = static_cast<int>(id.y()) * pool_stride_x - pool_pad_left;
209 const int idx_height = static_cast<int>(id.z()) * pool_stride_y - pool_pad_top;
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000210
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100211 const int pool_start_x = std::max(0, -idx_width);
212 const int pool_start_y = std::max(0, -idx_height);
213
214 const int pool_end_x = std::min(pool_size_x, input_dim_w - idx_width);
215 const int pool_end_y = std::min(pool_size_y, input_dim_h - idx_height);
216
217 const uint8_t *in_ptr_n = in_ptr_start + id[idx_batch] * n_stride;
218
219 const int in_ptr_y_offset = (z_stride * idx_height) + (pool_start_y * z_stride);
220 const int in_ptr_x_offset = (y_stride * idx_width) + (pool_start_x * y_stride);
221
222 int x_off = window_start_x;
223
224 for (; x_off <= (window_end_x - window_step_x); x_off += window_step_x)
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000225 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 vres = vdupq_n_f32(min_value);
227 vidx = vdupq_n_u32(0U);
228 const uint8_t *in_ptr_y = in_ptr_n + in_ptr_y_offset + in_ptr_x_offset;
229 uint32_t curr_kernel_index = pool_size_x * pool_start_y;
230 for (int y = pool_start_y; y < pool_end_y; ++y)
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000231 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100232 const uint8_t *in_ptr_x = in_ptr_y + (x_off * sizeof(float));
233 curr_kernel_index += pool_start_x;
234 for (int x = pool_start_x; x < pool_end_x; ++x)
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000235 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100236 const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(in_ptr_x));
237 const uint32x4_t vidx_curr = vdupq_n_u32(curr_kernel_index);
238 const uint32x4_t idxMask = vcgtq_f32(data, vres);
239 vidx = vbslq_u32(idxMask, vidx_curr, vidx);
240 vres = vmaxq_f32(vres, data);
241 in_ptr_x += y_stride;
242 curr_kernel_index++;
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000243 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100244 curr_kernel_index += (pool_size_x - pool_end_x);
245 in_ptr_y += z_stride;
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000246 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247 // Store result
248 vst1q_f32(reinterpret_cast<float *>(out.ptr()) + x_off, vres);
249 vst1q_u32(reinterpret_cast<uint32_t *>(indices.ptr()) + x_off, vidx);
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000250 }
251
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100252 // Left-overs loop
253 for (; x_off < window_end_x; ++x_off)
254 {
255 float res = min_value;
256 uint32_t idx = 0U;
257 const uint8_t *in_ptr_y = in_ptr_n + in_ptr_y_offset + in_ptr_x_offset;
258 for (int y = pool_start_y; y < pool_end_y; ++y)
259 {
260 const uint8_t *in_ptr_x = in_ptr_y + (x_off * sizeof(float));
261 for (int x = pool_start_x; x < pool_end_x; ++x)
262 {
263 const float data = *(reinterpret_cast<const float *>(in_ptr_x));
264 if (data > res)
265 {
266 idx = pool_size_x * y + x;
267 res = data;
268 }
269 in_ptr_x += y_stride;
270 }
271 in_ptr_y += z_stride;
272 }
273
274 // Store result
275 *(reinterpret_cast<float *>(out.ptr()) + x_off) = res;
276 *(reinterpret_cast<uint32_t *>(indices.ptr()) + x_off) = idx;
277 }
278 },
279 out, indices);
Sheri Zhang79144a62021-02-08 17:43:04 +0000280}
281
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100282void poolingMxN_fp32_neon_nhwc(const ITensor *src,
283 ITensor *dst0,
284 ITensor *dst1,
285 PoolingLayerInfo &pool_info,
286 const Window &window_src,
287 const Window &window)
Sheri Zhang79144a62021-02-08 17:43:04 +0000288{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100289 if ((pool_info.pool_type == PoolingType::MAX) && pool_info.use_kernel_indices && (dst1 != nullptr))
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000290 {
291 poolingMxN_fp32_neon_nhwc_kernel_indices(src, dst0, dst1, pool_info, window);
292 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100293 else if (pool_info.pool_size == Size2D(2, 2) && pool_info.pool_type == PoolingType::MAX &&
294 !pool_info.pad_stride_info.has_padding() && (dst1 != nullptr))
Sheri Zhang79144a62021-02-08 17:43:04 +0000295 {
296 pooling2_f32_maxpool_indices(src, dst0, dst1, pool_info, window_src, window);
297 }
298 else
299 {
300 const int window_start_x = window.x().start();
301 const int window_end_x = window.x().end();
302 const int window_step_x = 4;
303
304 Window window_out = window;
305 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
306
307 Iterator in(src, window_src);
308 Iterator out(dst0, window_out);
309
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100310 const int pool_size_x =
311 pool_info.is_global_pooling ? src->info()->tensor_shape().y() : pool_info.pool_size.width;
312 const int pool_size_y =
313 pool_info.is_global_pooling ? src->info()->tensor_shape().z() : pool_info.pool_size.height;
314 const int pool_pad_right = pool_info.pad_stride_info.pad_right();
315 const int pool_pad_top = pool_info.pad_stride_info.pad_top();
316 const int pool_pad_left = pool_info.pad_stride_info.pad_left();
317 const int pool_pad_bottom = pool_info.pad_stride_info.pad_bottom();
318 int pool_stride_x = 0;
319 int pool_stride_y = 0;
Sheri Zhang79144a62021-02-08 17:43:04 +0000320 std::tie(pool_stride_x, pool_stride_y) = pool_info.pad_stride_info.stride();
Adnan AlSinan227db8d2023-02-14 14:24:09 +0000321 const int upper_bound_w = src->info()->dimension(1) + (pool_info.exclude_padding ? 0 : pool_pad_right);
322 const int upper_bound_h = src->info()->dimension(2) + (pool_info.exclude_padding ? 0 : pool_pad_bottom);
323 const float min_value = get_initial_min<float>(pool_info.use_inf_as_limit);
Sheri Zhang79144a62021-02-08 17:43:04 +0000324 float32x4_t vres;
325
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100326 execute_window_loop(
327 window_out,
328 [&](const Coordinates &id)
Sheri Zhang79144a62021-02-08 17:43:04 +0000329 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100330 const int idx_width = id.y() * pool_stride_x;
331 const int idx_height = id.z() * pool_stride_y;
332 const int pool_limit_y = pool_pad_top - idx_height;
333 const int pool_limit_x = pool_pad_left - idx_width;
334
335 const int pool_start_y = std::max(0, window_src.z().start() + pool_limit_y);
336 const int pool_end_y = std::min(pool_size_y, window_src.z().end() + pool_limit_y);
337 const int pool_start_x = std::max(0, window_src.y().start() + pool_limit_x);
338 const int pool_end_x = std::min(pool_size_x, window_src.y().end() + pool_limit_x);
339
340 int x_off = window_start_x;
341 for (; x_off <= (window_end_x - window_step_x); x_off += window_step_x)
Sheri Zhang79144a62021-02-08 17:43:04 +0000342 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100343 if (pool_info.pool_type != PoolingType::MAX)
Sheri Zhang79144a62021-02-08 17:43:04 +0000344 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100345 // Calculate scale
346 const float scale = calculate_avg_scale_pool2d(
347 pool_info.exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w,
348 upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
349 const float32x4_t scale_v = vdupq_n_f32(scale);
Sheri Zhang79144a62021-02-08 17:43:04 +0000350
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100351 // Perform pooling
352 vres = vdupq_n_f32(0.0f);
353
354 for (int y = pool_start_y; y < pool_end_y; ++y)
355 {
356 for (int x = pool_start_x; x < pool_end_x; ++x)
Sheri Zhang79144a62021-02-08 17:43:04 +0000357 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100358 const float32x4_t data = vld1q_f32(
359 reinterpret_cast<const float *>(
360 in.ptr() +
361 (x - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
362 (y - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z())) +
363 x_off);
364
365 // Get power of 2 in case of l2 pooling and accumulate
366 if (pool_info.pool_type == PoolingType::L2)
367 {
368 vres = vmlaq_f32(vres, data, data);
369 }
370 else
371 {
372 vres = vaddq_f32(vres, data);
373 }
Sheri Zhang79144a62021-02-08 17:43:04 +0000374 }
375 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100376 // Divide by scale
377 vres = vmulq_f32(vres, scale_v);
Sheri Zhang79144a62021-02-08 17:43:04 +0000378 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100379 else
Sheri Zhang79144a62021-02-08 17:43:04 +0000380 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100381 vres = vdupq_n_f32(min_value);
382 for (int y = pool_start_y; y < pool_end_y; ++y)
Sheri Zhang79144a62021-02-08 17:43:04 +0000383 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100384 for (int x = pool_start_x; x < pool_end_x; ++x)
Sheri Zhang79144a62021-02-08 17:43:04 +0000385 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100386 const float32x4_t data = vld1q_f32(
387 reinterpret_cast<const float *>(
388 in.ptr() +
389 (x - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
390 (y - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z())) +
391 x_off);
392 vres = vmaxq_f32(vres, data);
Sheri Zhang79144a62021-02-08 17:43:04 +0000393 }
394 }
395 }
396
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100397 // Calculate square-root in case of l2 pooling
398 if (pool_info.pool_type == PoolingType::L2)
Sheri Zhang79144a62021-02-08 17:43:04 +0000399 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100400 float32x4_t l2_res = {static_cast<float>(sqrt(vgetq_lane_f32(vres, 0))),
401 static_cast<float>(sqrt(vgetq_lane_f32(vres, 1))),
402 static_cast<float>(sqrt(vgetq_lane_f32(vres, 2))),
403 static_cast<float>(sqrt(vgetq_lane_f32(vres, 3)))};
404 vres = l2_res;
405 }
406
407 // Store result
408 vst1q_f32(reinterpret_cast<float *>(out.ptr()) + x_off, vres);
409 }
410
411 // Left-overs loop
412 for (; x_off < window_end_x; ++x_off)
413 {
414 float res = 0.0f;
415
416 if (pool_info.pool_type != PoolingType::MAX)
417 {
418 // Calculate scale
419 const float scale = calculate_avg_scale_pool2d(
420 pool_info.exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w,
421 upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y);
422
423 for (int y = pool_start_y; y < pool_end_y; ++y)
Sheri Zhang79144a62021-02-08 17:43:04 +0000424 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100425 for (int x = pool_start_x; x < pool_end_x; ++x)
426 {
427 const float data =
428 *(reinterpret_cast<const float *>(
429 in.ptr() +
430 (x - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
431 (y - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z())) +
432 x_off);
433
434 // Get power of 2 in case of l2 pooling and accumulate
435 if (pool_info.pool_type == PoolingType::L2)
436 {
437 res += data * data;
438 }
439 else
440 {
441 res += data;
442 }
443 }
444 }
445
446 // Divide by scale
447 res *= scale;
448 }
449 else
450 {
451 res = min_value;
452 for (int y = pool_start_y; y < pool_end_y; ++y)
453 {
454 for (int x = pool_start_x; x < pool_end_x; ++x)
455 {
456 const float data =
457 *(reinterpret_cast<const float *>(
458 in.ptr() +
459 (x - pool_pad_left) * static_cast<int>(src->info()->strides_in_bytes().y()) +
460 (y - pool_pad_top) * static_cast<int>(src->info()->strides_in_bytes().z())) +
461 x_off);
462 res = std::max(res, data);
463 }
Sheri Zhang79144a62021-02-08 17:43:04 +0000464 }
465 }
Sheri Zhang79144a62021-02-08 17:43:04 +0000466
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100467 // Calculate square-root in case of l2 pooling
468 if (pool_info.pool_type == PoolingType::L2)
469 {
470 res = std::sqrt(res);
471 }
Sheri Zhang79144a62021-02-08 17:43:04 +0000472
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100473 // Store result
474 *(reinterpret_cast<float *>(out.ptr()) + x_off) = res;
475 }
476 },
477 in, out);
Sheri Zhang79144a62021-02-08 17:43:04 +0000478 }
479}
480} // namespace cpu
Ramy Elgammala8db6122023-05-08 03:33:43 +0100481} // namespace arm_compute