| 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 "concat_arm.h" |
| 16 | |
| 17 | #include "cpu.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | Concat_arm::Concat_arm() |
| 22 | { |
| 23 | #if __ARM_NEON |
| 24 | support_packing = true; |
| 25 | #if NCNN_ARM82 |
| 26 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 27 | #endif |
| 28 | #endif // __ARM_NEON |
| 29 | |
| 30 | #if NCNN_BF16 |
| 31 | support_bf16_storage = true; |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | int Concat_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 36 | { |
| 37 | int elembits = bottom_blobs[0].elembits(); |
| 38 | |
| 39 | #if NCNN_ARM82 |
| 40 | if (support_packing && opt.use_fp16_storage && elembits == 16) |
| 41 | return forward_bf16s_fp16s(bottom_blobs, top_blobs, opt); |
| 42 | #endif |
| 43 | |
| 44 | #if NCNN_BF16 |
| 45 | if (opt.use_bf16_storage && elembits == 16) |
| 46 | return forward_bf16s_fp16s(bottom_blobs, top_blobs, opt); |
| 47 | #endif |
| 48 | |
| 49 | int dims = bottom_blobs[0].dims; |
| 50 | int positive_axis = axis < 0 ? dims + axis : axis; |
| 51 | |
| 52 | if (dims == 1) // positive_axis == 0 |
| 53 | { |
| 54 | // concat vector |
| 55 | // total length |
| 56 | size_t elemsize = bottom_blobs[0].elemsize; |
| 57 | int elempack = bottom_blobs[0].elempack; |
| 58 | int top_w = 0; |
| 59 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 60 | { |
| 61 | const Mat& bottom_blob = bottom_blobs[b]; |
| 62 | top_w += bottom_blob.w * bottom_blob.elempack; |
| 63 | } |
| 64 | |
| 65 | int out_elempack = opt.use_packing_layout && top_w % 4 == 0 ? 4 : 1; |
| 66 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 67 | |
| 68 | Mat& top_blob = top_blobs[0]; |
| 69 | top_blob.create(top_w / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 70 | if (top_blob.empty()) |
| 71 | return -100; |
| 72 | |
| 73 | float* outptr = top_blob; |
| 74 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 75 | { |
| 76 | const Mat& bottom_blob = bottom_blobs[b]; |
| 77 | |
| 78 | const float* ptr = bottom_blob; |
| 79 | memcpy(outptr, ptr, bottom_blob.w * bottom_blob.elemsize); |
| 80 | |
| 81 | outptr += bottom_blob.w * bottom_blob.elempack; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (dims == 2 && positive_axis == 0) |
| 86 | { |
| 87 | // concat image |
| 88 | int w = bottom_blobs[0].w; |
| 89 | |
| 90 | // total height |
| 91 | size_t elemsize = bottom_blobs[0].elemsize; |
| 92 | int elempack = bottom_blobs[0].elempack; |
| 93 | int top_h = 0; |
| 94 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 95 | { |
| 96 | const Mat& bottom_blob = bottom_blobs[b]; |
| 97 | elemsize = std::min(elemsize, bottom_blob.elemsize); |
| 98 | elempack = std::min(elempack, bottom_blob.elempack); |
| 99 | top_h += bottom_blob.h * bottom_blob.elempack; |
| 100 | } |
| 101 | |
| 102 | int out_elempack = opt.use_packing_layout && top_h % 4 == 0 ? 4 : 1; |
| 103 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 104 | |
| 105 | Mat& top_blob = top_blobs[0]; |
| 106 | top_blob.create(w, top_h / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 107 | if (top_blob.empty()) |
| 108 | return -100; |
| 109 | |
| 110 | Mat top_blob_unpacked = top_blob; |
| 111 | if (elempack < out_elempack) |
| 112 | { |
| 113 | top_blob_unpacked.create(w, top_h / elempack, elemsize, elempack, opt.workspace_allocator); |
| 114 | if (top_blob_unpacked.empty()) |
| 115 | return -100; |
| 116 | } |
| 117 | |
| 118 | float* outptr = top_blob_unpacked; |
| 119 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 120 | { |
| 121 | const Mat& bottom_blob = bottom_blobs[b]; |
| 122 | |
| 123 | if (bottom_blob.elempack == 4 && elempack == 1) |
| 124 | { |
| 125 | for (int i = 0; i < bottom_blob.h; i++) |
| 126 | { |
| 127 | const float* r0 = bottom_blob.row(i); |
| 128 | |
| 129 | float* outptr0 = outptr; |
| 130 | float* outptr1 = outptr + w; |
| 131 | float* outptr2 = outptr + w * 2; |
| 132 | float* outptr3 = outptr + w * 3; |
| 133 | |
| 134 | for (int j = 0; j < w; j++) |
| 135 | { |
| 136 | *outptr0++ = r0[0]; |
| 137 | *outptr1++ = r0[1]; |
| 138 | *outptr2++ = r0[2]; |
| 139 | *outptr3++ = r0[3]; |
| 140 | |
| 141 | r0 += 4; |
| 142 | } |
| 143 | |
| 144 | outptr += w * 4; |
| 145 | } |
| 146 | } |
| 147 | else // if (bottom_blob.elempack == 1 && elempack == 1) if (bottom_blob.elempack == 4 && elempack == 4) |
| 148 | { |
| 149 | int size = w * bottom_blob.h; |
| 150 | |
| 151 | const float* ptr = bottom_blob; |
| 152 | memcpy(outptr, ptr, size * bottom_blob.elemsize); |
| 153 | |
| 154 | outptr += size * bottom_blob.elempack; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // packing |
| 159 | if (elempack < out_elempack) |
| 160 | { |
| 161 | convert_packing(top_blob_unpacked, top_blob, out_elempack, opt); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (dims == 2 && positive_axis == 1) |
| 166 | { |
| 167 | // interleave image row |
| 168 | int h = bottom_blobs[0].h; |
| 169 | size_t elemsize = bottom_blobs[0].elemsize; |
| 170 | int elempack = bottom_blobs[0].elempack; |
| 171 | |
| 172 | // total width |
| 173 | int top_w = 0; |
| 174 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 175 | { |
| 176 | const Mat& bottom_blob = bottom_blobs[b]; |
| 177 | top_w += bottom_blob.w; |
| 178 | } |
| 179 | |
| 180 | Mat& top_blob = top_blobs[0]; |
| 181 | top_blob.create(top_w, h, elemsize, elempack, opt.blob_allocator); |
| 182 | if (top_blob.empty()) |
| 183 | return -100; |
| 184 | |
| 185 | #pragma omp parallel for num_threads(opt.num_threads) |
| 186 | for (int i = 0; i < h; i++) |
| 187 | { |
| 188 | float* outptr = top_blob.row(i); |
| 189 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 190 | { |
| 191 | const Mat& bottom_blob = bottom_blobs[b]; |
| 192 | |
| 193 | const float* ptr = bottom_blob.row(i); |
| 194 | memcpy(outptr, ptr, bottom_blob.w * elemsize); |
| 195 | |
| 196 | outptr += bottom_blob.w * elempack; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if ((dims == 3 || dims == 4) && positive_axis == 0) |
| 202 | { |
| 203 | // concat dim |
| 204 | int w = bottom_blobs[0].w; |
| 205 | int h = bottom_blobs[0].h; |
| 206 | int d = bottom_blobs[0].d; |
| 207 | |
| 208 | // total channels |
| 209 | size_t elemsize = bottom_blobs[0].elemsize; |
| 210 | int elempack = bottom_blobs[0].elempack; |
| 211 | int top_channels = 0; |
| 212 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 213 | { |
| 214 | const Mat& bottom_blob = bottom_blobs[b]; |
| 215 | elemsize = std::min(elemsize, bottom_blob.elemsize); |
| 216 | elempack = std::min(elempack, bottom_blob.elempack); |
| 217 | top_channels += bottom_blob.c * bottom_blob.elempack; |
| 218 | } |
| 219 | |
| 220 | int out_elempack = opt.use_packing_layout && top_channels % 4 == 0 ? 4 : 1; |
| 221 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 222 | |
| 223 | Mat& top_blob = top_blobs[0]; |
| 224 | top_blob.create(w, h, d, top_channels / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 225 | if (top_blob.empty()) |
| 226 | return -100; |
| 227 | |
| 228 | top_blob.dims = dims; |
| 229 | |
| 230 | Mat top_blob_unpacked = top_blob; |
| 231 | if (elempack < out_elempack) |
| 232 | { |
| 233 | top_blob_unpacked.create(w, h, d, top_channels / elempack, elemsize, elempack, opt.workspace_allocator); |
| 234 | if (top_blob_unpacked.empty()) |
| 235 | return -100; |
| 236 | |
| 237 | top_blob_unpacked.dims = dims; |
| 238 | } |
| 239 | |
| 240 | int p = 0; |
| 241 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 242 | { |
| 243 | const Mat& bottom_blob = bottom_blobs[b]; |
| 244 | |
| 245 | if (bottom_blob.elempack == 4 && elempack == 1) |
| 246 | { |
| 247 | int size = bottom_blob.w * bottom_blob.h * bottom_blob.d; |
| 248 | |
| 249 | for (int q = 0; q < bottom_blob.c; q++) |
| 250 | { |
| 251 | const float* r0 = bottom_blob.channel(q); |
| 252 | |
| 253 | float* outptr0 = top_blob_unpacked.channel(p); |
| 254 | float* outptr1 = top_blob_unpacked.channel(p + 1); |
| 255 | float* outptr2 = top_blob_unpacked.channel(p + 2); |
| 256 | float* outptr3 = top_blob_unpacked.channel(p + 3); |
| 257 | |
| 258 | for (int i = 0; i < size; i++) |
| 259 | { |
| 260 | *outptr0++ = r0[0]; |
| 261 | *outptr1++ = r0[1]; |
| 262 | *outptr2++ = r0[2]; |
| 263 | *outptr3++ = r0[3]; |
| 264 | |
| 265 | r0 += 4; |
| 266 | } |
| 267 | |
| 268 | p += 4; |
| 269 | } |
| 270 | } |
| 271 | else // if (bottom_blob.elempack == 1 && elempack == 1) if (bottom_blob.elempack == 4 && elempack == 4) |
| 272 | { |
| 273 | int size = bottom_blob.total(); |
| 274 | |
| 275 | const float* ptr = bottom_blob; |
| 276 | float* outptr = top_blob_unpacked.channel(p); |
| 277 | memcpy(outptr, ptr, size * bottom_blob.elemsize); |
| 278 | |
| 279 | p += bottom_blob.c; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // packing |
| 284 | if (elempack < out_elempack) |
| 285 | { |
| 286 | convert_packing(top_blob_unpacked, top_blob, out_elempack, opt); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if ((dims == 3 && positive_axis == 1) || (dims == 4 && positive_axis == 2)) |
| 291 | { |
| 292 | // interleave dim height |
| 293 | int w = bottom_blobs[0].w; |
| 294 | int d = bottom_blobs[0].d; |
| 295 | int channels = bottom_blobs[0].c; |
| 296 | size_t elemsize = bottom_blobs[0].elemsize; |
| 297 | int elempack = bottom_blobs[0].elempack; |
| 298 | |
| 299 | // total height |
| 300 | int top_h = 0; |
| 301 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 302 | { |
| 303 | const Mat& bottom_blob = bottom_blobs[b]; |
| 304 | top_h += bottom_blob.h; |
| 305 | } |
| 306 | |
| 307 | Mat& top_blob = top_blobs[0]; |
| 308 | top_blob.create(w, top_h, d, channels, elemsize, elempack, opt.blob_allocator); |
| 309 | if (top_blob.empty()) |
| 310 | return -100; |
| 311 | |
| 312 | top_blob.dims = dims; |
| 313 | |
| 314 | #pragma omp parallel for num_threads(opt.num_threads) |
| 315 | for (int q = 0; q < channels; q++) |
| 316 | { |
| 317 | float* outptr = top_blob.channel(q); |
| 318 | |
| 319 | for (int i = 0; i < d; i++) |
| 320 | { |
| 321 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 322 | { |
| 323 | const Mat& bottom_blob = bottom_blobs[b]; |
| 324 | |
| 325 | int size = bottom_blob.w * bottom_blob.h; |
| 326 | |
| 327 | const float* ptr = bottom_blob.channel(q).depth(i); |
| 328 | memcpy(outptr, ptr, size * elemsize); |
| 329 | |
| 330 | outptr += size * elempack; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if ((dims == 3 && positive_axis == 2) || (dims == 4 && positive_axis == 3)) |
| 337 | { |
| 338 | // interleave dim width |
| 339 | int h = bottom_blobs[0].h; |
| 340 | int d = bottom_blobs[0].d; |
| 341 | int channels = bottom_blobs[0].c; |
| 342 | size_t elemsize = bottom_blobs[0].elemsize; |
| 343 | int elempack = bottom_blobs[0].elempack; |
| 344 | |
| 345 | // total height |
| 346 | int top_w = 0; |
| 347 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 348 | { |
| 349 | const Mat& bottom_blob = bottom_blobs[b]; |
| 350 | top_w += bottom_blob.w; |
| 351 | } |
| 352 | |
| 353 | Mat& top_blob = top_blobs[0]; |
| 354 | top_blob.create(top_w, h, d, channels, elemsize, elempack, opt.blob_allocator); |
| 355 | if (top_blob.empty()) |
| 356 | return -100; |
| 357 | |
| 358 | top_blob.dims = dims; |
| 359 | |
| 360 | #pragma omp parallel for num_threads(opt.num_threads) |
| 361 | for (int q = 0; q < channels; q++) |
| 362 | { |
| 363 | float* outptr = top_blob.channel(q); |
| 364 | |
| 365 | for (int i = 0; i < d; i++) |
| 366 | { |
| 367 | for (int j = 0; j < h; j++) |
| 368 | { |
| 369 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 370 | { |
| 371 | const Mat& bottom_blob = bottom_blobs[b]; |
| 372 | |
| 373 | const float* ptr = bottom_blob.channel(q).depth(i).row(j); |
| 374 | memcpy(outptr, ptr, bottom_blob.w * elemsize); |
| 375 | |
| 376 | outptr += bottom_blob.w * elempack; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (dims == 4 && positive_axis == 1) |
| 384 | { |
| 385 | // interleave dim depth |
| 386 | int w = bottom_blobs[0].w; |
| 387 | int h = bottom_blobs[0].h; |
| 388 | int channels = bottom_blobs[0].c; |
| 389 | size_t elemsize = bottom_blobs[0].elemsize; |
| 390 | int elempack = bottom_blobs[0].elempack; |
| 391 | |
| 392 | // total depth |
| 393 | int top_d = 0; |
| 394 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 395 | { |
| 396 | const Mat& bottom_blob = bottom_blobs[b]; |
| 397 | top_d += bottom_blob.d; |
| 398 | } |
| 399 | |
| 400 | Mat& top_blob = top_blobs[0]; |
| 401 | top_blob.create(w, h, top_d, channels, elemsize, elempack, opt.blob_allocator); |
| 402 | if (top_blob.empty()) |
| 403 | return -100; |
| 404 | |
| 405 | #pragma omp parallel for num_threads(opt.num_threads) |
| 406 | for (int q = 0; q < channels; q++) |
| 407 | { |
| 408 | float* outptr = top_blob.channel(q); |
| 409 | |
| 410 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 411 | { |
| 412 | const Mat& bottom_blob = bottom_blobs[b]; |
| 413 | |
| 414 | int size = bottom_blob.w * bottom_blob.h * bottom_blob.d; |
| 415 | |
| 416 | const float* ptr = bottom_blob.channel(q); |
| 417 | memcpy(outptr, ptr, size * elemsize); |
| 418 | |
| 419 | outptr += size * elempack; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int Concat_arm::forward_bf16s_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 428 | { |
| 429 | int dims = bottom_blobs[0].dims; |
| 430 | int positive_axis = axis < 0 ? dims + axis : axis; |
| 431 | |
| 432 | if (dims == 1) // positive_axis == 0 |
| 433 | { |
| 434 | // concat vector |
| 435 | // total length |
| 436 | size_t elemsize = bottom_blobs[0].elemsize; |
| 437 | int elempack = bottom_blobs[0].elempack; |
| 438 | int top_w = 0; |
| 439 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 440 | { |
| 441 | const Mat& bottom_blob = bottom_blobs[b]; |
| 442 | top_w += bottom_blob.w * bottom_blob.elempack; |
| 443 | } |
| 444 | |
| 445 | int out_elempack = 1; |
| 446 | if (opt.use_packing_layout) |
| 447 | { |
| 448 | #if NCNN_ARM82 |
| 449 | out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && top_w % 8 == 0 ? 8 : top_w % 4 == 0 ? 4 : 1; |
| 450 | #else |
| 451 | out_elempack = top_w % 4 == 0 ? 4 : 1; |
| 452 | #endif |
| 453 | } |
| 454 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 455 | |
| 456 | Mat& top_blob = top_blobs[0]; |
| 457 | top_blob.create(top_w / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 458 | if (top_blob.empty()) |
| 459 | return -100; |
| 460 | |
| 461 | unsigned short* outptr = top_blob; |
| 462 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 463 | { |
| 464 | const Mat& bottom_blob = bottom_blobs[b]; |
| 465 | |
| 466 | const unsigned short* ptr = bottom_blob; |
| 467 | memcpy(outptr, ptr, bottom_blob.w * bottom_blob.elemsize); |
| 468 | |
| 469 | outptr += bottom_blob.w * bottom_blob.elempack; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (dims == 2 && positive_axis == 0) |
| 474 | { |
| 475 | // concat image |
| 476 | int w = bottom_blobs[0].w; |
| 477 | |
| 478 | // total height |
| 479 | size_t elemsize = bottom_blobs[0].elemsize; |
| 480 | int elempack = bottom_blobs[0].elempack; |
| 481 | int top_h = 0; |
| 482 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 483 | { |
| 484 | const Mat& bottom_blob = bottom_blobs[b]; |
| 485 | elemsize = std::min(elemsize, bottom_blob.elemsize); |
| 486 | elempack = std::min(elempack, bottom_blob.elempack); |
| 487 | top_h += bottom_blob.h * bottom_blob.elempack; |
| 488 | } |
| 489 | |
| 490 | int out_elempack = 1; |
| 491 | if (opt.use_packing_layout) |
| 492 | { |
| 493 | #if NCNN_ARM82 |
| 494 | out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && top_h % 8 == 0 ? 8 : top_h % 4 == 0 ? 4 : 1; |
| 495 | #else |
| 496 | out_elempack = top_h % 4 == 0 ? 4 : 1; |
| 497 | #endif |
| 498 | } |
| 499 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 500 | |
| 501 | Mat& top_blob = top_blobs[0]; |
| 502 | top_blob.create(w, top_h / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 503 | if (top_blob.empty()) |
| 504 | return -100; |
| 505 | |
| 506 | Mat top_blob_unpacked = top_blob; |
| 507 | if (elempack < out_elempack) |
| 508 | { |
| 509 | top_blob_unpacked.create(w, top_h / elempack, elemsize, elempack, opt.workspace_allocator); |
| 510 | if (top_blob_unpacked.empty()) |
| 511 | return -100; |
| 512 | } |
| 513 | |
| 514 | unsigned short* outptr = top_blob_unpacked; |
| 515 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 516 | { |
| 517 | const Mat& bottom_blob = bottom_blobs[b]; |
| 518 | |
| 519 | #if NCNN_ARM82 |
| 520 | if (bottom_blob.elempack == 8 && elempack == 4) |
| 521 | { |
| 522 | for (int i = 0; i < bottom_blob.h; i++) |
| 523 | { |
| 524 | const unsigned short* r0 = bottom_blob.row<const unsigned short>(i); |
| 525 | |
| 526 | unsigned short* outptr0 = outptr; |
| 527 | unsigned short* outptr1 = outptr + w * 4; |
| 528 | |
| 529 | for (int j = 0; j < w; j++) |
| 530 | { |
| 531 | outptr0[0] = r0[0]; |
| 532 | outptr0[1] = r0[1]; |
| 533 | outptr0[2] = r0[2]; |
| 534 | outptr0[3] = r0[3]; |
| 535 | outptr1[0] = r0[4]; |
| 536 | outptr1[1] = r0[5]; |
| 537 | outptr1[2] = r0[6]; |
| 538 | outptr1[3] = r0[7]; |
| 539 | |
| 540 | outptr0 += 4; |
| 541 | outptr1 += 4; |
| 542 | r0 += 8; |
| 543 | } |
| 544 | |
| 545 | outptr += w * 8; |
| 546 | } |
| 547 | } |
| 548 | if (bottom_blob.elempack == 8 && elempack == 1) |
| 549 | { |
| 550 | for (int i = 0; i < bottom_blob.h; i++) |
| 551 | { |
| 552 | const unsigned short* r0 = bottom_blob.row<const unsigned short>(i); |
| 553 | |
| 554 | unsigned short* outptr0 = outptr; |
| 555 | unsigned short* outptr1 = outptr + w; |
| 556 | unsigned short* outptr2 = outptr + w * 2; |
| 557 | unsigned short* outptr3 = outptr + w * 3; |
| 558 | unsigned short* outptr4 = outptr + w * 4; |
| 559 | unsigned short* outptr5 = outptr + w * 5; |
| 560 | unsigned short* outptr6 = outptr + w * 6; |
| 561 | unsigned short* outptr7 = outptr + w * 7; |
| 562 | |
| 563 | for (int j = 0; j < w; j++) |
| 564 | { |
| 565 | *outptr0++ = r0[0]; |
| 566 | *outptr1++ = r0[1]; |
| 567 | *outptr2++ = r0[2]; |
| 568 | *outptr3++ = r0[3]; |
| 569 | *outptr4++ = r0[4]; |
| 570 | *outptr5++ = r0[5]; |
| 571 | *outptr6++ = r0[6]; |
| 572 | *outptr7++ = r0[7]; |
| 573 | |
| 574 | r0 += 8; |
| 575 | } |
| 576 | |
| 577 | outptr += w * 8; |
| 578 | } |
| 579 | } |
| 580 | #endif // NCNN_ARM82 |
| 581 | if (bottom_blob.elempack == 4 && elempack == 1) |
| 582 | { |
| 583 | for (int i = 0; i < bottom_blob.h; i++) |
| 584 | { |
| 585 | const unsigned short* r0 = bottom_blob.row<const unsigned short>(i); |
| 586 | |
| 587 | unsigned short* outptr0 = outptr; |
| 588 | unsigned short* outptr1 = outptr + w; |
| 589 | unsigned short* outptr2 = outptr + w * 2; |
| 590 | unsigned short* outptr3 = outptr + w * 3; |
| 591 | |
| 592 | for (int j = 0; j < w; j++) |
| 593 | { |
| 594 | *outptr0++ = r0[0]; |
| 595 | *outptr1++ = r0[1]; |
| 596 | *outptr2++ = r0[2]; |
| 597 | *outptr3++ = r0[3]; |
| 598 | |
| 599 | r0 += 4; |
| 600 | } |
| 601 | |
| 602 | outptr += w * 4; |
| 603 | } |
| 604 | } |
| 605 | if (bottom_blob.elempack == elempack) // 1-1 4-4 8-8 |
| 606 | { |
| 607 | int size = w * bottom_blob.h; |
| 608 | |
| 609 | const unsigned short* ptr = bottom_blob; |
| 610 | memcpy(outptr, ptr, size * bottom_blob.elemsize); |
| 611 | |
| 612 | outptr += size * bottom_blob.elempack; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // packing |
| 617 | if (elempack < out_elempack) |
| 618 | { |
| 619 | convert_packing(top_blob_unpacked, top_blob, out_elempack, opt); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (dims == 2 && positive_axis == 1) |
| 624 | { |
| 625 | // interleave image row |
| 626 | int h = bottom_blobs[0].h; |
| 627 | size_t elemsize = bottom_blobs[0].elemsize; |
| 628 | int elempack = bottom_blobs[0].elempack; |
| 629 | |
| 630 | // total width |
| 631 | int top_w = 0; |
| 632 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 633 | { |
| 634 | const Mat& bottom_blob = bottom_blobs[b]; |
| 635 | top_w += bottom_blob.w; |
| 636 | } |
| 637 | |
| 638 | Mat& top_blob = top_blobs[0]; |
| 639 | top_blob.create(top_w, h, elemsize, elempack, opt.blob_allocator); |
| 640 | if (top_blob.empty()) |
| 641 | return -100; |
| 642 | |
| 643 | #pragma omp parallel for num_threads(opt.num_threads) |
| 644 | for (int i = 0; i < h; i++) |
| 645 | { |
| 646 | unsigned short* outptr = top_blob.row<unsigned short>(i); |
| 647 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 648 | { |
| 649 | const Mat& bottom_blob = bottom_blobs[b]; |
| 650 | |
| 651 | const unsigned short* ptr = bottom_blob.row<unsigned short>(i); |
| 652 | memcpy(outptr, ptr, bottom_blob.w * elemsize); |
| 653 | |
| 654 | outptr += bottom_blob.w * elempack; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | if ((dims == 3 || dims == 4) && positive_axis == 0) |
| 660 | { |
| 661 | // concat dim |
| 662 | int w = bottom_blobs[0].w; |
| 663 | int h = bottom_blobs[0].h; |
| 664 | int d = bottom_blobs[0].d; |
| 665 | |
| 666 | // total channels |
| 667 | size_t elemsize = bottom_blobs[0].elemsize; |
| 668 | int elempack = bottom_blobs[0].elempack; |
| 669 | int top_channels = 0; |
| 670 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 671 | { |
| 672 | const Mat& bottom_blob = bottom_blobs[b]; |
| 673 | elemsize = std::min(elemsize, bottom_blob.elemsize); |
| 674 | elempack = std::min(elempack, bottom_blob.elempack); |
| 675 | top_channels += bottom_blob.c * bottom_blob.elempack; |
| 676 | } |
| 677 | |
| 678 | int out_elempack = 1; |
| 679 | if (opt.use_packing_layout) |
| 680 | { |
| 681 | #if NCNN_ARM82 |
| 682 | out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && top_channels % 8 == 0 ? 8 : top_channels % 4 == 0 ? 4 : 1; |
| 683 | #else |
| 684 | out_elempack = top_channels % 4 == 0 ? 4 : 1; |
| 685 | #endif |
| 686 | } |
| 687 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 688 | |
| 689 | Mat& top_blob = top_blobs[0]; |
| 690 | top_blob.create(w, h, d, top_channels / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 691 | if (top_blob.empty()) |
| 692 | return -100; |
| 693 | |
| 694 | top_blob.dims = dims; |
| 695 | |
| 696 | Mat top_blob_unpacked = top_blob; |
| 697 | if (elempack < out_elempack) |
| 698 | { |
| 699 | top_blob_unpacked.create(w, h, d, top_channels / elempack, elemsize, elempack, opt.workspace_allocator); |
| 700 | if (top_blob_unpacked.empty()) |
| 701 | return -100; |
| 702 | |
| 703 | top_blob_unpacked.dims = dims; |
| 704 | } |
| 705 | |
| 706 | int p = 0; |
| 707 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 708 | { |
| 709 | const Mat& bottom_blob = bottom_blobs[b]; |
| 710 | |
| 711 | #if NCNN_ARM82 |
| 712 | if (bottom_blob.elempack == 8 && elempack == 4) |
| 713 | { |
| 714 | int size = bottom_blob.w * bottom_blob.h * bottom_blob.d; |
| 715 | |
| 716 | for (int q = 0; q < bottom_blob.c; q++) |
| 717 | { |
| 718 | const unsigned short* r0 = bottom_blob.channel(q); |
| 719 | |
| 720 | unsigned short* outptr0 = top_blob_unpacked.channel(p); |
| 721 | unsigned short* outptr1 = top_blob_unpacked.channel(p + 1); |
| 722 | |
| 723 | for (int i = 0; i < size; i++) |
| 724 | { |
| 725 | outptr0[0] = r0[0]; |
| 726 | outptr0[1] = r0[1]; |
| 727 | outptr0[2] = r0[2]; |
| 728 | outptr0[3] = r0[3]; |
| 729 | outptr1[0] = r0[4]; |
| 730 | outptr1[1] = r0[5]; |
| 731 | outptr1[2] = r0[6]; |
| 732 | outptr1[3] = r0[7]; |
| 733 | |
| 734 | outptr0 += 4; |
| 735 | outptr1 += 4; |
| 736 | r0 += 8; |
| 737 | } |
| 738 | |
| 739 | p += 2; |
| 740 | } |
| 741 | } |
| 742 | if (bottom_blob.elempack == 8 && elempack == 1) |
| 743 | { |
| 744 | int size = bottom_blob.w * bottom_blob.h * bottom_blob.d; |
| 745 | |
| 746 | for (int q = 0; q < bottom_blob.c; q++) |
| 747 | { |
| 748 | const unsigned short* r0 = bottom_blob.channel(q); |
| 749 | |
| 750 | unsigned short* outptr0 = top_blob_unpacked.channel(p); |
| 751 | unsigned short* outptr1 = top_blob_unpacked.channel(p + 1); |
| 752 | unsigned short* outptr2 = top_blob_unpacked.channel(p + 2); |
| 753 | unsigned short* outptr3 = top_blob_unpacked.channel(p + 3); |
| 754 | unsigned short* outptr4 = top_blob_unpacked.channel(p + 4); |
| 755 | unsigned short* outptr5 = top_blob_unpacked.channel(p + 5); |
| 756 | unsigned short* outptr6 = top_blob_unpacked.channel(p + 6); |
| 757 | unsigned short* outptr7 = top_blob_unpacked.channel(p + 7); |
| 758 | |
| 759 | for (int i = 0; i < size; i++) |
| 760 | { |
| 761 | *outptr0++ = r0[0]; |
| 762 | *outptr1++ = r0[1]; |
| 763 | *outptr2++ = r0[2]; |
| 764 | *outptr3++ = r0[3]; |
| 765 | *outptr4++ = r0[4]; |
| 766 | *outptr5++ = r0[5]; |
| 767 | *outptr6++ = r0[6]; |
| 768 | *outptr7++ = r0[7]; |
| 769 | |
| 770 | r0 += 8; |
| 771 | } |
| 772 | |
| 773 | p += 8; |
| 774 | } |
| 775 | } |
| 776 | #endif // NCNN_ARM82 |
| 777 | if (bottom_blob.elempack == 4 && elempack == 1) |
| 778 | { |
| 779 | int size = bottom_blob.w * bottom_blob.h * bottom_blob.d; |
| 780 | |
| 781 | for (int q = 0; q < bottom_blob.c; q++) |
| 782 | { |
| 783 | const unsigned short* r0 = bottom_blob.channel(q); |
| 784 | |
| 785 | unsigned short* outptr0 = top_blob_unpacked.channel(p); |
| 786 | unsigned short* outptr1 = top_blob_unpacked.channel(p + 1); |
| 787 | unsigned short* outptr2 = top_blob_unpacked.channel(p + 2); |
| 788 | unsigned short* outptr3 = top_blob_unpacked.channel(p + 3); |
| 789 | |
| 790 | for (int i = 0; i < size; i++) |
| 791 | { |
| 792 | *outptr0++ = r0[0]; |
| 793 | *outptr1++ = r0[1]; |
| 794 | *outptr2++ = r0[2]; |
| 795 | *outptr3++ = r0[3]; |
| 796 | |
| 797 | r0 += 4; |
| 798 | } |
| 799 | |
| 800 | p += 4; |
| 801 | } |
| 802 | } |
| 803 | if (bottom_blob.elempack == elempack) // 1-1 4-4 8-8 |
| 804 | { |
| 805 | int size = bottom_blob.total(); |
| 806 | |
| 807 | const unsigned short* ptr = bottom_blob; |
| 808 | unsigned short* outptr = top_blob_unpacked.channel(p); |
| 809 | memcpy(outptr, ptr, size * bottom_blob.elemsize); |
| 810 | |
| 811 | p += bottom_blob.c; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // packing |
| 816 | if (elempack < out_elempack) |
| 817 | { |
| 818 | convert_packing(top_blob_unpacked, top_blob, out_elempack, opt); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if ((dims == 3 && positive_axis == 1) || (dims == 4 && positive_axis == 2)) |
| 823 | { |
| 824 | // interleave dim height |
| 825 | int w = bottom_blobs[0].w; |
| 826 | int d = bottom_blobs[0].d; |
| 827 | int channels = bottom_blobs[0].c; |
| 828 | size_t elemsize = bottom_blobs[0].elemsize; |
| 829 | int elempack = bottom_blobs[0].elempack; |
| 830 | |
| 831 | // total height |
| 832 | int top_h = 0; |
| 833 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 834 | { |
| 835 | const Mat& bottom_blob = bottom_blobs[b]; |
| 836 | top_h += bottom_blob.h; |
| 837 | } |
| 838 | |
| 839 | Mat& top_blob = top_blobs[0]; |
| 840 | top_blob.create(w, top_h, d, channels, elemsize, elempack, opt.blob_allocator); |
| 841 | if (top_blob.empty()) |
| 842 | return -100; |
| 843 | |
| 844 | top_blob.dims = dims; |
| 845 | |
| 846 | #pragma omp parallel for num_threads(opt.num_threads) |
| 847 | for (int q = 0; q < channels; q++) |
| 848 | { |
| 849 | unsigned short* outptr = top_blob.channel(q); |
| 850 | |
| 851 | for (int i = 0; i < d; i++) |
| 852 | { |
| 853 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 854 | { |
| 855 | const Mat& bottom_blob = bottom_blobs[b]; |
| 856 | |
| 857 | int size = bottom_blob.w * bottom_blob.h; |
| 858 | |
| 859 | const unsigned short* ptr = bottom_blob.channel(q).depth(i); |
| 860 | memcpy(outptr, ptr, size * elemsize); |
| 861 | |
| 862 | outptr += size * elempack; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | if ((dims == 3 && positive_axis == 2) || (dims == 4 && positive_axis == 3)) |
| 869 | { |
| 870 | // interleave dim width |
| 871 | int h = bottom_blobs[0].h; |
| 872 | int d = bottom_blobs[0].d; |
| 873 | int channels = bottom_blobs[0].c; |
| 874 | size_t elemsize = bottom_blobs[0].elemsize; |
| 875 | int elempack = bottom_blobs[0].elempack; |
| 876 | |
| 877 | // total height |
| 878 | int top_w = 0; |
| 879 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 880 | { |
| 881 | const Mat& bottom_blob = bottom_blobs[b]; |
| 882 | top_w += bottom_blob.w; |
| 883 | } |
| 884 | |
| 885 | Mat& top_blob = top_blobs[0]; |
| 886 | top_blob.create(top_w, h, d, channels, elemsize, elempack, opt.blob_allocator); |
| 887 | if (top_blob.empty()) |
| 888 | return -100; |
| 889 | |
| 890 | top_blob.dims = dims; |
| 891 | |
| 892 | #pragma omp parallel for num_threads(opt.num_threads) |
| 893 | for (int q = 0; q < channels; q++) |
| 894 | { |
| 895 | unsigned short* outptr = top_blob.channel(q); |
| 896 | |
| 897 | for (int i = 0; i < d; i++) |
| 898 | { |
| 899 | for (int j = 0; j < h; j++) |
| 900 | { |
| 901 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 902 | { |
| 903 | const Mat& bottom_blob = bottom_blobs[b]; |
| 904 | |
| 905 | const unsigned short* ptr = bottom_blob.channel(q).depth(i).row<const unsigned short>(j); |
| 906 | memcpy(outptr, ptr, bottom_blob.w * elemsize); |
| 907 | |
| 908 | outptr += bottom_blob.w * elempack; |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (dims == 4 && positive_axis == 1) |
| 916 | { |
| 917 | // interleave dim depth |
| 918 | int w = bottom_blobs[0].w; |
| 919 | int h = bottom_blobs[0].h; |
| 920 | int channels = bottom_blobs[0].c; |
| 921 | size_t elemsize = bottom_blobs[0].elemsize; |
| 922 | int elempack = bottom_blobs[0].elempack; |
| 923 | |
| 924 | // total depth |
| 925 | int top_d = 0; |
| 926 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 927 | { |
| 928 | const Mat& bottom_blob = bottom_blobs[b]; |
| 929 | top_d += bottom_blob.d; |
| 930 | } |
| 931 | |
| 932 | Mat& top_blob = top_blobs[0]; |
| 933 | top_blob.create(w, h, top_d, channels, elemsize, elempack, opt.blob_allocator); |
| 934 | if (top_blob.empty()) |
| 935 | return -100; |
| 936 | |
| 937 | #pragma omp parallel for num_threads(opt.num_threads) |
| 938 | for (int q = 0; q < channels; q++) |
| 939 | { |
| 940 | unsigned short* outptr = top_blob.channel(q); |
| 941 | |
| 942 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 943 | { |
| 944 | const Mat& bottom_blob = bottom_blobs[b]; |
| 945 | |
| 946 | int size = bottom_blob.w * bottom_blob.h * bottom_blob.d; |
| 947 | |
| 948 | const unsigned short* ptr = bottom_blob.channel(q); |
| 949 | memcpy(outptr, ptr, size * elemsize); |
| 950 | |
| 951 | outptr += size * elempack; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | } // namespace ncnn |