| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "pooling_arm.h" |
| 16 | |
| 17 | #include <float.h> |
| 18 | |
| 19 | #if __ARM_NEON |
| 20 | #include <arm_neon.h> |
| 21 | #include "arm_usability.h" |
| 22 | #endif // __ARM_NEON |
| 23 | |
| 24 | namespace ncnn { |
| 25 | |
| 26 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 27 | int Pooling_arm::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 28 | { |
| 29 | // max value in NxN window |
| 30 | // avg value in NxN window |
| 31 | |
| 32 | int w = bottom_blob.w; |
| 33 | int h = bottom_blob.h; |
| 34 | int channels = bottom_blob.c; |
| 35 | size_t elemsize = bottom_blob.elemsize; |
| 36 | int elempack = bottom_blob.elempack; |
| 37 | |
| 38 | // NCNN_LOGE("Pooling input %d x %d pad = %d %d %d %d ksize=%d %d stride=%d %d", w, h, pad_left, pad_right, pad_top, pad_bottom, kernel_w, kernel_h, stride_w, stride_h); |
| 39 | |
| 40 | if (global_pooling) |
| 41 | { |
| 42 | top_blob.create(channels, elemsize, elempack, opt.blob_allocator); |
| 43 | if (top_blob.empty()) |
| 44 | return -100; |
| 45 | |
| 46 | int size = w * h; |
| 47 | |
| 48 | if (pooling_type == PoolMethod_MAX) |
| 49 | { |
| 50 | if (elempack == 8) |
| 51 | { |
| 52 | #pragma omp parallel for num_threads(opt.num_threads) |
| 53 | for (int q = 0; q < channels; q++) |
| 54 | { |
| 55 | const __fp16* ptr = bottom_blob.channel(q); |
| 56 | |
| 57 | float16x8_t _max = vdupq_n_f16((__fp16)-FLT_MAX); |
| 58 | for (int i = 0; i < size; i++) |
| 59 | { |
| 60 | float16x8_t _val = vld1q_f16(ptr); |
| 61 | _max = vmaxq_f16(_max, _val); |
| 62 | ptr += 8; |
| 63 | } |
| 64 | |
| 65 | __fp16* outptr = top_blob; |
| 66 | vst1q_f16(outptr + q * 8, _max); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (elempack == 4) |
| 71 | { |
| 72 | #pragma omp parallel for num_threads(opt.num_threads) |
| 73 | for (int q = 0; q < channels; q++) |
| 74 | { |
| 75 | const __fp16* ptr = bottom_blob.channel(q); |
| 76 | |
| 77 | float16x4_t _max = vdup_n_f16((__fp16)-FLT_MAX); |
| 78 | for (int i = 0; i < size; i++) |
| 79 | { |
| 80 | float16x4_t _val = vld1_f16(ptr); |
| 81 | _max = vmax_f16(_max, _val); |
| 82 | ptr += 4; |
| 83 | } |
| 84 | |
| 85 | __fp16* outptr = top_blob; |
| 86 | vst1_f16(outptr + q * 4, _max); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (elempack == 1) |
| 91 | { |
| 92 | #pragma omp parallel for num_threads(opt.num_threads) |
| 93 | for (int q = 0; q < channels; q++) |
| 94 | { |
| 95 | const __fp16* ptr = bottom_blob.channel(q); |
| 96 | |
| 97 | __fp16 max = (__fp16)-FLT_MAX; |
| 98 | for (int i = 0; i < size; i++) |
| 99 | { |
| 100 | max = std::max(max, ptr[i]); |
| 101 | } |
| 102 | |
| 103 | __fp16* outptr = top_blob; |
| 104 | outptr[q] = max; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (pooling_type == PoolMethod_AVE) |
| 110 | { |
| 111 | if (elempack == 8) |
| 112 | { |
| 113 | #pragma omp parallel for num_threads(opt.num_threads) |
| 114 | for (int q = 0; q < channels; q++) |
| 115 | { |
| 116 | const __fp16* ptr = bottom_blob.channel(q); |
| 117 | |
| 118 | float32x4_t _sum0 = vdupq_n_f32(0.f); |
| 119 | float32x4_t _sum1 = vdupq_n_f32(0.f); |
| 120 | for (int i = 0; i < size; i++) |
| 121 | { |
| 122 | float16x8_t _val = vld1q_f16(ptr); |
| 123 | _sum0 = vaddq_f32(_sum0, vcvt_f32_f16(vget_low_f16(_val))); |
| 124 | _sum1 = vaddq_f32(_sum1, vcvt_f32_f16(vget_high_f16(_val))); |
| 125 | ptr += 8; |
| 126 | } |
| 127 | |
| 128 | float32x4_t _inv_size = vdupq_n_f32(1.f / size); |
| 129 | float32x4_t _avg0 = vmulq_f32(_sum0, _inv_size); |
| 130 | float32x4_t _avg1 = vmulq_f32(_sum1, _inv_size); |
| 131 | |
| 132 | __fp16* outptr = top_blob; |
| 133 | vst1q_f16(outptr + q * 8, vcombine_f16(vcvt_f16_f32(_avg0), vcvt_f16_f32(_avg1))); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (elempack == 4) |
| 138 | { |
| 139 | #pragma omp parallel for num_threads(opt.num_threads) |
| 140 | for (int q = 0; q < channels; q++) |
| 141 | { |
| 142 | const __fp16* ptr = bottom_blob.channel(q); |
| 143 | |
| 144 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 145 | for (int i = 0; i < size; i++) |
| 146 | { |
| 147 | float32x4_t _val = vcvt_f32_f16(vld1_f16(ptr)); |
| 148 | _sum = vaddq_f32(_sum, _val); |
| 149 | ptr += 4; |
| 150 | } |
| 151 | |
| 152 | float32x4_t _inv_size = vdupq_n_f32(1.f / size); |
| 153 | float32x4_t _avg = vmulq_f32(_sum, _inv_size); |
| 154 | |
| 155 | __fp16* outptr = top_blob; |
| 156 | vst1_f16(outptr + q * 4, vcvt_f16_f32(_avg)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (elempack == 1) |
| 161 | { |
| 162 | #pragma omp parallel for num_threads(opt.num_threads) |
| 163 | for (int q = 0; q < channels; q++) |
| 164 | { |
| 165 | const __fp16* ptr = bottom_blob.channel(q); |
| 166 | |
| 167 | float sum = 0.f; |
| 168 | for (int i = 0; i < size; i++) |
| 169 | { |
| 170 | sum += (float)ptr[i]; |
| 171 | } |
| 172 | |
| 173 | __fp16* outptr = top_blob; |
| 174 | outptr[q] = (__fp16)(sum / size); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | Mat bottom_blob_bordered; |
| 183 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 184 | if (bottom_blob_bordered.empty()) |
| 185 | return -100; |
| 186 | |
| 187 | w = bottom_blob_bordered.w; |
| 188 | h = bottom_blob_bordered.h; |
| 189 | |
| 190 | int outw = (w - kernel_w) / stride_w + 1; |
| 191 | int outh = (h - kernel_h) / stride_h + 1; |
| 192 | |
| 193 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator); |
| 194 | if (top_blob.empty()) |
| 195 | return -100; |
| 196 | |
| 197 | const int maxk = kernel_w * kernel_h; |
| 198 | |
| 199 | // kernel offsets |
| 200 | std::vector<int> _space_ofs(maxk); |
| 201 | int* space_ofs = &_space_ofs[0]; |
| 202 | { |
| 203 | int p1 = 0; |
| 204 | int p2 = 0; |
| 205 | int gap = w - kernel_w; |
| 206 | for (int i = 0; i < kernel_h; i++) |
| 207 | { |
| 208 | for (int j = 0; j < kernel_w; j++) |
| 209 | { |
| 210 | space_ofs[p1] = p2; |
| 211 | p1++; |
| 212 | p2++; |
| 213 | } |
| 214 | p2 += gap; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (pooling_type == PoolMethod_MAX) |
| 219 | { |
| 220 | if (elempack == 8) |
| 221 | { |
| 222 | #pragma omp parallel for num_threads(opt.num_threads) |
| 223 | for (int q = 0; q < channels; q++) |
| 224 | { |
| 225 | const Mat m = bottom_blob_bordered.channel(q); |
| 226 | __fp16* outptr = top_blob.channel(q); |
| 227 | |
| 228 | for (int i = 0; i < outh; i++) |
| 229 | { |
| 230 | for (int j = 0; j < outw; j++) |
| 231 | { |
| 232 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * 8; |
| 233 | |
| 234 | float16x8_t _max = vdupq_n_f16((__fp16)-FLT_MAX); |
| 235 | |
| 236 | for (int k = 0; k < maxk; k++) |
| 237 | { |
| 238 | float16x8_t _val = vld1q_f16(sptr + space_ofs[k] * 8); |
| 239 | _max = vmaxq_f16(_max, _val); |
| 240 | } |
| 241 | |
| 242 | vst1q_f16(outptr + j * 8, _max); |
| 243 | } |
| 244 | |
| 245 | outptr += outw * 8; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (elempack == 4) |
| 251 | { |
| 252 | #pragma omp parallel for num_threads(opt.num_threads) |
| 253 | for (int q = 0; q < channels; q++) |
| 254 | { |
| 255 | const Mat m = bottom_blob_bordered.channel(q); |
| 256 | __fp16* outptr = top_blob.channel(q); |
| 257 | |
| 258 | for (int i = 0; i < outh; i++) |
| 259 | { |
| 260 | for (int j = 0; j < outw; j++) |
| 261 | { |
| 262 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * 4; |
| 263 | |
| 264 | float16x4_t _max = vdup_n_f16((__fp16)-FLT_MAX); |
| 265 | |
| 266 | for (int k = 0; k < maxk; k++) |
| 267 | { |
| 268 | float16x4_t _val = vld1_f16(sptr + space_ofs[k] * 4); |
| 269 | _max = vmax_f16(_max, _val); |
| 270 | } |
| 271 | |
| 272 | vst1_f16(outptr + j * 4, _max); |
| 273 | } |
| 274 | |
| 275 | outptr += outw * 4; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (elempack == 1) |
| 281 | { |
| 282 | #pragma omp parallel for num_threads(opt.num_threads) |
| 283 | for (int q = 0; q < channels; q++) |
| 284 | { |
| 285 | const Mat m = bottom_blob_bordered.channel(q); |
| 286 | __fp16* outptr = top_blob.channel(q); |
| 287 | |
| 288 | for (int i = 0; i < outh; i++) |
| 289 | { |
| 290 | for (int j = 0; j < outw; j++) |
| 291 | { |
| 292 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w; |
| 293 | |
| 294 | __fp16 max = (__fp16)-FLT_MAX; |
| 295 | |
| 296 | for (int k = 0; k < maxk; k++) |
| 297 | { |
| 298 | __fp16 val = sptr[space_ofs[k]]; |
| 299 | max = std::max(max, val); |
| 300 | } |
| 301 | |
| 302 | outptr[j] = max; |
| 303 | } |
| 304 | |
| 305 | outptr += outw; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (pooling_type == PoolMethod_AVE) |
| 312 | { |
| 313 | if (avgpool_count_include_pad == 0) |
| 314 | { |
| 315 | int wtailpad = 0; |
| 316 | int htailpad = 0; |
| 317 | |
| 318 | if (pad_mode == 0) // full padding |
| 319 | { |
| 320 | wtailpad = bottom_blob_bordered.w - bottom_blob.w - pad_left - pad_right; |
| 321 | htailpad = bottom_blob_bordered.h - bottom_blob.h - pad_top - pad_bottom; |
| 322 | } |
| 323 | |
| 324 | if (elempack == 4) |
| 325 | { |
| 326 | #pragma omp parallel for num_threads(opt.num_threads) |
| 327 | for (int q = 0; q < channels; q++) |
| 328 | { |
| 329 | const Mat m = bottom_blob_bordered.channel(q); |
| 330 | __fp16* outptr = top_blob.channel(q); |
| 331 | |
| 332 | for (int i = 0; i < outh; i++) |
| 333 | { |
| 334 | int sy0 = i * stride_h; |
| 335 | |
| 336 | for (int j = 0; j < outw; j++) |
| 337 | { |
| 338 | int sx0 = j * stride_w; |
| 339 | |
| 340 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 341 | int area = 0; |
| 342 | |
| 343 | for (int ki = 0; ki < kernel_h; ki++) |
| 344 | { |
| 345 | int sy = sy0 + ki; |
| 346 | |
| 347 | if (sy < pad_top) |
| 348 | continue; |
| 349 | |
| 350 | if (sy >= h - pad_bottom - htailpad) |
| 351 | break; |
| 352 | |
| 353 | for (int kj = 0; kj < kernel_w; kj++) |
| 354 | { |
| 355 | int sx = sx0 + kj; |
| 356 | |
| 357 | if (sx < pad_left) |
| 358 | continue; |
| 359 | |
| 360 | if (sx >= w - pad_right - wtailpad) |
| 361 | break; |
| 362 | |
| 363 | float32x4_t _val = vcvt_f32_f16(vld1_f16(m.row<const __fp16>(sy) + sx * 4)); |
| 364 | _sum = vaddq_f32(_sum, _val); |
| 365 | area += 1; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | float32x4_t _inv_area = vdupq_n_f32(1.f / area); |
| 370 | float32x4_t _avg = vmulq_f32(_sum, _inv_area); |
| 371 | vst1_f16(outptr + j * 4, vcvt_f16_f32(_avg)); |
| 372 | } |
| 373 | |
| 374 | outptr += outw * 4; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (elempack == 1) |
| 380 | { |
| 381 | #pragma omp parallel for num_threads(opt.num_threads) |
| 382 | for (int q = 0; q < channels; q++) |
| 383 | { |
| 384 | const Mat m = bottom_blob_bordered.channel(q); |
| 385 | __fp16* outptr = top_blob.channel(q); |
| 386 | |
| 387 | for (int i = 0; i < outh; i++) |
| 388 | { |
| 389 | int sy0 = i * stride_h; |
| 390 | |
| 391 | for (int j = 0; j < outw; j++) |
| 392 | { |
| 393 | int sx0 = j * stride_w; |
| 394 | |
| 395 | float sum = 0.f; |
| 396 | int area = 0; |
| 397 | |
| 398 | for (int ki = 0; ki < kernel_h; ki++) |
| 399 | { |
| 400 | int sy = sy0 + ki; |
| 401 | |
| 402 | if (sy < pad_top) |
| 403 | continue; |
| 404 | |
| 405 | if (sy >= h - pad_bottom - htailpad) |
| 406 | break; |
| 407 | |
| 408 | for (int kj = 0; kj < kernel_w; kj++) |
| 409 | { |
| 410 | int sx = sx0 + kj; |
| 411 | |
| 412 | if (sx < pad_left) |
| 413 | continue; |
| 414 | |
| 415 | if (sx >= w - pad_right - wtailpad) |
| 416 | break; |
| 417 | |
| 418 | float val = (float)(m.row<const __fp16>(sy)[sx]); |
| 419 | sum += val; |
| 420 | area += 1; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | outptr[j] = (__fp16)(sum / area); |
| 425 | } |
| 426 | |
| 427 | outptr += outw; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (avgpool_count_include_pad == 1) |
| 434 | { |
| 435 | if (elempack == 4) |
| 436 | { |
| 437 | #pragma omp parallel for num_threads(opt.num_threads) |
| 438 | for (int q = 0; q < channels; q++) |
| 439 | { |
| 440 | const Mat m = bottom_blob_bordered.channel(q); |
| 441 | __fp16* outptr = top_blob.channel(q); |
| 442 | |
| 443 | float32x4_t _inv_maxk = vdupq_n_f32(1.f / maxk); |
| 444 | |
| 445 | for (int i = 0; i < outh; i++) |
| 446 | { |
| 447 | for (int j = 0; j < outw; j++) |
| 448 | { |
| 449 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * 4; |
| 450 | |
| 451 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 452 | |
| 453 | for (int k = 0; k < maxk; k++) |
| 454 | { |
| 455 | float32x4_t _val = vcvt_f32_f16(vld1_f16(sptr + space_ofs[k] * 4)); |
| 456 | _sum = vaddq_f32(_sum, _val); |
| 457 | } |
| 458 | |
| 459 | float32x4_t _avg = vmulq_f32(_sum, _inv_maxk); |
| 460 | vst1_f16(outptr + j * 4, vcvt_f16_f32(_avg)); |
| 461 | } |
| 462 | |
| 463 | outptr += outw * 4; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | if (elempack == 1) |
| 469 | { |
| 470 | #pragma omp parallel for num_threads(opt.num_threads) |
| 471 | for (int q = 0; q < channels; q++) |
| 472 | { |
| 473 | const Mat m = bottom_blob_bordered.channel(q); |
| 474 | __fp16* outptr = top_blob.channel(q); |
| 475 | |
| 476 | for (int i = 0; i < outh; i++) |
| 477 | { |
| 478 | for (int j = 0; j < outw; j++) |
| 479 | { |
| 480 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w; |
| 481 | |
| 482 | float sum = 0.f; |
| 483 | |
| 484 | for (int k = 0; k < maxk; k++) |
| 485 | { |
| 486 | float val = (float)(sptr[space_ofs[k]]); |
| 487 | sum += val; |
| 488 | } |
| 489 | |
| 490 | outptr[j] = (__fp16)(sum / maxk); |
| 491 | } |
| 492 | |
| 493 | outptr += outw; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | int Pooling_arm::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 504 | { |
| 505 | // max value in NxN window |
| 506 | // avg value in NxN window |
| 507 | |
| 508 | if (pooling_type == PoolMethod_MAX || global_pooling) |
| 509 | { |
| 510 | return forward_fp16s(bottom_blob, top_blob, opt); |
| 511 | } |
| 512 | |
| 513 | int w = bottom_blob.w; |
| 514 | int h = bottom_blob.h; |
| 515 | int channels = bottom_blob.c; |
| 516 | size_t elemsize = bottom_blob.elemsize; |
| 517 | int elempack = bottom_blob.elempack; |
| 518 | |
| 519 | // NCNN_LOGE("Pooling input %d x %d pad = %d %d %d %d ksize=%d %d stride=%d %d", w, h, pad_left, pad_right, pad_top, pad_bottom, kernel_w, kernel_h, stride_w, stride_h); |
| 520 | |
| 521 | Mat bottom_blob_bordered; |
| 522 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 523 | if (bottom_blob_bordered.empty()) |
| 524 | return -100; |
| 525 | |
| 526 | w = bottom_blob_bordered.w; |
| 527 | h = bottom_blob_bordered.h; |
| 528 | |
| 529 | int outw = (w - kernel_w) / stride_w + 1; |
| 530 | int outh = (h - kernel_h) / stride_h + 1; |
| 531 | |
| 532 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator); |
| 533 | if (top_blob.empty()) |
| 534 | return -100; |
| 535 | |
| 536 | const int maxk = kernel_w * kernel_h; |
| 537 | |
| 538 | // kernel offsets |
| 539 | std::vector<int> _space_ofs(maxk); |
| 540 | int* space_ofs = &_space_ofs[0]; |
| 541 | { |
| 542 | int p1 = 0; |
| 543 | int p2 = 0; |
| 544 | int gap = w - kernel_w; |
| 545 | for (int i = 0; i < kernel_h; i++) |
| 546 | { |
| 547 | for (int j = 0; j < kernel_w; j++) |
| 548 | { |
| 549 | space_ofs[p1] = p2; |
| 550 | p1++; |
| 551 | p2++; |
| 552 | } |
| 553 | p2 += gap; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (pooling_type == PoolMethod_AVE) |
| 558 | { |
| 559 | if (avgpool_count_include_pad == 0) |
| 560 | { |
| 561 | int wtailpad = 0; |
| 562 | int htailpad = 0; |
| 563 | |
| 564 | if (pad_mode == 0) // full padding |
| 565 | { |
| 566 | wtailpad = bottom_blob_bordered.w - bottom_blob.w - pad_left - pad_right; |
| 567 | htailpad = bottom_blob_bordered.h - bottom_blob.h - pad_top - pad_bottom; |
| 568 | } |
| 569 | |
| 570 | if (elempack == 8) |
| 571 | { |
| 572 | #pragma omp parallel for num_threads(opt.num_threads) |
| 573 | for (int q = 0; q < channels; q++) |
| 574 | { |
| 575 | const Mat m = bottom_blob_bordered.channel(q); |
| 576 | __fp16* outptr = top_blob.channel(q); |
| 577 | |
| 578 | for (int i = 0; i < outh; i++) |
| 579 | { |
| 580 | int sy0 = i * stride_h; |
| 581 | |
| 582 | for (int j = 0; j < outw; j++) |
| 583 | { |
| 584 | int sx0 = j * stride_w; |
| 585 | |
| 586 | float16x8_t _sum = vdupq_n_f16((__fp16)0.f); |
| 587 | int area = 0; |
| 588 | |
| 589 | for (int ki = 0; ki < kernel_h; ki++) |
| 590 | { |
| 591 | int sy = sy0 + ki; |
| 592 | |
| 593 | if (sy < pad_top) |
| 594 | continue; |
| 595 | |
| 596 | if (sy >= h - pad_bottom - htailpad) |
| 597 | break; |
| 598 | |
| 599 | for (int kj = 0; kj < kernel_w; kj++) |
| 600 | { |
| 601 | int sx = sx0 + kj; |
| 602 | |
| 603 | if (sx < pad_left) |
| 604 | continue; |
| 605 | |
| 606 | if (sx >= w - pad_right - wtailpad) |
| 607 | break; |
| 608 | |
| 609 | float16x8_t _val = vld1q_f16(m.row<const __fp16>(sy) + sx * 8); |
| 610 | _sum = vaddq_f16(_sum, _val); |
| 611 | area += 1; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | #if defined(_MSC_VER) && !defined(__clang__) |
| 616 | float16x4_t _inv_area0 = vcvt_f16_f32(vdupq_n_f32(1.f / area)); |
| 617 | float16x8_t _inv_area = vcombine_f16(_inv_area0, _inv_area0); |
| 618 | #else |
| 619 | float16x8_t _inv_area = vdupq_n_f16((__fp16)(1.f / area)); |
| 620 | #endif |
| 621 | float16x8_t _avg = vmulq_f16(_sum, _inv_area); |
| 622 | vst1q_f16(outptr + j * 8, _avg); |
| 623 | } |
| 624 | |
| 625 | outptr += outw * 8; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (elempack == 4) |
| 631 | { |
| 632 | #pragma omp parallel for num_threads(opt.num_threads) |
| 633 | for (int q = 0; q < channels; q++) |
| 634 | { |
| 635 | const Mat m = bottom_blob_bordered.channel(q); |
| 636 | __fp16* outptr = top_blob.channel(q); |
| 637 | |
| 638 | for (int i = 0; i < outh; i++) |
| 639 | { |
| 640 | int sy0 = i * stride_h; |
| 641 | |
| 642 | for (int j = 0; j < outw; j++) |
| 643 | { |
| 644 | int sx0 = j * stride_w; |
| 645 | |
| 646 | float16x4_t _sum = vdup_n_f16((__fp16)0.f); |
| 647 | int area = 0; |
| 648 | |
| 649 | for (int ki = 0; ki < kernel_h; ki++) |
| 650 | { |
| 651 | int sy = sy0 + ki; |
| 652 | |
| 653 | if (sy < pad_top) |
| 654 | continue; |
| 655 | |
| 656 | if (sy >= h - pad_bottom - htailpad) |
| 657 | break; |
| 658 | |
| 659 | for (int kj = 0; kj < kernel_w; kj++) |
| 660 | { |
| 661 | int sx = sx0 + kj; |
| 662 | |
| 663 | if (sx < pad_left) |
| 664 | continue; |
| 665 | |
| 666 | if (sx >= w - pad_right - wtailpad) |
| 667 | break; |
| 668 | |
| 669 | float16x4_t _val = vld1_f16(m.row<const __fp16>(sy) + sx * 4); |
| 670 | _sum = vadd_f16(_sum, _val); |
| 671 | area += 1; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | #if defined(_MSC_VER) && !defined(__clang__) |
| 676 | float16x4_t _inv_area = vcvt_f16_f32(vdupq_n_f32(1.f / area)); |
| 677 | #else |
| 678 | float16x4_t _inv_area = vdup_n_f16((__fp16)(1.f / area)); |
| 679 | #endif |
| 680 | float16x4_t _avg = vmul_f16(_sum, _inv_area); |
| 681 | vst1_f16(outptr + j * 4, _avg); |
| 682 | } |
| 683 | |
| 684 | outptr += outw * 4; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if (elempack == 1) |
| 690 | { |
| 691 | #pragma omp parallel for num_threads(opt.num_threads) |
| 692 | for (int q = 0; q < channels; q++) |
| 693 | { |
| 694 | const Mat m = bottom_blob_bordered.channel(q); |
| 695 | __fp16* outptr = top_blob.channel(q); |
| 696 | |
| 697 | for (int i = 0; i < outh; i++) |
| 698 | { |
| 699 | int sy0 = i * stride_h; |
| 700 | |
| 701 | for (int j = 0; j < outw; j++) |
| 702 | { |
| 703 | int sx0 = j * stride_w; |
| 704 | |
| 705 | __fp16 sum = (__fp16)0.f; |
| 706 | int area = 0; |
| 707 | |
| 708 | for (int ki = 0; ki < kernel_h; ki++) |
| 709 | { |
| 710 | int sy = sy0 + ki; |
| 711 | |
| 712 | if (sy < pad_top) |
| 713 | continue; |
| 714 | |
| 715 | if (sy >= h - pad_bottom - htailpad) |
| 716 | break; |
| 717 | |
| 718 | for (int kj = 0; kj < kernel_w; kj++) |
| 719 | { |
| 720 | int sx = sx0 + kj; |
| 721 | |
| 722 | if (sx < pad_left) |
| 723 | continue; |
| 724 | |
| 725 | if (sx >= w - pad_right - wtailpad) |
| 726 | break; |
| 727 | |
| 728 | __fp16 val = m.row<const __fp16>(sy)[sx]; |
| 729 | sum += val; |
| 730 | area += 1; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | outptr[j] = sum / (__fp16)area; |
| 735 | } |
| 736 | |
| 737 | outptr += outw; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if (avgpool_count_include_pad == 1) |
| 744 | { |
| 745 | if (elempack == 8) |
| 746 | { |
| 747 | #pragma omp parallel for num_threads(opt.num_threads) |
| 748 | for (int q = 0; q < channels; q++) |
| 749 | { |
| 750 | const Mat m = bottom_blob_bordered.channel(q); |
| 751 | __fp16* outptr = top_blob.channel(q); |
| 752 | |
| 753 | #if defined(_MSC_VER) && !defined(__clang__) |
| 754 | float16x4_t _inv_maxk0 = vcvt_f16_f32(vdupq_n_f32(1.f / maxk)); |
| 755 | float16x8_t _inv_maxk = vcombine_f16(_inv_maxk0, _inv_maxk0); |
| 756 | #else |
| 757 | float16x8_t _inv_maxk = vdupq_n_f16((__fp16)(1.f / maxk)); |
| 758 | #endif |
| 759 | |
| 760 | for (int i = 0; i < outh; i++) |
| 761 | { |
| 762 | for (int j = 0; j < outw; j++) |
| 763 | { |
| 764 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * 8; |
| 765 | |
| 766 | float16x8_t _sum = vdupq_n_f16((__fp16)0.f); |
| 767 | |
| 768 | for (int k = 0; k < maxk; k++) |
| 769 | { |
| 770 | float16x8_t _val = vld1q_f16(sptr + space_ofs[k] * 8); |
| 771 | _sum = vaddq_f16(_sum, _val); |
| 772 | } |
| 773 | |
| 774 | float16x8_t _avg = vmulq_f16(_sum, _inv_maxk); |
| 775 | vst1q_f16(outptr + j * 8, _avg); |
| 776 | } |
| 777 | |
| 778 | outptr += outw * 8; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | if (elempack == 4) |
| 784 | { |
| 785 | #pragma omp parallel for num_threads(opt.num_threads) |
| 786 | for (int q = 0; q < channels; q++) |
| 787 | { |
| 788 | const Mat m = bottom_blob_bordered.channel(q); |
| 789 | __fp16* outptr = top_blob.channel(q); |
| 790 | |
| 791 | #if defined(_MSC_VER) && !defined(__clang__) |
| 792 | float16x4_t _inv_maxk = vcvt_f16_f32(vdupq_n_f32(1.f / maxk)); |
| 793 | #else |
| 794 | float16x4_t _inv_maxk = vdup_n_f16((__fp16)(1.f / maxk)); |
| 795 | #endif |
| 796 | |
| 797 | for (int i = 0; i < outh; i++) |
| 798 | { |
| 799 | for (int j = 0; j < outw; j++) |
| 800 | { |
| 801 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * 4; |
| 802 | |
| 803 | float16x4_t _sum = vdup_n_f16((__fp16)0.f); |
| 804 | |
| 805 | for (int k = 0; k < maxk; k++) |
| 806 | { |
| 807 | float16x4_t _val = vld1_f16(sptr + space_ofs[k] * 4); |
| 808 | _sum = vadd_f16(_sum, _val); |
| 809 | } |
| 810 | |
| 811 | float16x4_t _avg = vmul_f16(_sum, _inv_maxk); |
| 812 | vst1_f16(outptr + j * 4, _avg); |
| 813 | } |
| 814 | |
| 815 | outptr += outw * 4; |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | if (elempack == 1) |
| 821 | { |
| 822 | #pragma omp parallel for num_threads(opt.num_threads) |
| 823 | for (int q = 0; q < channels; q++) |
| 824 | { |
| 825 | const Mat m = bottom_blob_bordered.channel(q); |
| 826 | __fp16* outptr = top_blob.channel(q); |
| 827 | |
| 828 | for (int i = 0; i < outh; i++) |
| 829 | { |
| 830 | for (int j = 0; j < outw; j++) |
| 831 | { |
| 832 | const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w; |
| 833 | |
| 834 | __fp16 sum = (__fp16)0.f; |
| 835 | |
| 836 | for (int k = 0; k < maxk; k++) |
| 837 | { |
| 838 | __fp16 val = sptr[space_ofs[k]]; |
| 839 | sum += val; |
| 840 | } |
| 841 | |
| 842 | outptr[j] = sum / (__fp16)maxk; |
| 843 | } |
| 844 | |
| 845 | outptr += outw; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 855 | |
| 856 | } // namespace ncnn |