Elffuss BraidHub

kikocis/ncnn public

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