Elffuss BraidHub

kikocis/ncnn public

master · 12.6 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 "fuse_pad_conv1d.h"
16
17#include "pass_level2.h"
18
19#include <math.h>
20#include <string.h>
21
22namespace pnnx {
23
24class fuse_pad_conv1d_pass : public GraphRewriterPass
25{
26public:
27 const char* match_pattern_graph() const
28 {
29 return R"PNNXIR(7767517
304 3
31pnnx.Input input 0 1 input
32F.pad op_pad 1 1 input a mode=constant pad=%pad value=%value
33nn.Conv1d op_0 1 1 a 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
34pnnx.Output output 1 0 out
35)PNNXIR";
36 }
37
38 const char* type_str() const
39 {
40 return "nn.Conv1d";
41 }
42
43 const char* name_str() const
44 {
45 return "conv1d";
46 }
47
48 bool match(const std::map<std::string, Parameter>& captured_params) const
49 {
50 // constant-0 + zeros
51 float pad_value = 0.f;
52 if (captured_params.at("value").type == 2)
53 pad_value = captured_params.at("value").i;
54 if (captured_params.at("value").type == 3)
55 pad_value = captured_params.at("value").f;
56
57 if (pad_value != 0.f)
58 return false;
59
60 const std::vector<int>& pad = captured_params.at("pad").ai;
61 for (int x : pad)
62 {
63 if (x < 0)
64 return false;
65 }
66
67 if (pad.size() != 2)
68 return false;
69
70 if (pad.size() == 2 && pad[0] != pad[1])
71 return false;
72
73 return true;
74 }
75
76 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
77 {
78 const std::vector<int>& pad = captured_params.at("pad").ai;
79 std::vector<int> padding = captured_params.at("padding").ai;
80 padding[0] += pad[0];
81
82 op->params["in_channels"] = captured_params.at("in_channels");
83 op->params["out_channels"] = captured_params.at("out_channels");
84 op->params["kernel_size"] = captured_params.at("kernel_size");
85 op->params["padding_mode"] = "zeros";
86 op->params["stride"] = captured_params.at("stride");
87 op->params["padding"] = padding;
88 op->params["dilation"] = captured_params.at("dilation");
89 op->params["groups"] = captured_params.at("groups");
90 op->params["bias"] = captured_params.at("bias");
91
92 op->attrs["weight"] = captured_attrs.at("op_0.weight");
93
94 if (captured_params.at("bias").b)
95 {
96 op->attrs["bias"] = captured_attrs.at("op_0.bias");
97 }
98 }
99};
100
101class fuse_pad_conv1d_pass_1 : public GraphRewriterPass
102{
103public:
104 const char* match_pattern_graph() const
105 {
106 return R"PNNXIR(7767517
1074 3
108pnnx.Input input 0 1 input
109F.pad op_pad 1 1 input a mode=%mode pad=%pad
110nn.Conv1d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=* padding=(0,0) dilation=%dilation groups=%groups bias=%bias @weight @bias
111pnnx.Output output 1 0 out
112)PNNXIR";
113 }
114
115 const char* type_str() const
116 {
117 return "nn.Conv1d";
118 }
119
120 const char* name_str() const
121 {
122 return "conv1d";
123 }
124
125 bool match(const std::map<std::string, Parameter>& captured_params) const
126 {
127 // reflect/replicate + nopad
128 if (captured_params.at("mode").s != "reflect" && captured_params.at("mode").s != "replicate")
129 return false;
130
131 const std::vector<int>& pad = captured_params.at("pad").ai;
132 for (int x : pad)
133 {
134 if (x < 0)
135 return false;
136 }
137
138 if (pad.size() != 2)
139 return false;
140
141 if (pad.size() == 2 && pad[0] != pad[1])
142 return false;
143
144 return true;
145 }
146
147 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
148 {
149 const std::vector<int>& pad = captured_params.at("pad").ai;
150 std::vector<int> padding(1);
151 padding[0] = pad[0];
152
153 op->params["in_channels"] = captured_params.at("in_channels");
154 op->params["out_channels"] = captured_params.at("out_channels");
155 op->params["kernel_size"] = captured_params.at("kernel_size");
156 op->params["padding_mode"] = captured_params.at("mode");
157 op->params["stride"] = captured_params.at("stride");
158 op->params["padding"] = padding;
159 op->params["dilation"] = captured_params.at("dilation");
160 op->params["groups"] = captured_params.at("groups");
161 op->params["bias"] = captured_params.at("bias");
162
163 op->attrs["weight"] = captured_attrs.at("op_0.weight");
164
165 if (captured_params.at("bias").b)
166 {
167 op->attrs["bias"] = captured_attrs.at("op_0.bias");
168 }
169 }
170};
171
172class fuse_pad_conv1d_pass_2 : public GraphRewriterPass
173{
174public:
175 const char* match_pattern_graph() const
176 {
177 return R"PNNXIR(7767517
1784 3
179pnnx.Input input 0 1 input
180nn.ConstantPad1d op_pad 1 1 input a padding=%pad value=%value
181nn.Conv1d op_0 1 1 a 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
182pnnx.Output output 1 0 out
183)PNNXIR";
184 }
185
186 const char* type_str() const
187 {
188 return "nn.Conv1d";
189 }
190
191 const char* name_str() const
192 {
193 return "conv1d";
194 }
195
196 bool match(const std::map<std::string, Parameter>& captured_params) const
197 {
198 // constant-0 + zeros
199 float pad_value = 0.f;
200 if (captured_params.at("value").type == 2)
201 pad_value = captured_params.at("value").i;
202 if (captured_params.at("value").type == 3)
203 pad_value = captured_params.at("value").f;
204
205 if (pad_value != 0.f)
206 return false;
207
208 const std::vector<int>& pad = captured_params.at("pad").ai;
209 for (int x : pad)
210 {
211 if (x < 0)
212 return false;
213 }
214
215 if (pad.size() != 2)
216 return false;
217
218 if (pad[0] != pad[1])
219 return false;
220
221 return true;
222 }
223
224 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
225 {
226 std::vector<int> padding = captured_params.at("padding").ai;
227 const std::vector<int>& pad = captured_params.at("pad").ai;
228 padding[0] += pad[0];
229
230 op->params["in_channels"] = captured_params.at("in_channels");
231 op->params["out_channels"] = captured_params.at("out_channels");
232 op->params["kernel_size"] = captured_params.at("kernel_size");
233 op->params["padding_mode"] = "zeros";
234 op->params["stride"] = captured_params.at("stride");
235 op->params["padding"] = padding;
236 op->params["dilation"] = captured_params.at("dilation");
237 op->params["groups"] = captured_params.at("groups");
238 op->params["bias"] = captured_params.at("bias");
239
240 op->attrs["weight"] = captured_attrs.at("op_0.weight");
241
242 if (captured_params.at("bias").b)
243 {
244 op->attrs["bias"] = captured_attrs.at("op_0.bias");
245 }
246 }
247};
248
249class fuse_pad_conv1d_pass_3 : public GraphRewriterPass
250{
251public:
252 const char* match_pattern_graph() const
253 {
254 return R"PNNXIR(7767517
2554 3
256pnnx.Input input 0 1 input
257nn.ReplicationPad1d op_pad 1 1 input a padding=%pad
258nn.Conv1d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=* padding=(0,0) dilation=%dilation groups=%groups bias=%bias @weight @bias
259pnnx.Output output 1 0 out
260)PNNXIR";
261 }
262
263 const char* type_str() const
264 {
265 return "nn.Conv1d";
266 }
267
268 const char* name_str() const
269 {
270 return "conv1d";
271 }
272
273 bool match(const std::map<std::string, Parameter>& captured_params) const
274 {
275 // replicate + nopad
276 const std::vector<int>& pad = captured_params.at("pad").ai;
277 for (int x : pad)
278 {
279 if (x < 0)
280 return false;
281 }
282
283 if (pad.size() != 2)
284 return false;
285
286 if (pad[0] != pad[1])
287 return false;
288
289 return true;
290 }
291
292 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
293 {
294 std::vector<int> padding(1);
295 const std::vector<int>& pad = captured_params.at("pad").ai;
296 padding[0] = pad[0];
297
298 op->params["in_channels"] = captured_params.at("in_channels");
299 op->params["out_channels"] = captured_params.at("out_channels");
300 op->params["kernel_size"] = captured_params.at("kernel_size");
301 op->params["padding_mode"] = "replicate";
302 op->params["stride"] = captured_params.at("stride");
303 op->params["padding"] = padding;
304 op->params["dilation"] = captured_params.at("dilation");
305 op->params["groups"] = captured_params.at("groups");
306 op->params["bias"] = captured_params.at("bias");
307
308 op->attrs["weight"] = captured_attrs.at("op_0.weight");
309
310 if (captured_params.at("bias").b)
311 {
312 op->attrs["bias"] = captured_attrs.at("op_0.bias");
313 }
314 }
315};
316
317class fuse_pad_conv1d_pass_4 : public GraphRewriterPass
318{
319public:
320 const char* match_pattern_graph() const
321 {
322 return R"PNNXIR(7767517
3234 3
324pnnx.Input input 0 1 input
325nn.ReflectionPad1d op_pad 1 1 input a padding=%pad
326nn.Conv1d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=* padding=(0,0) dilation=%dilation groups=%groups bias=%bias @weight @bias
327pnnx.Output output 1 0 out
328)PNNXIR";
329 }
330
331 const char* type_str() const
332 {
333 return "nn.Conv1d";
334 }
335
336 const char* name_str() const
337 {
338 return "conv1d";
339 }
340
341 bool match(const std::map<std::string, Parameter>& captured_params) const
342 {
343 // reflect + nopad
344 const std::vector<int>& pad = captured_params.at("pad").ai;
345 for (int x : pad)
346 {
347 if (x < 0)
348 return false;
349 }
350
351 if (pad.size() != 2)
352 return false;
353
354 if (pad[0] != pad[1])
355 return false;
356
357 return true;
358 }
359
360 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
361 {
362 std::vector<int> padding(1);
363 const std::vector<int>& pad = captured_params.at("pad").ai;
364 padding[0] = pad[0];
365
366 op->params["in_channels"] = captured_params.at("in_channels");
367 op->params["out_channels"] = captured_params.at("out_channels");
368 op->params["kernel_size"] = captured_params.at("kernel_size");
369 op->params["padding_mode"] = "reflect";
370 op->params["stride"] = captured_params.at("stride");
371 op->params["padding"] = padding;
372 op->params["dilation"] = captured_params.at("dilation");
373 op->params["groups"] = captured_params.at("groups");
374 op->params["bias"] = captured_params.at("bias");
375
376 op->attrs["weight"] = captured_attrs.at("op_0.weight");
377
378 if (captured_params.at("bias").b)
379 {
380 op->attrs["bias"] = captured_attrs.at("op_0.bias");
381 }
382 }
383};
384
385void fuse_pad_conv1d(Graph& graph)
386{
387 fuse_pad_conv1d_pass a;
388 fuse_pad_conv1d_pass_1 b;
389 fuse_pad_conv1d_pass_2 c;
390 fuse_pad_conv1d_pass_3 d;
391 fuse_pad_conv1d_pass_4 e;
392 int opindex = 0;
393
394 pnnx_graph_rewrite(graph, &a, opindex);
395 pnnx_graph_rewrite(graph, &b, opindex);
396 pnnx_graph_rewrite(graph, &c, opindex);
397 pnnx_graph_rewrite(graph, &d, opindex);
398 pnnx_graph_rewrite(graph, &e, opindex);
399}
400
401} // namespace pnnx