Elffuss BraidHub

kikocis/ncnn public

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