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