1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
15
#include "convolutiondepthwise_riscv.h"
18
#include "layer_type.h"
21
#include <riscv_vector.h>
22
#endif // __riscv_vector
24
#include "riscv_activation.h"
25
#include "riscv_usability.h"
29
#include "convolutiondepthwise_3x3.h"
32
#include "convolutiondepthwise_3x3_packn.h"
33
#include "convolutiondepthwise_5x5_packn.h"
36
#include "convolutiondepthwise_3x3_packn_fp16s.h"
37
#include "convolutiondepthwise_5x5_packn_fp16s.h"
39
#endif // __riscv_vector
41
ConvolutionDepthWise_riscv::ConvolutionDepthWise_riscv()
44
support_packing = true;
46
support_fp16_storage = true;
48
#endif // __riscv_vector
53
int ConvolutionDepthWise_riscv::create_pipeline(const Option& opt)
58
activation = create_activation_layer(activation_type, activation_params, opt);
61
if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u)
63
// TODO implement int8
68
#if __riscv_vector && __riscv_zfh
69
if (opt.use_fp16_storage)
71
return create_pipeline_fp16s(opt);
76
const int packn = csrr_vlenb() / 4;
79
const int maxk = kernel_w * kernel_h;
80
int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
83
if (channels == group && group == num_output)
87
if (opt.use_packing_layout)
89
elempack = channels % packn == 0 ? packn : 1;
95
if (elempack == packn)
97
Mat weight_data_r2 = weight_data.reshape(maxk, group);
98
convert_packing(weight_data_r2, weight_data_tm, packn, opt);
100
#endif // __riscv_vector
104
weight_data_tm = weight_data;
108
weight_data.release();
114
create_group_ops(opt);
117
weight_data.release();
122
int ConvolutionDepthWise_riscv::create_group_ops(const Option& opt)
124
// create Convolution op for each group
125
const int maxk = kernel_w * kernel_h;
126
int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
128
for (int i = 0; i < (int)group_ops.size(); i++)
133
const int channels_g = channels / group;
134
const int num_output_g = num_output / group;
136
group_ops.resize(group);
138
for (int g = 0; g < group; g++)
140
Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g).clone();
143
bias_data_g = bias_data.range(num_output_g * g, num_output_g);
145
ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution);
149
pd.set(0, num_output_g); // num_output
151
pd.set(11, kernel_h);
152
pd.set(2, dilation_w);
153
pd.set(12, dilation_h);
155
pd.set(13, stride_h);
156
pd.set(4, 0); // pad_w
157
pd.set(14, 0); // pad_h
158
pd.set(5, bias_term);
159
pd.set(6, maxk * channels_g * num_output_g); // weight_data_size
160
pd.set(8, int8_scale_term);
161
pd.set(9, activation_type);
162
pd.set(10, activation_params);
169
ncnn::Mat weights[5];
170
weights[0] = weight_data_g;
171
weights[1] = bias_data_g;
176
Mat weight_data_int8_scales_g(num_output_g);
177
weight_data_int8_scales_g.fill(weight_data_int8_scales[g]);
178
weights[2] = weight_data_int8_scales_g;
179
weights[3] = bottom_blob_int8_scales.range(g, 1);
181
if (int8_scale_term > 100)
183
weights[4] = top_blob_int8_scales.range(g, 1);
187
op->load_model(ModelBinFromMatArray(weights));
191
ncnn::Mat weights[4];
192
weights[0] = weight_data_g;
197
Mat weight_data_int8_scales_g(num_output_g);
198
weight_data_int8_scales_g.fill(weight_data_int8_scales[g]);
199
weights[1] = weight_data_int8_scales_g;
200
weights[2] = bottom_blob_int8_scales.range(g, 1);
202
if (int8_scale_term > 100)
204
weights[3] = top_blob_int8_scales.range(g, 1);
208
op->load_model(ModelBinFromMatArray(weights));
211
op->create_pipeline(opt);
219
int ConvolutionDepthWise_riscv::destroy_pipeline(const Option& opt)
223
activation->destroy_pipeline(opt);
228
for (int i = 0; i < (int)group_ops.size(); i++)
230
group_ops[i]->destroy_pipeline(opt);
238
int ConvolutionDepthWise_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
241
if (opt.use_int8_inference && int8_scale_term)
243
Mat bottom_blob_unpacked = bottom_blob;
244
if (bottom_blob.elempack != 1)
246
Option opt_pack1 = opt;
247
opt_pack1.blob_allocator = opt.workspace_allocator;
249
convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
252
Mat bottom_blob_unpacked_fp32 = bottom_blob_unpacked;
253
if (bottom_blob_unpacked.elembits() == 16)
255
Option opt_pack1 = opt;
256
opt_pack1.blob_allocator = opt.workspace_allocator;
258
cast_float16_to_float32(bottom_blob_unpacked, bottom_blob_unpacked_fp32, opt_pack1);
261
Option opt_unpacked = opt;
262
opt_unpacked.use_packing_layout = false;
263
return ConvolutionDepthWise::forward_int8(bottom_blob_unpacked_fp32, top_blob, opt_unpacked);
267
int elembits = bottom_blob.elembits();
269
#if __riscv_vector && __riscv_zfh
270
if (opt.use_fp16_storage && elembits == 16)
272
if (opt.use_fp16_arithmetic)
273
return forward_fp16sa(bottom_blob, top_blob, opt);
275
return forward_fp16s(bottom_blob, top_blob, opt);
280
const int packn = csrr_vlenb() / 4;
281
const size_t vl = vsetvl_e32m1(packn);
284
int w = bottom_blob.w;
285
int h = bottom_blob.h;
286
int channels = bottom_blob.c;
287
size_t elemsize = bottom_blob.elemsize;
288
int elempack = bottom_blob.elempack;
290
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
291
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
293
Mat bottom_blob_bordered;
294
make_padding(bottom_blob, bottom_blob_bordered, opt);
295
if (bottom_blob_bordered.empty())
298
w = bottom_blob_bordered.w;
299
h = bottom_blob_bordered.h;
301
int outw = (w - kernel_extent_w) / stride_w + 1;
302
int outh = (h - kernel_extent_h) / stride_h + 1;
303
int out_elempack = 1;
305
if (opt.use_packing_layout)
307
out_elempack = num_output % packn == 0 ? packn : 1;
310
size_t out_elemsize = elemsize / elempack * out_elempack;
312
top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
313
if (top_blob.empty())
317
if (channels * elempack == group && group == num_output)
320
if (elempack == packn)
322
if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
324
convdw3x3s1_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
328
activation->forward_inplace(top_blob, opt);
331
else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
333
convdw3x3s2_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
337
activation->forward_inplace(top_blob, opt);
340
else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
342
convdw5x5s1_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
346
activation->forward_inplace(top_blob, opt);
349
else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
351
convdw5x5s2_packn_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
355
activation->forward_inplace(top_blob, opt);
360
const int maxk = kernel_w * kernel_h;
363
std::vector<int> _space_ofs(maxk);
364
int* space_ofs = &_space_ofs[0];
368
int gap = w * dilation_h - kernel_w * dilation_w;
369
for (int i = 0; i < kernel_h; i++)
371
for (int j = 0; j < kernel_w; j++)
381
#pragma omp parallel for num_threads(opt.num_threads)
382
for (int g = 0; g < channels; g++)
384
float* outptr = top_blob.channel(g);
385
const float* kptr = (const float*)weight_data_tm + maxk * g * packn;
386
const Mat m = bottom_blob_bordered.channel(g);
388
for (int i = 0; i < outh; i++)
390
for (int j = 0; j < outw; j++)
392
vfloat32m1_t _sum = vfmv_v_f_f32m1(0.f, vl);
396
_sum = vle32_v_f32m1((const float*)bias_data + g * packn, vl);
399
const float* sptr = m.row(i * stride_h) + j * stride_w * packn;
401
for (int k = 0; k < maxk; k++)
403
vfloat32m1_t _val = vle32_v_f32m1(sptr + space_ofs[k] * packn, vl);
404
vfloat32m1_t _w = vle32_v_f32m1(kptr + k * packn, vl);
405
_sum = vfmacc_vv_f32m1(_sum, _val, _w, vl);
408
_sum = activation_ps(_sum, activation_type, activation_params, vl);
410
vse32_v_f32m1(outptr + j * packn, _sum, vl);
413
outptr += outw * packn;
418
#endif // __riscv_vector
422
if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
424
convdw3x3s1_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
428
activation->forward_inplace(top_blob, opt);
431
else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
433
convdw3x3s2_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
437
activation->forward_inplace(top_blob, opt);
442
const int maxk = kernel_w * kernel_h;
445
std::vector<int> _space_ofs(maxk);
446
int* space_ofs = &_space_ofs[0];
450
int gap = w * dilation_h - kernel_w * dilation_w;
451
for (int i = 0; i < kernel_h; i++)
453
for (int j = 0; j < kernel_w; j++)
463
#pragma omp parallel for num_threads(opt.num_threads)
464
for (int g = 0; g < group; g++)
466
float* outptr = top_blob.channel(g);
467
const float* kptr = (const float*)weight_data_tm + maxk * g;
468
const Mat m = bottom_blob_bordered.channel(g);
470
for (int i = 0; i < outh; i++)
472
for (int j = 0; j < outw; j++)
479
const float* sptr = m.row(i * stride_h) + j * stride_w;
481
for (int k = 0; k < maxk; k++)
483
float val = (float)sptr[space_ofs[k]];
484
float w = (float)kptr[k];
488
sum = activation_ss(sum, activation_type, activation_params);
503
const int channels_g = channels * elempack / group;
504
const int num_output_g = num_output / group;
507
int out_g_elempack = 1;
509
if (opt.use_packing_layout)
511
g_elempack = channels_g % packn == 0 ? packn : 1;
512
out_g_elempack = num_output_g % packn == 0 ? packn : 1;
517
Mat bottom_blob_bordered_unpacked = bottom_blob_bordered;
518
if (elempack > g_elempack)
521
opt_p.blob_allocator = opt.workspace_allocator;
522
convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, 1, opt_p);
525
Mat top_blob_unpacked = top_blob;
526
if (out_g_elempack < out_elempack)
528
top_blob_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator);
529
if (top_blob_unpacked.empty())
533
for (int g = 0; g < group; g++)
535
const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
536
Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
538
const ncnn::Layer* op = group_ops[g];
541
opt_g.blob_allocator = top_blob_unpacked.allocator;
544
op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
548
if (out_g_elempack < out_elempack)
550
convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
554
top_blob = top_blob_unpacked;
560
int ConvolutionDepthWise_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
562
const Mat& bottom_blob = bottom_blobs[0];
563
const Mat& _weight_data = bottom_blobs[1];
564
Mat& top_blob = top_blobs[0];
566
const int _kernel_w = _weight_data.w;
567
const int _kernel_h = _weight_data.h;
568
const int _num_output = _weight_data.c * _weight_data.elempack;
570
Mat weight_data_flattened;
571
flatten(_weight_data, weight_data_flattened, opt);
572
if (weight_data_flattened.empty())
576
if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && weight_data_flattened.elembits() == 16)
578
Mat weight_data_flattened_fp32;
579
cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
580
weight_data_flattened = weight_data_flattened_fp32;
584
// weight_data_flattened as pack1
585
weight_data_flattened.w *= weight_data_flattened.elempack;
586
weight_data_flattened.elemsize /= weight_data_flattened.elempack;
587
weight_data_flattened.elempack = 1;
589
Mat bias_data_flattened;
592
const Mat& _bias_data = bottom_blobs[2];
593
flatten(_bias_data, bias_data_flattened, opt);
594
if (bias_data_flattened.empty())
598
if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && bias_data_flattened.elembits() == 16)
600
Mat bias_data_flattened_fp32;
601
cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
602
bias_data_flattened = bias_data_flattened_fp32;
606
// bias_data_flattened as pack1
607
bias_data_flattened.w *= bias_data_flattened.elempack;
608
bias_data_flattened.elemsize /= bias_data_flattened.elempack;
609
bias_data_flattened.elempack = 1;
612
ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::ConvolutionDepthWise);
615
pd.set(0, _num_output);
616
pd.set(1, _kernel_w);
617
pd.set(11, _kernel_h);
618
pd.set(2, dilation_w);
619
pd.set(12, dilation_h);
621
pd.set(13, stride_h);
623
pd.set(15, pad_right);
625
pd.set(16, pad_bottom);
626
pd.set(18, pad_value);
627
pd.set(5, bias_term);
628
pd.set(6, weight_data_flattened.w);
630
pd.set(8, int8_scale_term);
631
pd.set(9, activation_type);
632
pd.set(10, activation_params);
636
ncnn::Mat weights[2];
637
weights[0] = weight_data_flattened;
638
weights[1] = bias_data_flattened;
640
op->load_model(ncnn::ModelBinFromMatArray(weights));
642
op->create_pipeline(opt);
644
op->forward(bottom_blob, top_blob, opt);
646
op->destroy_pipeline(opt);
653
#if __riscv_vector && __riscv_zfh
654
int ConvolutionDepthWise_riscv::create_pipeline_fp16s(const Option& opt)
656
const int packn = csrr_vlenb() / 2;
658
const int maxk = kernel_w * kernel_h;
659
int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
662
if (channels == group && group == num_output)
665
if (opt.use_packing_layout)
667
elempack = channels % packn == 0 ? packn : 1;
671
if (elempack == packn)
673
Mat weight_data_r2 = weight_data.reshape(maxk, group);
674
Mat weight_data_r2_packed;
675
convert_packing(weight_data_r2, weight_data_r2_packed, packn, opt);
677
ncnn::cast_float32_to_float16(weight_data_r2_packed, weight_data_tm, opt);
682
ncnn::cast_float32_to_float16(weight_data, weight_data_tm, opt);
685
ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt);
688
weight_data.release();
694
create_group_ops(opt);
697
weight_data.release();
702
int ConvolutionDepthWise_riscv::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
704
const int packn = csrr_vlenb() / 2;
705
const size_t vl = vsetvl_e16m1(packn);
707
int w = bottom_blob.w;
708
int h = bottom_blob.h;
709
int channels = bottom_blob.c;
710
size_t elemsize = bottom_blob.elemsize;
711
int elempack = bottom_blob.elempack;
713
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
714
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
716
Mat bottom_blob_bordered;
717
make_padding(bottom_blob, bottom_blob_bordered, opt);
718
if (bottom_blob_bordered.empty())
721
w = bottom_blob_bordered.w;
722
h = bottom_blob_bordered.h;
724
int outw = (w - kernel_extent_w) / stride_w + 1;
725
int outh = (h - kernel_extent_h) / stride_h + 1;
726
int out_elempack = (opt.use_packing_layout && num_output % packn == 0) ? packn : 1;
727
size_t out_elemsize = elemsize / elempack * out_elempack;
729
top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
730
if (top_blob.empty())
734
if (channels * elempack == group && group == num_output)
736
if (elempack == packn)
739
const int maxk = kernel_w * kernel_h;
742
std::vector<int> _space_ofs(maxk);
743
int* space_ofs = &_space_ofs[0];
747
int gap = w * dilation_h - kernel_w * dilation_w;
748
for (int i = 0; i < kernel_h; i++)
750
for (int j = 0; j < kernel_w; j++)
760
#pragma omp parallel for num_threads(opt.num_threads)
761
for (int g = 0; g < channels; g++)
763
__fp16* outptr = top_blob.channel(g);
764
const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g * packn;
765
const Mat m = bottom_blob_bordered.channel(g);
767
for (int i = 0; i < outh; i++)
769
for (int j = 0; j < outw; j++)
771
vfloat32m2_t _sum = vfmv_v_f_f32m2(0.f, vl);
775
_sum = vle32_v_f32m2((const float*)bias_data + g * packn, vl);
778
const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * packn;
780
for (int k = 0; k < maxk; k++)
782
vfloat16m1_t _val = vle16_v_f16m1(sptr + space_ofs[k] * packn, vl);
783
vfloat16m1_t _w = vle16_v_f16m1(kptr + k * packn, vl);
784
_sum = vfwmacc_vv_f32m2(_sum, _val, _w, vl);
787
_sum = activation_ps(_sum, activation_type, activation_params, vl);
789
vse16_v_f16m1(outptr + j * packn, vfncvt_f_f_w_f16m1(_sum, vl), vl);
792
outptr += outw * packn;
801
const int maxk = kernel_w * kernel_h;
804
std::vector<int> _space_ofs(maxk);
805
int* space_ofs = &_space_ofs[0];
809
int gap = w * dilation_h - kernel_w * dilation_w;
810
for (int i = 0; i < kernel_h; i++)
812
for (int j = 0; j < kernel_w; j++)
822
#pragma omp parallel for num_threads(opt.num_threads)
823
for (int g = 0; g < group; g++)
825
__fp16* outptr = top_blob.channel(g);
826
const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g;
827
const Mat m = bottom_blob_bordered.channel(g);
829
for (int i = 0; i < outh; i++)
831
for (int j = 0; j < outw; j++)
838
const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w;
840
for (int k = 0; k < maxk; k++)
842
float val = (float)sptr[space_ofs[k]];
843
float w = (float)kptr[k];
847
sum = activation_ss(sum, activation_type, activation_params);
849
outptr[j] = (__fp16)sum;
862
const int channels_g = channels * elempack / group;
863
const int num_output_g = num_output / group;
865
int g_elempack = (opt.use_packing_layout && channels_g % packn == 0) ? packn : 1;
866
int out_g_elempack = (opt.use_packing_layout && num_output_g % packn == 0) ? packn : 1;
869
Mat bottom_blob_bordered_unpacked = bottom_blob_bordered;
870
if (elempack > g_elempack)
873
opt_p.blob_allocator = opt.workspace_allocator;
874
convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, 1, opt_p);
877
Mat top_blob_unpacked = top_blob;
878
if (out_g_elempack < out_elempack)
880
top_blob_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator);
881
if (top_blob_unpacked.empty())
885
for (int g = 0; g < group; g++)
887
const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
888
Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
890
const ncnn::Layer* op = group_ops[g];
893
opt_g.blob_allocator = top_blob_unpacked.allocator;
896
op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
900
if (out_g_elempack < out_elempack)
902
convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
906
top_blob = top_blob_unpacked;
912
int ConvolutionDepthWise_riscv::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
914
const int packn = csrr_vlenb() / 2;
915
const size_t vl = vsetvl_e16m1(packn);
917
int w = bottom_blob.w;
918
int h = bottom_blob.h;
919
int channels = bottom_blob.c;
920
size_t elemsize = bottom_blob.elemsize;
921
int elempack = bottom_blob.elempack;
923
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
924
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
926
Mat bottom_blob_bordered;
927
make_padding(bottom_blob, bottom_blob_bordered, opt);
928
if (bottom_blob_bordered.empty())
931
w = bottom_blob_bordered.w;
932
h = bottom_blob_bordered.h;
934
int outw = (w - kernel_extent_w) / stride_w + 1;
935
int outh = (h - kernel_extent_h) / stride_h + 1;
936
int out_elempack = (opt.use_packing_layout && num_output % packn == 0) ? packn : 1;
937
size_t out_elemsize = elemsize / elempack * out_elempack;
939
top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
940
if (top_blob.empty())
944
if (channels * elempack == group && group == num_output)
946
if (elempack == packn)
948
if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
950
convdw3x3s1_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
954
activation->forward_inplace(top_blob, opt);
957
else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
959
convdw3x3s2_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
963
activation->forward_inplace(top_blob, opt);
966
else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
968
convdw5x5s1_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
972
activation->forward_inplace(top_blob, opt);
975
else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
977
convdw5x5s2_packn_fp16sa_rvv(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
981
activation->forward_inplace(top_blob, opt);
986
const int maxk = kernel_w * kernel_h;
989
std::vector<int> _space_ofs(maxk);
990
int* space_ofs = &_space_ofs[0];
994
int gap = w * dilation_h - kernel_w * dilation_w;
995
for (int i = 0; i < kernel_h; i++)
997
for (int j = 0; j < kernel_w; j++)
1007
#pragma omp parallel for num_threads(opt.num_threads)
1008
for (int g = 0; g < channels; g++)
1010
__fp16* outptr = top_blob.channel(g);
1011
const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g * packn;
1012
const Mat m = bottom_blob_bordered.channel(g);
1014
for (int i = 0; i < outh; i++)
1016
for (int j = 0; j < outw; j++)
1018
vfloat16m1_t _sum = vfmv_v_f_f16m1((__fp16)0.f, vl);
1022
_sum = vle16_v_f16m1((const __fp16*)bias_data_fp16 + g * packn, vl);
1025
const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w * packn;
1027
for (int k = 0; k < maxk; k++)
1029
vfloat16m1_t _val = vle16_v_f16m1(sptr + space_ofs[k] * packn, vl);
1030
vfloat16m1_t _w = vle16_v_f16m1(kptr + k * packn, vl);
1031
_sum = vfmacc_vv_f16m1(_sum, _val, _w, vl);
1034
_sum = activation_ps(_sum, activation_type, activation_params, vl);
1036
vse16_v_f16m1(outptr + j * packn, _sum, vl);
1039
outptr += outw * packn;
1048
const int maxk = kernel_w * kernel_h;
1051
std::vector<int> _space_ofs(maxk);
1052
int* space_ofs = &_space_ofs[0];
1056
int gap = w * dilation_h - kernel_w * dilation_w;
1057
for (int i = 0; i < kernel_h; i++)
1059
for (int j = 0; j < kernel_w; j++)
1069
#pragma omp parallel for num_threads(opt.num_threads)
1070
for (int g = 0; g < group; g++)
1072
__fp16* outptr = top_blob.channel(g);
1073
const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g;
1074
const Mat m = bottom_blob_bordered.channel(g);
1076
for (int i = 0; i < outh; i++)
1078
for (int j = 0; j < outw; j++)
1085
const __fp16* sptr = m.row<const __fp16>(i * stride_h) + j * stride_w;
1087
for (int k = 0; k < maxk; k++)
1089
__fp16 val = sptr[space_ofs[k]];
1094
sum = activation_ss(sum, activation_type, activation_params);
1096
outptr[j] = (__fp16)sum;
1108
// group convolution
1109
const int channels_g = channels * elempack / group;
1110
const int num_output_g = num_output / group;
1112
int g_elempack = (opt.use_packing_layout && channels_g % packn == 0) ? packn : 1;
1113
int out_g_elempack = (opt.use_packing_layout && num_output_g % packn == 0) ? packn : 1;
1116
Mat bottom_blob_bordered_unpacked = bottom_blob_bordered;
1117
if (elempack > g_elempack)
1120
opt_p.blob_allocator = opt.workspace_allocator;
1121
convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, g_elempack, opt_p);
1124
Mat top_blob_unpacked = top_blob;
1125
if (out_g_elempack < out_elempack)
1127
top_blob_unpacked.create(outw, outh, num_output / out_g_elempack, out_elemsize / out_elempack * out_g_elempack, out_g_elempack, opt.workspace_allocator);
1128
if (top_blob_unpacked.empty())
1132
for (int g = 0; g < group; g++)
1134
const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
1135
Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
1137
const ncnn::Layer* op = group_ops[g];
1140
opt_g.blob_allocator = top_blob_unpacked.allocator;
1143
op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
1147
if (out_g_elempack < out_elempack)
1149
convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
1153
top_blob = top_blob_unpacked;
1158
#endif // __riscv_vector && __riscv_zfh