Elffuss BraidHub

kikocis/ncnn public

master · 7.0 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
15static void deconvolution_packnto1_fp16s_rvv(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data, int kernel_w, int kernel_h, int dilation_w, int dilation_h, int stride_w, int stride_h, int activation_type, const Mat& activation_params, const Option& opt)
16{
17 const int packn = csrr_vlenb() / 2;
18 const size_t vl = vsetvl_e16m1(packn);
19
20 int w = bottom_blob.w;
21 int h = bottom_blob.h;
22 int channels = bottom_blob.c;
23
24 int outw = top_blob.w;
25 int outh = top_blob.h;
26 int outch = top_blob.c;
27
28 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
29 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
30
31 const int maxk = kernel_w * kernel_h;
32
33 const float* bias_data_ptr = bias_data;
34
35 // num_output
36 #pragma omp parallel for num_threads(opt.num_threads)
37 for (int p = 0; p < outch; p++)
38 {
39 __fp16* outptr = top_blob.channel(p);
40
41 for (int i = 0; i < outh; i++)
42 {
43 for (int j = 0; j < outw; j++)
44 {
45 float sum = 0.f;
46
47 if (bias_data_ptr)
48 {
49 sum = bias_data_ptr[p];
50 }
51
52 vfloat32m2_t _sum = vfmv_v_f_f32m2(0.f, vl);
53
54 const __fp16* kptr = (const __fp16*)weight_data_fp16 + maxk * channels * p * packn;
55
56 // channels
57 for (int q = 0; q < channels; q++)
58 {
59 const Mat m = bottom_blob.channel(q);
60
61 for (int y = 0; y < kernel_h; y++)
62 {
63 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
64 if (sys < 0 || sys % stride_h != 0)
65 continue;
66
67 int sy = sys / stride_h;
68 if (sy >= h)
69 continue;
70
71 for (int x = 0; x < kernel_w; x++)
72 {
73 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
74 if (sxs < 0 || sxs % stride_w != 0)
75 continue;
76
77 int sx = sxs / stride_w;
78 if (sx >= w)
79 continue;
80
81 const __fp16* sptr = m.row<const __fp16>(sy) + sx * packn;
82
83 int k = y * kernel_w + x;
84
85 vfloat16m1_t _val = vle16_v_f16m1(sptr, vl);
86 vfloat16m1_t _w = vle16_v_f16m1(kptr + k * packn, vl);
87 _sum = vfwmacc_vv_f32m2(_sum, _val, _w, vl);
88 }
89 }
90
91 kptr += maxk * packn;
92 }
93
94#if C906
95 // TODO
96 std::vector<float> ss(packn);
97 vse32_v_f32m2((float*)ss.data(), _sum, vl);
98 for (int i = 0; i < packn; i++)
99 {
100 sum += ss[i];
101 }
102#else
103 sum = vfmv_f_s_f32m1_f32(vfredusum_vs_f32m2_f32m1(vfloat32m1_t(), _sum, vfmv_s_f_f32m1(vfloat32m1_t(), sum, vl), vl));
104#endif
105
106 sum = activation_ss(sum, activation_type, activation_params);
107
108 outptr[j] = sum;
109 }
110
111 outptr += outw;
112 }
113 }
114}
115
116static void deconvolution_packnto1_fp16sa_rvv(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data_fp16, int kernel_w, int kernel_h, int dilation_w, int dilation_h, int stride_w, int stride_h, int activation_type, const Mat& activation_params, const Option& opt)
117{
118 const int packn = csrr_vlenb() / 2;
119 const size_t vl = vsetvl_e16m1(packn);
120
121 int w = bottom_blob.w;
122 int h = bottom_blob.h;
123 int channels = bottom_blob.c;
124
125 int outw = top_blob.w;
126 int outh = top_blob.h;
127 int outch = top_blob.c;
128
129 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
130 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
131
132 const int maxk = kernel_w * kernel_h;
133
134 const __fp16* bias_data_ptr = bias_data_fp16;
135
136 // num_output
137 #pragma omp parallel for num_threads(opt.num_threads)
138 for (int p = 0; p < outch; p++)
139 {
140 __fp16* outptr = top_blob.channel(p);
141
142 for (int i = 0; i < outh; i++)
143 {
144 for (int j = 0; j < outw; j++)
145 {
146 __fp16 sum = 0.f;
147
148 if (bias_data_ptr)
149 {
150 sum = bias_data_ptr[p];
151 }
152
153 vfloat16m1_t _sum = vfmv_v_f_f16m1(0.f, vl);
154
155 const __fp16* kptr = (const __fp16*)weight_data_fp16 + maxk * channels * p * packn;
156
157 // channels
158 for (int q = 0; q < channels; q++)
159 {
160 const Mat m = bottom_blob.channel(q);
161
162 for (int y = 0; y < kernel_h; y++)
163 {
164 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
165 if (sys < 0 || sys % stride_h != 0)
166 continue;
167
168 int sy = sys / stride_h;
169 if (sy >= h)
170 continue;
171
172 for (int x = 0; x < kernel_w; x++)
173 {
174 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
175 if (sxs < 0 || sxs % stride_w != 0)
176 continue;
177
178 int sx = sxs / stride_w;
179 if (sx >= w)
180 continue;
181
182 const __fp16* sptr = m.row<const __fp16>(sy) + sx * packn;
183
184 int k = y * kernel_w + x;
185
186 vfloat16m1_t _val = vle16_v_f16m1(sptr, vl);
187 vfloat16m1_t _w = vle16_v_f16m1(kptr + k * packn, vl);
188 _sum = vfmacc_vv_f16m1(_sum, _val, _w, vl);
189 }
190 }
191
192 kptr += maxk * packn;
193 }
194
195 sum = vfmv_f_s_f16m1_f16(vfredusum_vs_f16m1_f16m1(vfloat16m1_t(), _sum, vfmv_s_f_f16m1(vfloat16m1_t(), sum, vl), vl));
196
197 sum = activation_ss(sum, activation_type, activation_params);
198
199 outptr[j] = sum;
200 }
201
202 outptr += outw;
203 }
204 }
205}