Elffuss BraidHub

kikocis/ncnn public

master · 11.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 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 "deconvolutiondepthwise.h"
16
17#include "fused_activation.h"
18
19namespace ncnn {
20
21DeconvolutionDepthWise::DeconvolutionDepthWise()
22{
23 one_blob_only = true;
24 support_inplace = false;
25}
26
27int DeconvolutionDepthWise::load_param(const ParamDict& pd)
28{
29 num_output = pd.get(0, 0);
30 kernel_w = pd.get(1, 0);
31 kernel_h = pd.get(11, kernel_w);
32 dilation_w = pd.get(2, 1);
33 dilation_h = pd.get(12, dilation_w);
34 stride_w = pd.get(3, 1);
35 stride_h = pd.get(13, stride_w);
36 pad_left = pd.get(4, 0);
37 pad_right = pd.get(15, pad_left);
38 pad_top = pd.get(14, pad_left);
39 pad_bottom = pd.get(16, pad_top);
40 output_pad_right = pd.get(18, 0);
41 output_pad_bottom = pd.get(19, output_pad_right);
42 output_w = pd.get(20, 0);
43 output_h = pd.get(21, output_w);
44 bias_term = pd.get(5, 0);
45 weight_data_size = pd.get(6, 0);
46 group = pd.get(7, 1);
47 activation_type = pd.get(9, 0);
48 activation_params = pd.get(10, Mat());
49
50 dynamic_weight = pd.get(28, 0);
51
52 if (dynamic_weight)
53 {
54 one_blob_only = false;
55 }
56
57 return 0;
58}
59
60int DeconvolutionDepthWise::load_model(const ModelBin& mb)
61{
62 if (dynamic_weight)
63 return 0;
64
65 weight_data = mb.load(weight_data_size, 0);
66 if (weight_data.empty())
67 return -100;
68
69 if (bias_term)
70 {
71 bias_data = mb.load(num_output, 1);
72 if (bias_data.empty())
73 return -100;
74 }
75
76 return 0;
77}
78
79static int deconvolutiondepthwise(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int kernel_h, int stride_w, int stride_h, int dilation_w, int dilation_h, int group, int activation_type, const Mat& activation_params, const Option& opt)
80{
81 const int inch = bottom_blob.c;
82
83 const int outw = top_blob.w;
84 const int outch = top_blob.c;
85
86 const int maxk = kernel_w * kernel_h;
87
88 // kernel offsets
89 std::vector<int> _space_ofs(maxk);
90 int* space_ofs = &_space_ofs[0];
91 {
92 int p1 = 0;
93 int p2 = 0;
94 int gap = outw * dilation_h - kernel_w * dilation_w;
95 for (int i = 0; i < kernel_h; i++)
96 {
97 for (int j = 0; j < kernel_w; j++)
98 {
99 space_ofs[p1] = p2;
100 p1++;
101 p2 += dilation_w;
102 }
103 p2 += gap;
104 }
105 }
106
107 // depth-wise
108 if (inch == group && group == outch)
109 {
110 #pragma omp parallel for num_threads(opt.num_threads)
111 for (int g = 0; g < group; g++)
112 {
113 const float* inptr = bottom_blob.channel(g);
114 const float* kptr = (const float*)weight_data + maxk * g;
115 Mat out = top_blob.channel(g);
116
117 const float bias = bias_data.empty() ? 0.f : bias_data[g];
118
119 out.fill(bias);
120
121 // shadowed variable for less openmp task args
122 const int w = bottom_blob.w;
123 const int h = bottom_blob.h;
124 const int outw = top_blob.w;
125 const int outh = top_blob.h;
126
127 for (int i = 0; i < h; i++)
128 {
129 for (int j = 0; j < w; j++)
130 {
131 float* outptr = out.row(i * stride_h) + j * stride_w;
132
133 const float val = inptr[i * w + j];
134
135 for (int k = 0; k < maxk; k++)
136 {
137 float w = kptr[k];
138 outptr[space_ofs[k]] += val * w;
139 }
140 }
141 }
142
143 {
144 float* outptr = out;
145 int size = outw * outh;
146
147 for (int i = 0; i < size; i++)
148 {
149 outptr[i] = activation_ss(outptr[i], activation_type, activation_params);
150 }
151 }
152 }
153 }
154 else
155 {
156 const int inch_g = inch / group;
157 const int outch_g = outch / group;
158
159#ifdef _WIN32
160 #pragma omp parallel for num_threads(opt.num_threads)
161#else
162 #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
163#endif
164 for (int g = 0; g < group; g++)
165 {
166 for (int p = 0; p < outch_g; p++)
167 {
168 Mat out = top_blob.channel(g * outch_g + p);
169
170 const float* weight_data_ptr = (const float*)weight_data + maxk * inch_g * outch_g * g;
171
172 const float bias = bias_data.empty() ? 0.f : bias_data[g * outch_g + p];
173
174 out.fill(bias);
175
176 // shadowed variable for less openmp task args
177 const int w = bottom_blob.w;
178 const int h = bottom_blob.h;
179 const int outw = top_blob.w;
180 const int outh = top_blob.h;
181
182 for (int i = 0; i < h; i++)
183 {
184 for (int j = 0; j < w; j++)
185 {
186 float* outptr = out.row(i * stride_h) + j * stride_w;
187
188 const float* kptr = weight_data_ptr + maxk * inch_g * p;
189
190 for (int q = 0; q < inch_g; q++)
191 {
192 const float val = bottom_blob.channel(inch_g * g + q).row(i)[j];
193
194 for (int k = 0; k < maxk; k++)
195 {
196 outptr[space_ofs[k]] += val * kptr[k];
197 }
198
199 kptr += maxk;
200 }
201 }
202 }
203
204 {
205 float* outptr = out;
206 int size = outw * outh;
207
208 for (int i = 0; i < size; i++)
209 {
210 outptr[i] = activation_ss(outptr[i], activation_type, activation_params);
211 }
212 }
213 }
214 }
215 }
216
217 return 0;
218}
219
220int DeconvolutionDepthWise::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
221{
222 int w = bottom_blob.w;
223 int h = bottom_blob.h;
224 size_t elemsize = bottom_blob.elemsize;
225
226 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
227 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
228
229 int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
230 int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
231
232 Mat top_blob_bordered;
233 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
234 {
235 top_blob_bordered.create(outw, outh, num_output, elemsize, opt.workspace_allocator);
236 }
237 else
238 {
239 top_blob_bordered = top_blob;
240 top_blob_bordered.create(outw, outh, num_output, elemsize, opt.blob_allocator);
241 }
242 if (top_blob_bordered.empty())
243 return -100;
244
245 int ret = deconvolutiondepthwise(bottom_blob, top_blob_bordered, weight_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, dilation_w, dilation_h, group, activation_type, activation_params, opt);
246 if (ret != 0)
247 return ret;
248
249 cut_padding(top_blob_bordered, top_blob, opt);
250 if (top_blob.empty())
251 return -100;
252
253 return 0;
254}
255
256int DeconvolutionDepthWise::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
257{
258 const Mat& bottom_blob = bottom_blobs[0];
259 const Mat& _weight_data = bottom_blobs[1];
260 Mat& top_blob = top_blobs[0];
261
262 const int _num_input = bottom_blob.c;
263 const int _kernel_w = _weight_data.w;
264 const int _kernel_h = _weight_data.h;
265 const int _num_output = _weight_data.d * group;
266
267 Mat weight_data_flattened;
268 flatten(_weight_data, weight_data_flattened, opt);
269 if (weight_data_flattened.empty())
270 return -100;
271
272 // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw
273 Mat weight_data_transposed;
274 {
275 weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / group, 4u, opt.workspace_allocator);
276 if (weight_data_transposed.empty())
277 return -100;
278
279 const int outch_g = _num_output / group;
280 const int inch_g = _num_input / group;
281 const int maxk = _kernel_h * _kernel_w;
282
283 for (int g = 0; g < group; g++)
284 {
285 // reorder weight from inch-outch to outch-inch
286 float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk;
287 const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk;
288 for (int i = 0; i < outch_g; i++)
289 {
290 for (int j = 0; j < inch_g; j++)
291 {
292 for (int k = 0; k < maxk; k++)
293 {
294 wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
295 }
296 }
297 }
298 }
299 }
300
301 Mat bias_data_flattened;
302 if (bias_term)
303 {
304 const Mat& _bias_data = bottom_blobs[2];
305 flatten(_bias_data, bias_data_flattened, opt);
306 if (bias_data_flattened.empty())
307 return -100;
308 }
309
310 const int w = bottom_blob.w;
311 const int h = bottom_blob.h;
312
313 const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
314 const int kernel_extent_h = dilation_h * (_kernel_h - 1) + 1;
315
316 int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
317 int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
318
319 Mat top_blob_bordered;
320 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
321 {
322 top_blob_bordered.create(outw, outh, _num_output, 4u, opt.workspace_allocator);
323 }
324 else
325 {
326 top_blob_bordered = top_blob;
327 top_blob_bordered.create(outw, outh, _num_output, 4u, opt.blob_allocator);
328 }
329 if (top_blob_bordered.empty())
330 return -100;
331
332 int ret = deconvolutiondepthwise(bottom_blob, top_blob_bordered, weight_data_transposed, bias_data_flattened, _kernel_w, _kernel_h, stride_w, stride_h, dilation_w, dilation_h, group, activation_type, activation_params, opt);
333 if (ret != 0)
334 return ret;
335
336 cut_padding(top_blob_bordered, top_blob, opt);
337 if (top_blob.empty())
338 return -100;
339
340 return 0;
341}
342
343void DeconvolutionDepthWise::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const
344{
345 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
346 {
347 copy_cut_border(top_blob_bordered, top_blob, pad_top, pad_bottom, pad_left, pad_right, opt);
348 }
349 else if (output_w > 0 && output_h > 0)
350 {
351 int wcut = top_blob_bordered.w - output_w;
352 int hcut = top_blob_bordered.h - output_h;
353
354 if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233)
355 {
356 // onnx padding=SAME_UPPER
357 copy_cut_border(top_blob_bordered, top_blob, hcut / 2, hcut - hcut / 2, wcut / 2, wcut - wcut / 2, opt);
358 }
359 else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234)
360 {
361 // onnx padding=SAME_LOWER
362 copy_cut_border(top_blob_bordered, top_blob, hcut - hcut / 2, hcut / 2, wcut - wcut / 2, wcut / 2, opt);
363 }
364 }
365 else
366 {
367 top_blob = top_blob_bordered;
368 }
369}
370
371} // namespace ncnn