Elffuss BraidHub

kikocis/ncnn public

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