Elffuss BraidHub

kikocis/ncnn public

master · 41.1 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 "convolution_riscv.h"
16
17#include "benchmark.h"
18#include "cpu.h"
19#include "layer_type.h"
20
21#if __riscv_vector
22#include <riscv_vector.h>
23#endif // __riscv_vector
24
25#include "riscv_activation.h"
26#include "riscv_usability.h"
27
28#include "cpu.h"
29
30namespace ncnn {
31
32#include "convolution_sgemm.h"
33#include "convolution_winograd_transform.h"
34#include "convolution_winograd_dot.h"
35#include "convolution_1x1.h"
36#include "convolution_3x3.h"
37
38#if __riscv_vector
39#include "convolution_packn.h"
40#include "convolution_pack1ton.h"
41#include "convolution_packnto1.h"
42
43#include "convolution_sgemm_packn.h"
44#include "convolution_sgemm_pack1ton.h"
45#include "convolution_sgemm_packnto1.h"
46#include "convolution_winograd_transform_packn.h"
47#include "convolution_winograd_dot_packn.h"
48#include "convolution_1x1_packn.h"
49#include "convolution_1x1_pack1ton.h"
50#include "convolution_1x1_packnto1.h"
51#include "convolution_3x3_packn.h"
52#include "convolution_3x3_pack1ton.h"
53#include "convolution_7x7_pack1ton.h"
54
55#if __riscv_zfh
56#include "convolution_fp16s.h"
57#include "convolution_packn_fp16s.h"
58#include "convolution_pack1ton_fp16s.h"
59#include "convolution_packnto1_fp16s.h"
60
61#include "convolution_sgemm_fp16s.h"
62#include "convolution_sgemm_packn_fp16s.h"
63#include "convolution_sgemm_pack1ton_fp16s.h"
64#include "convolution_sgemm_packnto1_fp16s.h"
65#include "convolution_winograd_transform_packn_fp16s.h"
66#include "convolution_winograd_dot_packn_fp16s.h"
67#include "convolution_1x1_fp16s.h"
68#include "convolution_1x1_packn_fp16s.h"
69#include "convolution_1x1_pack1ton_fp16s.h"
70#include "convolution_1x1_packnto1_fp16s.h"
71#include "convolution_3x3_packn_fp16s.h"
72#include "convolution_3x3_pack1ton_fp16s.h"
73#include "convolution_7x7_pack1ton_fp16s.h"
74
75#endif
76#endif // __riscv_vector
77
78Convolution_riscv::Convolution_riscv()
79{
80#if __riscv_vector
81 support_packing = true;
82#if __riscv_zfh
83 support_fp16_storage = true;
84#endif
85#endif // __riscv_vector
86
87 activation = 0;
88}
89
90static void convolution_transform_kernel_packed_rvv(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack)
91{
92 const int maxk = kernel_w * kernel_h;
93
94 // src = kw-kh-inch-outch
95 // dst = pb-pa-kw-kh-inch/pa-outch/pb
96 {
97 Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
98
99 weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack);
100
101 for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
102 {
103 float* g00 = weight_data_tm.channel(q / out_elempack);
104
105 for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
106 {
107 for (int k = 0; k < maxk; k++)
108 {
109 for (int i = 0; i < elempack; i++)
110 {
111 for (int j = 0; j < out_elempack; j++)
112 {
113 const float* k00 = weight_data_r2.channel(q + j).row(p + i);
114
115 g00[0] = k00[k];
116
117 g00++;
118 }
119 }
120 }
121 }
122 }
123 }
124}
125
126int Convolution_riscv::create_pipeline(const Option& opt)
127{
128 if (dynamic_weight)
129 return 0;
130
131 activation = create_activation_layer(activation_type, activation_params, opt);
132
133#if NCNN_INT8
134 if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u)
135 {
136 // TODO implement int8
137 return 0;
138 }
139#endif
140
141#if __riscv_vector && __riscv_zfh
142 if (opt.use_fp16_storage)
143 {
144 return create_pipeline_fp16s(opt);
145 }
146#endif
147
148#if __riscv_vector
149 const int packn = csrr_vlenb() / 4;
150#endif
151
152 const int maxk = kernel_w * kernel_h;
153 const int num_input = weight_data_size / maxk / num_output;
154
155 int elempack = 1;
156 int out_elempack = 1;
157#if __riscv_vector
158 if (opt.use_packing_layout)
159 {
160 elempack = num_input % packn == 0 ? packn : 1;
161 out_elempack = num_output % packn == 0 ? packn : 1;
162 }
163#endif
164
165#if __riscv_vector
166 // packn
167 if (elempack == packn && out_elempack == packn)
168 {
169 if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
170 {
171 if ((opt.use_winograd63_convolution && num_input >= packn * 2 && num_output >= packn * 2 && num_input <= packn * 16 && num_output <= packn * 16) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
172 conv3x3s1_winograd63_transform_kernel_packn_rvv(weight_data, weight_winograd63_data, num_input, num_output, opt);
173 else if ((opt.use_winograd43_convolution && num_input >= packn * 2 && num_output >= packn * 2) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
174 conv3x3s1_winograd43_transform_kernel_packn_rvv(weight_data, weight_winograd43_data, num_input, num_output, opt);
175 else // if (opt.use_winograd23_convolution)
176 conv3x3s1_winograd23_transform_kernel_packn_rvv(weight_data, weight_winograd23_data, num_input, num_output, opt);
177 }
178 else
179 {
180 convolution_transform_kernel_packed_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
181 }
182 }
183
184 // pack1ton
185 if (elempack == 1 && out_elempack == packn)
186 {
187 convolution_transform_kernel_packed_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
188 }
189
190 // packnto1
191 if (elempack == packn && out_elempack == 1)
192 {
193 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
194 {
195 convolution_im2col_sgemm_transform_kernel_packnto1_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
196 }
197 else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
198 {
199 convolution_im2col_sgemm_transform_kernel_packnto1_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
200 }
201 else if (opt.use_sgemm_convolution)
202 {
203 convolution_im2col_sgemm_transform_kernel_packnto1_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
204 }
205 else
206 {
207 convolution_transform_kernel_packed_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
208 }
209 }
210#endif // __riscv_vector
211
212 // pack1
213 if (elempack == 1 && out_elempack == 1)
214 {
215 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
216 {
217 convolution_im2col_sgemm_transform_kernel_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
218 }
219 else if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
220 {
221 if ((opt.use_winograd43_convolution && num_input >= 16 && num_output >= 16) || !opt.use_winograd23_convolution)
222 {
223 conv3x3s1_winograd43_transform_kernel_rvv(weight_data, weight_winograd43_data, num_input, num_output, opt);
224 }
225 else if (opt.use_winograd23_convolution)
226 {
227 conv3x3s1_winograd23_transform_kernel_rvv(weight_data, weight_winograd23_data, num_input, num_output, opt);
228 }
229 }
230 else if (opt.use_sgemm_convolution)
231 {
232 convolution_im2col_sgemm_transform_kernel_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
233 }
234 else
235 {
236 weight_data_tm = weight_data;
237 }
238 }
239
240 if (opt.lightmode)
241 weight_data.release();
242
243 return 0;
244}
245
246int Convolution_riscv::destroy_pipeline(const Option& opt)
247{
248 if (activation)
249 {
250 activation->destroy_pipeline(opt);
251 delete activation;
252 activation = 0;
253 }
254
255 return 0;
256}
257
258int Convolution_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
259{
260#if NCNN_INT8
261 if (opt.use_int8_inference && int8_scale_term)
262 {
263 Mat bottom_blob_unpacked = bottom_blob;
264 if (bottom_blob.elempack != 1)
265 {
266 Option opt_pack1 = opt;
267 opt_pack1.blob_allocator = opt.workspace_allocator;
268
269 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
270 }
271
272 Mat bottom_blob_unpacked_fp32 = bottom_blob_unpacked;
273 if (bottom_blob_unpacked.elembits() == 16)
274 {
275 Option opt_pack1 = opt;
276 opt_pack1.blob_allocator = opt.workspace_allocator;
277
278 cast_float16_to_float32(bottom_blob_unpacked, bottom_blob_unpacked_fp32, opt_pack1);
279 }
280
281 Option opt_unpacked = opt;
282 opt_unpacked.use_packing_layout = false;
283 return Convolution::forward_int8(bottom_blob_unpacked_fp32, top_blob, opt_unpacked);
284 }
285#endif
286
287 // flattened blob, implement as InnerProduct
288 if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1)
289 {
290 Mat bottom_blob_3d;
291 if (bottom_blob.elemsize % 16 == 0)
292 {
293 bottom_blob_3d = bottom_blob;
294 bottom_blob_3d.dims = 3;
295 bottom_blob_3d.w = 1;
296 bottom_blob_3d.h = 1;
297 bottom_blob_3d.c = bottom_blob.w;
298 bottom_blob_3d.cstep = 1;
299 }
300 else
301 {
302 bottom_blob_3d = bottom_blob.reshape(1, 1, bottom_blob.w, opt.workspace_allocator);
303 }
304
305 Mat top_blob_3d;
306 int ret = forward(bottom_blob_3d, top_blob_3d, opt);
307 if (ret != 0)
308 return ret;
309
310 if (top_blob_3d.elemsize % 16 == 0)
311 {
312 top_blob = top_blob_3d;
313 top_blob.dims = 1;
314 top_blob.w = top_blob_3d.c;
315 top_blob.h = 1;
316 top_blob.c = 1;
317 bottom_blob_3d.cstep = top_blob_3d.c;
318 }
319 else
320 {
321 top_blob = top_blob_3d.reshape(top_blob_3d.c, opt.blob_allocator);
322 }
323
324 return 0;
325 }
326
327 int elembits = bottom_blob.elembits();
328
329#if __riscv_vector && __riscv_zfh
330 if (opt.use_fp16_storage && elembits == 16)
331 {
332 if (opt.use_fp16_arithmetic)
333 return forward_fp16sa(bottom_blob, top_blob, opt);
334 else
335 return forward_fp16s(bottom_blob, top_blob, opt);
336 }
337#endif
338
339#if __riscv_vector
340 const int packn = csrr_vlenb() / 4;
341#endif
342
343 int w = bottom_blob.w;
344 int h = bottom_blob.h;
345 int channels = bottom_blob.c;
346 size_t elemsize = bottom_blob.elemsize;
347 int elempack = bottom_blob.elempack;
348
349 // NCNN_LOGE("Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
350
351 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
352 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
353
354 Mat bottom_blob_bordered;
355 make_padding(bottom_blob, bottom_blob_bordered, opt);
356 if (bottom_blob_bordered.empty())
357 return -100;
358
359 w = bottom_blob_bordered.w;
360 h = bottom_blob_bordered.h;
361
362 int outw = (w - kernel_extent_w) / stride_w + 1;
363 int outh = (h - kernel_extent_h) / stride_h + 1;
364 int out_elempack = 1;
365#if __riscv_vector
366 if (opt.use_packing_layout)
367 {
368 out_elempack = num_output % packn == 0 ? packn : 1;
369 }
370#endif
371 size_t out_elemsize = elemsize / elempack * out_elempack;
372
373 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
374 if (top_blob.empty())
375 return -100;
376
377 const int num_input = channels * elempack;
378
379#if __riscv_vector
380 if (elempack == packn && out_elempack == packn)
381 {
382 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
383 {
384 conv1x1s1_sgemm_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
385
386 if (activation)
387 {
388 activation->forward_inplace(top_blob, opt);
389 }
390 }
391 else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
392 {
393 conv1x1s2_sgemm_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
394
395 if (activation)
396 {
397 activation->forward_inplace(top_blob, opt);
398 }
399 }
400 else if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
401 {
402 if ((opt.use_winograd63_convolution && num_input >= packn * 2 && num_output >= packn * 2 && num_input <= packn * 16 && num_output <= packn * 16) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
403 conv3x3s1_winograd63_packn_rvv(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data, opt);
404 else if ((opt.use_winograd43_convolution && num_input >= packn * 2 && num_output >= packn * 2) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
405 conv3x3s1_winograd43_packn_rvv(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, opt);
406 else // if (opt.use_winograd23_convolution)
407 conv3x3s1_winograd23_packn_rvv(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, opt);
408
409 if (activation)
410 {
411 activation->forward_inplace(top_blob, opt);
412 }
413 }
414 else if (opt.use_sgemm_convolution)
415 {
416 convolution_im2col_sgemm_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
417
418 if (activation)
419 {
420 activation->forward_inplace(top_blob, opt);
421 }
422 }
423 else
424 {
425 convolution_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
426 }
427 }
428
429 if (elempack == 1 && out_elempack == packn)
430 {
431 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
432 {
433 conv1x1s1_sgemm_pack1ton_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
434
435 if (activation)
436 {
437 activation->forward_inplace(top_blob, opt);
438 }
439 }
440 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
441 {
442 conv3x3s1_pack1ton_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
443
444 if (activation)
445 {
446 activation->forward_inplace(top_blob, opt);
447 }
448 }
449 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
450 {
451 conv3x3s2_pack1ton_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
452
453 if (activation)
454 {
455 activation->forward_inplace(top_blob, opt);
456 }
457 }
458 else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
459 {
460 conv7x7s2_pack1ton_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
461
462 if (activation)
463 {
464 activation->forward_inplace(top_blob, opt);
465 }
466 }
467 else if (opt.use_sgemm_convolution)
468 {
469 convolution_im2col_sgemm_pack1ton_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
470
471 if (activation)
472 {
473 activation->forward_inplace(top_blob, opt);
474 }
475 }
476 else
477 {
478 convolution_pack1ton_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
479 }
480 }
481
482 if (elempack == packn && out_elempack == 1)
483 {
484 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
485 {
486 conv1x1s1_sgemm_packnto1_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
487
488 if (activation)
489 {
490 activation->forward_inplace(top_blob, opt);
491 }
492 }
493 else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
494 {
495 conv1x1s2_sgemm_packnto1_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
496
497 if (activation)
498 {
499 activation->forward_inplace(top_blob, opt);
500 }
501 }
502 else if (opt.use_sgemm_convolution)
503 {
504 convolution_im2col_sgemm_packnto1_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
505
506 if (activation)
507 {
508 activation->forward_inplace(top_blob, opt);
509 }
510 }
511 else
512 {
513 convolution_packnto1_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
514 }
515 }
516#endif // __riscv_vector
517
518 if (elempack == 1 && out_elempack == 1)
519 {
520 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
521 {
522 conv1x1s1_sgemm_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
523
524 if (activation)
525 {
526 activation->forward_inplace(top_blob, opt);
527 }
528 }
529 else if (opt.use_winograd_convolution && (opt.use_winograd43_convolution || opt.use_winograd23_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
530 {
531 if ((opt.use_winograd43_convolution && num_input >= 16 && num_output >= 16) || !opt.use_winograd23_convolution)
532 {
533 conv3x3s1_winograd43_rvv(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, opt);
534 }
535 else if (opt.use_winograd23_convolution)
536 {
537 conv3x3s1_winograd23_rvv(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, opt);
538 }
539
540 if (activation)
541 {
542 activation->forward_inplace(top_blob, opt);
543 }
544 }
545 else if (opt.use_sgemm_convolution)
546 {
547 convolution_im2col_sgemm_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
548
549 if (activation)
550 {
551 activation->forward_inplace(top_blob, opt);
552 }
553 }
554 else
555 {
556 const int maxk = kernel_w * kernel_h;
557
558 // kernel offsets
559 std::vector<int> _space_ofs(maxk);
560 int* space_ofs = &_space_ofs[0];
561 {
562 int p1 = 0;
563 int p2 = 0;
564 int gap = w * dilation_h - kernel_w * dilation_w;
565 for (int i = 0; i < kernel_h; i++)
566 {
567 for (int j = 0; j < kernel_w; j++)
568 {
569 space_ofs[p1] = p2;
570 p1++;
571 p2 += dilation_w;
572 }
573 p2 += gap;
574 }
575 }
576
577 // num_output
578 #pragma omp parallel for num_threads(opt.num_threads)
579 for (int p = 0; p < num_output; p++)
580 {
581 float* outptr = top_blob.channel(p);
582
583 for (int i = 0; i < outh; i++)
584 {
585 for (int j = 0; j < outw; j++)
586 {
587 float sum = 0.f;
588
589 if (bias_term)
590 {
591 sum = bias_data[p];
592 }
593
594 const float* kptr = (const float*)weight_data_tm + maxk * channels * p;
595
596 // channels
597 for (int q = 0; q < channels; q++)
598 {
599 const Mat m = bottom_blob_bordered.channel(q);
600 const float* sptr = m.row(i * stride_h) + j * stride_w;
601
602 for (int k = 0; k < maxk; k++)
603 {
604 float val = sptr[space_ofs[k]];
605 float wt = kptr[k];
606 sum += val * wt;
607 }
608
609 kptr += maxk;
610 }
611
612 sum = activation_ss(sum, activation_type, activation_params);
613
614 outptr[j] = sum;
615 }
616
617 outptr += outw;
618 }
619 }
620 }
621 }
622
623 return 0;
624}
625
626int Convolution_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
627{
628 const Mat& bottom_blob = bottom_blobs[0];
629 const Mat& _weight_data = bottom_blobs[1];
630 Mat& top_blob = top_blobs[0];
631
632 const int _kernel_w = _weight_data.w;
633 const int _kernel_h = _weight_data.h;
634 const int _num_output = _weight_data.c * _weight_data.elempack;
635
636 Mat weight_data_flattened;
637 flatten(_weight_data, weight_data_flattened, opt);
638 if (weight_data_flattened.empty())
639 return -100;
640
641#if NCNN_RVV
642 if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && weight_data_flattened.elembits() == 16)
643 {
644 Mat weight_data_flattened_fp32;
645 cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
646 weight_data_flattened = weight_data_flattened_fp32;
647 }
648#endif // NCNN_RVV
649
650 // weight_data_flattened as pack1
651 weight_data_flattened.w *= weight_data_flattened.elempack;
652 weight_data_flattened.elemsize /= weight_data_flattened.elempack;
653 weight_data_flattened.elempack = 1;
654
655 Mat bias_data_flattened;
656 if (bias_term)
657 {
658 const Mat& _bias_data = bottom_blobs[2];
659 flatten(_bias_data, bias_data_flattened, opt);
660 if (bias_data_flattened.empty())
661 return -100;
662
663#if NCNN_RVV
664 if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && bias_data_flattened.elembits() == 16)
665 {
666 Mat bias_data_flattened_fp32;
667 cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
668 bias_data_flattened = bias_data_flattened_fp32;
669 }
670#endif // NCNN_RVV
671
672 // bias_data_flattened as pack1
673 bias_data_flattened.w *= bias_data_flattened.elempack;
674 bias_data_flattened.elemsize /= bias_data_flattened.elempack;
675 bias_data_flattened.elempack = 1;
676 }
677
678 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution);
679
680 ncnn::ParamDict pd;
681 pd.set(0, _num_output);
682 pd.set(1, _kernel_w);
683 pd.set(11, _kernel_h);
684 pd.set(2, dilation_w);
685 pd.set(12, dilation_h);
686 pd.set(3, stride_w);
687 pd.set(13, stride_h);
688 pd.set(4, pad_left);
689 pd.set(15, pad_right);
690 pd.set(14, pad_top);
691 pd.set(16, pad_bottom);
692 pd.set(18, pad_value);
693 pd.set(5, bias_term);
694 pd.set(6, weight_data_flattened.w);
695 pd.set(8, int8_scale_term);
696 pd.set(9, activation_type);
697 pd.set(10, activation_params);
698
699 op->load_param(pd);
700
701 ncnn::Mat weights[2];
702 weights[0] = weight_data_flattened;
703 weights[1] = bias_data_flattened;
704
705 op->load_model(ncnn::ModelBinFromMatArray(weights));
706
707 op->create_pipeline(opt);
708
709 op->forward(bottom_blob, top_blob, opt);
710
711 op->destroy_pipeline(opt);
712
713 delete op;
714
715 return 0;
716}
717
718#if __riscv_vector && __riscv_zfh
719static void convolution_transform_kernel_packed_fp16s_rvv(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack)
720{
721 const int maxk = kernel_w * kernel_h;
722
723 // src = kw-kh-inch-outch
724 // dst = pb-pa-kw-kh-inch/pa-outch/pb
725 {
726 Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
727
728 weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)2u * elempack * out_elempack, elempack * out_elempack);
729
730 for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
731 {
732 __fp16* g00 = weight_data_tm.channel(q / out_elempack);
733
734 for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
735 {
736 for (int k = 0; k < maxk; k++)
737 {
738 for (int i = 0; i < elempack; i++)
739 {
740 for (int j = 0; j < out_elempack; j++)
741 {
742 const float* k00 = weight_data_r2.channel(q + j).row(p + i);
743
744 g00[0] = (__fp16)k00[k];
745
746 g00++;
747 }
748 }
749 }
750 }
751 }
752 }
753}
754
755int Convolution_riscv::create_pipeline_fp16s(const Option& opt)
756{
757 const int packn = csrr_vlenb() / 2;
758
759 const int maxk = kernel_w * kernel_h;
760 const int num_input = weight_data_size / maxk / num_output;
761
762 int elempack = 1;
763 int out_elempack = 1;
764
765 if (opt.use_packing_layout)
766 {
767 elempack = num_input % packn == 0 ? packn : 1;
768 out_elempack = num_output % packn == 0 ? packn : 1;
769 }
770
771 // packn
772 if (elempack == packn && out_elempack == packn)
773 {
774 if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && opt.use_fp16_arithmetic && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
775 {
776 if ((opt.use_winograd63_convolution && num_input >= packn * 2 && num_output >= packn * 2 && num_input <= packn * 16 && num_output <= packn * 16) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
777 conv3x3s1_winograd63_transform_kernel_packn_fp16sa_rvv(weight_data, weight_winograd63_data, num_input, num_output, opt);
778 else if ((opt.use_winograd43_convolution && num_input >= packn * 2 && num_output >= packn * 2) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
779 conv3x3s1_winograd43_transform_kernel_packn_fp16sa_rvv(weight_data, weight_winograd43_data, num_input, num_output, opt);
780 else // if (opt.use_winograd23_convolution)
781 conv3x3s1_winograd23_transform_kernel_packn_fp16sa_rvv(weight_data, weight_winograd23_data, num_input, num_output, opt);
782 }
783 else
784 {
785 convolution_transform_kernel_packed_fp16s_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
786 }
787 }
788
789 // pack1ton
790 if (elempack == 1 && out_elempack == packn)
791 {
792 convolution_transform_kernel_packed_fp16s_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
793 }
794
795 // packnto1
796 if (elempack == packn && out_elempack == 1)
797 {
798 if (opt.use_fp16_arithmetic && kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
799 {
800 convolution_im2col_sgemm_transform_kernel_packnto1_fp16sa_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
801 }
802 else if (opt.use_fp16_arithmetic && kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
803 {
804 convolution_im2col_sgemm_transform_kernel_packnto1_fp16sa_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
805 }
806 else if (opt.use_fp16_arithmetic && opt.use_sgemm_convolution)
807 {
808 convolution_im2col_sgemm_transform_kernel_packnto1_fp16sa_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
809 }
810 else
811 {
812 convolution_transform_kernel_packed_fp16s_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
813 }
814 }
815
816 // pack1
817 if (elempack == 1 && out_elempack == 1)
818 {
819 if (opt.use_fp16_arithmetic && kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
820 {
821 convolution_im2col_sgemm_transform_kernel_fp16sa_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
822 }
823 else if (opt.use_fp16_arithmetic && opt.use_sgemm_convolution)
824 {
825 convolution_im2col_sgemm_transform_kernel_fp16sa_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
826 }
827 else
828 {
829 convolution_transform_kernel_packed_fp16s_rvv(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
830 }
831 }
832
833 if (opt.use_fp16_arithmetic)
834 {
835 ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt);
836 }
837
838 if (opt.lightmode)
839 weight_data.release();
840
841 return 0;
842}
843
844int Convolution_riscv::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
845{
846 const int packn = csrr_vlenb() / 2;
847
848 int w = bottom_blob.w;
849 int h = bottom_blob.h;
850 size_t elemsize = bottom_blob.elemsize;
851 int elempack = bottom_blob.elempack;
852
853 // NCNN_LOGE("Convolution forward_fp16s input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_left, pad_top, kernel_w, kernel_h, stride_w, stride_h);
854
855 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
856 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
857
858 Mat bottom_blob_bordered;
859 make_padding(bottom_blob, bottom_blob_bordered, opt);
860 if (bottom_blob_bordered.empty())
861 return -100;
862
863 w = bottom_blob_bordered.w;
864 h = bottom_blob_bordered.h;
865
866 int outw = (w - kernel_extent_w) / stride_w + 1;
867 int outh = (h - kernel_extent_h) / stride_h + 1;
868 int out_elempack = (opt.use_packing_layout && num_output % packn == 0) ? packn : 1;
869 size_t out_elemsize = elemsize / elempack * out_elempack;
870
871 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
872 if (top_blob.empty())
873 return -100;
874
875 if (elempack == packn && out_elempack == packn)
876 {
877 {
878 convolution_packn_fp16s_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
879 }
880 }
881
882 if (elempack == 1 && out_elempack == packn)
883 {
884 {
885 convolution_pack1ton_fp16s_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
886 }
887 }
888
889 if (elempack == packn && out_elempack == 1)
890 {
891 {
892 convolution_packnto1_fp16s_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
893 }
894 }
895
896 if (elempack == 1 && out_elempack == 1)
897 {
898 {
899 convolution_fp16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
900 }
901 }
902
903 return 0;
904}
905
906int Convolution_riscv::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
907{
908 const int packn = csrr_vlenb() / 2;
909
910 int w = bottom_blob.w;
911 int h = bottom_blob.h;
912 int channels = bottom_blob.c;
913 size_t elemsize = bottom_blob.elemsize;
914 int elempack = bottom_blob.elempack;
915
916 // NCNN_LOGE("Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
917
918 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
919 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
920
921 Mat bottom_blob_bordered;
922 make_padding(bottom_blob, bottom_blob_bordered, opt);
923 if (bottom_blob_bordered.empty())
924 return -100;
925
926 w = bottom_blob_bordered.w;
927 h = bottom_blob_bordered.h;
928
929 int outw = (w - kernel_extent_w) / stride_w + 1;
930 int outh = (h - kernel_extent_h) / stride_h + 1;
931 int out_elempack = (opt.use_packing_layout && num_output % packn == 0) ? packn : 1;
932 size_t out_elemsize = elemsize / elempack * out_elempack;
933
934 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
935 if (top_blob.empty())
936 return -100;
937
938 const int num_input = channels * elempack;
939
940 if (elempack == packn && out_elempack == packn)
941 {
942 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
943 {
944 conv1x1s1_sgemm_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
945
946 if (activation)
947 {
948 activation->forward_inplace(top_blob, opt);
949 }
950 }
951 else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
952 {
953 conv1x1s2_sgemm_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
954
955 if (activation)
956 {
957 activation->forward_inplace(top_blob, opt);
958 }
959 }
960 else if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
961 {
962 if ((opt.use_winograd63_convolution && num_input >= packn * 2 && num_output >= packn * 2 && num_input <= packn * 16 && num_output <= packn * 16) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
963 conv3x3s1_winograd63_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data_fp16, opt);
964 else if ((opt.use_winograd43_convolution && num_input >= packn * 2 && num_output >= packn * 2) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
965 conv3x3s1_winograd43_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data_fp16, opt);
966 else // if (opt.use_winograd23_convolution)
967 conv3x3s1_winograd23_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data_fp16, opt);
968
969 if (activation)
970 {
971 activation->forward_inplace(top_blob, opt);
972 }
973 }
974 else if (opt.use_sgemm_convolution)
975 {
976 convolution_im2col_sgemm_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
977
978 if (activation)
979 {
980 activation->forward_inplace(top_blob, opt);
981 }
982 }
983 else
984 {
985 convolution_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
986 }
987 }
988
989 if (elempack == 1 && out_elempack == packn)
990 {
991 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
992 {
993 conv1x1s1_sgemm_pack1ton_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
994
995 if (activation)
996 {
997 activation->forward_inplace(top_blob, opt);
998 }
999 }
1000 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
1001 {
1002 conv3x3s1_pack1ton_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
1003
1004 if (activation)
1005 {
1006 activation->forward_inplace(top_blob, opt);
1007 }
1008 }
1009 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
1010 {
1011 conv3x3s2_pack1ton_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
1012
1013 if (activation)
1014 {
1015 activation->forward_inplace(top_blob, opt);
1016 }
1017 }
1018 else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
1019 {
1020 conv7x7s2_pack1ton_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
1021
1022 if (activation)
1023 {
1024 activation->forward_inplace(top_blob, opt);
1025 }
1026 }
1027 else if (opt.use_sgemm_convolution)
1028 {
1029 convolution_im2col_sgemm_pack1ton_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
1030
1031 if (activation)
1032 {
1033 activation->forward_inplace(top_blob, opt);
1034 }
1035 }
1036 else
1037 {
1038 convolution_pack1ton_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
1039 }
1040 }
1041
1042 if (elempack == packn && out_elempack == 1)
1043 {
1044 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
1045 {
1046 conv1x1s1_sgemm_packnto1_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
1047
1048 if (activation)
1049 {
1050 activation->forward_inplace(top_blob, opt);
1051 }
1052 }
1053 else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
1054 {
1055 conv1x1s2_sgemm_packnto1_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
1056
1057 if (activation)
1058 {
1059 activation->forward_inplace(top_blob, opt);
1060 }
1061 }
1062 else if (opt.use_sgemm_convolution)
1063 {
1064 convolution_im2col_sgemm_packnto1_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
1065
1066 if (activation)
1067 {
1068 activation->forward_inplace(top_blob, opt);
1069 }
1070 }
1071 else
1072 {
1073 convolution_packnto1_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
1074 }
1075 }
1076
1077 if (elempack == 1 && out_elempack == 1)
1078 {
1079 if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
1080 {
1081 conv1x1s1_sgemm_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
1082
1083 if (activation)
1084 {
1085 activation->forward_inplace(top_blob, opt);
1086 }
1087 }
1088 else if (opt.use_sgemm_convolution)
1089 {
1090 convolution_im2col_sgemm_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
1091
1092 if (activation)
1093 {
1094 activation->forward_inplace(top_blob, opt);
1095 }
1096 }
1097 else
1098 {
1099 convolution_fp16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
1100 }
1101 }
1102
1103 return 0;
1104}
1105#endif // __riscv_vector && __riscv_zfh
1106
1107} // namespace ncnn