Elffuss BraidHub

kikocis/ncnn public

master · 34.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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 conv3x3s1_winograd63_transform_input_packn_fp16sa_rvv(const Mat& bottom_blob, Mat& bottom_blob_tm, const Option& opt)
16{
17 const int packn = csrr_vlenb() / 2;
18 const size_t vl = vsetvl_e16m1(packn);
19
20 const int w = bottom_blob.w;
21 const int h = bottom_blob.h;
22 const int inch = bottom_blob.c;
23
24 const int w_tiles = (w - 2) / 6;
25 const int h_tiles = (h - 2) / 6;
26 const int tiles = w_tiles * h_tiles;
27
28 // const float itm[8][8] = {
29 // {1.0f, 0.0f, -5.25f, 0.00f, 5.25f, 0.00f, -1.0f, 0.0f},
30 //
31 // {0.0f, 1.0f, 1.00f, -4.25f, -4.25f, 1.00f, 1.0f, 0.0f},
32 // {0.0f, -1.0f, 1.00f, 4.25f, -4.25f, -1.00f, 1.0f, 0.0f},
33 //
34 // {0.0f, 0.5f, 0.25f, -2.50f, -1.25f, 2.00f, 1.0f, 0.0f},
35 // {0.0f, -0.5f, 0.25f, 2.50f, -1.25f, -2.00f, 1.0f, 0.0f},
36 //
37 // {0.0f, 2.0f, 4.00f, -2.50f, -5.00f, 0.50f, 1.0f, 0.0f},
38 // {0.0f, -2.0f, 4.00f, 2.50f, -5.00f, -0.50f, 1.0f, 0.0f},
39 //
40 // {0.0f, -1.0f, 0.00f, 5.25f, 0.00f, -5.25f, 0.0f, 1.0f}
41 // };
42
43 // 0 = r00 - r06 + (r04 - r02) * 5.25
44 // 7 = r07 - r01 + (r03 - r05) * 5.25
45
46 // 1 = (r02 + r06 - r04 * 4.25) + (r01 - r03 * 4.25 + r05)
47 // 2 = (r02 + r06 - r04 * 4.25) - (r01 - r03 * 4.25 + r05)
48
49 // 3 = (r06 + r02 * 0.25 - r04 * 1.25) + (r01 * 0.5 - r03 * 2.5 + r05 * 2)
50 // 4 = (r06 + r02 * 0.25 - r04 * 1.25) - (r01 * 0.5 - r03 * 2.5 + r05 * 2)
51
52 // reuse r04 * 1.25
53 // reuse r03 * 2.5
54 // 5 = (r06 + (r02 - r04 * 1.25) * 4) + (r01 * 2 - r03 * 2.5 + r05 * 0.5)
55 // 6 = (r06 + (r02 - r04 * 1.25) * 4) - (r01 * 2 - r03 * 2.5 + r05 * 0.5)
56
57 #pragma omp parallel for num_threads(opt.num_threads)
58 for (int q = 0; q < inch; q++)
59 {
60 const Mat img0 = bottom_blob.channel(q);
61 Mat img0_tm = bottom_blob_tm.channel(q);
62
63 // NOTE c99 variable length array
64 __fp16 tmp[8][8][packn];
65
66 // tile
67 for (int i = 0; i < h_tiles; i++)
68 {
69 for (int j = 0; j < w_tiles; j++)
70 {
71 const __fp16* r0 = img0.row<const __fp16>(i * 6) + (j * 6) * packn;
72
73 for (int m = 0; m < 8; m++)
74 {
75 vfloat16m1_t _r00 = vle16_v_f16m1(r0, vl);
76 vfloat16m1_t _r01 = vle16_v_f16m1(r0 + packn, vl);
77 vfloat16m1_t _r02 = vle16_v_f16m1(r0 + packn * 2, vl);
78 vfloat16m1_t _r03 = vle16_v_f16m1(r0 + packn * 3, vl);
79 vfloat16m1_t _r04 = vle16_v_f16m1(r0 + packn * 4, vl);
80 vfloat16m1_t _r05 = vle16_v_f16m1(r0 + packn * 5, vl);
81 vfloat16m1_t _r06 = vle16_v_f16m1(r0 + packn * 6, vl);
82 vfloat16m1_t _r07 = vle16_v_f16m1(r0 + packn * 7, vl);
83
84 vfloat16m1_t _tmp0m = vfmacc_vf_f16m1(vfsub_vv_f16m1(_r00, _r06, vl), 5.25f, vfsub_vv_f16m1(_r04, _r02, vl), vl);
85 vfloat16m1_t _tmp7m = vfmacc_vf_f16m1(vfsub_vv_f16m1(_r07, _r01, vl), 5.25f, vfsub_vv_f16m1(_r03, _r05, vl), vl);
86 vse16_v_f16m1(tmp[0][m], _tmp0m, vl);
87 vse16_v_f16m1(tmp[7][m], _tmp7m, vl);
88
89 vfloat16m1_t _tmp12a = vfmacc_vf_f16m1(vfadd_vv_f16m1(_r02, _r06, vl), -4.25f, _r04, vl);
90 vfloat16m1_t _tmp12b = vfmacc_vf_f16m1(vfadd_vv_f16m1(_r01, _r05, vl), -4.25f, _r03, vl);
91
92 vfloat16m1_t _tmp1m = vfadd_vv_f16m1(_tmp12a, _tmp12b, vl);
93 vfloat16m1_t _tmp2m = vfsub_vv_f16m1(_tmp12a, _tmp12b, vl);
94 vse16_v_f16m1(tmp[1][m], _tmp1m, vl);
95 vse16_v_f16m1(tmp[2][m], _tmp2m, vl);
96
97 vfloat16m1_t _tmp34a = vfmacc_vf_f16m1(vfmacc_vf_f16m1(_r06, 0.25f, _r02, vl), -1.25f, _r04, vl);
98 vfloat16m1_t _tmp34b = vfmacc_vf_f16m1(vfmacc_vf_f16m1(vfmul_vf_f16m1(_r01, 0.5f, vl), -2.5f, _r03, vl), 2.f, _r05, vl);
99
100 vfloat16m1_t _tmp3m = vfadd_vv_f16m1(_tmp34a, _tmp34b, vl);
101 vfloat16m1_t _tmp4m = vfsub_vv_f16m1(_tmp34a, _tmp34b, vl);
102 vse16_v_f16m1(tmp[3][m], _tmp3m, vl);
103 vse16_v_f16m1(tmp[4][m], _tmp4m, vl);
104
105 vfloat16m1_t _tmp56a = vfmacc_vf_f16m1(_r06, 4.f, vfmacc_vf_f16m1(_r02, -1.25f, _r04, vl), vl);
106 vfloat16m1_t _tmp56b = vfmacc_vf_f16m1(vfmacc_vf_f16m1(vfmul_vf_f16m1(_r01, 2.f, vl), -2.5f, _r03, vl), 0.5f, _r05, vl);
107
108 vfloat16m1_t _tmp5m = vfadd_vv_f16m1(_tmp56a, _tmp56b, vl);
109 vfloat16m1_t _tmp6m = vfsub_vv_f16m1(_tmp56a, _tmp56b, vl);
110 vse16_v_f16m1(tmp[5][m], _tmp5m, vl);
111 vse16_v_f16m1(tmp[6][m], _tmp6m, vl);
112
113 r0 += w * packn;
114 }
115
116 __fp16* r0_tm_0 = (__fp16*)img0_tm + (i * w_tiles + j) * packn;
117 __fp16* r0_tm_1 = r0_tm_0 + tiles * packn;
118 __fp16* r0_tm_2 = r0_tm_0 + tiles * packn * 2;
119 __fp16* r0_tm_3 = r0_tm_0 + tiles * packn * 3;
120 __fp16* r0_tm_4 = r0_tm_0 + tiles * packn * 4;
121 __fp16* r0_tm_5 = r0_tm_0 + tiles * packn * 5;
122 __fp16* r0_tm_6 = r0_tm_0 + tiles * packn * 6;
123 __fp16* r0_tm_7 = r0_tm_0 + tiles * packn * 7;
124
125 for (int m = 0; m < 8; m++)
126 {
127 vfloat16m1_t _tmp00 = vle16_v_f16m1(tmp[m][0], vl);
128 vfloat16m1_t _tmp01 = vle16_v_f16m1(tmp[m][1], vl);
129 vfloat16m1_t _tmp02 = vle16_v_f16m1(tmp[m][2], vl);
130 vfloat16m1_t _tmp03 = vle16_v_f16m1(tmp[m][3], vl);
131 vfloat16m1_t _tmp04 = vle16_v_f16m1(tmp[m][4], vl);
132 vfloat16m1_t _tmp05 = vle16_v_f16m1(tmp[m][5], vl);
133 vfloat16m1_t _tmp06 = vle16_v_f16m1(tmp[m][6], vl);
134 vfloat16m1_t _tmp07 = vle16_v_f16m1(tmp[m][7], vl);
135
136 vfloat16m1_t _r0tm0 = vfmacc_vf_f16m1(vfsub_vv_f16m1(_tmp00, _tmp06, vl), 5.25f, vfsub_vv_f16m1(_tmp04, _tmp02, vl), vl);
137 vfloat16m1_t _r0tm7 = vfmacc_vf_f16m1(vfsub_vv_f16m1(_tmp07, _tmp01, vl), 5.25f, vfsub_vv_f16m1(_tmp03, _tmp05, vl), vl);
138
139 vfloat16m1_t _tmp12a = vfmacc_vf_f16m1(vfadd_vv_f16m1(_tmp02, _tmp06, vl), -4.25f, _tmp04, vl);
140 vfloat16m1_t _tmp12b = vfmacc_vf_f16m1(vfadd_vv_f16m1(_tmp01, _tmp05, vl), -4.25f, _tmp03, vl);
141
142 vfloat16m1_t _r0tm1 = vfadd_vv_f16m1(_tmp12a, _tmp12b, vl);
143 vfloat16m1_t _r0tm2 = vfsub_vv_f16m1(_tmp12a, _tmp12b, vl);
144
145 vfloat16m1_t _tmp34a = vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp06, 0.25f, _tmp02, vl), -1.25f, _tmp04, vl);
146 vfloat16m1_t _tmp34b = vfmacc_vf_f16m1(vfmacc_vf_f16m1(vfmul_vf_f16m1(_tmp01, 0.5f, vl), -2.5f, _tmp03, vl), 2.f, _tmp05, vl);
147
148 vfloat16m1_t _r0tm3 = vfadd_vv_f16m1(_tmp34a, _tmp34b, vl);
149 vfloat16m1_t _r0tm4 = vfsub_vv_f16m1(_tmp34a, _tmp34b, vl);
150
151 vfloat16m1_t _tmp56a = vfmacc_vf_f16m1(_tmp06, 4.f, vfmacc_vf_f16m1(_tmp02, -1.25f, _tmp04, vl), vl);
152 vfloat16m1_t _tmp56b = vfmacc_vf_f16m1(vfmacc_vf_f16m1(vfmul_vf_f16m1(_tmp01, 2.f, vl), -2.5f, _tmp03, vl), 0.5f, _tmp05, vl);
153
154 vfloat16m1_t _r0tm5 = vfadd_vv_f16m1(_tmp56a, _tmp56b, vl);
155 vfloat16m1_t _r0tm6 = vfsub_vv_f16m1(_tmp56a, _tmp56b, vl);
156
157 vse16_v_f16m1(r0_tm_0, _r0tm0, vl);
158 vse16_v_f16m1(r0_tm_1, _r0tm1, vl);
159 vse16_v_f16m1(r0_tm_2, _r0tm2, vl);
160 vse16_v_f16m1(r0_tm_3, _r0tm3, vl);
161 vse16_v_f16m1(r0_tm_4, _r0tm4, vl);
162 vse16_v_f16m1(r0_tm_5, _r0tm5, vl);
163 vse16_v_f16m1(r0_tm_6, _r0tm6, vl);
164 vse16_v_f16m1(r0_tm_7, _r0tm7, vl);
165
166 r0_tm_0 += tiles * packn * 8;
167 r0_tm_1 += tiles * packn * 8;
168 r0_tm_2 += tiles * packn * 8;
169 r0_tm_3 += tiles * packn * 8;
170 r0_tm_4 += tiles * packn * 8;
171 r0_tm_5 += tiles * packn * 8;
172 r0_tm_6 += tiles * packn * 8;
173 r0_tm_7 += tiles * packn * 8;
174 }
175 }
176 }
177 }
178}
179
180static void conv3x3s1_winograd63_transform_output_packn_fp16sa_rvv(const Mat& top_blob_tm, Mat& top_blob, const Mat& bias, const Option& opt)
181{
182 const int packn = csrr_vlenb() / 2;
183 const size_t vl = vsetvl_e16m1(packn);
184
185 const int outw = top_blob.w;
186 const int outh = top_blob.h;
187 const int outch = top_blob.c;
188
189 const int w_tiles = outw / 6;
190 const int h_tiles = outh / 6;
191 const int tiles = w_tiles * h_tiles;
192
193 const __fp16* biasptr = bias;
194
195 // const float otm[6][8] = {
196 // {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 32.0f, 32.0f, 0.0f},
197 // {0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 16.0f,-16.0f, 0.0f},
198 // {0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 8.0f, 8.0f, 0.0f},
199 // {0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 4.0f, -4.0f, 0.0f},
200 // {0.0f, 1.0f, 1.0f, 16.0f, 16.0f, 2.0f, 2.0f, 0.0f},
201 // {0.0f, 1.0f, -1.0f, 32.0f, -32.0f, 1.0f, -1.0f, 1.0f}
202 // };
203
204 // 0 = r0 + (r1 + r2) + (r3 + r4) + (r5 + r6) * 32
205 // 1 = (r1 - r2) + (r3 - r4) * 2 + (r5 - r6) * 16
206 // 2 = (r1 + r2) + (r3 + r4) * 4 + (r5 + r6) * 8
207 // 3 = (r1 - r2) + (r3 - r4) * 8 + (r5 - r6) * 4
208 // 4 = (r1 + r2) + (r3 + r4) * 16+ (r5 + r6) * 2
209 // 5 = r7 + (r1 - r2) + (r3 - r4) * 32+ (r5 - r6)
210
211 #pragma omp parallel for num_threads(opt.num_threads)
212 for (int p = 0; p < outch; p++)
213 {
214 const Mat out0_tm = top_blob_tm.channel(p);
215 Mat out0 = top_blob.channel(p);
216
217 vfloat16m1_t _bias0 = biasptr ? vle16_v_f16m1(biasptr + p * packn, vl) : vfmv_v_f_f16m1(0.f, vl);
218
219 // NOTE c99 variable length array
220 __fp16 tmp[6][8][packn];
221
222 // tile
223 for (int i = 0; i < h_tiles; i++)
224 {
225 for (int j = 0; j < w_tiles; j++)
226 {
227 const __fp16* output0_tm_0 = (const __fp16*)out0_tm + (i * w_tiles + j) * packn;
228 const __fp16* output0_tm_1 = output0_tm_0 + tiles * packn;
229 const __fp16* output0_tm_2 = output0_tm_0 + tiles * packn * 2;
230 const __fp16* output0_tm_3 = output0_tm_0 + tiles * packn * 3;
231 const __fp16* output0_tm_4 = output0_tm_0 + tiles * packn * 4;
232 const __fp16* output0_tm_5 = output0_tm_0 + tiles * packn * 5;
233 const __fp16* output0_tm_6 = output0_tm_0 + tiles * packn * 6;
234 const __fp16* output0_tm_7 = output0_tm_0 + tiles * packn * 7;
235
236 __fp16* output0 = out0.row<__fp16>(i * 6) + (j * 6) * packn;
237
238 for (int m = 0; m < 8; m++)
239 {
240 vfloat16m1_t _out0tm0 = vle16_v_f16m1(output0_tm_0, vl);
241 vfloat16m1_t _out0tm1 = vle16_v_f16m1(output0_tm_1, vl);
242 vfloat16m1_t _out0tm2 = vle16_v_f16m1(output0_tm_2, vl);
243 vfloat16m1_t _out0tm3 = vle16_v_f16m1(output0_tm_3, vl);
244 vfloat16m1_t _out0tm4 = vle16_v_f16m1(output0_tm_4, vl);
245 vfloat16m1_t _out0tm5 = vle16_v_f16m1(output0_tm_5, vl);
246 vfloat16m1_t _out0tm6 = vle16_v_f16m1(output0_tm_6, vl);
247 vfloat16m1_t _out0tm7 = vle16_v_f16m1(output0_tm_7, vl);
248
249 vfloat16m1_t _tmp024a = vfadd_vv_f16m1(_out0tm1, _out0tm2, vl);
250 vfloat16m1_t _tmp135a = vfsub_vv_f16m1(_out0tm1, _out0tm2, vl);
251
252 vfloat16m1_t _tmp024b = vfadd_vv_f16m1(_out0tm3, _out0tm4, vl);
253 vfloat16m1_t _tmp135b = vfsub_vv_f16m1(_out0tm3, _out0tm4, vl);
254
255 vfloat16m1_t _tmp024c = vfadd_vv_f16m1(_out0tm5, _out0tm6, vl);
256 vfloat16m1_t _tmp135c = vfsub_vv_f16m1(_out0tm5, _out0tm6, vl);
257
258 vfloat16m1_t _tmp0m = vfadd_vv_f16m1(vfadd_vv_f16m1(_out0tm0, _tmp024a, vl), vfmacc_vf_f16m1(_tmp024b, 32.f, _tmp024c, vl), vl);
259 vfloat16m1_t _tmp2m = vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp024a, 4.f, _tmp024b, vl), 8.f, _tmp024c, vl);
260 vfloat16m1_t _tmp4m = vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp024a, 16.f, _tmp024b, vl), 2.f, _tmp024c, vl);
261 vse16_v_f16m1(tmp[0][m], _tmp0m, vl);
262 vse16_v_f16m1(tmp[2][m], _tmp2m, vl);
263 vse16_v_f16m1(tmp[4][m], _tmp4m, vl);
264
265 vfloat16m1_t _tmp1m = vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp135a, 2.f, _tmp135b, vl), 16.f, _tmp135c, vl);
266 vfloat16m1_t _tmp3m = vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp135a, 8.f, _tmp135b, vl), 4.f, _tmp135c, vl);
267 vfloat16m1_t _tmp5m = vfadd_vv_f16m1(vfadd_vv_f16m1(_out0tm7, _tmp135a, vl), vfmacc_vf_f16m1(_tmp135c, 32.f, _tmp135b, vl), vl);
268 vse16_v_f16m1(tmp[1][m], _tmp1m, vl);
269 vse16_v_f16m1(tmp[3][m], _tmp3m, vl);
270 vse16_v_f16m1(tmp[5][m], _tmp5m, vl);
271
272 output0_tm_0 += tiles * packn * 8;
273 output0_tm_1 += tiles * packn * 8;
274 output0_tm_2 += tiles * packn * 8;
275 output0_tm_3 += tiles * packn * 8;
276 output0_tm_4 += tiles * packn * 8;
277 output0_tm_5 += tiles * packn * 8;
278 output0_tm_6 += tiles * packn * 8;
279 output0_tm_7 += tiles * packn * 8;
280 }
281
282 for (int m = 0; m < 6; m++)
283 {
284 vfloat16m1_t _tmp00 = vle16_v_f16m1(tmp[m][0], vl);
285 vfloat16m1_t _tmp01 = vle16_v_f16m1(tmp[m][1], vl);
286 vfloat16m1_t _tmp02 = vle16_v_f16m1(tmp[m][2], vl);
287 vfloat16m1_t _tmp03 = vle16_v_f16m1(tmp[m][3], vl);
288 vfloat16m1_t _tmp04 = vle16_v_f16m1(tmp[m][4], vl);
289 vfloat16m1_t _tmp05 = vle16_v_f16m1(tmp[m][5], vl);
290 vfloat16m1_t _tmp06 = vle16_v_f16m1(tmp[m][6], vl);
291 vfloat16m1_t _tmp07 = vle16_v_f16m1(tmp[m][7], vl);
292
293 vfloat16m1_t _tmp024a = vfadd_vv_f16m1(_tmp01, _tmp02, vl);
294 vfloat16m1_t _tmp135a = vfsub_vv_f16m1(_tmp01, _tmp02, vl);
295
296 vfloat16m1_t _tmp024b = vfadd_vv_f16m1(_tmp03, _tmp04, vl);
297 vfloat16m1_t _tmp135b = vfsub_vv_f16m1(_tmp03, _tmp04, vl);
298
299 vfloat16m1_t _tmp024c = vfadd_vv_f16m1(_tmp05, _tmp06, vl);
300 vfloat16m1_t _tmp135c = vfsub_vv_f16m1(_tmp05, _tmp06, vl);
301
302 vfloat16m1_t _out00 = vfadd_vv_f16m1(_bias0, vfadd_vv_f16m1(vfadd_vv_f16m1(_tmp00, _tmp024a, vl), vfmacc_vf_f16m1(_tmp024b, 32.f, _tmp024c, vl), vl), vl);
303 vfloat16m1_t _out02 = vfadd_vv_f16m1(_bias0, vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp024a, 4.f, _tmp024b, vl), 8.f, _tmp024c, vl), vl);
304 vfloat16m1_t _out04 = vfadd_vv_f16m1(_bias0, vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp024a, 16.f, _tmp024b, vl), 2.f, _tmp024c, vl), vl);
305 vse16_v_f16m1(output0, _out00, vl);
306 vse16_v_f16m1(output0 + packn * 2, _out02, vl);
307 vse16_v_f16m1(output0 + packn * 4, _out04, vl);
308
309 vfloat16m1_t _out01 = vfadd_vv_f16m1(_bias0, vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp135a, 2.f, _tmp135b, vl), 16.f, _tmp135c, vl), vl);
310 vfloat16m1_t _out03 = vfadd_vv_f16m1(_bias0, vfmacc_vf_f16m1(vfmacc_vf_f16m1(_tmp135a, 8.f, _tmp135b, vl), 4.f, _tmp135c, vl), vl);
311 vfloat16m1_t _out05 = vfadd_vv_f16m1(_bias0, vfadd_vv_f16m1(vfadd_vv_f16m1(_tmp07, _tmp135a, vl), vfmacc_vf_f16m1(_tmp135c, 32.f, _tmp135b, vl), vl), vl);
312 vse16_v_f16m1(output0 + packn, _out01, vl);
313 vse16_v_f16m1(output0 + packn * 3, _out03, vl);
314 vse16_v_f16m1(output0 + packn * 5, _out05, vl);
315
316 output0 += outw * packn;
317 }
318 }
319 }
320 }
321}
322
323static void conv3x3s1_winograd43_transform_input_packn_fp16sa_rvv(const Mat& bottom_blob, Mat& bottom_blob_tm, const Option& opt)
324{
325 const int packn = csrr_vlenb() / 2;
326 const size_t vl = vsetvl_e16m1(packn);
327
328 const int w = bottom_blob.w;
329 const int h = bottom_blob.h;
330 const int inch = bottom_blob.c;
331
332 const int w_tiles = (w - 2) / 4;
333 const int h_tiles = (h - 2) / 4;
334 const int tiles = w_tiles * h_tiles;
335
336 const float sq2 = 1.41421356237;
337 const float sq2_d2 = 1.41421356237 / 2;
338
339 // const float itm[6][6] = {
340 // {1.0f, 0.0f, -2.5f, 0.0f, 1.0f, 0.0f},
341 // {0.0f, -sq2, -2.0f, sq2/2, 1.0f, 0.0f},
342 // {0.0f, sq2, -2.0f, -sq2/2, 1.0f, 0.0f},
343 // {0.0f, -sq2/2, -0.5f, sq2, 1.0f, 0.0f},
344 // {0.0f, sq2/2, -0.5f, -sq2, 1.0f, 0.0f},
345 // {0.0f, 1.0f, 0.0f, -2.5f, 0.0f, 1.0f}
346 // };
347
348 // 0 = r00 - 2.5f * r02 + r04
349 // 1 = -(sq2 * r01 - sq2_d2 * r03) + (r04 - 2 * r02)
350 // 2 = (sq2 * r01 - sq2_d2 * r03) + (r04 - 2 * r02)
351 // 3 = -(sq2_d2 * r01 - sq2 * r03) + (r04 - 0.5f * r02)
352 // 4 = (sq2_d2 * r01 - sq2 * r03) + (r04 - 0.5f * r02)
353 // 5 = r01 - 2.5f * r03 + r05
354
355 #pragma omp parallel for num_threads(opt.num_threads)
356 for (int q = 0; q < inch; q++)
357 {
358 const Mat img0 = bottom_blob.channel(q);
359 Mat img0_tm = bottom_blob_tm.channel(q);
360
361 // NOTE c99 variable length array
362 __fp16 tmp[6][6][packn];
363
364 // tile
365 for (int i = 0; i < h_tiles; i++)
366 {
367 for (int j = 0; j < w_tiles; j++)
368 {
369 const __fp16* r0 = img0.row<const __fp16>(i * 4) + (j * 4) * packn;
370
371 for (int m = 0; m < 6; m++)
372 {
373 vfloat16m1_t _r00 = vle16_v_f16m1(r0, vl);
374 vfloat16m1_t _r01 = vle16_v_f16m1(r0 + packn, vl);
375 vfloat16m1_t _r02 = vle16_v_f16m1(r0 + packn * 2, vl);
376 vfloat16m1_t _r03 = vle16_v_f16m1(r0 + packn * 3, vl);
377 vfloat16m1_t _r04 = vle16_v_f16m1(r0 + packn * 4, vl);
378 vfloat16m1_t _r05 = vle16_v_f16m1(r0 + packn * 5, vl);
379
380 vfloat16m1_t _tmp01a = vfmacc_vf_f16m1(vfmul_vf_f16m1(_r01, sq2, vl), -sq2_d2, _r03, vl);
381 vfloat16m1_t _tmp01b = vfmacc_vf_f16m1(_r04, -2.f, _r02, vl);
382 vfloat16m1_t _tmp23a = vfmacc_vf_f16m1(vfmul_vf_f16m1(_r01, sq2_d2, vl), -sq2, _r03, vl);
383 vfloat16m1_t _tmp23b = vfmacc_vf_f16m1(_r04, -0.5f, _r02, vl);
384
385 vfloat16m1_t _tmp0m = vfmacc_vf_f16m1(vfadd_vv_f16m1(_r00, _r04, vl), -2.5f, _r02, vl);
386 vfloat16m1_t _tmp1m = vfsub_vv_f16m1(_tmp01b, _tmp01a, vl);
387 vfloat16m1_t _tmp2m = vfadd_vv_f16m1(_tmp01b, _tmp01a, vl);
388 vfloat16m1_t _tmp3m = vfsub_vv_f16m1(_tmp23b, _tmp23a, vl);
389 vfloat16m1_t _tmp4m = vfadd_vv_f16m1(_tmp23b, _tmp23a, vl);
390 vfloat16m1_t _tmp5m = vfmacc_vf_f16m1(vfadd_vv_f16m1(_r01, _r05, vl), -2.5f, _r03, vl);
391
392 vse16_v_f16m1(tmp[0][m], _tmp0m, vl);
393 vse16_v_f16m1(tmp[1][m], _tmp1m, vl);
394 vse16_v_f16m1(tmp[2][m], _tmp2m, vl);
395 vse16_v_f16m1(tmp[3][m], _tmp3m, vl);
396 vse16_v_f16m1(tmp[4][m], _tmp4m, vl);
397 vse16_v_f16m1(tmp[5][m], _tmp5m, vl);
398
399 r0 += w * packn;
400 }
401
402 __fp16* r0_tm_0 = (__fp16*)img0_tm + (i * w_tiles + j) * packn;
403 __fp16* r0_tm_1 = r0_tm_0 + tiles * packn;
404 __fp16* r0_tm_2 = r0_tm_0 + tiles * packn * 2;
405 __fp16* r0_tm_3 = r0_tm_0 + tiles * packn * 3;
406 __fp16* r0_tm_4 = r0_tm_0 + tiles * packn * 4;
407 __fp16* r0_tm_5 = r0_tm_0 + tiles * packn * 5;
408
409 for (int m = 0; m < 6; m++)
410 {
411 vfloat16m1_t _r00 = vle16_v_f16m1(tmp[m][0], vl);
412 vfloat16m1_t _r01 = vle16_v_f16m1(tmp[m][1], vl);
413 vfloat16m1_t _r02 = vle16_v_f16m1(tmp[m][2], vl);
414 vfloat16m1_t _r03 = vle16_v_f16m1(tmp[m][3], vl);
415 vfloat16m1_t _r04 = vle16_v_f16m1(tmp[m][4], vl);
416 vfloat16m1_t _r05 = vle16_v_f16m1(tmp[m][5], vl);
417
418 vfloat16m1_t _tmp01a = vfmacc_vf_f16m1(vfmul_vf_f16m1(_r01, sq2, vl), -sq2_d2, _r03, vl);
419 vfloat16m1_t _tmp01b = vfmacc_vf_f16m1(_r04, -2.f, _r02, vl);
420 vfloat16m1_t _tmp23a = vfmacc_vf_f16m1(vfmul_vf_f16m1(_r01, sq2_d2, vl), -sq2, _r03, vl);
421 vfloat16m1_t _tmp23b = vfmacc_vf_f16m1(_r04, -0.5f, _r02, vl);
422
423 vfloat16m1_t _tmp0m = vfmacc_vf_f16m1(vfadd_vv_f16m1(_r00, _r04, vl), -2.5f, _r02, vl);
424 vfloat16m1_t _tmp1m = vfsub_vv_f16m1(_tmp01b, _tmp01a, vl);
425 vfloat16m1_t _tmp2m = vfadd_vv_f16m1(_tmp01b, _tmp01a, vl);
426 vfloat16m1_t _tmp3m = vfsub_vv_f16m1(_tmp23b, _tmp23a, vl);
427 vfloat16m1_t _tmp4m = vfadd_vv_f16m1(_tmp23b, _tmp23a, vl);
428 vfloat16m1_t _tmp5m = vfmacc_vf_f16m1(vfadd_vv_f16m1(_r01, _r05, vl), -2.5f, _r03, vl);
429
430 vse16_v_f16m1(r0_tm_0, _tmp0m, vl);
431 vse16_v_f16m1(r0_tm_1, _tmp1m, vl);
432 vse16_v_f16m1(r0_tm_2, _tmp2m, vl);
433 vse16_v_f16m1(r0_tm_3, _tmp3m, vl);
434 vse16_v_f16m1(r0_tm_4, _tmp4m, vl);
435 vse16_v_f16m1(r0_tm_5, _tmp5m, vl);
436
437 r0_tm_0 += tiles * packn * 6;
438 r0_tm_1 += tiles * packn * 6;
439 r0_tm_2 += tiles * packn * 6;
440 r0_tm_3 += tiles * packn * 6;
441 r0_tm_4 += tiles * packn * 6;
442 r0_tm_5 += tiles * packn * 6;
443 }
444 }
445 }
446 }
447}
448
449static void conv3x3s1_winograd43_transform_output_packn_fp16sa_rvv(const Mat& top_blob_tm, Mat& top_blob, const Mat& bias, const Option& opt)
450{
451 const int packn = csrr_vlenb() / 2;
452 const size_t vl = vsetvl_e16m1(packn);
453
454 const int outw = top_blob.w;
455 const int outh = top_blob.h;
456 const int outch = top_blob.c;
457
458 const int w_tiles = outw / 4;
459 const int h_tiles = outh / 4;
460 const int tiles = w_tiles * h_tiles;
461
462 const __fp16* biasptr = bias;
463
464 const float sq2 = 1.41421356237;
465 const float sq2_m2 = 1.41421356237 * 2;
466 const float sq2_d2 = 1.41421356237 / 2;
467 const float sq2_d4 = 1.41421356237 / 4;
468
469 // const float otm[4][6] = {
470 // {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f},
471 // {0.0f, sq2/2, -sq2/2, sq2, -sq2, 0.0f},
472 // {0.0f, 0.5f, 0.5f, 2.0f, 2.0f, 0.0f},
473 // {0.0f, sq2/4, -sq2/4, sq2*2, -sq2*2, 1.0f}
474 // };
475
476 // 0 = r00 + (r01 + r02) + (r03 + r04)
477 // 1 = (r01 - r02) * sq2_d2 + (r03 - r04) * sq2
478 // 2 = (r01 + r02) * 0.5f + (r03 + r04) * 2
479 // 3 = r05 + (r01 - r02) * sq2_d4 + (r03 - r04) * sq2_m2
480
481 #pragma omp parallel for num_threads(opt.num_threads)
482 for (int p = 0; p < outch; p++)
483 {
484 const Mat out0_tm = top_blob_tm.channel(p);
485 Mat out0 = top_blob.channel(p);
486
487 vfloat16m1_t _bias0 = biasptr ? vle16_v_f16m1(biasptr + p * packn, vl) : vfmv_v_f_f16m1(0.f, vl);
488
489 // NOTE variable length array
490 __fp16 tmp[4][6][packn];
491
492 // tile
493 for (int i = 0; i < h_tiles; i++)
494 {
495 for (int j = 0; j < w_tiles; j++)
496 {
497 const __fp16* output0_tm_0 = (const __fp16*)out0_tm + (i * w_tiles + j) * packn;
498 const __fp16* output0_tm_1 = output0_tm_0 + tiles * packn;
499 const __fp16* output0_tm_2 = output0_tm_0 + tiles * packn * 2;
500 const __fp16* output0_tm_3 = output0_tm_0 + tiles * packn * 3;
501 const __fp16* output0_tm_4 = output0_tm_0 + tiles * packn * 4;
502 const __fp16* output0_tm_5 = output0_tm_0 + tiles * packn * 5;
503
504 __fp16* output0 = out0.row<__fp16>(i * 4) + (j * 4) * packn;
505
506 for (int m = 0; m < 6; m++)
507 {
508 vfloat16m1_t _r00 = vle16_v_f16m1(output0_tm_0, vl);
509 vfloat16m1_t _r01 = vle16_v_f16m1(output0_tm_1, vl);
510 vfloat16m1_t _r02 = vle16_v_f16m1(output0_tm_2, vl);
511 vfloat16m1_t _r03 = vle16_v_f16m1(output0_tm_3, vl);
512 vfloat16m1_t _r04 = vle16_v_f16m1(output0_tm_4, vl);
513 vfloat16m1_t _r05 = vle16_v_f16m1(output0_tm_5, vl);
514
515 vfloat16m1_t _tmp02a = vfadd_vv_f16m1(_r01, _r02, vl);
516 vfloat16m1_t _tmp02b = vfadd_vv_f16m1(_r03, _r04, vl);
517 vfloat16m1_t _tmp13a = vfsub_vv_f16m1(_r01, _r02, vl);
518 vfloat16m1_t _tmp13b = vfsub_vv_f16m1(_r03, _r04, vl);
519
520 vfloat16m1_t _tmp0m = vfadd_vv_f16m1(vfadd_vv_f16m1(_r00, _tmp02a, vl), _tmp02b, vl);
521 vfloat16m1_t _tmp1m = vfmacc_vf_f16m1(vfmul_vf_f16m1(_tmp13a, sq2_d2, vl), sq2, _tmp13b, vl);
522 vfloat16m1_t _tmp2m = vfmacc_vf_f16m1(vfmul_vf_f16m1(_tmp02a, 0.5f, vl), 2.f, _tmp02b, vl);
523 vfloat16m1_t _tmp3m = vfmacc_vf_f16m1(vfmacc_vf_f16m1(_r05, sq2_d4, _tmp13a, vl), sq2_m2, _tmp13b, vl);
524
525 vse16_v_f16m1(tmp[0][m], _tmp0m, vl);
526 vse16_v_f16m1(tmp[1][m], _tmp1m, vl);
527 vse16_v_f16m1(tmp[2][m], _tmp2m, vl);
528 vse16_v_f16m1(tmp[3][m], _tmp3m, vl);
529
530 output0_tm_0 += tiles * packn * 6;
531 output0_tm_1 += tiles * packn * 6;
532 output0_tm_2 += tiles * packn * 6;
533 output0_tm_3 += tiles * packn * 6;
534 output0_tm_4 += tiles * packn * 6;
535 output0_tm_5 += tiles * packn * 6;
536 }
537
538 for (int m = 0; m < 4; m++)
539 {
540 vfloat16m1_t _r00 = vle16_v_f16m1(tmp[m][0], vl);
541 vfloat16m1_t _r01 = vle16_v_f16m1(tmp[m][1], vl);
542 vfloat16m1_t _r02 = vle16_v_f16m1(tmp[m][2], vl);
543 vfloat16m1_t _r03 = vle16_v_f16m1(tmp[m][3], vl);
544 vfloat16m1_t _r04 = vle16_v_f16m1(tmp[m][4], vl);
545 vfloat16m1_t _r05 = vle16_v_f16m1(tmp[m][5], vl);
546
547 vfloat16m1_t _tmp02a = vfadd_vv_f16m1(_r01, _r02, vl);
548 vfloat16m1_t _tmp02b = vfadd_vv_f16m1(_r03, _r04, vl);
549 vfloat16m1_t _tmp13a = vfsub_vv_f16m1(_r01, _r02, vl);
550 vfloat16m1_t _tmp13b = vfsub_vv_f16m1(_r03, _r04, vl);
551
552 vfloat16m1_t _out00 = vfadd_vv_f16m1(_bias0, vfadd_vv_f16m1(vfadd_vv_f16m1(_r00, _tmp02a, vl), _tmp02b, vl), vl);
553 vfloat16m1_t _out01 = vfadd_vv_f16m1(_bias0, vfmacc_vf_f16m1(vfmul_vf_f16m1(_tmp13a, sq2_d2, vl), sq2, _tmp13b, vl), vl);
554 vfloat16m1_t _out02 = vfadd_vv_f16m1(_bias0, vfmacc_vf_f16m1(vfmul_vf_f16m1(_tmp02a, 0.5f, vl), 2.f, _tmp02b, vl), vl);
555 vfloat16m1_t _out03 = vfadd_vv_f16m1(_bias0, vfmacc_vf_f16m1(vfmacc_vf_f16m1(_r05, sq2_d4, _tmp13a, vl), sq2_m2, _tmp13b, vl), vl);
556
557 vse16_v_f16m1(output0, _out00, vl);
558 vse16_v_f16m1(output0 + packn, _out01, vl);
559 vse16_v_f16m1(output0 + packn * 2, _out02, vl);
560 vse16_v_f16m1(output0 + packn * 3, _out03, vl);
561
562 output0 += outw * packn;
563 }
564 }
565 }
566 }
567}
568
569static void conv3x3s1_winograd23_transform_input_packn_fp16sa_rvv(const Mat& bottom_blob, Mat& bottom_blob_tm, const Option& opt)
570{
571 const int packn = csrr_vlenb() / 2;
572 const size_t vl = vsetvl_e16m1(packn);
573
574 const int w = bottom_blob.w;
575 const int h = bottom_blob.h;
576 const int inch = bottom_blob.c;
577
578 const int w_tiles = (w - 2) / 2;
579 const int h_tiles = (h - 2) / 2;
580 const int tiles = w_tiles * h_tiles;
581
582 // const float itm[4][4] = {
583 // {1.0f, 0.0f, -1.0f, 0.0f},
584 // {0.0f, 1.0f, 1.00f, 0.0f},
585 // {0.0f, -1.0f, 1.00f, 0.0f},
586 // {0.0f, -1.0f, 0.00f, 1.0f}
587 // };
588
589 // 0 = r00 - r02
590 // 1 = r01 + r02
591 // 2 = r02 - r01
592 // 3 = r03 - r01
593
594 #pragma omp parallel for num_threads(opt.num_threads)
595 for (int q = 0; q < inch; q++)
596 {
597 const Mat img0 = bottom_blob.channel(q);
598 Mat img0_tm = bottom_blob_tm.channel(q);
599
600 // NOTE c99 variable length array
601 __fp16 tmp[4][4][packn];
602
603 // tile
604 for (int i = 0; i < h_tiles; i++)
605 {
606 for (int j = 0; j < w_tiles; j++)
607 {
608 const __fp16* r0 = img0.row<const __fp16>(i * 2) + (j * 2) * packn;
609
610 for (int m = 0; m < 4; m++)
611 {
612 vfloat16m1_t _r00 = vle16_v_f16m1(r0, vl);
613 vfloat16m1_t _r01 = vle16_v_f16m1(r0 + packn, vl);
614 vfloat16m1_t _r02 = vle16_v_f16m1(r0 + packn * 2, vl);
615 vfloat16m1_t _r03 = vle16_v_f16m1(r0 + packn * 3, vl);
616
617 vfloat16m1_t _tmp0m = vfsub_vv_f16m1(_r00, _r02, vl);
618 vfloat16m1_t _tmp1m = vfadd_vv_f16m1(_r01, _r02, vl);
619 vfloat16m1_t _tmp2m = vfsub_vv_f16m1(_r02, _r01, vl);
620 vfloat16m1_t _tmp3m = vfsub_vv_f16m1(_r03, _r01, vl);
621
622 vse16_v_f16m1(tmp[0][m], _tmp0m, vl);
623 vse16_v_f16m1(tmp[1][m], _tmp1m, vl);
624 vse16_v_f16m1(tmp[2][m], _tmp2m, vl);
625 vse16_v_f16m1(tmp[3][m], _tmp3m, vl);
626
627 r0 += w * packn;
628 }
629
630 __fp16* r0_tm_0 = (__fp16*)img0_tm + (i * w_tiles + j) * packn;
631 __fp16* r0_tm_1 = r0_tm_0 + tiles * packn;
632 __fp16* r0_tm_2 = r0_tm_0 + tiles * packn * 2;
633 __fp16* r0_tm_3 = r0_tm_0 + tiles * packn * 3;
634
635 for (int m = 0; m < 4; m++)
636 {
637 vfloat16m1_t _tmp00 = vle16_v_f16m1(tmp[m][0], vl);
638 vfloat16m1_t _tmp01 = vle16_v_f16m1(tmp[m][1], vl);
639 vfloat16m1_t _tmp02 = vle16_v_f16m1(tmp[m][2], vl);
640 vfloat16m1_t _tmp03 = vle16_v_f16m1(tmp[m][3], vl);
641
642 vfloat16m1_t _r0tm0 = vfsub_vv_f16m1(_tmp00, _tmp02, vl);
643 vfloat16m1_t _r0tm1 = vfadd_vv_f16m1(_tmp01, _tmp02, vl);
644 vfloat16m1_t _r0tm2 = vfsub_vv_f16m1(_tmp02, _tmp01, vl);
645 vfloat16m1_t _r0tm3 = vfsub_vv_f16m1(_tmp03, _tmp01, vl);
646
647 vse16_v_f16m1(r0_tm_0, _r0tm0, vl);
648 vse16_v_f16m1(r0_tm_1, _r0tm1, vl);
649 vse16_v_f16m1(r0_tm_2, _r0tm2, vl);
650 vse16_v_f16m1(r0_tm_3, _r0tm3, vl);
651
652 r0_tm_0 += tiles * packn * 4;
653 r0_tm_1 += tiles * packn * 4;
654 r0_tm_2 += tiles * packn * 4;
655 r0_tm_3 += tiles * packn * 4;
656 }
657 }
658 }
659 }
660}
661
662static void conv3x3s1_winograd23_transform_output_packn_fp16sa_rvv(const Mat& top_blob_tm, Mat& top_blob, const Mat& bias, const Option& opt)
663{
664 const int packn = csrr_vlenb() / 2;
665 const size_t vl = vsetvl_e16m1(packn);
666
667 const int outw = top_blob.w;
668 const int outh = top_blob.h;
669 const int outch = top_blob.c;
670
671 const int w_tiles = outw / 2;
672 const int h_tiles = outh / 2;
673 const int tiles = w_tiles * h_tiles;
674
675 const __fp16* biasptr = bias;
676
677 // const float otm[2][4] = {
678 // {1.0f, 1.0f, 1.0f, 0.0f},
679 // {0.0f, 1.0f, -1.0f, 1.0f}
680 // };
681
682 // 0 = r00 + r01 + r02
683 // 1 = r01 - r02 + r03
684
685 #pragma omp parallel for num_threads(opt.num_threads)
686 for (int p = 0; p < outch; p++)
687 {
688 const Mat out0_tm = top_blob_tm.channel(p);
689 Mat out0 = top_blob.channel(p);
690
691 vfloat16m1_t _bias0 = biasptr ? vle16_v_f16m1(biasptr + p * packn, vl) : vfmv_v_f_f16m1(0.f, vl);
692
693 // NOTE variable length array
694 __fp16 tmp[2][4][packn];
695
696 // tile
697 for (int i = 0; i < h_tiles; i++)
698 {
699 for (int j = 0; j < w_tiles; j++)
700 {
701 const __fp16* output0_tm_0 = (const __fp16*)out0_tm + (i * w_tiles + j) * packn;
702 const __fp16* output0_tm_1 = output0_tm_0 + tiles * packn;
703 const __fp16* output0_tm_2 = output0_tm_0 + tiles * packn * 2;
704 const __fp16* output0_tm_3 = output0_tm_0 + tiles * packn * 3;
705
706 __fp16* output0 = out0.row<__fp16>(i * 2) + (j * 2) * packn;
707
708 for (int m = 0; m < 4; m++)
709 {
710 vfloat16m1_t _out0tm0 = vle16_v_f16m1(output0_tm_0, vl);
711 vfloat16m1_t _out0tm1 = vle16_v_f16m1(output0_tm_1, vl);
712 vfloat16m1_t _out0tm2 = vle16_v_f16m1(output0_tm_2, vl);
713 vfloat16m1_t _out0tm3 = vle16_v_f16m1(output0_tm_3, vl);
714
715 vfloat16m1_t _tmp0m = vfadd_vv_f16m1(vfadd_vv_f16m1(_out0tm0, _out0tm1, vl), _out0tm2, vl);
716 vfloat16m1_t _tmp1m = vfadd_vv_f16m1(vfsub_vv_f16m1(_out0tm1, _out0tm2, vl), _out0tm3, vl);
717
718 vse16_v_f16m1(tmp[0][m], _tmp0m, vl);
719 vse16_v_f16m1(tmp[1][m], _tmp1m, vl);
720
721 output0_tm_0 += tiles * packn * 4;
722 output0_tm_1 += tiles * packn * 4;
723 output0_tm_2 += tiles * packn * 4;
724 output0_tm_3 += tiles * packn * 4;
725 }
726
727 for (int m = 0; m < 2; m++)
728 {
729 vfloat16m1_t _tmp00 = vle16_v_f16m1(tmp[m][0], vl);
730 vfloat16m1_t _tmp01 = vle16_v_f16m1(tmp[m][1], vl);
731 vfloat16m1_t _tmp02 = vle16_v_f16m1(tmp[m][2], vl);
732 vfloat16m1_t _tmp03 = vle16_v_f16m1(tmp[m][3], vl);
733
734 vfloat16m1_t _out00 = vfadd_vv_f16m1(_bias0, vfadd_vv_f16m1(vfadd_vv_f16m1(_tmp00, _tmp01, vl), _tmp02, vl), vl);
735 vfloat16m1_t _out01 = vfadd_vv_f16m1(_bias0, vfadd_vv_f16m1(vfsub_vv_f16m1(_tmp01, _tmp02, vl), _tmp03, vl), vl);
736
737 vse16_v_f16m1(output0, _out00, vl);
738 vse16_v_f16m1(output0 + packn, _out01, vl);
739
740 output0 += outw * packn;
741 }
742 }
743 }
744 }
745}