Elffuss BraidHub

kikocis/ncnn public

master · 9.1 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_level2.h"
16
17namespace pnnx {
18
19class F_conv2d : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
2517 16
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 weight
28pnnx.Input input_2 0 1 bias
29pnnx.Input input_3 0 1 stride
30pnnx.Input input_4 0 1 padding
31pnnx.Input input_5 0 1 dilation
32pnnx.Input input_6 0 1 groups
33prim::Constant op_0 0 1 transposed value=False
34prim::Constant op_1 0 1 output_padding_h value=0
35prim::Constant op_2 0 1 output_padding_w value=0
36prim::ListConstruct op_3 2 1 output_padding_h output_padding_w output_padding
37prim::Constant op_4 0 1 benchmark value=*
38prim::Constant op_5 0 1 deterministic value=*
39prim::Constant op_6 0 1 cudnn_enabled value=*
40prim::Constant op_7 0 1 allow_tf32 value=*
41aten::_convolution op_8 13 1 input weight bias stride padding dilation transposed output_padding groups benchmark deterministic cudnn_enabled allow_tf32 out
42pnnx.Output output 1 0 out
43)PNNXIR";
44 }
45
46 const char* type_str() const
47 {
48 return "F.conv2d";
49 }
50};
51
52REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv2d, 10)
53
54class F_conv2d_cudnn_relu : public GraphRewriterPass
55{
56public:
57 const char* match_pattern_graph() const
58 {
59 return R"PNNXIR(7767517
609 8
61pnnx.Input input_0 0 1 input
62pnnx.Input input_1 0 1 weight
63pnnx.Input input_2 0 1 bias
64pnnx.Input input_3 0 1 stride
65pnnx.Input input_4 0 1 padding
66pnnx.Input input_5 0 1 dilation
67pnnx.Input input_6 0 1 groups
68aten::cudnn_convolution_relu op_0 7 1 input weight bias stride padding dilation groups out
69pnnx.Output output 1 0 out
70)PNNXIR";
71 }
72
73 const char* replace_pattern_graph() const
74 {
75 return R"PNNXIR(7767517
7610 9
77pnnx.Input input_0 0 1 input
78pnnx.Input input_1 0 1 weight
79pnnx.Input input_2 0 1 bias
80pnnx.Input input_3 0 1 stride
81pnnx.Input input_4 0 1 padding
82pnnx.Input input_5 0 1 dilation
83pnnx.Input input_6 0 1 groups
84F.conv2d conv 7 1 input weight bias stride padding dilation groups conv
85F.relu relu 1 1 conv out
86pnnx.Output output 1 0 out
87)PNNXIR";
88 }
89};
90
91REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv2d_cudnn_relu, 10)
92
93class F_conv2d_cudnn_add_relu : public GraphRewriterPass
94{
95public:
96 const char* match_pattern_graph() const
97 {
98 return R"PNNXIR(7767517
9911 10
100pnnx.Input input_0 0 1 input
101pnnx.Input input_1 0 1 weight
102pnnx.Input input_2 0 1 z
103pnnx.Input input_3 0 1 alpha
104pnnx.Input input_4 0 1 bias
105pnnx.Input input_5 0 1 stride
106pnnx.Input input_6 0 1 padding
107pnnx.Input input_7 0 1 dilation
108pnnx.Input input_8 0 1 groups
109aten::cudnn_convolution_add_relu op_0 9 1 input weight z alpha bias stride padding dilation groups out
110pnnx.Output output 1 0 out
111)PNNXIR";
112 }
113
114 const char* replace_pattern_graph() const
115 {
116 return R"PNNXIR(7767517
11713 12
118pnnx.Input input_0 0 1 input
119pnnx.Input input_1 0 1 weight
120pnnx.Input input_2 0 1 z
121pnnx.Input input_3 0 1 alpha
122pnnx.Input input_4 0 1 bias
123pnnx.Input input_5 0 1 stride
124pnnx.Input input_6 0 1 padding
125pnnx.Input input_7 0 1 dilation
126pnnx.Input input_8 0 1 groups
127F.conv2d conv 7 1 input weight bias stride padding dilation groups conv
128aten::add add 3 1 conv z alpha add
129F.relu relu 1 1 add out
130pnnx.Output output 1 0 out
131)PNNXIR";
132 }
133};
134
135REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv2d_cudnn_add_relu, 10)
136
137class F_conv2d_1 : public GraphRewriterPass
138{
139public:
140 const char* match_pattern_graph() const
141 {
142 return R"PNNXIR(7767517
1439 8
144pnnx.Input input_0 0 1 input
145pnnx.Input input_1 0 1 weight
146pnnx.Input input_2 0 1 bias
147pnnx.Input input_3 0 1 stride
148pnnx.Input input_4 0 1 padding
149pnnx.Input input_5 0 1 dilation
150pnnx.Input input_6 0 1 groups
151aten::conv2d op_0 7 1 input weight bias stride padding dilation groups out
152pnnx.Output output 1 0 out
153)PNNXIR";
154 }
155
156 const char* type_str() const
157 {
158 return "F.conv2d";
159 }
160};
161
162REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv2d_1, 10)
163
164class F_conv2d_onnx : public GraphRewriterPass
165{
166public:
167 const char* match_pattern_graph() const
168 {
169 return R"PNNXIR(7767517
1705 4
171pnnx.Input input_0 0 1 input
172pnnx.Input input_1 0 1 weight
173pnnx.Input input_2 0 1 bias
174Conv op_0 3 1 input weight bias out %*=%*
175pnnx.Output output 1 0 out
176)PNNXIR";
177 }
178
179 const char* type_str() const
180 {
181 return "F.conv2d";
182 }
183
184 bool match(const std::map<std::string, Parameter>& captured_params) const
185 {
186 if (captured_params.find("op_0.kernel_shape") != captured_params.end())
187 {
188 if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 2)
189 return false;
190 }
191
192 if (captured_params.find("op_0.dilations") != captured_params.end())
193 {
194 if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 2)
195 return false;
196 }
197
198 if (captured_params.find("op_0.strides") != captured_params.end())
199 {
200 if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 2)
201 return false;
202 }
203
204 if (captured_params.find("op_0.pads") != captured_params.end())
205 {
206 if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 4)
207 return false;
208
209 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
210 if (pads[0] != pads[2] || pads[1] != pads[3])
211 return false;
212 }
213
214 return true;
215 }
216
217 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
218 {
219 if (captured_params.find("op_0.dilations") != captured_params.end())
220 {
221 op->params["dilation"] = captured_params.at("op_0.dilations");
222 }
223 else
224 {
225 op->params["dilation"] = {1, 1};
226 }
227
228 if (captured_params.find("op_0.strides") != captured_params.end())
229 {
230 op->params["stride"] = captured_params.at("op_0.strides");
231 }
232 else
233 {
234 op->params["stride"] = {1, 1};
235 }
236
237 if (captured_params.find("op_0.pads") != captured_params.end())
238 {
239 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
240 op->params["padding"] = {pads[0], pads[1]};
241 }
242 else
243 {
244 op->params["padding"] = {0, 0};
245 }
246
247 if (captured_params.find("op_0.group") != captured_params.end())
248 {
249 op->params["groups"] = captured_params.at("op_0.group");
250 }
251 else
252 {
253 op->params["groups"] = 1;
254 }
255 }
256};
257
258REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv2d_onnx, 10)
259
260class F_conv2d_onnx_1 : public F_conv2d_onnx
261{
262public:
263 const char* match_pattern_graph() const
264 {
265 return R"PNNXIR(7767517
2664 3
267pnnx.Input input_0 0 1 input
268pnnx.Input input_1 0 1 weight
269Conv op_0 2 1 input weight out %*=%*
270pnnx.Output output 1 0 out
271)PNNXIR";
272 }
273
274 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
275 {
276 F_conv2d_onnx::write(op, captured_params);
277
278 op->params["bias"] = Parameter();
279 }
280};
281
282REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv2d_onnx_1, 10)
283
284} // namespace pnnx