Elffuss BraidHub

kikocis/ncnn public

master · 14.7 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_static_conv.h"
16
17#include "pass_level2.h"
18
19#include <math.h>
20#include <string.h>
21
22namespace pnnx {
23
24class fuse_static_Fconv1d_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
32pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kw)f32
33F.conv1d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups
34pnnx.Output output 1 0 out
35)PNNXIR";
36 }
37
38 const char* replace_pattern_graph() const
39 {
40 return R"PNNXIR(7767517
413 2
42pnnx.Input input 0 1 input
43nn.Conv1d conv1d 1 1 input out out_channels=%out_channels kernel_size=(%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data
44pnnx.Output output 1 0 out
45)PNNXIR";
46 }
47
48 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
49 {
50 GraphRewriterPass::write(ops, captured_params, captured_attrs);
51
52 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
53 const int groups = captured_params.at("groups").i;
54
55 ops.at("conv1d")->params["in_channels"] = in_channels_per_group * groups;
56 }
57};
58
59class fuse_static_Fconv1d_pass_2 : public GraphRewriterPass
60{
61public:
62 const char* match_pattern_graph() const
63 {
64 return R"PNNXIR(7767517
655 4
66pnnx.Input input 0 1 input
67pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kw)f32
68pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32
69F.conv1d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation groups=%groups
70pnnx.Output output 1 0 out
71)PNNXIR";
72 }
73
74 const char* replace_pattern_graph() const
75 {
76 return R"PNNXIR(7767517
773 2
78pnnx.Input input 0 1 input
79nn.Conv1d conv1d 1 1 input out out_channels=%out_channels kernel_size=(%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
80pnnx.Output output 1 0 out
81)PNNXIR";
82 }
83
84 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
85 {
86 GraphRewriterPass::write(ops, captured_params, captured_attrs);
87
88 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
89 const int groups = captured_params.at("groups").i;
90
91 ops.at("conv1d")->params["in_channels"] = in_channels_per_group * groups;
92 }
93};
94
95class fuse_static_Fconv1d_pass_3 : public GraphRewriterPass
96{
97public:
98 const char* match_pattern_graph() const
99 {
100 return R"PNNXIR(7767517
1016 5
102pnnx.Input input 0 1 input
103pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kw)f32
104pnnx.Attribute op_bias 0 1 bias @data=(1,%out_channels,1)f32
105F.conv1d op_0 2 1 input weight a bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups
106pnnx.Expression op_1 2 1 a bias out expr=add(@0,@1)
107pnnx.Output output 1 0 out
108)PNNXIR";
109 }
110
111 const char* replace_pattern_graph() const
112 {
113 return R"PNNXIR(7767517
1143 2
115pnnx.Input input 0 1 input
116nn.Conv1d conv1d 1 1 input out out_channels=%out_channels kernel_size=(%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
117pnnx.Output output 1 0 out
118)PNNXIR";
119 }
120
121 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
122 {
123 GraphRewriterPass::write(ops, captured_params, captured_attrs);
124
125 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
126 const int groups = captured_params.at("groups").i;
127
128 ops.at("conv1d")->params["in_channels"] = in_channels_per_group * groups;
129 }
130};
131
132class fuse_static_Fconv2d_pass : public GraphRewriterPass
133{
134public:
135 const char* match_pattern_graph() const
136 {
137 return R"PNNXIR(7767517
1384 3
139pnnx.Input input 0 1 input
140pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kh,%kw)f32
141F.conv2d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups
142pnnx.Output output 1 0 out
143)PNNXIR";
144 }
145
146 const char* replace_pattern_graph() const
147 {
148 return R"PNNXIR(7767517
1493 2
150pnnx.Input input 0 1 input
151nn.Conv2d conv2d 1 1 input out out_channels=%out_channels kernel_size=(%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data
152pnnx.Output output 1 0 out
153)PNNXIR";
154 }
155
156 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
157 {
158 GraphRewriterPass::write(ops, captured_params, captured_attrs);
159
160 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
161 const int groups = captured_params.at("groups").i;
162
163 ops.at("conv2d")->params["in_channels"] = in_channels_per_group * groups;
164 }
165};
166
167class fuse_static_Fconv2d_pass_2 : public GraphRewriterPass
168{
169public:
170 const char* match_pattern_graph() const
171 {
172 return R"PNNXIR(7767517
1735 4
174pnnx.Input input 0 1 input
175pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kh,%kw)f32
176pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32
177F.conv2d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation groups=%groups
178pnnx.Output output 1 0 out
179)PNNXIR";
180 }
181
182 const char* replace_pattern_graph() const
183 {
184 return R"PNNXIR(7767517
1853 2
186pnnx.Input input 0 1 input
187nn.Conv2d conv2d 1 1 input out out_channels=%out_channels kernel_size=(%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
188pnnx.Output output 1 0 out
189)PNNXIR";
190 }
191
192 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
193 {
194 GraphRewriterPass::write(ops, captured_params, captured_attrs);
195
196 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
197 const int groups = captured_params.at("groups").i;
198
199 ops.at("conv2d")->params["in_channels"] = in_channels_per_group * groups;
200 }
201};
202
203class fuse_static_Fconv2d_pass_3 : public GraphRewriterPass
204{
205public:
206 const char* match_pattern_graph() const
207 {
208 return R"PNNXIR(7767517
2096 5
210pnnx.Input input 0 1 input
211pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kh,%kw)f32
212pnnx.Attribute op_bias 0 1 bias @data=(1,%out_channels,1,1)f32
213F.conv2d op_0 2 1 input weight a bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups
214pnnx.Expression op_1 2 1 a bias out expr=add(@0,@1)
215pnnx.Output output 1 0 out
216)PNNXIR";
217 }
218
219 const char* replace_pattern_graph() const
220 {
221 return R"PNNXIR(7767517
2223 2
223pnnx.Input input 0 1 input
224nn.Conv2d conv2d 1 1 input out out_channels=%out_channels kernel_size=(%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
225pnnx.Output output 1 0 out
226)PNNXIR";
227 }
228
229 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
230 {
231 GraphRewriterPass::write(ops, captured_params, captured_attrs);
232
233 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
234 const int groups = captured_params.at("groups").i;
235
236 ops.at("conv2d")->params["in_channels"] = in_channels_per_group * groups;
237 }
238};
239
240class fuse_static_Fconv3d_pass : public GraphRewriterPass
241{
242public:
243 const char* match_pattern_graph() const
244 {
245 return R"PNNXIR(7767517
2464 3
247pnnx.Input input 0 1 input
248pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kd,%kh,%kw)f32
249F.conv3d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups
250pnnx.Output output 1 0 out
251)PNNXIR";
252 }
253
254 const char* replace_pattern_graph() const
255 {
256 return R"PNNXIR(7767517
2573 2
258pnnx.Input input 0 1 input
259nn.Conv3d conv3d 1 1 input out out_channels=%out_channels kernel_size=(%kd,%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data
260pnnx.Output output 1 0 out
261)PNNXIR";
262 }
263
264 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
265 {
266 GraphRewriterPass::write(ops, captured_params, captured_attrs);
267
268 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
269 const int groups = captured_params.at("groups").i;
270
271 ops.at("conv3d")->params["in_channels"] = in_channels_per_group * groups;
272 }
273};
274
275class fuse_static_Fconv3d_pass_2 : public GraphRewriterPass
276{
277public:
278 const char* match_pattern_graph() const
279 {
280 return R"PNNXIR(7767517
2815 4
282pnnx.Input input 0 1 input
283pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kd,%kh,%kw)f32
284pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32
285F.conv3d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation groups=%groups
286pnnx.Output output 1 0 out
287)PNNXIR";
288 }
289
290 const char* replace_pattern_graph() const
291 {
292 return R"PNNXIR(7767517
2933 2
294pnnx.Input input 0 1 input
295nn.Conv3d conv3d 1 1 input out out_channels=%out_channels kernel_size=(%kd,%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
296pnnx.Output output 1 0 out
297)PNNXIR";
298 }
299
300 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
301 {
302 GraphRewriterPass::write(ops, captured_params, captured_attrs);
303
304 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
305 const int groups = captured_params.at("groups").i;
306
307 ops.at("conv3d")->params["in_channels"] = in_channels_per_group * groups;
308 }
309};
310
311class fuse_static_Fconv3d_pass_3 : public GraphRewriterPass
312{
313public:
314 const char* match_pattern_graph() const
315 {
316 return R"PNNXIR(7767517
3176 5
318pnnx.Input input 0 1 input
319pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kd,%kh,%kw)f32
320pnnx.Attribute op_bias 0 1 bias @data=(1,%out_channels,1,1,1)f32
321F.conv3d op_0 2 1 input weight a bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups
322pnnx.Expression op_1 2 1 a bias out expr=add(@0,@1)
323pnnx.Output output 1 0 out
324)PNNXIR";
325 }
326
327 const char* replace_pattern_graph() const
328 {
329 return R"PNNXIR(7767517
3303 2
331pnnx.Input input 0 1 input
332nn.Conv3d conv3d 1 1 input out out_channels=%out_channels kernel_size=(%kd,%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
333pnnx.Output output 1 0 out
334)PNNXIR";
335 }
336
337 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
338 {
339 GraphRewriterPass::write(ops, captured_params, captured_attrs);
340
341 const int in_channels_per_group = captured_params.at("in_channels_per_group").i;
342 const int groups = captured_params.at("groups").i;
343
344 ops.at("conv3d")->params["in_channels"] = in_channels_per_group * groups;
345 }
346};
347
348void fuse_static_conv(Graph& graph)
349{
350 fuse_static_Fconv1d_pass_3 a3;
351 fuse_static_Fconv2d_pass_3 a4;
352 fuse_static_Fconv3d_pass_3 a5;
353
354 fuse_static_Fconv1d_pass a;
355 fuse_static_Fconv1d_pass_2 b;
356 fuse_static_Fconv2d_pass c;
357 fuse_static_Fconv2d_pass_2 d;
358 fuse_static_Fconv3d_pass e;
359 fuse_static_Fconv3d_pass_2 f;
360 int opindex = 0;
361
362 pnnx_graph_rewrite(graph, &a3, opindex);
363 pnnx_graph_rewrite(graph, &a4, opindex);
364 pnnx_graph_rewrite(graph, &a5, opindex);
365
366 pnnx_graph_rewrite(graph, &a, opindex);
367 pnnx_graph_rewrite(graph, &b, opindex);
368 pnnx_graph_rewrite(graph, &c, opindex);
369 pnnx_graph_rewrite(graph, &d, opindex);
370 pnnx_graph_rewrite(graph, &e, opindex);
371 pnnx_graph_rewrite(graph, &f, opindex);
372}
373
374} // namespace pnnx