Elffuss BraidHub

kikocis/ncnn public

master · 22.4 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 "padding_riscv.h"
16
17#if __riscv_vector
18#include <riscv_vector.h>
19#endif // __riscv_vector
20
21#include "riscv_usability.h"
22
23namespace ncnn {
24
25#if __riscv_vector
26#include "padding_packn.h"
27#endif // __riscv_vector
28
29Padding_riscv::Padding_riscv()
30{
31#if __riscv_vector
32 support_packing = true;
33#if __riscv_zfh
34 support_fp16_storage = true;
35#endif
36#endif // __riscv_vector
37
38#if NCNN_BF16
39 support_bf16_storage = true;
40#endif
41}
42
43int Padding_riscv::create_pipeline(const Option& opt)
44{
45#if __riscv_vector && __riscv_zfh
46 if (opt.use_fp16_storage)
47 {
48 ncnn::cast_float32_to_float16(per_channel_pad_data, per_channel_pad_data_fp16, opt);
49 }
50#endif
51
52#if NCNN_BF16
53 if (opt.use_bf16_storage)
54 {
55 value_bf16 = float32_to_bfloat16(value);
56
57 ncnn::cast_float32_to_bfloat16(per_channel_pad_data, per_channel_pad_data_bf16, opt);
58 }
59#endif
60
61 return 0;
62}
63
64int Padding_riscv::destroy_pipeline(const Option& /*opt*/)
65{
66 return 0;
67}
68
69int Padding_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
70{
71 if (top == 0 && bottom == 0 && left == 0 && right == 0 && front == 0 && behind == 0)
72 {
73 top_blob = bottom_blob;
74 return 0;
75 }
76
77 int elembits = bottom_blob.elembits();
78
79 if (elembits == 8)
80 return forward_int8(bottom_blob, top_blob, opt);
81
82#if __riscv_vector && __riscv_zfh
83 if (opt.use_fp16_storage && elembits == 16)
84 return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
85#endif
86
87#if NCNN_BF16
88 if (opt.use_bf16_storage && elembits == 16)
89 return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
90#endif
91
92#if __riscv_vector
93 const int packn = csrr_vlenb() / 4;
94 const size_t vl = vsetvl_e32m1(packn);
95#endif
96
97 int w = bottom_blob.w;
98 int h = bottom_blob.h;
99 int d = bottom_blob.d;
100 int channels = bottom_blob.c;
101 int dims = bottom_blob.dims;
102 size_t elemsize = bottom_blob.elemsize;
103 int elempack = bottom_blob.elempack;
104
105#if __riscv_vector
106 if (elempack == packn)
107 {
108 if (dims == 1)
109 {
110 int outw = w * elempack + left + right;
111
112 int out_elempack = outw % packn == 0 ? packn : 1;
113 size_t out_elemsize = elemsize / elempack * out_elempack;
114
115 if (left % packn == 0 && out_elempack == packn && type == 0)
116 {
117 top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
118 if (top_blob.empty())
119 return -100;
120
121 vfloat32m1_t pad_value = vfmv_v_f_f32m1(value, vl);
122 padding_constant_packn_float32_rvv(bottom_blob, top_blob, 0, 0, left / packn, right / packn, pad_value);
123
124 return 0;
125 }
126 }
127
128 if (dims == 2)
129 {
130 int outw = w + left + right;
131 int outh = h * elempack + top + bottom;
132
133 int out_elempack = outh % packn == 0 ? packn : 1;
134 size_t out_elemsize = elemsize / elempack * out_elempack;
135
136 if (top % packn == 0 && out_elempack == packn && type == 0)
137 {
138 top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
139 if (top_blob.empty())
140 return -100;
141
142 vfloat32m1_t pad_value = vfmv_v_f_f32m1(value, vl);
143 padding_constant_packn_float32_rvv(bottom_blob, top_blob, top / packn, bottom / packn, left, right, pad_value);
144
145 return 0;
146 }
147 }
148
149 if (dims == 3)
150 {
151 int outw = w + left + right;
152 int outh = h + top + bottom;
153 int outc = channels * elempack + front + behind;
154
155 int out_elempack = outc % packn == 0 ? packn : 1;
156 size_t out_elemsize = elemsize / elempack * out_elempack;
157
158 if (front % packn == 0 && out_elempack == packn && !(outc != channels * elempack && type != 0))
159 {
160 top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
161 if (top_blob.empty())
162 return -100;
163
164 int front_ = front / elempack;
165 #pragma omp parallel for num_threads(opt.num_threads)
166 for (int q = 0; q < outc / out_elempack; q++)
167 {
168 Mat borderm = top_blob.channel(q);
169
170 vfloat32m1_t pad_value = per_channel_pad_data_size ? vle32_v_f32m1((const float*)per_channel_pad_data + q * packn, vl) : vfmv_v_f_f32m1(value, vl);
171 //Channel padding
172 if ((q - front_) < 0 || (q - front_) >= channels)
173 {
174 borderm.fill(pad_value);
175 }
176 else
177 {
178 const Mat m = bottom_blob.channel(q - front_);
179 if (type == 0)
180 padding_constant_packn_float32_rvv(m, borderm, top, bottom, left, right, pad_value);
181 if (type == 1)
182 padding_replicate_packn_float32_rvv(m, borderm, top, bottom, left, right);
183 if (type == 2)
184 padding_reflect_packn_float32_rvv(m, borderm, top, bottom, left, right);
185 }
186 }
187
188 return 0;
189 }
190 }
191
192 if (dims == 4)
193 {
194 int outw = w + left + right;
195 int outh = h + top + bottom;
196 int outd = d + front + behind;
197
198 if (type == 0)
199 {
200 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
201 if (top_blob.empty())
202 return -100;
203
204 #pragma omp parallel for num_threads(opt.num_threads)
205 for (int q = 0; q < channels; q++)
206 {
207 vfloat32m1_t pad_value = per_channel_pad_data_size ? vle32_v_f32m1((const float*)per_channel_pad_data + q * packn, vl) : vfmv_v_f_f32m1(value, vl);
208
209 for (int z = 0; z < outd; z++)
210 {
211 Mat borderm = top_blob.channel(q).depth(z);
212
213 // depth padding
214 if ((z - front) < 0 || (z - front) >= d)
215 {
216 borderm.fill(pad_value);
217 }
218 else
219 {
220 const Mat m = bottom_blob.channel(q).depth(z - front);
221 padding_constant_packn_float32_rvv(m, borderm, top, bottom, left, right, pad_value);
222 }
223 }
224 }
225
226 return 0;
227 }
228 }
229 }
230#endif // __riscv_vector
231
232 Mat bottom_blob_unpacked = bottom_blob;
233 if (elempack != 1)
234 {
235 Option opt_pack1 = opt;
236 opt_pack1.blob_allocator = opt.workspace_allocator;
237
238 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
239 }
240
241 Mat top_blob_unpacked;
242 int ret = Padding::forward(bottom_blob_unpacked, top_blob_unpacked, opt);
243
244 if (ret != 0)
245 return ret;
246
247 int out_elempack = 1;
248#if __riscv_vector
249 if (opt.use_packing_layout)
250 {
251 out_elempack = top_blob_unpacked.c % packn == 0 ? packn : 1;
252 }
253#endif
254
255 convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
256
257 return 0;
258}
259
260int Padding_riscv::forward_bf16s_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
261{
262#if __riscv_vector
263 const int packn = csrr_vlenb() / 2;
264 const size_t vl = vsetvl_e16m1(packn);
265#endif
266
267 int w = bottom_blob.w;
268 int h = bottom_blob.h;
269 int d = bottom_blob.d;
270 int channels = bottom_blob.c;
271 int dims = bottom_blob.dims;
272 size_t elemsize = bottom_blob.elemsize;
273 int elempack = bottom_blob.elempack;
274
275#if __riscv_vector
276 if (elempack == packn)
277 {
278 if (dims == 1)
279 {
280 int outw = w * elempack + left + right;
281
282 int out_elempack = outw % packn == 0 ? packn : 1;
283 size_t out_elemsize = elemsize / elempack * out_elempack;
284
285 if (left % packn == 0 && out_elempack == packn && type == 0)
286 {
287 top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
288 if (top_blob.empty())
289 return -100;
290
291 // clang-format off
292 // *INDENT-OFF*
293 vuint16m1_t pad_value;
294#if __riscv_zfh
295 if (opt.use_fp16_storage)
296 {
297 pad_value = vreinterpret_v_f16m1_u16m1(vfmv_v_f_f16m1((__fp16)value, vl));
298 }
299 else
300#endif
301#if NCNN_BF16
302 if (opt.use_bf16_storage)
303 {
304 pad_value = vmv_v_x_u16m1(value_bf16, vl);
305 }
306 else
307#endif
308 {
309 }
310 // *INDENT-ON*
311 // clang-format on
312 padding_constant_packn_uint16_rvv(bottom_blob, top_blob, 0, 0, left / packn, right / packn, pad_value);
313
314 return 0;
315 }
316 }
317
318 if (dims == 2)
319 {
320 int outw = w + left + right;
321 int outh = h * elempack + top + bottom;
322
323 int out_elempack = outh % packn == 0 ? packn : 1;
324 size_t out_elemsize = elemsize / elempack * out_elempack;
325
326 if (top % packn == 0 && out_elempack == packn && type == 0)
327 {
328 top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
329 if (top_blob.empty())
330 return -100;
331
332 // clang-format off
333 // *INDENT-OFF*
334 vuint16m1_t pad_value;
335#if __riscv_zfh
336 if (opt.use_fp16_storage)
337 {
338 pad_value = vreinterpret_v_f16m1_u16m1(vfmv_v_f_f16m1((__fp16)value, vl));
339 }
340 else
341#endif
342#if NCNN_BF16
343 if (opt.use_bf16_storage)
344 {
345 pad_value = vmv_v_x_u16m1(value_bf16, vl);
346 }
347 else
348#endif
349 {
350 }
351 // *INDENT-ON*
352 // clang-format on
353 padding_constant_packn_uint16_rvv(bottom_blob, top_blob, top / packn, bottom / packn, left, right, pad_value);
354
355 return 0;
356 }
357 }
358
359 if (dims == 3)
360 {
361 int outw = w + left + right;
362 int outh = h + top + bottom;
363 int outc = channels * elempack + front + behind;
364
365 int out_elempack = outc % packn == 0 ? packn : 1;
366 size_t out_elemsize = elemsize / elempack * out_elempack;
367
368 if (front % packn == 0 && out_elempack == packn && !(outc != channels * elempack && type != 0))
369 {
370 top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
371 if (top_blob.empty())
372 return -100;
373
374 int front_ = front / elempack;
375 #pragma omp parallel for num_threads(opt.num_threads)
376 for (int q = 0; q < outc / out_elempack; q++)
377 {
378 Mat borderm = top_blob.channel(q);
379
380 // clang-format off
381 // *INDENT-OFF*
382 vuint16m1_t pad_value;
383#if __riscv_zfh
384 if (opt.use_fp16_storage)
385 {
386 pad_value = per_channel_pad_data_size ? vreinterpret_v_f16m1_u16m1(vle16_v_f16m1((const __fp16*)per_channel_pad_data_fp16 + q * packn, vl)) : vreinterpret_v_f16m1_u16m1(vfmv_v_f_f16m1((__fp16)value, vl));
387 }
388 else
389#endif
390#if NCNN_BF16
391 if (opt.use_bf16_storage)
392 {
393 pad_value = per_channel_pad_data_size ? vle16_v_u16m1((const unsigned short*)per_channel_pad_data_bf16 + q * packn, vl) : vmv_v_x_u16m1(value_bf16, vl);
394 }
395 else
396#endif
397 {
398 }
399 // *INDENT-ON*
400 // clang-format on
401
402 //Channel padding
403 if ((q - front_) < 0 || (q - front_) >= channels)
404 {
405 borderm.fill(pad_value);
406 }
407 else
408 {
409 const Mat m = bottom_blob.channel(q - front_);
410 if (type == 0)
411 padding_constant_packn_uint16_rvv(m, borderm, top, bottom, left, right, pad_value);
412 if (type == 1)
413 padding_replicate_packn_uint16_rvv(m, borderm, top, bottom, left, right);
414 if (type == 2)
415 padding_reflect_packn_uint16_rvv(m, borderm, top, bottom, left, right);
416 }
417 }
418
419 return 0;
420 }
421 }
422
423 if (dims == 4)
424 {
425 int outw = w + left + right;
426 int outh = h + top + bottom;
427 int outd = d + front + behind;
428
429 if (type == 0)
430 {
431 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
432 if (top_blob.empty())
433 return -100;
434
435 #pragma omp parallel for num_threads(opt.num_threads)
436 for (int q = 0; q < channels; q++)
437 {
438 // clang-format off
439 // *INDENT-OFF*
440 vuint16m1_t pad_value;
441#if __riscv_zfh
442 if (opt.use_fp16_storage)
443 {
444 pad_value = per_channel_pad_data_size ? vreinterpret_v_f16m1_u16m1(vle16_v_f16m1((const __fp16*)per_channel_pad_data_fp16 + q * packn, vl)) : vreinterpret_v_f16m1_u16m1(vfmv_v_f_f16m1((__fp16)value, vl));
445 }
446 else
447#endif
448#if NCNN_BF16
449 if (opt.use_bf16_storage)
450 {
451 pad_value = per_channel_pad_data_size ? vle16_v_u16m1((const unsigned short*)per_channel_pad_data_bf16 + q * packn, vl) : vmv_v_x_u16m1(value_bf16, vl);
452 }
453 else
454#endif
455 {
456 }
457 // *INDENT-ON*
458 // clang-format on
459
460 for (int z = 0; z < outd; z++)
461 {
462 Mat borderm = top_blob.channel(q).depth(z);
463
464 // depth padding
465 if ((z - front) < 0 || (z - front) >= d)
466 {
467 borderm.fill(pad_value);
468 }
469 else
470 {
471 const Mat m = bottom_blob.channel(q).depth(z - front);
472 padding_constant_packn_uint16_rvv(m, borderm, top, bottom, left, right, pad_value);
473 }
474 }
475 }
476
477 return 0;
478 }
479 }
480 }
481#endif // __riscv_vector
482
483 Mat bottom_blob_unpacked = bottom_blob;
484 if (elempack != 1)
485 {
486 Option opt_pack1 = opt;
487 opt_pack1.blob_allocator = opt.workspace_allocator;
488
489 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
490 }
491
492 Mat top_blob_unpacked;
493 int ret = Padding::forward(bottom_blob_unpacked, top_blob_unpacked, opt);
494 if (ret != 0)
495 return ret;
496
497 int out_elempack = 1;
498#if __riscv_vector
499 if (opt.use_packing_layout)
500 {
501 out_elempack = top_blob_unpacked.c % packn == 0 ? packn : 1;
502 }
503#endif
504
505 convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
506
507 return 0;
508}
509
510int Padding_riscv::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
511{
512#if __riscv_vector
513 const int packn = csrr_vlenb() / 1;
514 const size_t vl = vsetvl_e8m1(packn);
515#endif
516
517 int w = bottom_blob.w;
518 int h = bottom_blob.h;
519 int d = bottom_blob.d;
520 int channels = bottom_blob.c;
521 int dims = bottom_blob.dims;
522 size_t elemsize = bottom_blob.elemsize;
523 int elempack = bottom_blob.elempack;
524
525#if __riscv_vector
526 if (elempack == packn)
527 {
528 if (dims == 1)
529 {
530 int outw = w * elempack + left + right;
531
532 int out_elempack = outw % packn == 0 ? packn : 1;
533 size_t out_elemsize = elemsize / elempack * out_elempack;
534
535 if (left % packn == 0 && out_elempack == packn && type == 0)
536 {
537 top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
538 if (top_blob.empty())
539 return -100;
540
541 vint8m1_t pad_value = vmv_v_x_i8m1((signed char)value, vl);
542 padding_constant_packn_int8_rvv(bottom_blob, top_blob, 0, 0, left / packn, right / packn, pad_value);
543
544 return 0;
545 }
546 }
547
548 if (dims == 2)
549 {
550 int outw = w + left + right;
551 int outh = h * elempack + top + bottom;
552
553 int out_elempack = outh % packn == 0 ? packn : 1;
554 size_t out_elemsize = elemsize / elempack * out_elempack;
555
556 if (top % packn == 0 && out_elempack == packn && type == 0)
557 {
558 top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
559 if (top_blob.empty())
560 return -100;
561
562 vint8m1_t pad_value = vmv_v_x_i8m1((signed char)value, vl);
563 padding_constant_packn_int8_rvv(bottom_blob, top_blob, top / packn, bottom / packn, left, right, pad_value);
564
565 return 0;
566 }
567 }
568
569 if (dims == 3)
570 {
571 int outw = w + left + right;
572 int outh = h + top + bottom;
573 int outc = channels * elempack + front + behind;
574
575 int out_elempack = outc % packn == 0 ? packn : 1;
576 size_t out_elemsize = elemsize / elempack * out_elempack;
577
578 if (front % packn == 0 && out_elempack == packn && !(outc != channels * elempack && type != 0))
579 {
580 top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
581 if (top_blob.empty())
582 return -100;
583
584 int front_ = front / elempack;
585 #pragma omp parallel for num_threads(opt.num_threads)
586 for (int q = 0; q < outc / out_elempack; q++)
587 {
588 Mat borderm = top_blob.channel(q);
589
590 // TODO perchannel
591 // vint8m1_t pad_value = per_channel_pad_data_size ? vle8_v_i8m1(per_channel_pad_data + q * packn) : vmv_v_x_i8m1((signed char)value);
592 vint8m1_t pad_value = vmv_v_x_i8m1((signed char)value, vl);
593
594 //Channel padding
595 if ((q - front_) < 0 || (q - front_) >= channels)
596 {
597 borderm.fill(pad_value);
598 }
599 else
600 {
601 const Mat m = bottom_blob.channel(q - front_);
602 if (type == 0)
603 padding_constant_packn_int8_rvv(m, borderm, top, bottom, left, right, pad_value);
604 if (type == 1)
605 padding_replicate_packn_int8_rvv(m, borderm, top, bottom, left, right);
606 if (type == 2)
607 padding_reflect_packn_int8_rvv(m, borderm, top, bottom, left, right);
608 }
609 }
610
611 return 0;
612 }
613 }
614
615 if (dims == 4)
616 {
617 int outw = w + left + right;
618 int outh = h + top + bottom;
619 int outd = d + front + behind;
620
621 if (type == 0)
622 {
623 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
624 if (top_blob.empty())
625 return -100;
626
627 #pragma omp parallel for num_threads(opt.num_threads)
628 for (int q = 0; q < channels; q++)
629 {
630 // TODO perchannel
631 // vint8m1_t pad_value = per_channel_pad_data_size ? vle8_v_i8m1(per_channel_pad_data + q * packn) : vmv_v_x_i8m1((signed char)value);
632 vint8m1_t pad_value = vmv_v_x_i8m1((signed char)value, vl);
633
634 for (int z = 0; z < outd; z++)
635 {
636 Mat borderm = top_blob.channel(q).depth(z);
637
638 // depth padding
639 if ((z - front) < 0 || (z - front) >= d)
640 {
641 borderm.fill(pad_value);
642 }
643 else
644 {
645 const Mat m = bottom_blob.channel(q).depth(z - front);
646 padding_constant_packn_int8_rvv(m, borderm, top, bottom, left, right, pad_value);
647 }
648 }
649 }
650
651 return 0;
652 }
653 }
654 }
655#endif // __riscv_vector
656
657 Mat bottom_blob_unpacked = bottom_blob;
658 if (elempack != 1)
659 {
660 Option opt_pack1 = opt;
661 opt_pack1.blob_allocator = opt.workspace_allocator;
662
663 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
664 }
665
666 Mat top_blob_unpacked;
667 int ret = Padding::forward(bottom_blob_unpacked, top_blob_unpacked, opt);
668 if (ret != 0)
669 return ret;
670
671 int out_elempack = 1;
672#if __riscv_vector
673 if (opt.use_packing_layout)
674 {
675 out_elempack = top_blob_unpacked.c % packn == 0 ? packn : 1;
676 }
677#endif
678
679 convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
680
681 return 0;
682}
683
684} // namespace ncnn