Elffuss BraidHub

kikocis/ncnn public

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