Elffuss BraidHub

kikocis/ncnn public

master · 7.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_winograd43_transform_kernel_int8_lsx(const Mat& kernel, Mat& kernel_tm_packed, int inch, int outch, const Option& opt)
16{
17 // winograd43 transform kernel
18 Mat kernel_tm(6 * 6, inch, outch, (size_t)2u);
19
20 const short ktm[6][3] = {
21 {6, 0, 0},
22 {-4, -4, -4},
23 {-4, 4, -4},
24 {1, 2, 4},
25 {1, -2, 4},
26 {0, 0, 6}
27 };
28
29 #pragma omp parallel for num_threads(opt.num_threads)
30 for (int p = 0; p < outch; p++)
31 {
32 for (int q = 0; q < inch; q++)
33 {
34 const signed char* kernel0 = (const signed char*)kernel + p * inch * 9 + q * 9;
35 short* kernel_tm0 = kernel_tm.channel(p).row<short>(q);
36
37 // transform kernel
38 const signed char* k0 = kernel0;
39 const signed char* k1 = kernel0 + 3;
40 const signed char* k2 = kernel0 + 6;
41
42 // h
43 short tmp[6][3];
44 for (int i = 0; i < 6; i++)
45 {
46 tmp[i][0] = k0[0] * ktm[i][0] + k0[1] * ktm[i][1] + k0[2] * ktm[i][2];
47 tmp[i][1] = k1[0] * ktm[i][0] + k1[1] * ktm[i][1] + k1[2] * ktm[i][2];
48 tmp[i][2] = k2[0] * ktm[i][0] + k2[1] * ktm[i][1] + k2[2] * ktm[i][2];
49 }
50
51 // U
52 for (int j = 0; j < 6; j++)
53 {
54 short* tmpp = &tmp[j][0];
55
56 for (int i = 0; i < 6; i++)
57 {
58 kernel_tm0[j * 6 + i] = tmpp[0] * ktm[i][0] + tmpp[1] * ktm[i][1] + tmpp[2] * ktm[i][2];
59 }
60 }
61 }
62 }
63
64 // interleave
65 // src = 36-inch-outch
66 // dst = 2b-inch-36-outch/2b
67#if __loongarch_sx
68 if (outch >= 4)
69 {
70 if (inch >= 4)
71 kernel_tm_packed.create(inch / 4 + inch % 4, 36, outch / 4 + outch % 4, (size_t)2u * 16, 16);
72 else
73 kernel_tm_packed.create(inch, 36, outch / 4 + outch % 4, (size_t)2u * 4, 4);
74 }
75#else // __loongarch_sx
76 if (outch >= 2)
77 {
78 kernel_tm_packed.create(inch, 36, outch / 2 + outch % 2, (size_t)2u * 2, 2);
79 }
80#endif // __loongarch_sx
81 else
82 {
83#if __loongarch_sx
84 if (inch >= 4)
85 kernel_tm_packed.create(inch / 4 + inch % 4, 36, outch, (size_t)2u * 4, 4);
86 else
87#endif // __loongarch_sx
88 {
89 kernel_tm_packed.create(inch, 36, outch, (size_t)2u, 1);
90 }
91 }
92
93 int p = 0;
94#if __loongarch_sx
95 for (; p + 3 < outch; p += 4)
96 {
97 const Mat k0 = kernel_tm.channel(p);
98 const Mat k1 = kernel_tm.channel(p + 1);
99 const Mat k2 = kernel_tm.channel(p + 2);
100 const Mat k3 = kernel_tm.channel(p + 3);
101
102 Mat g0 = kernel_tm_packed.channel(p / 4);
103
104 for (int k = 0; k < 36; k++)
105 {
106 short* g00 = g0.row<short>(k);
107
108 int q = 0;
109 for (; q + 3 < inch; q += 4)
110 {
111 g00[0] = k0.row<const short>(q)[k];
112 g00[1] = k0.row<const short>(q + 1)[k];
113 g00[2] = k0.row<const short>(q + 2)[k];
114 g00[3] = k0.row<const short>(q + 3)[k];
115 g00[4] = k1.row<const short>(q)[k];
116 g00[5] = k1.row<const short>(q + 1)[k];
117 g00[6] = k1.row<const short>(q + 2)[k];
118 g00[7] = k1.row<const short>(q + 3)[k];
119 g00[8] = k2.row<const short>(q)[k];
120 g00[9] = k2.row<const short>(q + 1)[k];
121 g00[10] = k2.row<const short>(q + 2)[k];
122 g00[11] = k2.row<const short>(q + 3)[k];
123 g00[12] = k3.row<const short>(q)[k];
124 g00[13] = k3.row<const short>(q + 1)[k];
125 g00[14] = k3.row<const short>(q + 2)[k];
126 g00[15] = k3.row<const short>(q + 3)[k];
127 g00 += 16;
128 }
129 for (; q < inch; q++)
130 {
131 g00[0] = k0.row<const short>(q)[k];
132 g00[1] = k1.row<const short>(q)[k];
133 g00[2] = k2.row<const short>(q)[k];
134 g00[3] = k3.row<const short>(q)[k];
135 g00 += 4;
136 }
137 }
138 }
139#else // __loongarch_sx
140 for (; p + 1 < outch; p += 2)
141 {
142 const Mat k0 = kernel_tm.channel(p);
143 const Mat k1 = kernel_tm.channel(p + 1);
144
145 Mat g0 = kernel_tm_packed.channel(p / 2);
146
147 for (int k = 0; k < 36; k++)
148 {
149 short* g00 = g0.row<short>(k);
150
151 int q = 0;
152 for (; q < inch; q++)
153 {
154 g00[0] = k0.row<const short>(q)[k];
155 g00[1] = k1.row<const short>(q)[k];
156 g00 += 2;
157 }
158 }
159 }
160#endif // __loongarch_sx
161 for (; p < outch; p++)
162 {
163 const Mat k0 = kernel_tm.channel(p);
164
165#if __loongarch_sx
166 Mat g0 = kernel_tm_packed.channel(p / 4 + p % 4);
167#else
168 Mat g0 = kernel_tm_packed.channel(p / 2 + p % 2);
169#endif
170
171 for (int k = 0; k < 36; k++)
172 {
173 short* g00 = g0.row<short>(k);
174
175 int q = 0;
176#if __loongarch_sx
177 for (; q + 3 < inch; q += 4)
178 {
179 g00[0] = k0.row<const short>(q)[k];
180 g00[1] = k0.row<const short>(q + 1)[k];
181 g00[2] = k0.row<const short>(q + 2)[k];
182 g00[3] = k0.row<const short>(q + 3)[k];
183 g00 += 4;
184 }
185#endif // __loongarch_sx
186 for (; q < inch; q++)
187 {
188 g00[0] = k0.row<const short>(q)[k];
189 g00 += 1;
190 }
191 }
192 }
193}
194
195static void conv3x3s1_winograd43_int8_lsx(const Mat& bottom_blob, Mat& top_blob, const Mat& kernel_tm, const Option& opt)
196{
197 int w = bottom_blob.w;
198 int h = bottom_blob.h;
199 int inch = bottom_blob.c;
200 // size_t elemsize = bottom_blob.elemsize;
201 int elempack = bottom_blob.elempack;
202
203 int outw = top_blob.w;
204 int outh = top_blob.h;
205 int outch = top_blob.c;
206
207 // pad to 4n+2
208 Mat bottom_blob_bordered = bottom_blob;
209
210 outw = (outw + 3) / 4 * 4;
211 outh = (outh + 3) / 4 * 4;
212
213 w = outw + 2;
214 h = outh + 2;
215 copy_make_border(bottom_blob, bottom_blob_bordered, 0, h - bottom_blob.h, 0, w - bottom_blob.w, BORDER_CONSTANT, 0.f, opt);
216
217 // BEGIN transform input
218 Mat bottom_blob_tm;
219 {
220 int w_tiles = outw / 4;
221 int h_tiles = outh / 4;
222 const int tiles = w_tiles * h_tiles;
223
224 bottom_blob_tm.create(tiles, 36, inch, 2u * elempack, elempack, opt.workspace_allocator);
225 conv3x3s1_winograd43_transform_input_int8_lsx(bottom_blob_bordered, bottom_blob_tm, opt);
226 }
227 bottom_blob_bordered = Mat();
228 // END transform input
229
230 // BEGIN dot
231 Mat top_blob_tm;
232 convolution_winograd_dot_int8_lsx(bottom_blob_tm, outch, kernel_tm, top_blob_tm, opt);
233 // END dot
234
235 // BEGIN transform output
236 Mat top_blob_bordered;
237 if (outw == top_blob.w && outh == top_blob.h)
238 {
239 top_blob_bordered = top_blob;
240 }
241 else
242 {
243 top_blob_bordered.create(outw, outh, outch, 4u, 1, opt.workspace_allocator);
244 }
245 {
246 conv3x3s1_winograd43_transform_output_int8_lsx(top_blob_tm, top_blob_bordered, opt);
247 }
248 // END transform output
249
250 // cut result pad
251 copy_cut_border(top_blob_bordered, top_blob, 0, top_blob_bordered.h - top_blob.h, 0, top_blob_bordered.w - top_blob.w, opt);
252}