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