Elffuss BraidHub

kikocis/ncnn public

master · 8.5 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 "convolutiondepthwise1d.h"
16
17#include "layer_type.h"
18
19#include "fused_activation.h"
20
21namespace ncnn {
22
23ConvolutionDepthWise1D::ConvolutionDepthWise1D()
24{
25 one_blob_only = true;
26 support_inplace = false;
27}
28
29int ConvolutionDepthWise1D::load_param(const ParamDict& pd)
30{
31 num_output = pd.get(0, 0);
32 kernel_w = pd.get(1, 0);
33 dilation_w = pd.get(2, 1);
34 stride_w = pd.get(3, 1);
35 pad_left = pd.get(4, 0);
36 pad_right = pd.get(15, pad_left);
37 pad_value = pd.get(18, 0.f);
38 bias_term = pd.get(5, 0);
39 weight_data_size = pd.get(6, 0);
40 group = pd.get(7, 1);
41 activation_type = pd.get(9, 0);
42 activation_params = pd.get(10, Mat());
43
44 dynamic_weight = pd.get(19, 0);
45
46 if (dynamic_weight)
47 {
48 one_blob_only = false;
49 }
50
51 if (num_output % group != 0)
52 {
53 // reject invalid group
54 return -100;
55 }
56
57 return 0;
58}
59
60int ConvolutionDepthWise1D::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 convolutiondepthwise1d(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)
80{
81 const int h = bottom_blob.h;
82
83 const int outw = top_blob.w;
84 const int outh = top_blob.h;
85
86 const int bias_term = bias_data.empty() ? 0 : 1;
87
88 // depth-wise
89 if (h == group && group == outh)
90 {
91 #pragma omp parallel for num_threads(opt.num_threads)
92 for (int g = 0; g < group; g++)
93 {
94 float* outptr = top_blob.row(g);
95 const float* kptr = (const float*)weight_data + kernel_w * g;
96
97 for (int j = 0; j < outw; j++)
98 {
99 float sum = 0.f;
100
101 if (bias_term)
102 sum = bias_data[g];
103
104 const float* sptr = bottom_blob.row(g) + j * stride_w;
105
106 for (int k = 0; k < kernel_w; k++)
107 {
108 float val = *sptr;
109 float w = kptr[k];
110 sum += val * w;
111
112 sptr += dilation_w;
113 }
114
115 outptr[j] = activation_ss(sum, activation_type, activation_params);
116 }
117 }
118 }
119 else
120 {
121 // group convolution
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 float* outptr = top_blob.row(g * outh_g + p);
135 const float* weight_data_ptr = (const float*)weight_data + kernel_w * h_g * outh_g * g;
136
137 for (int j = 0; j < outw; j++)
138 {
139 float sum = 0.f;
140
141 if (bias_term)
142 sum = bias_data[outh_g * g + p];
143
144 const float* kptr = weight_data_ptr + kernel_w * h_g * p;
145
146 for (int q = 0; q < h_g; q++)
147 {
148 const float* sptr = bottom_blob.row(h_g * g + q) + j * stride_w;
149
150 for (int k = 0; k < kernel_w; k++)
151 {
152 float val = *sptr;
153 float w = kptr[k];
154 sum += val * w;
155
156 sptr += dilation_w;
157 }
158
159 kptr += kernel_w;
160 }
161
162 outptr[j] = activation_ss(sum, activation_type, activation_params);
163 }
164 }
165 }
166 }
167
168 return 0;
169}
170
171int ConvolutionDepthWise1D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
172{
173 Mat bottom_blob_bordered;
174 make_padding(bottom_blob, bottom_blob_bordered, opt);
175 if (bottom_blob_bordered.empty())
176 return -100;
177
178 const int w = bottom_blob_bordered.w;
179 const size_t elemsize = bottom_blob.elemsize;
180
181 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
182
183 const int outw = (w - kernel_extent_w) / stride_w + 1;
184
185 top_blob.create(outw, num_output, elemsize, opt.blob_allocator);
186 if (top_blob.empty())
187 return -100;
188
189 int ret = convolutiondepthwise1d(bottom_blob_bordered, top_blob, weight_data, bias_data, kernel_w, stride_w, dilation_w, group, activation_type, activation_params, opt);
190 if (ret != 0)
191 return ret;
192
193 return 0;
194}
195
196int ConvolutionDepthWise1D::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
197{
198 const Mat& bottom_blob = bottom_blobs[0];
199 const Mat& _weight_data = bottom_blobs[1];
200 Mat& top_blob = top_blobs[0];
201
202 const int _kernel_w = _weight_data.w;
203 const int _num_output = _weight_data.c;
204
205 Mat weight_data_flattened;
206 flatten(_weight_data, weight_data_flattened, opt);
207 if (weight_data_flattened.empty())
208 return -100;
209
210 Mat bias_data_flattened;
211 if (bias_term)
212 {
213 const Mat& _bias_data = bottom_blobs[2];
214 flatten(_bias_data, bias_data_flattened, opt);
215 if (bias_data_flattened.empty())
216 return -100;
217 }
218
219 Mat bottom_blob_bordered;
220 make_padding(bottom_blob, bottom_blob_bordered, _kernel_w, opt);
221 if (bottom_blob_bordered.empty())
222 return -100;
223
224 const int w = bottom_blob_bordered.w;
225 const size_t elemsize = bottom_blob_bordered.elemsize;
226
227 const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
228
229 const int outw = (w - kernel_extent_w) / stride_w + 1;
230
231 top_blob.create(outw, _num_output, elemsize, opt.blob_allocator);
232 if (top_blob.empty())
233 return -100;
234
235 int ret = convolutiondepthwise1d(bottom_blob_bordered, top_blob, weight_data_flattened, bias_data_flattened, _kernel_w, stride_w, dilation_w, group, activation_type, activation_params, opt);
236 if (ret != 0)
237 return ret;
238
239 return 0;
240}
241
242void ConvolutionDepthWise1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const
243{
244 make_padding(bottom_blob, bottom_blob_bordered, kernel_w, opt);
245}
246
247void ConvolutionDepthWise1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, int _kernel_w, const Option& opt) const
248{
249 int w = bottom_blob.w;
250
251 const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
252
253 bottom_blob_bordered = bottom_blob;
254 if (pad_left > 0 || pad_right > 0)
255 {
256 Option opt_b = opt;
257 opt_b.blob_allocator = opt.workspace_allocator;
258 copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b);
259 }
260 else if (pad_left == -233 && pad_right == -233)
261 {
262 // tensorflow padding=SAME or onnx padding=SAME_UPPER
263 int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
264 if (wpad > 0)
265 {
266 Option opt_b = opt;
267 opt_b.blob_allocator = opt.workspace_allocator;
268 copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
269 }
270 }
271 else if (pad_left == -234 && pad_right == -234)
272 {
273 // onnx padding=SAME_LOWER
274 int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
275 if (wpad > 0)
276 {
277 Option opt_b = opt;
278 opt_b.blob_allocator = opt.workspace_allocator;
279 copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
280 }
281 }
282}
283
284} // namespace ncnn