Elffuss BraidHub

kikocis/ncnn public

master · 11.6 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
15#include "deconvolution_mips.h"
16
17#include "layer_type.h"
18
19#if __mips_msa
20#include <msa.h>
21#endif // __mips_msa
22
23#include "mips_activation.h"
24#include "mips_usability.h"
25
26namespace ncnn {
27
28#if __mips_msa
29#include "deconvolution_pack4.h"
30#include "deconvolution_pack1to4.h"
31#include "deconvolution_pack4to1.h"
32#endif // __mips_msa
33
34Deconvolution_mips::Deconvolution_mips()
35{
36#if __mips_msa
37 support_packing = true;
38#endif // __mips_msa
39}
40
41int Deconvolution_mips::create_pipeline(const Option& opt)
42{
43 if (dynamic_weight)
44 return 0;
45
46 const int maxk = kernel_w * kernel_h;
47 int num_input = weight_data_size / maxk / num_output;
48
49 Mat weight_data_transposed(weight_data.w);
50 {
51 float* pt = weight_data_transposed;
52 const float* p = weight_data;
53
54 for (int i = 0; i < num_input * num_output; i++)
55 {
56 for (int k = 0; k < maxk; k++)
57 {
58 pt[maxk - 1 - k] = p[k];
59 }
60
61 p += maxk;
62 pt += maxk;
63 }
64 }
65
66 int elempack = 1;
67 int out_elempack = 1;
68#if __mips_msa
69 if (opt.use_packing_layout)
70 {
71 elempack = num_input % 4 == 0 ? 4 : 1;
72 out_elempack = num_output % 4 == 0 ? 4 : 1;
73 }
74#endif
75
76 // src = kw-kh-inch-outch
77 // dst = pb-pa-kw-kh-inch/pa-outch/pb
78 {
79 Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output);
80
81 weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack);
82
83 for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
84 {
85 float* g00 = weight_data_tm.channel(q / out_elempack);
86
87 for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
88 {
89 for (int k = 0; k < maxk; k++)
90 {
91 for (int i = 0; i < elempack; i++)
92 {
93 for (int j = 0; j < out_elempack; j++)
94 {
95 const float* k00 = weight_data_r2.channel(q + j).row(p + i);
96
97 g00[0] = k00[k];
98
99 g00++;
100 }
101 }
102 }
103 }
104 }
105 }
106
107#if __mips_msa
108 // pack4
109 if (elempack == 4 && out_elempack == 4)
110 {
111 }
112
113 // pack1ton
114 if (elempack == 1 && out_elempack == 4)
115 {
116 }
117
118 // pack4to1
119 if (elempack == 4 && out_elempack == 1)
120 {
121 }
122#endif // __mips_msa
123
124 // pack1
125 if (elempack == 1 && out_elempack == 1)
126 {
127 }
128
129 if (opt.lightmode)
130 weight_data.release();
131
132 return 0;
133}
134
135int Deconvolution_mips::destroy_pipeline(const Option& opt)
136{
137 return 0;
138}
139
140int Deconvolution_mips::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
141{
142 // deconvolv with NxN kernel
143 // value = value + bias
144
145 int w = bottom_blob.w;
146 int h = bottom_blob.h;
147 int channels = bottom_blob.c;
148 size_t elemsize = bottom_blob.elemsize;
149 int elempack = bottom_blob.elempack;
150
151 // NCNN_LOGE("Deconvolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
152
153 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
154 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
155
156 int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
157 int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
158 int out_elempack = 1;
159#if __mips_msa
160 if (opt.use_packing_layout)
161 {
162 out_elempack = num_output % 4 == 0 ? 4 : 1;
163 }
164#endif
165 size_t out_elemsize = elemsize / elempack * out_elempack;
166
167 Mat top_blob_bordered;
168 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
169 {
170 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator);
171 }
172 else
173 {
174 top_blob_bordered = top_blob;
175 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
176 }
177 if (top_blob_bordered.empty())
178 return -100;
179
180 const int maxk = kernel_w * kernel_h;
181
182#if __mips_msa
183 if (elempack == 4 && out_elempack == 4)
184 {
185 {
186 deconvolution_pack4_msa(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
187 }
188 }
189
190 if (elempack == 1 && out_elempack == 4)
191 {
192 {
193 deconvolution_pack1to4_msa(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
194 }
195 }
196
197 if (elempack == 4 && out_elempack == 1)
198 {
199 {
200 deconvolution_pack4to1_msa(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
201 }
202 }
203#endif // __mips_msa
204
205 if (elempack == 1 && out_elempack == 1)
206 {
207 {
208 // num_output
209 #pragma omp parallel for num_threads(opt.num_threads)
210 for (int p = 0; p < num_output; p++)
211 {
212 float* outptr = top_blob_bordered.channel(p);
213
214 for (int i = 0; i < outh; i++)
215 {
216 for (int j = 0; j < outw; j++)
217 {
218 float sum = 0.f;
219
220 if (bias_term)
221 {
222 sum = bias_data[p];
223 }
224
225 const float* kptr = (const float*)weight_data_tm.channel(p);
226
227 // channels
228 for (int q = 0; q < channels; q++)
229 {
230 const Mat m = bottom_blob.channel(q);
231
232 for (int y = 0; y < kernel_h; y++)
233 {
234 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
235 if (sys < 0 || sys % stride_h != 0)
236 continue;
237
238 int sy = sys / stride_h;
239 if (sy >= h)
240 continue;
241
242 const float* sptr = m.row(sy);
243
244 for (int x = 0; x < kernel_w; x++)
245 {
246 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
247 if (sxs < 0 || sxs % stride_w != 0)
248 continue;
249
250 int sx = sxs / stride_w;
251 if (sx >= w)
252 continue;
253
254 float val = sptr[sx];
255
256 int k = y * kernel_w + x;
257
258 float w = kptr[k];
259
260 sum += val * w;
261 }
262 }
263
264 kptr += maxk;
265 }
266
267 sum = activation_ss(sum, activation_type, activation_params);
268
269 outptr[j] = sum;
270 }
271
272 outptr += outw;
273 }
274 }
275 }
276 }
277
278 cut_padding(top_blob_bordered, top_blob, opt);
279 if (top_blob.empty())
280 return -100;
281
282 return 0;
283}
284
285int Deconvolution_mips::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
286{
287 const Mat& bottom_blob = bottom_blobs[0];
288 const Mat& _weight_data = bottom_blobs[1];
289 Mat& top_blob = top_blobs[0];
290
291 const int _num_input = bottom_blob.c * bottom_blob.elempack;
292 const int _kernel_w = _weight_data.w;
293 const int _kernel_h = _weight_data.h;
294 const int _num_output = _weight_data.d * 1;
295
296 Mat weight_data_flattened;
297 flatten(_weight_data, weight_data_flattened, opt);
298 if (weight_data_flattened.empty())
299 return -100;
300
301 // weight_data_flattened as pack1
302 weight_data_flattened.w *= weight_data_flattened.elempack;
303 weight_data_flattened.elemsize /= weight_data_flattened.elempack;
304 weight_data_flattened.elempack = 1;
305
306 // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw
307 Mat weight_data_transposed;
308 {
309 weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / 1, 4u, opt.workspace_allocator);
310 if (weight_data_transposed.empty())
311 return -100;
312
313 const int outch_g = _num_output / 1;
314 const int inch_g = _num_input / 1;
315 const int maxk = _kernel_h * _kernel_w;
316
317 for (int g = 0; g < 1; g++)
318 {
319 // reorder weight from inch-outch to outch-inch
320 float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk;
321 const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk;
322 for (int i = 0; i < outch_g; i++)
323 {
324 for (int j = 0; j < inch_g; j++)
325 {
326 for (int k = 0; k < maxk; k++)
327 {
328 wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
329 }
330 }
331 }
332 }
333 }
334
335 Mat bias_data_flattened;
336 if (bias_term)
337 {
338 const Mat& _bias_data = bottom_blobs[2];
339 flatten(_bias_data, bias_data_flattened, opt);
340 if (bias_data_flattened.empty())
341 return -100;
342
343 // bias_data_flattened as pack1
344 bias_data_flattened.w *= bias_data_flattened.elempack;
345 bias_data_flattened.elemsize /= bias_data_flattened.elempack;
346 bias_data_flattened.elempack = 1;
347 }
348
349 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution);
350
351 ncnn::ParamDict pd;
352 pd.set(0, _num_output);
353 pd.set(1, _kernel_w);
354 pd.set(11, _kernel_h);
355 pd.set(2, dilation_w);
356 pd.set(12, dilation_h);
357 pd.set(3, stride_w);
358 pd.set(13, stride_h);
359 pd.set(4, pad_left);
360 pd.set(15, pad_right);
361 pd.set(14, pad_top);
362 pd.set(16, pad_bottom);
363 pd.set(18, output_pad_right);
364 pd.set(19, output_pad_bottom);
365 pd.set(20, output_w);
366 pd.set(21, output_h);
367 pd.set(5, bias_term);
368 pd.set(6, weight_data_transposed.w);
369 pd.set(9, activation_type);
370 pd.set(10, activation_params);
371
372 op->load_param(pd);
373
374 ncnn::Mat weights[2];
375 weights[0] = weight_data_transposed;
376 weights[1] = bias_data_flattened;
377
378 op->load_model(ncnn::ModelBinFromMatArray(weights));
379
380 op->create_pipeline(opt);
381
382 op->forward(bottom_blob, top_blob, opt);
383
384 op->destroy_pipeline(opt);
385
386 delete op;
387
388 return 0;
389}
390
391} // namespace ncnn