| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 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 "reshape_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 "x86_usability.h" |
| 25 | |
| 26 | namespace ncnn { |
| 27 | |
| 28 | Reshape_x86::Reshape_x86() |
| 29 | { |
| 30 | #if __SSE2__ |
| 31 | support_packing = true; |
| 32 | #endif // __SSE2__ |
| 33 | } |
| 34 | |
| 35 | int Reshape_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 36 | { |
| 37 | int elempack = bottom_blob.elempack; |
| 38 | |
| 39 | if (permute == 1) |
| 40 | { |
| 41 | // TODO implement permute on-the-fly |
| 42 | Option opt_pack = opt; |
| 43 | opt_pack.blob_allocator = opt.workspace_allocator; |
| 44 | |
| 45 | Mat bottom_blob_unpacked; |
| 46 | convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack); |
| 47 | |
| 48 | Mat top_blob_unpacked; |
| 49 | int ret = Reshape::forward(bottom_blob_unpacked, top_blob_unpacked, opt_pack); |
| 50 | if (ret != 0) |
| 51 | return ret; |
| 52 | |
| 53 | int out_elempack = 1; |
| 54 | #if __SSE2__ |
| 55 | if (opt.use_packing_layout) |
| 56 | { |
| 57 | // resolve dst_elempack |
| 58 | int dims = top_blob_unpacked.dims; |
| 59 | #if __AVX512F__ |
| 60 | if (dims == 1) out_elempack = top_blob_unpacked.w % 16 == 0 ? 16 : top_blob_unpacked.w % 8 == 0 ? 8 : top_blob_unpacked.w % 4 == 0 ? 4 : 1; |
| 61 | if (dims == 2) out_elempack = top_blob_unpacked.h % 16 == 0 ? 16 : top_blob_unpacked.h % 8 == 0 ? 8 : top_blob_unpacked.h % 4 == 0 ? 4 : 1; |
| 62 | if (dims == 3 || dims == 4) out_elempack = top_blob_unpacked.c % 16 == 0 ? 16 : top_blob_unpacked.c % 8 == 0 ? 8 : top_blob_unpacked.c % 4 == 0 ? 4 : 1; |
| 63 | #elif __AVX__ |
| 64 | if (dims == 1) out_elempack = top_blob_unpacked.w % 8 == 0 ? 8 : top_blob_unpacked.w % 4 == 0 ? 4 : 1; |
| 65 | if (dims == 2) out_elempack = top_blob_unpacked.h % 8 == 0 ? 8 : top_blob_unpacked.h % 4 == 0 ? 4 : 1; |
| 66 | if (dims == 3 || dims == 4) out_elempack = top_blob_unpacked.c % 8 == 0 ? 8 : top_blob_unpacked.c % 4 == 0 ? 4 : 1; |
| 67 | #else |
| 68 | if (dims == 1) out_elempack = top_blob_unpacked.w % 4 == 0 ? 4 : 1; |
| 69 | if (dims == 2) out_elempack = top_blob_unpacked.h % 4 == 0 ? 4 : 1; |
| 70 | if (dims == 3 || dims == 4) out_elempack = top_blob_unpacked.c % 4 == 0 ? 4 : 1; |
| 71 | #endif |
| 72 | } |
| 73 | #endif // __SSE2__ |
| 74 | convert_packing(top_blob_unpacked, top_blob, out_elempack, opt); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | if (ndim == 1) |
| 80 | { |
| 81 | // flatten |
| 82 | flatten(bottom_blob, top_blob, opt); |
| 83 | if (top_blob.empty()) |
| 84 | return -100; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int dims = bottom_blob.dims; |
| 90 | size_t elemsize = bottom_blob.elemsize; |
| 91 | |
| 92 | int total = bottom_blob.w * bottom_blob.h * bottom_blob.d * bottom_blob.c * elempack; |
| 93 | |
| 94 | if (ndim == 2) |
| 95 | { |
| 96 | int _w = w; |
| 97 | int _h = h; |
| 98 | |
| 99 | if (_w == 0) |
| 100 | _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w; |
| 101 | if (_h == 0) |
| 102 | _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h; |
| 103 | |
| 104 | if (_w == -1) |
| 105 | _w = total / _h; |
| 106 | if (_h == -1) |
| 107 | _h = total / _w; |
| 108 | |
| 109 | int out_elempack = 1; |
| 110 | #if __SSE2__ |
| 111 | if (opt.use_packing_layout) |
| 112 | { |
| 113 | #if __AVX512F__ |
| 114 | out_elempack = _h % 16 == 0 ? 16 : _h % 8 == 0 ? 8 : _h % 4 == 0 ? 4 : 1; |
| 115 | #elif __AVX__ |
| 116 | out_elempack = _h % 8 == 0 ? 8 : _h % 4 == 0 ? 4 : 1; |
| 117 | #else |
| 118 | out_elempack = _h % 4 == 0 ? 4 : 1; |
| 119 | #endif |
| 120 | } |
| 121 | #endif // __SSE2__ |
| 122 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 123 | |
| 124 | if (dims == 2 && bottom_blob.h * elempack == _h && elempack == out_elempack) |
| 125 | { |
| 126 | top_blob = bottom_blob; |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | if (out_elempack == 1) |
| 131 | { |
| 132 | // flatten |
| 133 | flatten(bottom_blob, top_blob, opt); |
| 134 | if (top_blob.empty()) |
| 135 | return -100; |
| 136 | |
| 137 | top_blob.dims = 2; |
| 138 | top_blob.w = _w; |
| 139 | top_blob.h = _h; |
| 140 | top_blob.cstep = (size_t)_w * _h; |
| 141 | top_blob.elemsize = out_elemsize; |
| 142 | top_blob.elempack = out_elempack; |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | // flatten |
| 148 | Mat bottom_blob_flattened = bottom_blob; |
| 149 | { |
| 150 | Option opt_flatten = opt; |
| 151 | opt_flatten.blob_allocator = opt.workspace_allocator; |
| 152 | |
| 153 | flatten(bottom_blob, bottom_blob_flattened, opt_flatten); |
| 154 | if (bottom_blob_flattened.empty()) |
| 155 | return -100; |
| 156 | } |
| 157 | |
| 158 | top_blob.create(_w, _h / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 159 | if (top_blob.empty()) |
| 160 | return -100; |
| 161 | |
| 162 | int outw = top_blob.w; |
| 163 | int outh = top_blob.h; |
| 164 | |
| 165 | #if __SSE2__ |
| 166 | #if __AVX__ |
| 167 | #if __AVX512F__ |
| 168 | if (out_elempack == 16) |
| 169 | { |
| 170 | #pragma omp parallel for num_threads(opt.num_threads) |
| 171 | for (int i = 0; i < outh; i++) |
| 172 | { |
| 173 | const float* ptr0 = (const float*)bottom_blob_flattened + outw * i * 16; |
| 174 | const float* ptr1 = (const float*)bottom_blob_flattened + outw * (i * 16 + 1); |
| 175 | const float* ptr2 = (const float*)bottom_blob_flattened + outw * (i * 16 + 2); |
| 176 | const float* ptr3 = (const float*)bottom_blob_flattened + outw * (i * 16 + 3); |
| 177 | const float* ptr4 = (const float*)bottom_blob_flattened + outw * (i * 16 + 4); |
| 178 | const float* ptr5 = (const float*)bottom_blob_flattened + outw * (i * 16 + 5); |
| 179 | const float* ptr6 = (const float*)bottom_blob_flattened + outw * (i * 16 + 6); |
| 180 | const float* ptr7 = (const float*)bottom_blob_flattened + outw * (i * 16 + 7); |
| 181 | const float* ptr8 = (const float*)bottom_blob_flattened + outw * (i * 16 + 8); |
| 182 | const float* ptr9 = (const float*)bottom_blob_flattened + outw * (i * 16 + 9); |
| 183 | const float* ptra = (const float*)bottom_blob_flattened + outw * (i * 16 + 10); |
| 184 | const float* ptrb = (const float*)bottom_blob_flattened + outw * (i * 16 + 11); |
| 185 | const float* ptrc = (const float*)bottom_blob_flattened + outw * (i * 16 + 12); |
| 186 | const float* ptrd = (const float*)bottom_blob_flattened + outw * (i * 16 + 13); |
| 187 | const float* ptre = (const float*)bottom_blob_flattened + outw * (i * 16 + 14); |
| 188 | const float* ptrf = (const float*)bottom_blob_flattened + outw * (i * 16 + 15); |
| 189 | float* outptr = top_blob.row(i); |
| 190 | |
| 191 | int j = 0; |
| 192 | for (; j + 15 < outw; j += 16) |
| 193 | { |
| 194 | __m512 _row0 = _mm512_loadu_ps(ptr0); |
| 195 | __m512 _row1 = _mm512_loadu_ps(ptr1); |
| 196 | __m512 _row2 = _mm512_loadu_ps(ptr2); |
| 197 | __m512 _row3 = _mm512_loadu_ps(ptr3); |
| 198 | __m512 _row4 = _mm512_loadu_ps(ptr4); |
| 199 | __m512 _row5 = _mm512_loadu_ps(ptr5); |
| 200 | __m512 _row6 = _mm512_loadu_ps(ptr6); |
| 201 | __m512 _row7 = _mm512_loadu_ps(ptr7); |
| 202 | __m512 _row8 = _mm512_loadu_ps(ptr8); |
| 203 | __m512 _row9 = _mm512_loadu_ps(ptr9); |
| 204 | __m512 _rowa = _mm512_loadu_ps(ptra); |
| 205 | __m512 _rowb = _mm512_loadu_ps(ptrb); |
| 206 | __m512 _rowc = _mm512_loadu_ps(ptrc); |
| 207 | __m512 _rowd = _mm512_loadu_ps(ptrd); |
| 208 | __m512 _rowe = _mm512_loadu_ps(ptre); |
| 209 | __m512 _rowf = _mm512_loadu_ps(ptrf); |
| 210 | |
| 211 | transpose16x16_ps(_row0, _row1, _row2, _row3, _row4, _row5, _row6, _row7, _row8, _row9, _rowa, _rowb, _rowc, _rowd, _rowe, _rowf); |
| 212 | |
| 213 | _mm512_storeu_ps(outptr, _row0); |
| 214 | _mm512_storeu_ps(outptr + 16, _row1); |
| 215 | _mm512_storeu_ps(outptr + 16 * 2, _row2); |
| 216 | _mm512_storeu_ps(outptr + 16 * 3, _row3); |
| 217 | _mm512_storeu_ps(outptr + 16 * 4, _row4); |
| 218 | _mm512_storeu_ps(outptr + 16 * 5, _row5); |
| 219 | _mm512_storeu_ps(outptr + 16 * 6, _row6); |
| 220 | _mm512_storeu_ps(outptr + 16 * 7, _row7); |
| 221 | _mm512_storeu_ps(outptr + 16 * 8, _row8); |
| 222 | _mm512_storeu_ps(outptr + 16 * 9, _row9); |
| 223 | _mm512_storeu_ps(outptr + 16 * 10, _rowa); |
| 224 | _mm512_storeu_ps(outptr + 16 * 11, _rowb); |
| 225 | _mm512_storeu_ps(outptr + 16 * 12, _rowc); |
| 226 | _mm512_storeu_ps(outptr + 16 * 13, _rowd); |
| 227 | _mm512_storeu_ps(outptr + 16 * 14, _rowe); |
| 228 | _mm512_storeu_ps(outptr + 16 * 15, _rowf); |
| 229 | |
| 230 | ptr0 += 16; |
| 231 | ptr1 += 16; |
| 232 | ptr2 += 16; |
| 233 | ptr3 += 16; |
| 234 | ptr4 += 16; |
| 235 | ptr5 += 16; |
| 236 | ptr6 += 16; |
| 237 | ptr7 += 16; |
| 238 | ptr8 += 16; |
| 239 | ptr9 += 16; |
| 240 | ptra += 16; |
| 241 | ptrb += 16; |
| 242 | ptrc += 16; |
| 243 | ptrd += 16; |
| 244 | ptre += 16; |
| 245 | ptrf += 16; |
| 246 | outptr += 256; |
| 247 | } |
| 248 | for (; j < outw; j++) |
| 249 | { |
| 250 | outptr[0] = *ptr0++; |
| 251 | outptr[1] = *ptr1++; |
| 252 | outptr[2] = *ptr2++; |
| 253 | outptr[3] = *ptr3++; |
| 254 | outptr[4] = *ptr4++; |
| 255 | outptr[5] = *ptr5++; |
| 256 | outptr[6] = *ptr6++; |
| 257 | outptr[7] = *ptr7++; |
| 258 | outptr[8] = *ptr8++; |
| 259 | outptr[9] = *ptr9++; |
| 260 | outptr[10] = *ptra++; |
| 261 | outptr[11] = *ptrb++; |
| 262 | outptr[12] = *ptrc++; |
| 263 | outptr[13] = *ptrd++; |
| 264 | outptr[14] = *ptre++; |
| 265 | outptr[15] = *ptrf++; |
| 266 | |
| 267 | outptr += 16; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | #endif // __AVX512F__ |
| 272 | |
| 273 | if (out_elempack == 8) |
| 274 | { |
| 275 | #pragma omp parallel for num_threads(opt.num_threads) |
| 276 | for (int i = 0; i < outh; i++) |
| 277 | { |
| 278 | const float* ptr0 = (const float*)bottom_blob_flattened + outw * i * 8; |
| 279 | const float* ptr1 = (const float*)bottom_blob_flattened + outw * (i * 8 + 1); |
| 280 | const float* ptr2 = (const float*)bottom_blob_flattened + outw * (i * 8 + 2); |
| 281 | const float* ptr3 = (const float*)bottom_blob_flattened + outw * (i * 8 + 3); |
| 282 | const float* ptr4 = (const float*)bottom_blob_flattened + outw * (i * 8 + 4); |
| 283 | const float* ptr5 = (const float*)bottom_blob_flattened + outw * (i * 8 + 5); |
| 284 | const float* ptr6 = (const float*)bottom_blob_flattened + outw * (i * 8 + 6); |
| 285 | const float* ptr7 = (const float*)bottom_blob_flattened + outw * (i * 8 + 7); |
| 286 | float* outptr = top_blob.row(i); |
| 287 | |
| 288 | int j = 0; |
| 289 | for (; j + 7 < outw; j += 8) |
| 290 | { |
| 291 | __m256 _row0 = _mm256_loadu_ps(ptr0); |
| 292 | __m256 _row1 = _mm256_loadu_ps(ptr1); |
| 293 | __m256 _row2 = _mm256_loadu_ps(ptr2); |
| 294 | __m256 _row3 = _mm256_loadu_ps(ptr3); |
| 295 | __m256 _row4 = _mm256_loadu_ps(ptr4); |
| 296 | __m256 _row5 = _mm256_loadu_ps(ptr5); |
| 297 | __m256 _row6 = _mm256_loadu_ps(ptr6); |
| 298 | __m256 _row7 = _mm256_loadu_ps(ptr7); |
| 299 | |
| 300 | transpose8x8_ps(_row0, _row1, _row2, _row3, _row4, _row5, _row6, _row7); |
| 301 | |
| 302 | _mm256_storeu_ps(outptr, _row0); |
| 303 | _mm256_storeu_ps(outptr + 8, _row1); |
| 304 | _mm256_storeu_ps(outptr + 16, _row2); |
| 305 | _mm256_storeu_ps(outptr + 24, _row3); |
| 306 | _mm256_storeu_ps(outptr + 32, _row4); |
| 307 | _mm256_storeu_ps(outptr + 40, _row5); |
| 308 | _mm256_storeu_ps(outptr + 48, _row6); |
| 309 | _mm256_storeu_ps(outptr + 56, _row7); |
| 310 | |
| 311 | ptr0 += 8; |
| 312 | ptr1 += 8; |
| 313 | ptr2 += 8; |
| 314 | ptr3 += 8; |
| 315 | ptr4 += 8; |
| 316 | ptr5 += 8; |
| 317 | ptr6 += 8; |
| 318 | ptr7 += 8; |
| 319 | outptr += 64; |
| 320 | } |
| 321 | for (; j < outw; j++) |
| 322 | { |
| 323 | outptr[0] = *ptr0++; |
| 324 | outptr[1] = *ptr1++; |
| 325 | outptr[2] = *ptr2++; |
| 326 | outptr[3] = *ptr3++; |
| 327 | outptr[4] = *ptr4++; |
| 328 | outptr[5] = *ptr5++; |
| 329 | outptr[6] = *ptr6++; |
| 330 | outptr[7] = *ptr7++; |
| 331 | |
| 332 | outptr += 8; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | #endif // __AVX__ |
| 337 | |
| 338 | if (out_elempack == 4) |
| 339 | { |
| 340 | #pragma omp parallel for num_threads(opt.num_threads) |
| 341 | for (int i = 0; i < outh; i++) |
| 342 | { |
| 343 | const float* ptr0 = (const float*)bottom_blob_flattened + outw * i * 4; |
| 344 | const float* ptr1 = (const float*)bottom_blob_flattened + outw * (i * 4 + 1); |
| 345 | const float* ptr2 = (const float*)bottom_blob_flattened + outw * (i * 4 + 2); |
| 346 | const float* ptr3 = (const float*)bottom_blob_flattened + outw * (i * 4 + 3); |
| 347 | float* outptr = top_blob.row(i); |
| 348 | |
| 349 | int j = 0; |
| 350 | for (; j + 3 < outw; j += 4) |
| 351 | { |
| 352 | __m128 _row0 = _mm_loadu_ps(ptr0); |
| 353 | __m128 _row1 = _mm_loadu_ps(ptr1); |
| 354 | __m128 _row2 = _mm_loadu_ps(ptr2); |
| 355 | __m128 _row3 = _mm_loadu_ps(ptr3); |
| 356 | |
| 357 | _MM_TRANSPOSE4_PS(_row0, _row1, _row2, _row3); |
| 358 | |
| 359 | _mm_storeu_ps(outptr, _row0); |
| 360 | _mm_storeu_ps(outptr + 4, _row1); |
| 361 | _mm_storeu_ps(outptr + 8, _row2); |
| 362 | _mm_storeu_ps(outptr + 12, _row3); |
| 363 | |
| 364 | ptr0 += 4; |
| 365 | ptr1 += 4; |
| 366 | ptr2 += 4; |
| 367 | ptr3 += 4; |
| 368 | outptr += 16; |
| 369 | } |
| 370 | for (; j < outw; j++) |
| 371 | { |
| 372 | outptr[0] = *ptr0++; |
| 373 | outptr[1] = *ptr1++; |
| 374 | outptr[2] = *ptr2++; |
| 375 | outptr[3] = *ptr3++; |
| 376 | |
| 377 | outptr += 4; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | #endif // __SSE2__ |
| 382 | } |
| 383 | |
| 384 | if (ndim == 3 || ndim == 4) |
| 385 | { |
| 386 | int _w = w; |
| 387 | int _h = h; |
| 388 | int _d = d; |
| 389 | int _c = c; |
| 390 | |
| 391 | if (ndim == 3) |
| 392 | { |
| 393 | if (_w == 0) |
| 394 | _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w; |
| 395 | if (_h == 0) |
| 396 | _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h; |
| 397 | if (_c == 0) |
| 398 | _c = dims == 3 ? bottom_blob.c * elempack : bottom_blob.c; |
| 399 | |
| 400 | if (_w == -1) |
| 401 | _w = total / _c / _h; |
| 402 | if (_h == -1) |
| 403 | _h = total / _c / _w; |
| 404 | if (_c == -1) |
| 405 | _c = total / _h / _w; |
| 406 | |
| 407 | _d = 1; |
| 408 | } |
| 409 | else // if (ndim == 4) |
| 410 | { |
| 411 | if (_w == 0) |
| 412 | _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w; |
| 413 | if (_h == 0) |
| 414 | _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h; |
| 415 | if (_d == 0) |
| 416 | _d = bottom_blob.d; |
| 417 | if (_c == 0) |
| 418 | _c = (dims == 3 || dims == 4) ? bottom_blob.c * elempack : bottom_blob.c; |
| 419 | |
| 420 | if (_w == -1) |
| 421 | _w = total / _c / _d / _h; |
| 422 | if (_h == -1) |
| 423 | _h = total / _c / _d / _w; |
| 424 | if (_d == -1) |
| 425 | _d = total / _c / _h / _w; |
| 426 | if (_c == -1) |
| 427 | _c = total / _d / _h / _w; |
| 428 | } |
| 429 | |
| 430 | int out_elempack = 1; |
| 431 | #if __SSE2__ |
| 432 | if (opt.use_packing_layout) |
| 433 | { |
| 434 | #if __AVX512F__ |
| 435 | out_elempack = _c % 16 == 0 ? 16 : _c % 8 == 0 ? 8 : _c % 4 == 0 ? 4 : 1; |
| 436 | #elif __AVX__ |
| 437 | out_elempack = _c % 8 == 0 ? 8 : _c % 4 == 0 ? 4 : 1; |
| 438 | #else |
| 439 | out_elempack = _c % 4 == 0 ? 4 : 1; |
| 440 | #endif |
| 441 | } |
| 442 | #endif // __SSE2__ |
| 443 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 444 | |
| 445 | if ((dims == 3 || dims == 4) && bottom_blob.c * elempack == _c && elempack == out_elempack) |
| 446 | { |
| 447 | top_blob = bottom_blob; |
| 448 | top_blob.dims = ndim; |
| 449 | top_blob.w = _w; |
| 450 | top_blob.h = _h; |
| 451 | top_blob.d = _d; |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | // flatten |
| 456 | Mat bottom_blob_flattened = bottom_blob; |
| 457 | { |
| 458 | Option opt_flatten = opt; |
| 459 | opt_flatten.blob_allocator = opt.workspace_allocator; |
| 460 | |
| 461 | flatten(bottom_blob, bottom_blob_flattened, opt_flatten); |
| 462 | if (bottom_blob_flattened.empty()) |
| 463 | return -100; |
| 464 | } |
| 465 | |
| 466 | if (ndim == 3) |
| 467 | { |
| 468 | top_blob.create(_w, _h, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 469 | } |
| 470 | else // if (ndim == 4) |
| 471 | { |
| 472 | top_blob.create(_w, _h, _d, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 473 | } |
| 474 | if (top_blob.empty()) |
| 475 | return -100; |
| 476 | |
| 477 | int size = top_blob.w * top_blob.h * top_blob.d; |
| 478 | |
| 479 | #if __SSE2__ |
| 480 | #if __AVX__ |
| 481 | #if __AVX512F__ |
| 482 | if (out_elempack == 16) |
| 483 | { |
| 484 | #pragma omp parallel for num_threads(opt.num_threads) |
| 485 | for (int q = 0; q < top_blob.c; q++) |
| 486 | { |
| 487 | const float* ptr0 = (const float*)bottom_blob_flattened + size * q * 16; |
| 488 | const float* ptr1 = (const float*)bottom_blob_flattened + size * (q * 16 + 1); |
| 489 | const float* ptr2 = (const float*)bottom_blob_flattened + size * (q * 16 + 2); |
| 490 | const float* ptr3 = (const float*)bottom_blob_flattened + size * (q * 16 + 3); |
| 491 | const float* ptr4 = (const float*)bottom_blob_flattened + size * (q * 16 + 4); |
| 492 | const float* ptr5 = (const float*)bottom_blob_flattened + size * (q * 16 + 5); |
| 493 | const float* ptr6 = (const float*)bottom_blob_flattened + size * (q * 16 + 6); |
| 494 | const float* ptr7 = (const float*)bottom_blob_flattened + size * (q * 16 + 7); |
| 495 | const float* ptr8 = (const float*)bottom_blob_flattened + size * (q * 16 + 8); |
| 496 | const float* ptr9 = (const float*)bottom_blob_flattened + size * (q * 16 + 9); |
| 497 | const float* ptra = (const float*)bottom_blob_flattened + size * (q * 16 + 10); |
| 498 | const float* ptrb = (const float*)bottom_blob_flattened + size * (q * 16 + 11); |
| 499 | const float* ptrc = (const float*)bottom_blob_flattened + size * (q * 16 + 12); |
| 500 | const float* ptrd = (const float*)bottom_blob_flattened + size * (q * 16 + 13); |
| 501 | const float* ptre = (const float*)bottom_blob_flattened + size * (q * 16 + 14); |
| 502 | const float* ptrf = (const float*)bottom_blob_flattened + size * (q * 16 + 15); |
| 503 | float* outptr = top_blob.channel(q); |
| 504 | |
| 505 | int i = 0; |
| 506 | for (; i + 15 < size; i += 16) |
| 507 | { |
| 508 | __m512 _row0 = _mm512_loadu_ps(ptr0); |
| 509 | __m512 _row1 = _mm512_loadu_ps(ptr1); |
| 510 | __m512 _row2 = _mm512_loadu_ps(ptr2); |
| 511 | __m512 _row3 = _mm512_loadu_ps(ptr3); |
| 512 | __m512 _row4 = _mm512_loadu_ps(ptr4); |
| 513 | __m512 _row5 = _mm512_loadu_ps(ptr5); |
| 514 | __m512 _row6 = _mm512_loadu_ps(ptr6); |
| 515 | __m512 _row7 = _mm512_loadu_ps(ptr7); |
| 516 | __m512 _row8 = _mm512_loadu_ps(ptr8); |
| 517 | __m512 _row9 = _mm512_loadu_ps(ptr9); |
| 518 | __m512 _rowa = _mm512_loadu_ps(ptra); |
| 519 | __m512 _rowb = _mm512_loadu_ps(ptrb); |
| 520 | __m512 _rowc = _mm512_loadu_ps(ptrc); |
| 521 | __m512 _rowd = _mm512_loadu_ps(ptrd); |
| 522 | __m512 _rowe = _mm512_loadu_ps(ptre); |
| 523 | __m512 _rowf = _mm512_loadu_ps(ptrf); |
| 524 | |
| 525 | transpose16x16_ps(_row0, _row1, _row2, _row3, _row4, _row5, _row6, _row7, _row8, _row9, _rowa, _rowb, _rowc, _rowd, _rowe, _rowf); |
| 526 | |
| 527 | _mm512_storeu_ps(outptr, _row0); |
| 528 | _mm512_storeu_ps(outptr + 16, _row1); |
| 529 | _mm512_storeu_ps(outptr + 16 * 2, _row2); |
| 530 | _mm512_storeu_ps(outptr + 16 * 3, _row3); |
| 531 | _mm512_storeu_ps(outptr + 16 * 4, _row4); |
| 532 | _mm512_storeu_ps(outptr + 16 * 5, _row5); |
| 533 | _mm512_storeu_ps(outptr + 16 * 6, _row6); |
| 534 | _mm512_storeu_ps(outptr + 16 * 7, _row7); |
| 535 | _mm512_storeu_ps(outptr + 16 * 8, _row8); |
| 536 | _mm512_storeu_ps(outptr + 16 * 9, _row9); |
| 537 | _mm512_storeu_ps(outptr + 16 * 10, _rowa); |
| 538 | _mm512_storeu_ps(outptr + 16 * 11, _rowb); |
| 539 | _mm512_storeu_ps(outptr + 16 * 12, _rowc); |
| 540 | _mm512_storeu_ps(outptr + 16 * 13, _rowd); |
| 541 | _mm512_storeu_ps(outptr + 16 * 14, _rowe); |
| 542 | _mm512_storeu_ps(outptr + 16 * 15, _rowf); |
| 543 | |
| 544 | ptr0 += 16; |
| 545 | ptr1 += 16; |
| 546 | ptr2 += 16; |
| 547 | ptr3 += 16; |
| 548 | ptr4 += 16; |
| 549 | ptr5 += 16; |
| 550 | ptr6 += 16; |
| 551 | ptr7 += 16; |
| 552 | ptr8 += 16; |
| 553 | ptr9 += 16; |
| 554 | ptra += 16; |
| 555 | ptrb += 16; |
| 556 | ptrc += 16; |
| 557 | ptrd += 16; |
| 558 | ptre += 16; |
| 559 | ptrf += 16; |
| 560 | outptr += 256; |
| 561 | } |
| 562 | for (; i < size; i++) |
| 563 | { |
| 564 | outptr[0] = *ptr0++; |
| 565 | outptr[1] = *ptr1++; |
| 566 | outptr[2] = *ptr2++; |
| 567 | outptr[3] = *ptr3++; |
| 568 | outptr[4] = *ptr4++; |
| 569 | outptr[5] = *ptr5++; |
| 570 | outptr[6] = *ptr6++; |
| 571 | outptr[7] = *ptr7++; |
| 572 | outptr[8] = *ptr8++; |
| 573 | outptr[9] = *ptr9++; |
| 574 | outptr[10] = *ptra++; |
| 575 | outptr[11] = *ptrb++; |
| 576 | outptr[12] = *ptrc++; |
| 577 | outptr[13] = *ptrd++; |
| 578 | outptr[14] = *ptre++; |
| 579 | outptr[15] = *ptrf++; |
| 580 | |
| 581 | outptr += 16; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | #endif // __AVX512F__ |
| 586 | |
| 587 | if (out_elempack == 8) |
| 588 | { |
| 589 | #pragma omp parallel for num_threads(opt.num_threads) |
| 590 | for (int q = 0; q < top_blob.c; q++) |
| 591 | { |
| 592 | const float* ptr0 = (const float*)bottom_blob_flattened + size * q * 8; |
| 593 | const float* ptr1 = (const float*)bottom_blob_flattened + size * (q * 8 + 1); |
| 594 | const float* ptr2 = (const float*)bottom_blob_flattened + size * (q * 8 + 2); |
| 595 | const float* ptr3 = (const float*)bottom_blob_flattened + size * (q * 8 + 3); |
| 596 | const float* ptr4 = (const float*)bottom_blob_flattened + size * (q * 8 + 4); |
| 597 | const float* ptr5 = (const float*)bottom_blob_flattened + size * (q * 8 + 5); |
| 598 | const float* ptr6 = (const float*)bottom_blob_flattened + size * (q * 8 + 6); |
| 599 | const float* ptr7 = (const float*)bottom_blob_flattened + size * (q * 8 + 7); |
| 600 | float* outptr = top_blob.channel(q); |
| 601 | |
| 602 | int i = 0; |
| 603 | for (; i + 7 < size; i += 8) |
| 604 | { |
| 605 | __m256 _row0 = _mm256_loadu_ps(ptr0); |
| 606 | __m256 _row1 = _mm256_loadu_ps(ptr1); |
| 607 | __m256 _row2 = _mm256_loadu_ps(ptr2); |
| 608 | __m256 _row3 = _mm256_loadu_ps(ptr3); |
| 609 | __m256 _row4 = _mm256_loadu_ps(ptr4); |
| 610 | __m256 _row5 = _mm256_loadu_ps(ptr5); |
| 611 | __m256 _row6 = _mm256_loadu_ps(ptr6); |
| 612 | __m256 _row7 = _mm256_loadu_ps(ptr7); |
| 613 | |
| 614 | transpose8x8_ps(_row0, _row1, _row2, _row3, _row4, _row5, _row6, _row7); |
| 615 | |
| 616 | _mm256_storeu_ps(outptr, _row0); |
| 617 | _mm256_storeu_ps(outptr + 8, _row1); |
| 618 | _mm256_storeu_ps(outptr + 16, _row2); |
| 619 | _mm256_storeu_ps(outptr + 24, _row3); |
| 620 | _mm256_storeu_ps(outptr + 32, _row4); |
| 621 | _mm256_storeu_ps(outptr + 40, _row5); |
| 622 | _mm256_storeu_ps(outptr + 48, _row6); |
| 623 | _mm256_storeu_ps(outptr + 56, _row7); |
| 624 | |
| 625 | ptr0 += 8; |
| 626 | ptr1 += 8; |
| 627 | ptr2 += 8; |
| 628 | ptr3 += 8; |
| 629 | ptr4 += 8; |
| 630 | ptr5 += 8; |
| 631 | ptr6 += 8; |
| 632 | ptr7 += 8; |
| 633 | outptr += 64; |
| 634 | } |
| 635 | for (; i < size; i++) |
| 636 | { |
| 637 | outptr[0] = *ptr0++; |
| 638 | outptr[1] = *ptr1++; |
| 639 | outptr[2] = *ptr2++; |
| 640 | outptr[3] = *ptr3++; |
| 641 | outptr[4] = *ptr4++; |
| 642 | outptr[5] = *ptr5++; |
| 643 | outptr[6] = *ptr6++; |
| 644 | outptr[7] = *ptr7++; |
| 645 | |
| 646 | outptr += 8; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | #endif // __AVX__ |
| 651 | |
| 652 | if (out_elempack == 4) |
| 653 | { |
| 654 | #pragma omp parallel for num_threads(opt.num_threads) |
| 655 | for (int q = 0; q < top_blob.c; q++) |
| 656 | { |
| 657 | const float* ptr0 = (const float*)bottom_blob_flattened + size * q * 4; |
| 658 | const float* ptr1 = (const float*)bottom_blob_flattened + size * (q * 4 + 1); |
| 659 | const float* ptr2 = (const float*)bottom_blob_flattened + size * (q * 4 + 2); |
| 660 | const float* ptr3 = (const float*)bottom_blob_flattened + size * (q * 4 + 3); |
| 661 | float* outptr = top_blob.channel(q); |
| 662 | |
| 663 | int i = 0; |
| 664 | for (; i + 3 < size; i += 4) |
| 665 | { |
| 666 | __m128 _row0 = _mm_loadu_ps(ptr0); |
| 667 | __m128 _row1 = _mm_loadu_ps(ptr1); |
| 668 | __m128 _row2 = _mm_loadu_ps(ptr2); |
| 669 | __m128 _row3 = _mm_loadu_ps(ptr3); |
| 670 | |
| 671 | _MM_TRANSPOSE4_PS(_row0, _row1, _row2, _row3); |
| 672 | |
| 673 | _mm_storeu_ps(outptr, _row0); |
| 674 | _mm_storeu_ps(outptr + 4, _row1); |
| 675 | _mm_storeu_ps(outptr + 8, _row2); |
| 676 | _mm_storeu_ps(outptr + 12, _row3); |
| 677 | |
| 678 | ptr0 += 4; |
| 679 | ptr1 += 4; |
| 680 | ptr2 += 4; |
| 681 | ptr3 += 4; |
| 682 | outptr += 16; |
| 683 | } |
| 684 | for (; i < size; i++) |
| 685 | { |
| 686 | outptr[0] = *ptr0++; |
| 687 | outptr[1] = *ptr1++; |
| 688 | outptr[2] = *ptr2++; |
| 689 | outptr[3] = *ptr3++; |
| 690 | |
| 691 | outptr += 4; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | #endif // __SSE2__ |
| 696 | |
| 697 | if (out_elempack == 1) |
| 698 | { |
| 699 | #pragma omp parallel for num_threads(opt.num_threads) |
| 700 | for (int q = 0; q < top_blob.c; q++) |
| 701 | { |
| 702 | const float* ptr = (const float*)bottom_blob_flattened + size * q; |
| 703 | float* outptr = top_blob.channel(q); |
| 704 | |
| 705 | int i = 0; |
| 706 | #if __SSE2__ |
| 707 | #if __AVX__ |
| 708 | for (; i + 7 < size; i += 8) |
| 709 | { |
| 710 | __m256 _v = _mm256_loadu_ps(ptr); |
| 711 | _mm256_storeu_ps(outptr, _v); |
| 712 | ptr += 8; |
| 713 | outptr += 8; |
| 714 | } |
| 715 | #endif |
| 716 | for (; i + 3 < size; i += 4) |
| 717 | { |
| 718 | __m128 _v = _mm_loadu_ps(ptr); |
| 719 | _mm_storeu_ps(outptr, _v); |
| 720 | ptr += 4; |
| 721 | outptr += 4; |
| 722 | } |
| 723 | #endif // __SSE2__ |
| 724 | for (; i < size; i++) |
| 725 | { |
| 726 | *outptr++ = *ptr++; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | } // namespace ncnn |