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