Elffuss BraidHub

kikocis/ncnn public

master · 6.8 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 "convolution1d.h"
16
17#include "fused_activation.h"
18
19namespace ncnn {
20
21Convolution1D::Convolution1D()
22{
23 one_blob_only = true;
24 support_inplace = false;
25}
26
27int Convolution1D::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 pad_value = pd.get(18, 0.f);
36 bias_term = pd.get(5, 0);
37 weight_data_size = pd.get(6, 0);
38 activation_type = pd.get(9, 0);
39 activation_params = pd.get(10, Mat());
40
41 dynamic_weight = pd.get(19, 0);
42
43 if (dynamic_weight)
44 {
45 one_blob_only = false;
46 }
47
48 return 0;
49}
50
51int Convolution1D::load_model(const ModelBin& mb)
52{
53 if (dynamic_weight)
54 return 0;
55
56 weight_data = mb.load(weight_data_size, 0);
57 if (weight_data.empty())
58 return -100;
59
60 if (bias_term)
61 {
62 bias_data = mb.load(num_output, 1);
63 if (bias_data.empty())
64 return -100;
65 }
66
67 return 0;
68}
69
70static int convolution1d(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 activation_type, const Mat& activation_params, const Option& opt)
71{
72 const int h = bottom_blob.h;
73
74 const int outw = top_blob.w;
75 const int outh = top_blob.h;
76
77 const int bias_term = bias_data.empty() ? 0 : 1;
78
79 #pragma omp parallel for num_threads(opt.num_threads)
80 for (int p = 0; p < outh; p++)
81 {
82 float* outptr = top_blob.row(p);
83
84 for (int j = 0; j < outw; j++)
85 {
86 float sum = 0.f;
87
88 if (bias_term)
89 sum = bias_data[p];
90
91 const float* kptr = (const float*)weight_data + kernel_w * h * p;
92
93 for (int q = 0; q < h; q++)
94 {
95 const float* sptr = bottom_blob.row(q) + j * stride_w;
96
97 for (int k = 0; k < kernel_w; k++)
98 {
99 float val = *sptr;
100 float wt = kptr[k];
101 sum += val * wt;
102
103 sptr += dilation_w;
104 }
105
106 kptr += kernel_w;
107 }
108
109 sum = activation_ss(sum, activation_type, activation_params);
110
111 outptr[j] = sum;
112 }
113 }
114
115 return 0;
116}
117
118int Convolution1D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
119{
120 Mat bottom_blob_bordered;
121 make_padding(bottom_blob, bottom_blob_bordered, opt);
122 if (bottom_blob_bordered.empty())
123 return -100;
124
125 const int w = bottom_blob_bordered.w;
126 const size_t elemsize = bottom_blob_bordered.elemsize;
127
128 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
129
130 const int outw = (w - kernel_extent_w) / stride_w + 1;
131
132 top_blob.create(outw, num_output, elemsize, opt.blob_allocator);
133 if (top_blob.empty())
134 return -100;
135
136 int ret = convolution1d(bottom_blob_bordered, top_blob, weight_data, bias_data, kernel_w, stride_w, dilation_w, activation_type, activation_params, opt);
137 if (ret != 0)
138 return ret;
139
140 return 0;
141}
142
143int Convolution1D::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
144{
145 const Mat& bottom_blob = bottom_blobs[0];
146 const Mat& _weight_data = bottom_blobs[1];
147 Mat& top_blob = top_blobs[0];
148
149 const int _kernel_w = _weight_data.w;
150 const int _num_output = _weight_data.c;
151
152 Mat weight_data_flattened;
153 flatten(_weight_data, weight_data_flattened, opt);
154 if (weight_data_flattened.empty())
155 return -100;
156
157 Mat bias_data_flattened;
158 if (bias_term)
159 {
160 const Mat& _bias_data = bottom_blobs[2];
161 flatten(_bias_data, bias_data_flattened, opt);
162 if (bias_data_flattened.empty())
163 return -100;
164 }
165
166 Mat bottom_blob_bordered;
167 make_padding(bottom_blob, bottom_blob_bordered, _kernel_w, opt);
168 if (bottom_blob_bordered.empty())
169 return -100;
170
171 const int w = bottom_blob_bordered.w;
172 const size_t elemsize = bottom_blob_bordered.elemsize;
173
174 const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
175
176 const int outw = (w - kernel_extent_w) / stride_w + 1;
177
178 top_blob.create(outw, _num_output, elemsize, opt.blob_allocator);
179 if (top_blob.empty())
180 return -100;
181
182 int ret = convolution1d(bottom_blob_bordered, top_blob, weight_data_flattened, bias_data_flattened, _kernel_w, stride_w, dilation_w, activation_type, activation_params, opt);
183 if (ret != 0)
184 return ret;
185
186 return 0;
187}
188
189void Convolution1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const
190{
191 make_padding(bottom_blob, bottom_blob_bordered, kernel_w, opt);
192}
193
194void Convolution1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, int _kernel_w, const Option& opt) const
195{
196 int w = bottom_blob.w;
197
198 const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
199
200 bottom_blob_bordered = bottom_blob;
201 if (pad_left > 0 || pad_right > 0)
202 {
203 Option opt_b = opt;
204 opt_b.blob_allocator = opt.workspace_allocator;
205 copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b);
206 }
207 else if (pad_left == -233 && pad_right == -233)
208 {
209 // tensorflow padding=SAME or onnx padding=SAME_UPPER
210 int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
211 if (wpad > 0)
212 {
213 Option opt_b = opt;
214 opt_b.blob_allocator = opt.workspace_allocator;
215 copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
216 }
217 }
218 else if (pad_left == -234 && pad_right == -234)
219 {
220 // onnx padding=SAME_LOWER
221 int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
222 if (wpad > 0)
223 {
224 Option opt_b = opt;
225 opt_b.blob_allocator = opt.workspace_allocator;
226 copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
227 }
228 }
229}
230
231} // namespace ncnn