Elffuss BraidHub

kikocis/ncnn public

master · 38.2 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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_riscv.h"
16
17#if __riscv_vector
18#include <riscv_vector.h>
19#include "riscv_usability.h"
20#endif // __riscv_vector
21
22namespace ncnn {
23
24#include "interp_bicubic.h"
25#include "interp_bilinear.h"
26
27#if __riscv_vector
28#include "interp_bicubic_packn.h"
29#include "interp_bilinear_packn.h"
30#if __riscv_zfh
31#include "interp_bicubic_fp16s.h"
32#include "interp_bicubic_packn_fp16s.h"
33#include "interp_bilinear_fp16s.h"
34#include "interp_bilinear_packn_fp16s.h"
35#endif
36#endif
37
38Interp_riscv::Interp_riscv()
39{
40#if __riscv_vector
41 support_packing = true;
42#if __riscv_zfh
43 support_fp16_storage = true;
44#endif
45#endif // __riscv_vector
46}
47
48int Interp_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
49{
50 const Mat& bottom_blob = bottom_blobs[0];
51 const Mat& reference_blob = bottom_blobs[1];
52 Mat& top_blob = top_blobs[0];
53
54 int elembits = bottom_blob.elembits();
55
56#if __riscv_vector && __riscv_zfh
57 if (opt.use_fp16_storage && elembits == 16)
58 {
59 if (opt.use_fp16_arithmetic)
60 return forward_fp16sa(bottom_blobs, top_blobs, opt);
61 else
62 return forward_fp16s(bottom_blobs, top_blobs, opt);
63 }
64#endif
65
66#if __riscv_vector
67 const int packn = csrr_vlenb() / 4;
68#endif
69
70 int h = bottom_blob.h;
71 int w = bottom_blob.w;
72 int channels = bottom_blob.c;
73 int dims = bottom_blob.dims;
74 size_t elemsize = bottom_blob.elemsize;
75 int elempack = bottom_blob.elempack;
76
77 int outw = reference_blob.w;
78 int outh = reference_blob.h;
79
80 if (dims == 1)
81 {
82 top_blob.create(outw, outh, w, elemsize, elempack, opt.blob_allocator);
83 if (top_blob.empty())
84 return -100;
85
86#if __riscv_vector
87 if (elempack == packn)
88 {
89 const size_t vl = vsetvl_e32m1(packn);
90
91 #pragma omp parallel for num_threads(opt.num_threads)
92 for (int q = 0; q < w; q++)
93 {
94 Mat top_blob_c = top_blob.channel(q);
95 vfloat32m1_t _v = vle32_v_f32m1((const float*)bottom_blob + q * packn, vl);
96 top_blob_c.fill(_v);
97 }
98
99 return 0;
100 }
101#endif // __riscv_vector
102
103 #pragma omp parallel for num_threads(opt.num_threads)
104 for (int q = 0; q < w; q++)
105 {
106 Mat top_blob_c = top_blob.channel(q);
107 const float v = bottom_blob[q];
108 top_blob_c.fill(v);
109 }
110
111 return 0;
112 }
113
114 if (dims == 2)
115 {
116 if (outw == w)
117 {
118 top_blob = bottom_blob;
119 return 0;
120 }
121
122 top_blob.create(outw, h, elemsize, elempack, opt.blob_allocator);
123 if (top_blob.empty())
124 return -100;
125
126#if __riscv_vector
127 if (elempack == packn)
128 {
129 if (resize_type == 1) // nearest
130 {
131 const size_t vl = vsetvl_e32m1(packn);
132
133 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
134
135 #pragma omp parallel for num_threads(opt.num_threads)
136 for (int y = 0; y < h; y++)
137 {
138 const float* ptr = bottom_blob.row(y);
139 float* outptr = top_blob.row(y);
140 for (int x = 0; x < outw; x++)
141 {
142 int in_x = std::min((int)(x * ws), (w - 1));
143
144 vfloat32m1_t _p = vle32_v_f32m1(ptr + in_x * packn, vl);
145 vse32_v_f32m1(outptr, _p, vl);
146
147 outptr += packn;
148 }
149 }
150 }
151
152 if (resize_type == 2) // bilinear
153 {
154 const size_t vl = vsetvl_e32m1(packn);
155
156 int* buf = new int[outw + outw * packn];
157
158 int* xofs = buf;
159 float* alpha = (float*)(buf + outw);
160
161 linear_coeffs(w, outw, xofs, alpha, align_corner);
162
163 #pragma omp parallel for num_threads(opt.num_threads)
164 for (int y = 0; y < h; y++)
165 {
166 const float* ptr = bottom_blob.row(y);
167 float* outptr = top_blob.row(y);
168 const float* alphap = alpha;
169
170 for (int x = 0; x < outw; x++)
171 {
172 int sx = xofs[x] * packn;
173 const float* Sp = ptr + sx;
174
175 vfloat32m1_t _S0 = vle32_v_f32m1(Sp, vl);
176 vfloat32m1_t _S1 = vle32_v_f32m1(Sp + packn, vl);
177 vfloat32m1_t _p = vfmacc_vf_f32m1(vfmul_vf_f32m1(_S0, alphap[0], vl), alphap[1], _S1, vl);
178
179 vse32_v_f32m1(outptr, _p, vl);
180
181 alphap += 2;
182 outptr += packn;
183 }
184 }
185
186 delete[] buf;
187 }
188
189 if (resize_type == 3) // bicubic
190 {
191 const size_t vl = vsetvl_e32m1(packn);
192
193 int* buf = new int[outw + outw * packn];
194
195 int* xofs = buf;
196 float* alpha = (float*)(buf + outw);
197
198 cubic_coeffs(w, outw, xofs, alpha, align_corner);
199
200 #pragma omp parallel for num_threads(opt.num_threads)
201 for (int y = 0; y < h; y++)
202 {
203 const float* ptr = bottom_blob.row(y);
204 float* outptr = top_blob.row(y);
205 const float* alphap = alpha;
206
207 for (int x = 0; x < outw; x++)
208 {
209 int sx = xofs[x] * packn;
210 const float* Sp = ptr + sx;
211
212 vfloat32m1_t _S0 = vle32_v_f32m1(Sp - packn, vl);
213 vfloat32m1_t _S1 = vle32_v_f32m1(Sp, vl);
214 vfloat32m1_t _S2 = vle32_v_f32m1(Sp + packn, vl);
215 vfloat32m1_t _S3 = vle32_v_f32m1(Sp + packn * 2, vl);
216 vfloat32m1_t _p = vfmacc_vf_f32m1(vfmacc_vf_f32m1(vfmacc_vf_f32m1(vfmul_vf_f32m1(_S0, alphap[0], vl), alphap[1], _S1, vl), alphap[2], _S2, vl), alphap[3], _S3, vl);
217
218 vse32_v_f32m1(outptr, _p, vl);
219
220 alphap += 4;
221 outptr += packn;
222 }
223 }
224
225 delete[] buf;
226 }
227
228 return 0;
229 }
230#endif // __riscv_vector
231
232 if (resize_type == 1) // nearest
233 {
234 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
235
236 #pragma omp parallel for num_threads(opt.num_threads)
237 for (int y = 0; y < h; y++)
238 {
239 const float* ptr = bottom_blob.row(y);
240 float* outptr = top_blob.row(y);
241 for (int x = 0; x < outw; x++)
242 {
243 int in_x = std::min((int)(x * ws), (w - 1));
244 *outptr++ = ptr[in_x];
245 }
246 }
247 }
248
249 if (resize_type == 2) // bilinear
250 {
251 int* buf = new int[outw + outw * 2];
252
253 int* xofs = buf;
254 float* alpha = (float*)(buf + outw);
255
256 linear_coeffs(w, outw, xofs, alpha, align_corner);
257
258 #pragma omp parallel for num_threads(opt.num_threads)
259 for (int y = 0; y < h; y++)
260 {
261 const float* ptr = bottom_blob.row(y);
262 float* outptr = top_blob.row(y);
263 const float* alphap = alpha;
264
265 for (int x = 0; x < outw; x++)
266 {
267 int sx = xofs[x];
268 const float* Sp = ptr + sx;
269 float a0 = alphap[0];
270 float a1 = alphap[1];
271 *outptr++ = Sp[0] * a0 + Sp[1] * a1;
272 alphap += 2;
273 }
274 }
275
276 delete[] buf;
277 }
278
279 if (resize_type == 3) // bicubic
280 {
281 int* buf = new int[outw + outw * 4];
282
283 int* xofs = buf;
284 float* alpha = (float*)(buf + outw);
285
286 cubic_coeffs(w, outw, xofs, alpha, align_corner);
287
288 #pragma omp parallel for num_threads(opt.num_threads)
289 for (int y = 0; y < h; y++)
290 {
291 const float* ptr = bottom_blob.row(y);
292 float* outptr = top_blob.row(y);
293 const float* alphap = alpha;
294
295 for (int x = 0; x < outw; x++)
296 {
297 int sx = xofs[x];
298 const float* Sp = ptr + sx;
299 float a0 = alphap[0];
300 float a1 = alphap[1];
301 float a2 = alphap[2];
302 float a3 = alphap[3];
303 *outptr++ = Sp[-1] * a0 + Sp[0] * a1 + Sp[1] * a2 + Sp[2] * a3;
304 alphap += 4;
305 }
306 }
307
308 delete[] buf;
309 }
310
311 return 0;
312 }
313
314 if (outw == w && outh == h)
315 {
316 top_blob = bottom_blob;
317 return 0;
318 }
319
320 top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator);
321 if (top_blob.empty())
322 return -100;
323
324#if __riscv_vector
325 if (elempack == packn)
326 {
327 if (resize_type == 1) // nearest
328 {
329 const size_t vl = vsetvl_e32m1(packn);
330
331 const float hs = output_height ? h / (float)outh : 1.f / height_scale;
332 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
333
334 #pragma omp parallel for num_threads(opt.num_threads)
335 for (int q = 0; q < channels; q++)
336 {
337 const Mat src = bottom_blob.channel(q);
338 Mat dst = top_blob.channel(q);
339
340 for (int y = 0; y < outh; y++)
341 {
342 int in_y = std::min((int)(y * hs), (h - 1));
343
344 const float* ptr = src.row(in_y);
345 float* outptr = dst.row(y);
346 for (int x = 0; x < outw; x++)
347 {
348 int in_x = std::min((int)(x * ws), (w - 1));
349
350 vfloat32m1_t _p = vle32_v_f32m1(ptr + in_x * packn, vl);
351 vse32_v_f32m1(outptr, _p, vl);
352
353 outptr += packn;
354 }
355 }
356 }
357 }
358
359 if (resize_type == 2) // bilinear
360 {
361 int* buf = new int[outw + outh + outw * 2 + outh * 2];
362
363 int* xofs = buf; //new int[outw];
364 int* yofs = buf + outw; //new int[outh];
365
366 float* alpha = (float*)(buf + outw + outh); //new float[outw * 2];
367 float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2];
368
369 linear_coeffs(w, outw, xofs, alpha, align_corner);
370 linear_coeffs(h, outh, yofs, beta, align_corner);
371
372 #pragma omp parallel for num_threads(opt.num_threads)
373 for (int q = 0; q < channels; q++)
374 {
375 const Mat src = bottom_blob.channel(q);
376 Mat dst = top_blob.channel(q);
377
378 resize_bilinear_image_packn(src, dst, alpha, xofs, beta, yofs);
379 }
380
381 delete[] buf;
382 }
383
384 if (resize_type == 3) // bicubic
385 {
386 int* buf = new int[outw + outh + outw * 4 + outh * 4];
387
388 int* xofs = buf; //new int[outw];
389 int* yofs = buf + outw; //new int[outh];
390
391 float* alpha = (float*)(buf + outw + outh); //new float[outw * 4];
392 float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4];
393
394 cubic_coeffs(w, outw, xofs, alpha, align_corner);
395 cubic_coeffs(h, outh, yofs, beta, align_corner);
396
397 #pragma omp parallel for num_threads(opt.num_threads)
398 for (int q = 0; q < channels; q++)
399 {
400 const Mat src = bottom_blob.channel(q);
401 Mat dst = top_blob.channel(q);
402
403 resize_bicubic_image_packn(src, dst, alpha, xofs, beta, yofs);
404 }
405
406 delete[] buf;
407 }
408
409 return 0;
410 }
411#endif // __riscv_vector
412
413 if (resize_type == 1) // nearest
414 {
415 const float hs = output_height ? h / (float)outh : 1.f / height_scale;
416 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
417
418 #pragma omp parallel for num_threads(opt.num_threads)
419 for (int q = 0; q < channels; q++)
420 {
421 const Mat src = bottom_blob.channel(q);
422 Mat dst = top_blob.channel(q);
423
424 for (int y = 0; y < outh; y++)
425 {
426 int in_y = std::min((int)(y * hs), (h - 1));
427
428 const float* ptr = src.row(in_y);
429 float* outptr = dst.row(y);
430 for (int x = 0; x < outw; x++)
431 {
432 int in_x = std::min((int)(x * ws), (w - 1));
433 *outptr++ = ptr[in_x];
434 }
435 }
436 }
437 }
438
439 if (resize_type == 2) // bilinear
440 {
441 int* buf = new int[outw + outh + outw * 2 + outh * 2];
442
443 int* xofs = buf; //new int[outw];
444 int* yofs = buf + outw; //new int[outh];
445
446 float* alpha = (float*)(buf + outw + outh); //new float[outw * 2];
447 float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2];
448
449 linear_coeffs(w, outw, xofs, alpha, align_corner);
450 linear_coeffs(h, outh, yofs, beta, align_corner);
451
452 #pragma omp parallel for num_threads(opt.num_threads)
453 for (int q = 0; q < channels; q++)
454 {
455 const Mat src = bottom_blob.channel(q);
456 Mat dst = top_blob.channel(q);
457
458 resize_bilinear_image(src, dst, alpha, xofs, beta, yofs);
459 }
460
461 delete[] buf;
462 }
463
464 if (resize_type == 3) // bicubic
465 {
466 int* buf = new int[outw + outh + outw * 4 + outh * 4];
467
468 int* xofs = buf; //new int[outw];
469 int* yofs = buf + outw; //new int[outh];
470
471 float* alpha = (float*)(buf + outw + outh); //new float[outw * 4];
472 float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4];
473
474 cubic_coeffs(w, outw, xofs, alpha, align_corner);
475 cubic_coeffs(h, outh, yofs, beta, align_corner);
476
477 #pragma omp parallel for num_threads(opt.num_threads)
478 for (int q = 0; q < channels; q++)
479 {
480 const Mat src = bottom_blob.channel(q);
481 Mat dst = top_blob.channel(q);
482
483 resize_bicubic_image(src, dst, alpha, xofs, beta, yofs);
484 }
485
486 delete[] buf;
487 }
488
489 return 0;
490}
491
492#if __riscv_vector && __riscv_zfh
493int Interp_riscv::forward_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
494{
495 const int packn = csrr_vlenb() / 2;
496
497 const Mat& bottom_blob = bottom_blobs[0];
498 const Mat& reference_blob = bottom_blobs[1];
499 Mat& top_blob = top_blobs[0];
500
501 int h = bottom_blob.h;
502 int w = bottom_blob.w;
503 int channels = bottom_blob.c;
504 int dims = bottom_blob.dims;
505 size_t elemsize = bottom_blob.elemsize;
506 int elempack = bottom_blob.elempack;
507
508 int outw = reference_blob.w;
509 int outh = reference_blob.h;
510
511 if (dims == 1)
512 {
513 top_blob.create(outw, outh, w, elemsize, elempack, opt.blob_allocator);
514 if (top_blob.empty())
515 return -100;
516
517 if (elempack == packn)
518 {
519 const size_t vl = vsetvl_e16m1(packn);
520
521 #pragma omp parallel for num_threads(opt.num_threads)
522 for (int q = 0; q < w; q++)
523 {
524 Mat top_blob_c = top_blob.channel(q);
525 vfloat16m1_t _v = vle16_v_f16m1((const __fp16*)bottom_blob + q * packn, vl);
526 top_blob_c.fill(_v);
527 }
528
529 return 0;
530 }
531
532 #pragma omp parallel for num_threads(opt.num_threads)
533 for (int q = 0; q < w; q++)
534 {
535 Mat top_blob_c = top_blob.channel(q);
536 const __fp16* ptr = bottom_blob;
537 top_blob_c.fill(ptr[q]);
538 }
539
540 return 0;
541 }
542
543 if (dims == 2)
544 {
545 if (outw == w)
546 {
547 top_blob = bottom_blob;
548 return 0;
549 }
550
551 top_blob.create(outw, h, elemsize, elempack, opt.blob_allocator);
552 if (top_blob.empty())
553 return -100;
554
555 if (elempack == packn)
556 {
557 if (resize_type == 1) // nearest
558 {
559 const size_t vl = vsetvl_e16m1(packn);
560
561 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
562
563 #pragma omp parallel for num_threads(opt.num_threads)
564 for (int y = 0; y < h; y++)
565 {
566 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
567 __fp16* outptr = top_blob.row<__fp16>(y);
568 for (int x = 0; x < outw; x++)
569 {
570 int in_x = std::min((int)(x * ws), (w - 1));
571
572 vfloat16m1_t _p = vle16_v_f16m1(ptr + in_x * packn, vl);
573 vse16_v_f16m1(outptr, _p, vl);
574
575 outptr += packn;
576 }
577 }
578 }
579
580 if (resize_type == 2) // bilinear
581 {
582 const size_t vl = vsetvl_e16m1(packn);
583
584 int* buf = new int[outw + outw * packn];
585
586 int* xofs = buf;
587 float* alpha = (float*)(buf + outw);
588
589 linear_coeffs(w, outw, xofs, alpha, align_corner);
590
591 #pragma omp parallel for num_threads(opt.num_threads)
592 for (int y = 0; y < h; y++)
593 {
594 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
595 __fp16* outptr = top_blob.row<__fp16>(y);
596 const float* alphap = alpha;
597
598 for (int x = 0; x < outw; x++)
599 {
600 int sx = xofs[x] * packn;
601 const __fp16* Sp = ptr + sx;
602
603 vfloat16m1_t _S0 = vle16_v_f16m1(Sp, vl);
604 vfloat16m1_t _S1 = vle16_v_f16m1(Sp + packn, vl);
605 vfloat32m2_t _p = vfwmacc_vf_f32m2(vfwmul_vf_f32m2(_S0, alphap[0], vl), alphap[1], _S1, vl);
606
607 vse16_v_f16m1(outptr, vfncvt_f_f_w_f16m1(_p, vl), vl);
608
609 alphap += 2;
610 outptr += packn;
611 }
612 }
613
614 delete[] buf;
615 }
616
617 if (resize_type == 3) // bicubic
618 {
619 const size_t vl = vsetvl_e16m1(packn);
620
621 int* buf = new int[outw + outw * packn];
622
623 int* xofs = buf;
624 float* alpha = (float*)(buf + outw);
625
626 cubic_coeffs(w, outw, xofs, alpha, align_corner);
627
628 #pragma omp parallel for num_threads(opt.num_threads)
629 for (int y = 0; y < h; y++)
630 {
631 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
632 __fp16* outptr = top_blob.row<__fp16>(y);
633 const float* alphap = alpha;
634
635 for (int x = 0; x < outw; x++)
636 {
637 int sx = xofs[x] * packn;
638 const __fp16* Sp = ptr + sx;
639
640 vfloat16m1_t _S0 = vle16_v_f16m1(Sp - packn, vl);
641 vfloat16m1_t _S1 = vle16_v_f16m1(Sp, vl);
642 vfloat16m1_t _S2 = vle16_v_f16m1(Sp + packn, vl);
643 vfloat16m1_t _S3 = vle16_v_f16m1(Sp + packn * 2, vl);
644 vfloat32m2_t _p = vfwmacc_vf_f32m2(vfwmacc_vf_f32m2(vfwmacc_vf_f32m2(vfwmul_vf_f32m2(_S0, alphap[0], vl), alphap[1], _S1, vl), alphap[2], _S2, vl), alphap[3], _S3, vl);
645
646 vse16_v_f16m1(outptr, vfncvt_f_f_w_f16m1(_p, vl), vl);
647
648 alphap += 4;
649 outptr += packn;
650 }
651 }
652
653 delete[] buf;
654 }
655
656 return 0;
657 }
658
659 if (resize_type == 1) // nearest
660 {
661 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
662
663 #pragma omp parallel for num_threads(opt.num_threads)
664 for (int y = 0; y < h; y++)
665 {
666 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
667 __fp16* outptr = top_blob.row<__fp16>(y);
668 for (int x = 0; x < outw; x++)
669 {
670 int in_x = std::min((int)(x * ws), (w - 1));
671 *outptr++ = ptr[in_x];
672 }
673 }
674 }
675
676 if (resize_type == 2) // bilinear
677 {
678 int* buf = new int[outw + outw * 2];
679
680 int* xofs = buf;
681 float* alpha = (float*)(buf + outw);
682
683 linear_coeffs(w, outw, xofs, alpha, align_corner);
684
685 #pragma omp parallel for num_threads(opt.num_threads)
686 for (int y = 0; y < h; y++)
687 {
688 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
689 __fp16* outptr = top_blob.row<__fp16>(y);
690 const float* alphap = alpha;
691
692 for (int x = 0; x < outw; x++)
693 {
694 int sx = xofs[x];
695 const __fp16* Sp = ptr + sx;
696 float a0 = alphap[0];
697 float a1 = alphap[1];
698 *outptr++ = (__fp16)((float)Sp[0] * a0 + (float)Sp[1] * a1);
699 alphap += 2;
700 }
701 }
702
703 delete[] buf;
704 }
705
706 if (resize_type == 3) // bicubic
707 {
708 int* buf = new int[outw + outw * 4];
709
710 int* xofs = buf;
711 float* alpha = (float*)(buf + outw);
712
713 cubic_coeffs(w, outw, xofs, alpha, align_corner);
714
715 #pragma omp parallel for num_threads(opt.num_threads)
716 for (int y = 0; y < h; y++)
717 {
718 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
719 __fp16* outptr = top_blob.row<__fp16>(y);
720 const float* alphap = alpha;
721
722 for (int x = 0; x < outw; x++)
723 {
724 int sx = xofs[x];
725 const __fp16* Sp = ptr + sx;
726 float a0 = alphap[0];
727 float a1 = alphap[1];
728 float a2 = alphap[2];
729 float a3 = alphap[3];
730 *outptr++ = (__fp16)((float)Sp[-1] * a0 + (float)Sp[0] * a1 + (float)Sp[1] * a2 + (float)Sp[2] * a3);
731 alphap += 4;
732 }
733 }
734
735 delete[] buf;
736 }
737
738 return 0;
739 }
740
741 if (outw == w && outh == h)
742 {
743 top_blob = bottom_blob;
744 return 0;
745 }
746
747 top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator);
748 if (top_blob.empty())
749 return -100;
750
751 if (elempack == packn)
752 {
753 if (resize_type == 1) // nearest
754 {
755 const size_t vl = vsetvl_e16m1(packn);
756
757 const float hs = output_height ? h / (float)outh : 1.f / height_scale;
758 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
759
760 #pragma omp parallel for num_threads(opt.num_threads)
761 for (int q = 0; q < channels; q++)
762 {
763 const Mat src = bottom_blob.channel(q);
764 Mat dst = top_blob.channel(q);
765
766 for (int y = 0; y < outh; y++)
767 {
768 int in_y = std::min((int)(y * hs), (h - 1));
769
770 const __fp16* ptr = src.row<const __fp16>(in_y);
771 __fp16* outptr = dst.row<__fp16>(y);
772 for (int x = 0; x < outw; x++)
773 {
774 int in_x = std::min((int)(x * ws), (w - 1));
775
776 vfloat16m1_t _p = vle16_v_f16m1(ptr + in_x * packn, vl);
777 vse16_v_f16m1(outptr, _p, vl);
778
779 outptr += packn;
780 }
781 }
782 }
783 }
784
785 if (resize_type == 2) // bilinear
786 {
787 int* buf = new int[outw + outh + outw * 2 + outh * 2];
788
789 int* xofs = buf; //new int[outw];
790 int* yofs = buf + outw; //new int[outh];
791
792 float* alpha = (float*)(buf + outw + outh); //new float[outw * 2];
793 float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2];
794
795 linear_coeffs(w, outw, xofs, alpha, align_corner);
796 linear_coeffs(h, outh, yofs, beta, align_corner);
797
798 #pragma omp parallel for num_threads(opt.num_threads)
799 for (int q = 0; q < channels; q++)
800 {
801 const Mat src = bottom_blob.channel(q);
802 Mat dst = top_blob.channel(q);
803
804 resize_bilinear_image_packn_fp16s(src, dst, alpha, xofs, beta, yofs);
805 }
806
807 delete[] buf;
808 }
809
810 if (resize_type == 3) // bicubic
811 {
812 int* buf = new int[outw + outh + outw * 4 + outh * 4];
813
814 int* xofs = buf; //new int[outw];
815 int* yofs = buf + outw; //new int[outh];
816
817 float* alpha = (float*)(buf + outw + outh); //new float[outw * 4];
818 float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4];
819
820 cubic_coeffs(w, outw, xofs, alpha, align_corner);
821 cubic_coeffs(h, outh, yofs, beta, align_corner);
822
823 #pragma omp parallel for num_threads(opt.num_threads)
824 for (int q = 0; q < channels; q++)
825 {
826 const Mat src = bottom_blob.channel(q);
827 Mat dst = top_blob.channel(q);
828
829 resize_bicubic_image_packn_fp16s(src, dst, alpha, xofs, beta, yofs);
830 }
831
832 delete[] buf;
833 }
834
835 return 0;
836 }
837
838 if (resize_type == 1) // nearest
839 {
840 const float hs = output_height ? h / (float)outh : 1.f / height_scale;
841 const float ws = output_width ? w / (float)outw : 1.f / width_scale;
842
843 #pragma omp parallel for num_threads(opt.num_threads)
844 for (int q = 0; q < channels; q++)
845 {
846 const Mat src = bottom_blob.channel(q);
847 Mat dst = top_blob.channel(q);
848
849 for (int y = 0; y < outh; y++)
850 {
851 int in_y = std::min((int)(y * hs), (h - 1));
852
853 const __fp16* ptr = src.row<const __fp16>(in_y);
854 __fp16* outptr = dst.row<__fp16>(y);
855 for (int x = 0; x < outw; x++)
856 {
857 int in_x = std::min((int)(x * ws), (w - 1));
858 *outptr++ = ptr[in_x];
859 }
860 }
861 }
862 }
863
864 if (resize_type == 2) // bilinear
865 {
866 int* buf = new int[outw + outh + outw * 2 + outh * 2];
867
868 int* xofs = buf; //new int[outw];
869 int* yofs = buf + outw; //new int[outh];
870
871 float* alpha = (float*)(buf + outw + outh); //new float[outw * 2];
872 float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2];
873
874 linear_coeffs(w, outw, xofs, alpha, align_corner);
875 linear_coeffs(h, outh, yofs, beta, align_corner);
876
877 #pragma omp parallel for num_threads(opt.num_threads)
878 for (int q = 0; q < channels; q++)
879 {
880 const Mat src = bottom_blob.channel(q);
881 Mat dst = top_blob.channel(q);
882
883 resize_bilinear_image_fp16s(src, dst, alpha, xofs, beta, yofs);
884 }
885
886 delete[] buf;
887 }
888
889 if (resize_type == 3) // bicubic
890 {
891 int* buf = new int[outw + outh + outw * 4 + outh * 4];
892
893 int* xofs = buf; //new int[outw];
894 int* yofs = buf + outw; //new int[outh];
895
896 float* alpha = (float*)(buf + outw + outh); //new float[outw * 4];
897 float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4];
898
899 cubic_coeffs(w, outw, xofs, alpha, align_corner);
900 cubic_coeffs(h, outh, yofs, beta, align_corner);
901
902 #pragma omp parallel for num_threads(opt.num_threads)
903 for (int q = 0; q < channels; q++)
904 {
905 const Mat src = bottom_blob.channel(q);
906 Mat dst = top_blob.channel(q);
907
908 resize_bicubic_image_fp16s(src, dst, alpha, xofs, beta, yofs);
909 }
910
911 delete[] buf;
912 }
913
914 return 0;
915}
916
917int Interp_riscv::forward_fp16sa(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
918{
919 const int packn = csrr_vlenb() / 2;
920
921 const Mat& bottom_blob = bottom_blobs[0];
922 const Mat& reference_blob = bottom_blobs[1];
923 Mat& top_blob = top_blobs[0];
924
925 int h = bottom_blob.h;
926 int w = bottom_blob.w;
927 int channels = bottom_blob.c;
928 int dims = bottom_blob.dims;
929 size_t elemsize = bottom_blob.elemsize;
930 int elempack = bottom_blob.elempack;
931
932 int outw = reference_blob.w;
933 int outh = reference_blob.h;
934
935 if (dims == 1 || resize_type == 1) // nearest
936 {
937 return forward_fp16s(bottom_blobs, top_blobs, opt);
938 }
939
940 if (dims == 2)
941 {
942 if (outw == w)
943 {
944 top_blob = bottom_blob;
945 return 0;
946 }
947
948 top_blob.create(outw, h, elemsize, elempack, opt.blob_allocator);
949 if (top_blob.empty())
950 return -100;
951
952 if (elempack == packn)
953 {
954 if (resize_type == 2) // bilinear
955 {
956 const size_t vl = vsetvl_e16m1(packn);
957
958 int* buf = new int[outw + outw * packn];
959
960 int* xofs = buf;
961 __fp16* alpha = (__fp16*)(buf + outw);
962
963 linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
964
965 #pragma omp parallel for num_threads(opt.num_threads)
966 for (int y = 0; y < h; y++)
967 {
968 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
969 __fp16* outptr = top_blob.row<__fp16>(y);
970 const __fp16* alphap = alpha;
971
972 for (int x = 0; x < outw; x++)
973 {
974 int sx = xofs[x] * packn;
975 const __fp16* Sp = ptr + sx;
976
977 vfloat16m1_t _S0 = vle16_v_f16m1(Sp, vl);
978 vfloat16m1_t _S1 = vle16_v_f16m1(Sp + packn, vl);
979 vfloat16m1_t _p = vfmacc_vf_f16m1(vfmul_vf_f16m1(_S0, alphap[0], vl), alphap[1], _S1, vl);
980
981 vse16_v_f16m1(outptr, _p, vl);
982
983 alphap += 2;
984 outptr += packn;
985 }
986 }
987
988 delete[] buf;
989 }
990
991 if (resize_type == 3) // bicubic
992 {
993 const size_t vl = vsetvl_e16m1(packn);
994
995 int* buf = new int[outw + outw * packn];
996
997 int* xofs = buf;
998 __fp16* alpha = (__fp16*)(buf + outw);
999
1000 cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
1001
1002 #pragma omp parallel for num_threads(opt.num_threads)
1003 for (int y = 0; y < h; y++)
1004 {
1005 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
1006 __fp16* outptr = top_blob.row<__fp16>(y);
1007 const __fp16* alphap = alpha;
1008
1009 for (int x = 0; x < outw; x++)
1010 {
1011 int sx = xofs[x] * packn;
1012 const __fp16* Sp = ptr + sx;
1013
1014 vfloat16m1_t _S0 = vle16_v_f16m1(Sp - packn, vl);
1015 vfloat16m1_t _S1 = vle16_v_f16m1(Sp, vl);
1016 vfloat16m1_t _S2 = vle16_v_f16m1(Sp + packn, vl);
1017 vfloat16m1_t _S3 = vle16_v_f16m1(Sp + packn * 2, vl);
1018 vfloat16m1_t _p = vfmacc_vf_f16m1(vfmacc_vf_f16m1(vfmacc_vf_f16m1(vfmul_vf_f16m1(_S0, alphap[0], vl), alphap[1], _S1, vl), alphap[2], _S2, vl), alphap[3], _S3, vl);
1019
1020 vse16_v_f16m1(outptr, _p, vl);
1021
1022 alphap += 4;
1023 outptr += packn;
1024 }
1025 }
1026
1027 delete[] buf;
1028 }
1029
1030 return 0;
1031 }
1032
1033 if (resize_type == 2) // bilinear
1034 {
1035 int* buf = new int[outw + outw * 2];
1036
1037 int* xofs = buf;
1038 __fp16* alpha = (__fp16*)(buf + outw);
1039
1040 linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
1041
1042 #pragma omp parallel for num_threads(opt.num_threads)
1043 for (int y = 0; y < h; y++)
1044 {
1045 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
1046 __fp16* outptr = top_blob.row<__fp16>(y);
1047 const __fp16* alphap = alpha;
1048
1049 for (int x = 0; x < outw; x++)
1050 {
1051 int sx = xofs[x];
1052 const __fp16* Sp = ptr + sx;
1053 __fp16 a0 = alphap[0];
1054 __fp16 a1 = alphap[1];
1055 *outptr++ = Sp[0] * a0 + Sp[1] * a1;
1056 alphap += 2;
1057 }
1058 }
1059
1060 delete[] buf;
1061 }
1062
1063 if (resize_type == 3) // bicubic
1064 {
1065 int* buf = new int[outw + outw * 4];
1066
1067 int* xofs = buf;
1068 __fp16* alpha = (__fp16*)(buf + outw);
1069
1070 cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
1071
1072 #pragma omp parallel for num_threads(opt.num_threads)
1073 for (int y = 0; y < h; y++)
1074 {
1075 const __fp16* ptr = bottom_blob.row<const __fp16>(y);
1076 __fp16* outptr = top_blob.row<__fp16>(y);
1077 const __fp16* alphap = alpha;
1078
1079 for (int x = 0; x < outw; x++)
1080 {
1081 int sx = xofs[x];
1082 const __fp16* Sp = ptr + sx;
1083 __fp16 a0 = alphap[0];
1084 __fp16 a1 = alphap[1];
1085 __fp16 a2 = alphap[2];
1086 __fp16 a3 = alphap[3];
1087 *outptr++ = Sp[-1] * a0 + Sp[0] * a1 + Sp[1] * a2 + Sp[2] * a3;
1088 alphap += 4;
1089 }
1090 }
1091
1092 delete[] buf;
1093 }
1094
1095 return 0;
1096 }
1097
1098 if (outw == w && outh == h)
1099 {
1100 top_blob = bottom_blob;
1101 return 0;
1102 }
1103
1104 top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator);
1105 if (top_blob.empty())
1106 return -100;
1107
1108 if (elempack == packn)
1109 {
1110 if (resize_type == 2) // bilinear
1111 {
1112 int* buf = new int[outw + outh + outw * 2 + outh * 2];
1113
1114 int* xofs = buf; //new int[outw];
1115 int* yofs = buf + outw; //new int[outh];
1116
1117 __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 2];
1118 __fp16* beta = (__fp16*)(buf + outw + outh + outw * 2); //new __fp16[outh * 2];
1119
1120 linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
1121 linear_coeffs_fp16sa(h, outh, yofs, beta, align_corner);
1122
1123 #pragma omp parallel for num_threads(opt.num_threads)
1124 for (int q = 0; q < channels; q++)
1125 {
1126 const Mat src = bottom_blob.channel(q);
1127 Mat dst = top_blob.channel(q);
1128
1129 resize_bilinear_image_packn_fp16sa(src, dst, alpha, xofs, beta, yofs);
1130 }
1131
1132 delete[] buf;
1133 }
1134
1135 if (resize_type == 3) // bicubic
1136 {
1137 int* buf = new int[outw + outh + outw * 4 + outh * 4];
1138
1139 int* xofs = buf; //new int[outw];
1140 int* yofs = buf + outw; //new int[outh];
1141
1142 __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 4];
1143 __fp16* beta = (__fp16*)(buf + outw + outh + outw * 4); //new __fp16[outh * 4];
1144
1145 cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
1146 cubic_coeffs_fp16sa(h, outh, yofs, beta, align_corner);
1147
1148 #pragma omp parallel for num_threads(opt.num_threads)
1149 for (int q = 0; q < channels; q++)
1150 {
1151 const Mat src = bottom_blob.channel(q);
1152 Mat dst = top_blob.channel(q);
1153
1154 resize_bicubic_image_packn_fp16sa(src, dst, alpha, xofs, beta, yofs);
1155 }
1156
1157 delete[] buf;
1158 }
1159
1160 return 0;
1161 }
1162
1163 if (resize_type == 2) // bilinear
1164 {
1165 int* buf = new int[outw + outh + outw * 2 + outh * 2];
1166
1167 int* xofs = buf; //new int[outw];
1168 int* yofs = buf + outw; //new int[outh];
1169
1170 __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 2];
1171 __fp16* beta = (__fp16*)(buf + outw + outh + outw * 2); //new __fp16[outh * 2];
1172
1173 linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
1174 linear_coeffs_fp16sa(h, outh, yofs, beta, align_corner);
1175
1176 #pragma omp parallel for num_threads(opt.num_threads)
1177 for (int q = 0; q < channels; q++)
1178 {
1179 const Mat src = bottom_blob.channel(q);
1180 Mat dst = top_blob.channel(q);
1181
1182 resize_bilinear_image_fp16sa(src, dst, alpha, xofs, beta, yofs);
1183 }
1184
1185 delete[] buf;
1186 }
1187
1188 if (resize_type == 3) // bicubic
1189 {
1190 int* buf = new int[outw + outh + outw * 4 + outh * 4];
1191
1192 int* xofs = buf; //new int[outw];
1193 int* yofs = buf + outw; //new int[outh];
1194
1195 __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 4];
1196 __fp16* beta = (__fp16*)(buf + outw + outh + outw * 4); //new __fp16[outh * 4];
1197
1198 cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner);
1199 cubic_coeffs_fp16sa(h, outh, yofs, beta, align_corner);
1200
1201 #pragma omp parallel for num_threads(opt.num_threads)
1202 for (int q = 0; q < channels; q++)
1203 {
1204 const Mat src = bottom_blob.channel(q);
1205 Mat dst = top_blob.channel(q);
1206
1207 resize_bicubic_image_fp16sa(src, dst, alpha, xofs, beta, yofs);
1208 }
1209
1210 delete[] buf;
1211 }
1212
1213 return 0;
1214}
1215#endif // __riscv_vector && __riscv_zfh
1216
1217} // namespace ncnn