Elffuss BraidHub

kikocis/ncnn public

master · 9.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 "pass_ncnn.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21class nn_Conv1d : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
273 2
28pnnx.Input input 0 1 input
29nn.Conv1d op_0 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=zeros padding=%padding dilation=%dilation groups=1 bias=%bias @weight @bias
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "Convolution1D";
37 }
38
39 const char* name_str() const
40 {
41 return "conv1d";
42 }
43
44 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
45 {
46 op->params["0"] = captured_params.at("out_channels");
47 op->params["1"] = captured_params.at("kernel_size").ai[0];
48 op->params["2"] = captured_params.at("dilation").ai[0];
49 op->params["3"] = captured_params.at("stride").ai[0];
50 if (captured_params.at("padding").type == 4)
51 {
52 if (captured_params.at("padding").s == "same")
53 op->params["4"] = -233;
54 else if (captured_params.at("padding").s == "valid")
55 op->params["4"] = 0;
56 }
57 else
58 {
59 op->params["4"] = captured_params.at("padding").ai[0];
60 }
61 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
62 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
63
64 op->attrs["0"] = Attribute();
65 op->attrs["0"].data = {0, 0, 0, 0};
66 op->attrs["1"] = captured_attrs.at("op_0.weight");
67 if (captured_params.at("bias").b)
68 op->attrs["2"] = captured_attrs.at("op_0.bias");
69 }
70};
71
72class nn_Conv1d_1 : public GraphRewriterPass
73{
74public:
75 const char* match_pattern_graph() const
76 {
77 return R"PNNXIR(7767517
783 2
79pnnx.Input input 0 1 input
80nn.Conv1d op_0 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=zeros padding=%padding dilation=%dilation groups=%groups bias=%bias @weight @bias
81pnnx.Output output 1 0 out
82)PNNXIR";
83 }
84
85 const char* type_str() const
86 {
87 return "ConvolutionDepthWise1D";
88 }
89
90 const char* name_str() const
91 {
92 return "convdw1d";
93 }
94
95 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
96 {
97 op->params["0"] = captured_params.at("out_channels");
98 op->params["1"] = captured_params.at("kernel_size").ai[0];
99 op->params["2"] = captured_params.at("dilation").ai[0];
100 op->params["3"] = captured_params.at("stride").ai[0];
101 if (captured_params.at("padding").type == 4)
102 {
103 if (captured_params.at("padding").s == "same")
104 op->params["4"] = -233;
105 else if (captured_params.at("padding").s == "valid")
106 op->params["4"] = 0;
107 }
108 else
109 {
110 op->params["4"] = captured_params.at("padding").ai[0];
111 }
112 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
113 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
114 op->params["7"] = captured_params.at("groups");
115
116 op->attrs["0"] = Attribute();
117 op->attrs["0"].data = {0, 0, 0, 0};
118 op->attrs["1"] = captured_attrs.at("op_0.weight");
119 if (captured_params.at("bias").b)
120 op->attrs["2"] = captured_attrs.at("op_0.bias");
121 }
122};
123
124class nn_Conv1d_2 : public GraphRewriterPass
125{
126public:
127 const char* match_pattern_graph() const
128 {
129 return R"PNNXIR(7767517
1303 2
131pnnx.Input input 0 1 input
132nn.Conv1d op_0 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=%padding_mode padding=%padding dilation=%dilation groups=1 bias=%bias @weight @bias
133pnnx.Output output 1 0 out
134)PNNXIR";
135 }
136
137 const char* replace_pattern_graph() const
138 {
139 return R"PNNXIR(7767517
1404 3
141pnnx.Input input 0 1 input
142Padding pad 1 1 input a
143Convolution1D conv 1 1 a out
144pnnx.Output output 1 0 out
145)PNNXIR";
146 }
147
148 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
149 {
150 const std::string& padding_mode = captured_params.at("padding_mode").s;
151 if (padding_mode == "zeros")
152 return false;
153
154 const Operator* conv = matched_operators.at("op_0");
155 if (conv->params.at("padding").type == 4 && conv->params.at("padding").s == "same")
156 {
157 const std::vector<int> input_shape = conv->inputs[0]->shape;
158 if (input_shape.size() != 2 && input_shape.size() != 3)
159 {
160 fprintf(stderr, "can not resolve pads without shape\n");
161 return false;
162 }
163 }
164
165 return true;
166 }
167
168 void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
169 {
170 std::vector<int> padding;
171 if (captured_params.at("padding").type == 4)
172 {
173 if (captured_params.at("padding").s == "same")
174 {
175 // resolve pads
176 const std::vector<int> input_shape = ops.at("pad")->inputs[0]->shape;
177 const int w = input_shape[input_shape.size() - 1];
178 const int kernel_w = captured_params.at("kernel_size").ai[0];
179 const int dilation_w = captured_params.at("dilation").ai[0];
180 const int stride_w = captured_params.at("stride").ai[0];
181
182 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
183
184 int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
185
186 padding = std::vector<int>{wpad / 2, wpad - wpad / 2};
187 }
188 else if (captured_params.at("padding").s == "valid")
189 {
190 padding = std::vector<int>{0, 0};
191 }
192 }
193 else
194 {
195 int wpad = captured_params.at("padding").ai[0];
196 padding = std::vector<int>{wpad, wpad};
197 }
198
199 ops.at("pad")->params["0"] = 0;
200 ops.at("pad")->params["1"] = 0;
201 ops.at("pad")->params["2"] = padding[0];
202 ops.at("pad")->params["3"] = padding[1];
203
204 std::string padding_mode = captured_params.at("padding_mode").s;
205 if (padding_mode == "reflect")
206 {
207 ops.at("pad")->params["4"] = 2; // type=reflect
208 }
209 else if (padding_mode == "replicate")
210 {
211 ops.at("pad")->params["4"] = 1; // type=replicate
212 }
213 else
214 {
215 fprintf(stderr, "unsupported padding_mode %s\n", padding_mode.c_str());
216 }
217
218 ops.at("conv")->params["0"] = captured_params.at("out_channels");
219 ops.at("conv")->params["1"] = captured_params.at("kernel_size").ai[0];
220 ops.at("conv")->params["2"] = captured_params.at("dilation").ai[0];
221 ops.at("conv")->params["3"] = captured_params.at("stride").ai[0];
222 ops.at("conv")->params["4"] = 0;
223 ops.at("conv")->params["5"] = captured_params.at("bias").b ? 1 : 0;
224 ops.at("conv")->params["6"] = captured_attrs.at("op_0.weight").elemcount();
225 ops.at("conv")->params["7"] = captured_params.find("groups") != captured_params.end() ? captured_params.at("groups") : 1;
226
227 ops.at("conv")->attrs["0"] = Attribute();
228 ops.at("conv")->attrs["0"].data = {0, 0, 0, 0};
229 ops.at("conv")->attrs["1"] = captured_attrs.at("op_0.weight");
230 if (captured_params.at("bias").b)
231 ops.at("conv")->attrs["2"] = captured_attrs.at("op_0.bias");
232 }
233};
234
235class nn_Conv1d_3 : public nn_Conv1d_2
236{
237public:
238 const char* match_pattern_graph() const
239 {
240 return R"PNNXIR(7767517
2413 2
242pnnx.Input input 0 1 input
243nn.Conv1d op_0 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=%padding_mode padding=%padding dilation=%dilation groups=%groups bias=%bias @weight @bias
244pnnx.Output output 1 0 out
245)PNNXIR";
246 }
247
248 const char* replace_pattern_graph() const
249 {
250 return R"PNNXIR(7767517
2514 3
252pnnx.Input input 0 1 input
253Padding pad 1 1 input a
254ConvolutionDepthWise1D conv 1 1 a out
255pnnx.Output output 1 0 out
256)PNNXIR";
257 }
258};
259
260REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Conv1d, 20)
261REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Conv1d_1, 21)
262REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Conv1d_2, 22)
263REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Conv1d_3, 23)
264
265} // namespace ncnn
266
267} // namespace pnnx