Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 1 | /* |
Adnan AlSinan | 227db8d | 2023-02-14 14:24:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2023 Arm Limited. |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 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/Helpers.h" |
| 25 | #include "arm_compute/core/ITensor.h" |
| 26 | #include "arm_compute/core/Types.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 27 | |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 28 | #include "src/core/helpers/WindowHelpers.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | #include "src/core/NEON/wrapper/intrinsics/intrinsics.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 30 | #include "src/cpu/kernels/pool2d/neon/list.h" |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace cpu |
| 35 | { |
| 36 | namespace |
| 37 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 38 | void 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 44 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | int pool_stride_x = 0; |
| 60 | int pool_stride_y = 0; |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 61 | 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 Park | fe56edb | 2021-04-13 20:21:11 +0100 | [diff] [blame] | 66 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 71 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | execute_window_loop( |
| 73 | window_out, |
| 74 | [&](const Coordinates &id) |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 75 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 80 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 83 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 84 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 96 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 97 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 111 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 127 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | // Store indices |
| 129 | vst1q_u32(reinterpret_cast<uint32_t *>(indices.ptr()) + x_off, tmp_indices2); |
| 130 | } |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 131 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | // 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 160 | } |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 161 | } // namespace |
| 162 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 163 | void poolingMxN_fp32_neon_nhwc_kernel_indices( |
| 164 | const ITensor *src, ITensor *dst0, ITensor *dst1, const PoolingLayerInfo &pool_info, const Window &window) |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 165 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 166 | const int window_start_x = window.x().start(); |
| 167 | const int window_end_x = window.x().end(); |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 168 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 182 | int pool_stride_x = 0; |
| 183 | int pool_stride_y = 0; |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 184 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 191 | constexpr int idx_width = 1; |
| 192 | constexpr int idx_height = 2; |
| 193 | constexpr int idx_batch = 3; |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 194 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 204 | execute_window_loop( |
| 205 | window_out, |
| 206 | [&](const Coordinates &id) |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 207 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 208 | 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 AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 210 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 211 | 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 AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 225 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 226 | 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 AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 231 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 232 | 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 AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 235 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 236 | 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 AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 243 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 244 | curr_kernel_index += (pool_size_x - pool_end_x); |
| 245 | in_ptr_y += z_stride; |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 246 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 247 | // 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 AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 252 | // 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 282 | void 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 288 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 289 | if ((pool_info.pool_type == PoolingType::MAX) && pool_info.use_kernel_indices && (dst1 != nullptr)) |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 290 | { |
| 291 | poolingMxN_fp32_neon_nhwc_kernel_indices(src, dst0, dst1, pool_info, window); |
| 292 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 293 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 295 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 310 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 320 | std::tie(pool_stride_x, pool_stride_y) = pool_info.pad_stride_info.stride(); |
Adnan AlSinan | 227db8d | 2023-02-14 14:24:09 +0000 | [diff] [blame] | 321 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 324 | float32x4_t vres; |
| 325 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 326 | execute_window_loop( |
| 327 | window_out, |
| 328 | [&](const Coordinates &id) |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 329 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 330 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 342 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 343 | if (pool_info.pool_type != PoolingType::MAX) |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 344 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 345 | // 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 350 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 351 | // 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 357 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 358 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 374 | } |
| 375 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 376 | // Divide by scale |
| 377 | vres = vmulq_f32(vres, scale_v); |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 378 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 379 | else |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 380 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 381 | vres = vdupq_n_f32(min_value); |
| 382 | for (int y = pool_start_y; y < pool_end_y; ++y) |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 383 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 384 | for (int x = pool_start_x; x < pool_end_x; ++x) |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 385 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 386 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 397 | // Calculate square-root in case of l2 pooling |
| 398 | if (pool_info.pool_type == PoolingType::L2) |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 399 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 400 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 424 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 425 | 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 Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 466 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 467 | // Calculate square-root in case of l2 pooling |
| 468 | if (pool_info.pool_type == PoolingType::L2) |
| 469 | { |
| 470 | res = std::sqrt(res); |
| 471 | } |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 472 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 473 | // Store result |
| 474 | *(reinterpret_cast<float *>(out.ptr()) + x_off) = res; |
| 475 | } |
| 476 | }, |
| 477 | in, out); |
Sheri Zhang | 79144a6 | 2021-02-08 17:43:04 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } // namespace cpu |
Ramy Elgammal | a8db612 | 2023-05-08 03:33:43 +0100 | [diff] [blame] | 481 | } // namespace arm_compute |