| 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 "flatten_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | #include "cpu.h" |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | Flatten_arm::Flatten_arm() |
| 26 | { |
| 27 | #if __ARM_NEON |
| 28 | support_packing = true; |
| 29 | #if NCNN_ARM82 |
| 30 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 31 | #endif |
| 32 | #endif // __ARM_NEON |
| 33 | |
| 34 | #if NCNN_BF16 |
| 35 | support_bf16_storage = true; |
| 36 | #endif // NCNN_BF16 |
| 37 | } |
| 38 | |
| 39 | int Flatten_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 40 | { |
| 41 | int elembits = bottom_blob.elembits(); |
| 42 | |
| 43 | if (elembits == 8) |
| 44 | return forward_int8(bottom_blob, top_blob, opt); |
| 45 | |
| 46 | #if NCNN_ARM82 |
| 47 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 48 | return forward_bf16s_fp16s(bottom_blob, top_blob, opt); |
| 49 | #endif |
| 50 | |
| 51 | #if NCNN_BF16 |
| 52 | if (opt.use_bf16_storage && elembits == 16) |
| 53 | return forward_bf16s_fp16s(bottom_blob, top_blob, opt); |
| 54 | #endif |
| 55 | |
| 56 | int dims = bottom_blob.dims; |
| 57 | |
| 58 | if (dims == 1) |
| 59 | { |
| 60 | top_blob = bottom_blob; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int w = bottom_blob.w; |
| 65 | int h = bottom_blob.h; |
| 66 | int d = bottom_blob.d; |
| 67 | int channels = bottom_blob.c; |
| 68 | size_t elemsize = bottom_blob.elemsize; |
| 69 | int elempack = bottom_blob.elempack; |
| 70 | int size = w * h * d; |
| 71 | |
| 72 | int total = size * channels * elempack; |
| 73 | |
| 74 | int out_elempack = 1; |
| 75 | #if __ARM_NEON |
| 76 | if (opt.use_packing_layout) |
| 77 | { |
| 78 | out_elempack = total % 4 == 0 ? 4 : 1; |
| 79 | } |
| 80 | #endif |
| 81 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 82 | |
| 83 | if (out_elempack == 1) |
| 84 | { |
| 85 | return Flatten::forward(bottom_blob, top_blob, opt); |
| 86 | } |
| 87 | |
| 88 | if (dims == 2 && elempack == 1) // out_elempack == 4 |
| 89 | { |
| 90 | top_blob = bottom_blob; |
| 91 | top_blob.dims = 1; |
| 92 | top_blob.w = total / out_elempack; |
| 93 | top_blob.h = 1; |
| 94 | top_blob.cstep = top_blob.w; |
| 95 | top_blob.elemsize = out_elemsize; |
| 96 | top_blob.elempack = out_elempack; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | top_blob.create(total / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 101 | if (top_blob.empty()) |
| 102 | return -100; |
| 103 | |
| 104 | if (dims == 2) |
| 105 | { |
| 106 | if (elempack == 4) // out_elempack == 4 |
| 107 | { |
| 108 | #pragma omp parallel for num_threads(opt.num_threads) |
| 109 | for (int i = 0; i < h; i++) |
| 110 | { |
| 111 | const float* ptr = bottom_blob.row(i); |
| 112 | float* outptr0 = (float*)top_blob + w * i * 4; |
| 113 | float* outptr1 = (float*)top_blob + w * (i * 4 + 1); |
| 114 | float* outptr2 = (float*)top_blob + w * (i * 4 + 2); |
| 115 | float* outptr3 = (float*)top_blob + w * (i * 4 + 3); |
| 116 | |
| 117 | int j = 0; |
| 118 | #if __ARM_NEON |
| 119 | for (; j + 3 < w; j += 4) |
| 120 | { |
| 121 | float32x4x4_t _v4 = vld4q_f32(ptr); |
| 122 | vst1q_f32(outptr0, _v4.val[0]); |
| 123 | vst1q_f32(outptr1, _v4.val[1]); |
| 124 | vst1q_f32(outptr2, _v4.val[2]); |
| 125 | vst1q_f32(outptr3, _v4.val[3]); |
| 126 | |
| 127 | ptr += 16; |
| 128 | outptr0 += 4; |
| 129 | outptr1 += 4; |
| 130 | outptr2 += 4; |
| 131 | outptr3 += 4; |
| 132 | } |
| 133 | #endif |
| 134 | for (; j < w; j++) |
| 135 | { |
| 136 | *outptr0++ = ptr[0]; |
| 137 | *outptr1++ = ptr[1]; |
| 138 | *outptr2++ = ptr[2]; |
| 139 | *outptr3++ = ptr[3]; |
| 140 | |
| 141 | ptr += 4; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (dims == 3 || dims == 4) |
| 148 | { |
| 149 | if (elempack == 4) // out_elempack == 4 |
| 150 | { |
| 151 | #pragma omp parallel for num_threads(opt.num_threads) |
| 152 | for (int q = 0; q < channels; q++) |
| 153 | { |
| 154 | const float* ptr = bottom_blob.channel(q); |
| 155 | float* outptr0 = (float*)top_blob + size * q * 4; |
| 156 | float* outptr1 = (float*)top_blob + size * (q * 4 + 1); |
| 157 | float* outptr2 = (float*)top_blob + size * (q * 4 + 2); |
| 158 | float* outptr3 = (float*)top_blob + size * (q * 4 + 3); |
| 159 | |
| 160 | int i = 0; |
| 161 | #if __ARM_NEON |
| 162 | for (; i + 3 < size; i += 4) |
| 163 | { |
| 164 | float32x4x4_t _v4 = vld4q_f32(ptr); |
| 165 | vst1q_f32(outptr0, _v4.val[0]); |
| 166 | vst1q_f32(outptr1, _v4.val[1]); |
| 167 | vst1q_f32(outptr2, _v4.val[2]); |
| 168 | vst1q_f32(outptr3, _v4.val[3]); |
| 169 | |
| 170 | ptr += 16; |
| 171 | outptr0 += 4; |
| 172 | outptr1 += 4; |
| 173 | outptr2 += 4; |
| 174 | outptr3 += 4; |
| 175 | } |
| 176 | #endif |
| 177 | for (; i < size; i++) |
| 178 | { |
| 179 | *outptr0++ = ptr[0]; |
| 180 | *outptr1++ = ptr[1]; |
| 181 | *outptr2++ = ptr[2]; |
| 182 | *outptr3++ = ptr[3]; |
| 183 | |
| 184 | ptr += 4; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (elempack == 1) // out_elempack == 4 |
| 190 | { |
| 191 | #pragma omp parallel for num_threads(opt.num_threads) |
| 192 | for (int q = 0; q < channels; q++) |
| 193 | { |
| 194 | const float* ptr = bottom_blob.channel(q); |
| 195 | float* outptr = (float*)top_blob + size * q; |
| 196 | |
| 197 | int i = 0; |
| 198 | #if __ARM_NEON |
| 199 | for (; i + 3 < size; i += 4) |
| 200 | { |
| 201 | float32x4_t _v = vld1q_f32(ptr); |
| 202 | vst1q_f32(outptr, _v); |
| 203 | ptr += 4; |
| 204 | outptr += 4; |
| 205 | } |
| 206 | #endif |
| 207 | for (; i < size; i++) |
| 208 | { |
| 209 | *outptr++ = *ptr++; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int Flatten_arm::forward_bf16s_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 219 | { |
| 220 | int dims = bottom_blob.dims; |
| 221 | |
| 222 | if (dims == 1) |
| 223 | { |
| 224 | top_blob = bottom_blob; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | int w = bottom_blob.w; |
| 229 | int h = bottom_blob.h; |
| 230 | int d = bottom_blob.d; |
| 231 | int channels = bottom_blob.c; |
| 232 | size_t elemsize = bottom_blob.elemsize; |
| 233 | int elempack = bottom_blob.elempack; |
| 234 | int size = w * h * d; |
| 235 | |
| 236 | int total = size * channels * elempack; |
| 237 | |
| 238 | int out_elempack = 1; |
| 239 | #if __ARM_NEON |
| 240 | if (opt.use_packing_layout) |
| 241 | { |
| 242 | #if NCNN_ARM82 |
| 243 | out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && total % 8 == 0 ? 8 : total % 4 == 0 ? 4 : 1; |
| 244 | #else |
| 245 | out_elempack = total % 4 == 0 ? 4 : 1; |
| 246 | #endif |
| 247 | } |
| 248 | #endif |
| 249 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 250 | |
| 251 | if (out_elempack == 1) |
| 252 | { |
| 253 | return Flatten::forward(bottom_blob, top_blob, opt); |
| 254 | } |
| 255 | |
| 256 | if (dims == 2 && elempack == 1) // out_elempack == 4 || out_elempack == 8 |
| 257 | { |
| 258 | top_blob = bottom_blob; |
| 259 | top_blob.dims = 1; |
| 260 | top_blob.w = total / out_elempack; |
| 261 | top_blob.h = 1; |
| 262 | top_blob.cstep = top_blob.w; |
| 263 | top_blob.elemsize = out_elemsize; |
| 264 | top_blob.elempack = out_elempack; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | top_blob.create(total / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 269 | if (top_blob.empty()) |
| 270 | return -100; |
| 271 | |
| 272 | if (dims == 2) |
| 273 | { |
| 274 | #if NCNN_ARM82 |
| 275 | if (elempack == 8) // out_elempack == 8 |
| 276 | { |
| 277 | #pragma omp parallel for num_threads(opt.num_threads) |
| 278 | for (int i = 0; i < h; i++) |
| 279 | { |
| 280 | const unsigned short* ptr = bottom_blob.row<const unsigned short>(i); |
| 281 | unsigned short* outptr0 = (unsigned short*)top_blob + w * i * 8; |
| 282 | unsigned short* outptr1 = (unsigned short*)top_blob + w * (i * 8 + 1); |
| 283 | unsigned short* outptr2 = (unsigned short*)top_blob + w * (i * 8 + 2); |
| 284 | unsigned short* outptr3 = (unsigned short*)top_blob + w * (i * 8 + 3); |
| 285 | unsigned short* outptr4 = (unsigned short*)top_blob + w * (i * 8 + 4); |
| 286 | unsigned short* outptr5 = (unsigned short*)top_blob + w * (i * 8 + 5); |
| 287 | unsigned short* outptr6 = (unsigned short*)top_blob + w * (i * 8 + 6); |
| 288 | unsigned short* outptr7 = (unsigned short*)top_blob + w * (i * 8 + 7); |
| 289 | |
| 290 | int j = 0; |
| 291 | for (; j + 3 < w; j += 4) |
| 292 | { |
| 293 | uint16x8x4_t _v4 = vld4q_u16(ptr); |
| 294 | uint16x8_t _v_01 = vuzp1q_u16(_v4.val[0], _v4.val[1]); |
| 295 | uint16x8_t _v_23 = vuzp1q_u16(_v4.val[2], _v4.val[3]); |
| 296 | uint16x8_t _v_45 = vuzp2q_u16(_v4.val[0], _v4.val[1]); |
| 297 | uint16x8_t _v_67 = vuzp2q_u16(_v4.val[2], _v4.val[3]); |
| 298 | vst1_u16(outptr0, vget_low_u16(_v_01)); |
| 299 | vst1_u16(outptr1, vget_high_u16(_v_01)); |
| 300 | vst1_u16(outptr2, vget_low_u16(_v_23)); |
| 301 | vst1_u16(outptr3, vget_high_u16(_v_23)); |
| 302 | vst1_u16(outptr4, vget_low_u16(_v_45)); |
| 303 | vst1_u16(outptr5, vget_high_u16(_v_45)); |
| 304 | vst1_u16(outptr6, vget_low_u16(_v_67)); |
| 305 | vst1_u16(outptr7, vget_high_u16(_v_67)); |
| 306 | |
| 307 | ptr += 32; |
| 308 | outptr0 += 4; |
| 309 | outptr1 += 4; |
| 310 | outptr2 += 4; |
| 311 | outptr3 += 4; |
| 312 | outptr4 += 4; |
| 313 | outptr5 += 4; |
| 314 | outptr6 += 4; |
| 315 | outptr7 += 4; |
| 316 | } |
| 317 | for (; j < w; j++) |
| 318 | { |
| 319 | *outptr0++ = ptr[0]; |
| 320 | *outptr1++ = ptr[1]; |
| 321 | *outptr2++ = ptr[2]; |
| 322 | *outptr3++ = ptr[3]; |
| 323 | *outptr4++ = ptr[4]; |
| 324 | *outptr5++ = ptr[5]; |
| 325 | *outptr6++ = ptr[6]; |
| 326 | *outptr7++ = ptr[7]; |
| 327 | |
| 328 | ptr += 8; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | #endif // NCNN_ARM82 |
| 333 | |
| 334 | if (elempack == 4) // out_elempack == 4 || out_elempack == 8 |
| 335 | { |
| 336 | #pragma omp parallel for num_threads(opt.num_threads) |
| 337 | for (int i = 0; i < h; i++) |
| 338 | { |
| 339 | const unsigned short* ptr = bottom_blob.row<const unsigned short>(i); |
| 340 | unsigned short* outptr0 = (unsigned short*)top_blob + w * i * 4; |
| 341 | unsigned short* outptr1 = (unsigned short*)top_blob + w * (i * 4 + 1); |
| 342 | unsigned short* outptr2 = (unsigned short*)top_blob + w * (i * 4 + 2); |
| 343 | unsigned short* outptr3 = (unsigned short*)top_blob + w * (i * 4 + 3); |
| 344 | |
| 345 | int j = 0; |
| 346 | #if __ARM_NEON |
| 347 | for (; j + 3 < w; j += 4) |
| 348 | { |
| 349 | uint16x4x4_t _v4 = vld4_u16(ptr); |
| 350 | vst1_u16(outptr0, _v4.val[0]); |
| 351 | vst1_u16(outptr1, _v4.val[1]); |
| 352 | vst1_u16(outptr2, _v4.val[2]); |
| 353 | vst1_u16(outptr3, _v4.val[3]); |
| 354 | |
| 355 | ptr += 16; |
| 356 | outptr0 += 4; |
| 357 | outptr1 += 4; |
| 358 | outptr2 += 4; |
| 359 | outptr3 += 4; |
| 360 | } |
| 361 | #endif |
| 362 | for (; j < w; j++) |
| 363 | { |
| 364 | *outptr0++ = ptr[0]; |
| 365 | *outptr1++ = ptr[1]; |
| 366 | *outptr2++ = ptr[2]; |
| 367 | *outptr3++ = ptr[3]; |
| 368 | |
| 369 | ptr += 4; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (dims == 3 || dims == 4) |
| 376 | { |
| 377 | #if NCNN_ARM82 |
| 378 | if (elempack == 8) // out_elempack == 8 |
| 379 | { |
| 380 | #pragma omp parallel for num_threads(opt.num_threads) |
| 381 | for (int q = 0; q < channels; q++) |
| 382 | { |
| 383 | const unsigned short* ptr = bottom_blob.channel(q); |
| 384 | unsigned short* outptr0 = (unsigned short*)top_blob + size * q * 8; |
| 385 | unsigned short* outptr1 = (unsigned short*)top_blob + size * (q * 8 + 1); |
| 386 | unsigned short* outptr2 = (unsigned short*)top_blob + size * (q * 8 + 2); |
| 387 | unsigned short* outptr3 = (unsigned short*)top_blob + size * (q * 8 + 3); |
| 388 | unsigned short* outptr4 = (unsigned short*)top_blob + size * (q * 8 + 4); |
| 389 | unsigned short* outptr5 = (unsigned short*)top_blob + size * (q * 8 + 5); |
| 390 | unsigned short* outptr6 = (unsigned short*)top_blob + size * (q * 8 + 6); |
| 391 | unsigned short* outptr7 = (unsigned short*)top_blob + size * (q * 8 + 7); |
| 392 | |
| 393 | int i = 0; |
| 394 | for (; i + 3 < size; i += 4) |
| 395 | { |
| 396 | uint16x8x4_t _v4 = vld4q_u16(ptr); |
| 397 | uint16x8_t _v_01 = vuzp1q_u16(_v4.val[0], _v4.val[1]); |
| 398 | uint16x8_t _v_23 = vuzp1q_u16(_v4.val[2], _v4.val[3]); |
| 399 | uint16x8_t _v_45 = vuzp2q_u16(_v4.val[0], _v4.val[1]); |
| 400 | uint16x8_t _v_67 = vuzp2q_u16(_v4.val[2], _v4.val[3]); |
| 401 | vst1_u16(outptr0, vget_low_u16(_v_01)); |
| 402 | vst1_u16(outptr1, vget_high_u16(_v_01)); |
| 403 | vst1_u16(outptr2, vget_low_u16(_v_23)); |
| 404 | vst1_u16(outptr3, vget_high_u16(_v_23)); |
| 405 | vst1_u16(outptr4, vget_low_u16(_v_45)); |
| 406 | vst1_u16(outptr5, vget_high_u16(_v_45)); |
| 407 | vst1_u16(outptr6, vget_low_u16(_v_67)); |
| 408 | vst1_u16(outptr7, vget_high_u16(_v_67)); |
| 409 | |
| 410 | ptr += 32; |
| 411 | outptr0 += 4; |
| 412 | outptr1 += 4; |
| 413 | outptr2 += 4; |
| 414 | outptr3 += 4; |
| 415 | outptr4 += 4; |
| 416 | outptr5 += 4; |
| 417 | outptr6 += 4; |
| 418 | outptr7 += 4; |
| 419 | } |
| 420 | for (; i < size; i++) |
| 421 | { |
| 422 | *outptr0++ = ptr[0]; |
| 423 | *outptr1++ = ptr[1]; |
| 424 | *outptr2++ = ptr[2]; |
| 425 | *outptr3++ = ptr[3]; |
| 426 | *outptr4++ = ptr[4]; |
| 427 | *outptr5++ = ptr[5]; |
| 428 | *outptr6++ = ptr[6]; |
| 429 | *outptr7++ = ptr[7]; |
| 430 | |
| 431 | ptr += 8; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | #endif // NCNN_ARM82 |
| 436 | |
| 437 | if (elempack == 4) // out_elempack == 4 || out_elempack == 8 |
| 438 | { |
| 439 | #pragma omp parallel for num_threads(opt.num_threads) |
| 440 | for (int q = 0; q < channels; q++) |
| 441 | { |
| 442 | const unsigned short* ptr = bottom_blob.channel(q); |
| 443 | unsigned short* outptr0 = (unsigned short*)top_blob + size * q * 4; |
| 444 | unsigned short* outptr1 = (unsigned short*)top_blob + size * (q * 4 + 1); |
| 445 | unsigned short* outptr2 = (unsigned short*)top_blob + size * (q * 4 + 2); |
| 446 | unsigned short* outptr3 = (unsigned short*)top_blob + size * (q * 4 + 3); |
| 447 | |
| 448 | int i = 0; |
| 449 | #if __ARM_NEON |
| 450 | for (; i + 3 < size; i += 4) |
| 451 | { |
| 452 | uint16x4x4_t _v4 = vld4_u16(ptr); |
| 453 | vst1_u16(outptr0, _v4.val[0]); |
| 454 | vst1_u16(outptr1, _v4.val[1]); |
| 455 | vst1_u16(outptr2, _v4.val[2]); |
| 456 | vst1_u16(outptr3, _v4.val[3]); |
| 457 | |
| 458 | ptr += 16; |
| 459 | outptr0 += 4; |
| 460 | outptr1 += 4; |
| 461 | outptr2 += 4; |
| 462 | outptr3 += 4; |
| 463 | } |
| 464 | #endif |
| 465 | for (; i < size; i++) |
| 466 | { |
| 467 | *outptr0++ = ptr[0]; |
| 468 | *outptr1++ = ptr[1]; |
| 469 | *outptr2++ = ptr[2]; |
| 470 | *outptr3++ = ptr[3]; |
| 471 | |
| 472 | ptr += 4; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (elempack == 1) // out_elempack == 4 || out_elempack == 8 |
| 478 | { |
| 479 | #pragma omp parallel for num_threads(opt.num_threads) |
| 480 | for (int q = 0; q < channels; q++) |
| 481 | { |
| 482 | const unsigned short* ptr = bottom_blob.channel(q); |
| 483 | unsigned short* outptr = (unsigned short*)top_blob + size * q; |
| 484 | |
| 485 | int i = 0; |
| 486 | #if __ARM_NEON |
| 487 | for (; i + 3 < size; i += 4) |
| 488 | { |
| 489 | uint16x4_t _v = vld1_u16(ptr); |
| 490 | vst1_u16(outptr, _v); |
| 491 | ptr += 4; |
| 492 | outptr += 4; |
| 493 | } |
| 494 | #endif |
| 495 | for (; i < size; i++) |
| 496 | { |
| 497 | *outptr++ = *ptr++; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | int Flatten_arm::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 507 | { |
| 508 | int dims = bottom_blob.dims; |
| 509 | |
| 510 | if (dims == 1) |
| 511 | { |
| 512 | top_blob = bottom_blob; |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | int w = bottom_blob.w; |
| 517 | int h = bottom_blob.h; |
| 518 | int d = bottom_blob.d; |
| 519 | int channels = bottom_blob.c; |
| 520 | size_t elemsize = bottom_blob.elemsize; |
| 521 | int elempack = bottom_blob.elempack; |
| 522 | int size = w * h * d; |
| 523 | |
| 524 | int total = size * channels * elempack; |
| 525 | |
| 526 | int out_elempack = 1; |
| 527 | if (opt.use_packing_layout) |
| 528 | { |
| 529 | out_elempack = total % 8 == 0 ? 8 : 1; |
| 530 | } |
| 531 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 532 | |
| 533 | if (out_elempack == 1) |
| 534 | { |
| 535 | return Flatten::forward(bottom_blob, top_blob, opt); |
| 536 | } |
| 537 | |
| 538 | if (dims == 2 && elempack == 1) // out_elempack == 8 |
| 539 | { |
| 540 | top_blob = bottom_blob; |
| 541 | top_blob.dims = 1; |
| 542 | top_blob.w = total / out_elempack; |
| 543 | top_blob.h = 1; |
| 544 | top_blob.cstep = top_blob.w; |
| 545 | top_blob.elemsize = out_elemsize; |
| 546 | top_blob.elempack = out_elempack; |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | top_blob.create(total / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 551 | if (top_blob.empty()) |
| 552 | return -100; |
| 553 | |
| 554 | if (dims == 2) |
| 555 | { |
| 556 | if (elempack == 8) // out_elempack == 8 |
| 557 | { |
| 558 | #pragma omp parallel for num_threads(opt.num_threads) |
| 559 | for (int i = 0; i < h; i++) |
| 560 | { |
| 561 | const signed char* ptr = bottom_blob.row<const signed char>(i); |
| 562 | signed char* outptr0 = (signed char*)top_blob + w * i * 8; |
| 563 | signed char* outptr1 = (signed char*)top_blob + w * (i * 8 + 1); |
| 564 | signed char* outptr2 = (signed char*)top_blob + w * (i * 8 + 2); |
| 565 | signed char* outptr3 = (signed char*)top_blob + w * (i * 8 + 3); |
| 566 | signed char* outptr4 = (signed char*)top_blob + w * (i * 8 + 4); |
| 567 | signed char* outptr5 = (signed char*)top_blob + w * (i * 8 + 5); |
| 568 | signed char* outptr6 = (signed char*)top_blob + w * (i * 8 + 6); |
| 569 | signed char* outptr7 = (signed char*)top_blob + w * (i * 8 + 7); |
| 570 | |
| 571 | int j = 0; |
| 572 | for (; j < w; j++) |
| 573 | { |
| 574 | *outptr0++ = ptr[0]; |
| 575 | *outptr1++ = ptr[1]; |
| 576 | *outptr2++ = ptr[2]; |
| 577 | *outptr3++ = ptr[3]; |
| 578 | *outptr4++ = ptr[4]; |
| 579 | *outptr5++ = ptr[5]; |
| 580 | *outptr6++ = ptr[6]; |
| 581 | *outptr7++ = ptr[7]; |
| 582 | |
| 583 | ptr += 8; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (dims == 3 || dims == 4) |
| 590 | { |
| 591 | if (elempack == 8) // out_elempack == 8 |
| 592 | { |
| 593 | #pragma omp parallel for num_threads(opt.num_threads) |
| 594 | for (int q = 0; q < channels; q++) |
| 595 | { |
| 596 | const signed char* ptr = bottom_blob.channel(q); |
| 597 | signed char* outptr0 = (signed char*)top_blob + size * q * 8; |
| 598 | signed char* outptr1 = (signed char*)top_blob + size * (q * 8 + 1); |
| 599 | signed char* outptr2 = (signed char*)top_blob + size * (q * 8 + 2); |
| 600 | signed char* outptr3 = (signed char*)top_blob + size * (q * 8 + 3); |
| 601 | signed char* outptr4 = (signed char*)top_blob + size * (q * 8 + 4); |
| 602 | signed char* outptr5 = (signed char*)top_blob + size * (q * 8 + 5); |
| 603 | signed char* outptr6 = (signed char*)top_blob + size * (q * 8 + 6); |
| 604 | signed char* outptr7 = (signed char*)top_blob + size * (q * 8 + 7); |
| 605 | |
| 606 | int i = 0; |
| 607 | for (; i < size; i++) |
| 608 | { |
| 609 | *outptr0++ = ptr[0]; |
| 610 | *outptr1++ = ptr[1]; |
| 611 | *outptr2++ = ptr[2]; |
| 612 | *outptr3++ = ptr[3]; |
| 613 | *outptr4++ = ptr[4]; |
| 614 | *outptr5++ = ptr[5]; |
| 615 | *outptr6++ = ptr[6]; |
| 616 | *outptr7++ = ptr[7]; |
| 617 | |
| 618 | ptr += 8; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (elempack == 1) // out_elempack == 8 |
| 624 | { |
| 625 | #pragma omp parallel for num_threads(opt.num_threads) |
| 626 | for (int q = 0; q < channels; q++) |
| 627 | { |
| 628 | const signed char* ptr = bottom_blob.channel(q); |
| 629 | signed char* outptr = (signed char*)top_blob + size * q; |
| 630 | |
| 631 | int i = 0; |
| 632 | for (; i < size; i++) |
| 633 | { |
| 634 | *outptr++ = *ptr++; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | } // namespace ncnn |