Elffuss BraidHub

kikocis/ncnn public

master · 10.1 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_convtranspose.h"
16
17#include "pass_level2.h"
18
19#include <math.h>
20#include <string.h>
21
22namespace pnnx {
23
24class fuse_static_Fconvtranspose1d_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=(%in_channels,%out_channels_per_group,%kw)f32
33F.conv_transpose1d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding 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.ConvTranspose1d conv_transpose1d 1 1 input out in_channels=%in_channels kernel_size=(%kw) stride=%stride padding=%padding output_padding=%output_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 out_channels_per_group = captured_params.at("out_channels_per_group").i;
53 const int groups = captured_params.at("groups").i;
54
55 ops.at("conv_transpose1d")->params["out_channels"] = out_channels_per_group * groups;
56 }
57};
58
59class fuse_static_Fconvtranspose1d_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=(%in_channels,%out_channels_per_group,%kw)f32
68pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32
69F.conv_transpose1d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding 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.ConvTranspose1d conv_transpose1d 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=(%kw) stride=%stride padding=%padding output_padding=%output_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 bool match(const std::map<std::string, Parameter>& captured_params) const
85 {
86 const int out_channels_per_group = captured_params.at("out_channels_per_group").i;
87 const int out_channels = captured_params.at("out_channels").i;
88 const int groups = captured_params.at("groups").i;
89
90 if (out_channels != out_channels_per_group * groups)
91 return false;
92
93 return true;
94 }
95};
96
97class fuse_static_Fconvtranspose2d_pass : public GraphRewriterPass
98{
99public:
100 const char* match_pattern_graph() const
101 {
102 return R"PNNXIR(7767517
1034 3
104pnnx.Input input 0 1 input
105pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kh,%kw)f32
106F.conv_transpose2d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups
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.ConvTranspose2d conv_transpose2d 1 1 input out in_channels=%in_channels kernel_size=(%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.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 out_channels_per_group = captured_params.at("out_channels_per_group").i;
126 const int groups = captured_params.at("groups").i;
127
128 ops.at("conv_transpose2d")->params["out_channels"] = out_channels_per_group * groups;
129 }
130};
131
132class fuse_static_Fconvtranspose2d_pass_2 : public GraphRewriterPass
133{
134public:
135 const char* match_pattern_graph() const
136 {
137 return R"PNNXIR(7767517
1385 4
139pnnx.Input input 0 1 input
140pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kh,%kw)f32
141pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32
142F.conv_transpose2d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups
143pnnx.Output output 1 0 out
144)PNNXIR";
145 }
146
147 const char* replace_pattern_graph() const
148 {
149 return R"PNNXIR(7767517
1503 2
151pnnx.Input input 0 1 input
152nn.ConvTranspose2d conv_transpose2d 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=(%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
153pnnx.Output output 1 0 out
154)PNNXIR";
155 }
156
157 bool match(const std::map<std::string, Parameter>& captured_params) const
158 {
159 const int out_channels_per_group = captured_params.at("out_channels_per_group").i;
160 const int out_channels = captured_params.at("out_channels").i;
161 const int groups = captured_params.at("groups").i;
162
163 if (out_channels != out_channels_per_group * groups)
164 return false;
165
166 return true;
167 }
168};
169
170class fuse_static_Fconvtranspose3d_pass : public GraphRewriterPass
171{
172public:
173 const char* match_pattern_graph() const
174 {
175 return R"PNNXIR(7767517
1764 3
177pnnx.Input input 0 1 input
178pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kd,%kh,%kw)f32
179F.conv_transpose3d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups
180pnnx.Output output 1 0 out
181)PNNXIR";
182 }
183
184 const char* replace_pattern_graph() const
185 {
186 return R"PNNXIR(7767517
1873 2
188pnnx.Input input 0 1 input
189nn.ConvTranspose3d conv_transpose3d 1 1 input out in_channels=%in_channels kernel_size=(%kd,%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data
190pnnx.Output output 1 0 out
191)PNNXIR";
192 }
193
194 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
195 {
196 GraphRewriterPass::write(ops, captured_params, captured_attrs);
197
198 const int out_channels_per_group = captured_params.at("out_channels_per_group").i;
199 const int groups = captured_params.at("groups").i;
200
201 ops.at("conv_transpose3d")->params["out_channels"] = out_channels_per_group * groups;
202 }
203};
204
205class fuse_static_Fconvtranspose3d_pass_2 : public GraphRewriterPass
206{
207public:
208 const char* match_pattern_graph() const
209 {
210 return R"PNNXIR(7767517
2115 4
212pnnx.Input input 0 1 input
213pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kd,%kh,%kw)f32
214pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32
215F.conv_transpose3d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups
216pnnx.Output output 1 0 out
217)PNNXIR";
218 }
219
220 const char* replace_pattern_graph() const
221 {
222 return R"PNNXIR(7767517
2233 2
224pnnx.Input input 0 1 input
225nn.ConvTranspose3d conv_transpose3d 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=(%kd,%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data
226pnnx.Output output 1 0 out
227)PNNXIR";
228 }
229
230 bool match(const std::map<std::string, Parameter>& captured_params) const
231 {
232 const int out_channels_per_group = captured_params.at("out_channels_per_group").i;
233 const int out_channels = captured_params.at("out_channels").i;
234 const int groups = captured_params.at("groups").i;
235
236 if (out_channels != out_channels_per_group * groups)
237 return false;
238
239 return true;
240 }
241};
242
243void fuse_static_convtranspose(Graph& graph)
244{
245 fuse_static_Fconvtranspose1d_pass a;
246 fuse_static_Fconvtranspose1d_pass_2 b;
247 fuse_static_Fconvtranspose2d_pass c;
248 fuse_static_Fconvtranspose2d_pass_2 d;
249 fuse_static_Fconvtranspose3d_pass e;
250 fuse_static_Fconvtranspose3d_pass_2 f;
251 int opindex = 0;
252
253 pnnx_graph_rewrite(graph, &a, opindex);
254 pnnx_graph_rewrite(graph, &b, opindex);
255 pnnx_graph_rewrite(graph, &c, opindex);
256 pnnx_graph_rewrite(graph, &d, opindex);
257 pnnx_graph_rewrite(graph, &e, opindex);
258 pnnx_graph_rewrite(graph, &f, opindex);
259}
260
261} // namespace pnnx