Elffuss BraidHub

kikocis/ncnn public

master · 10.3 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_max_pool2d : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
258 7
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 kernel_size
28pnnx.Input input_2 0 1 stride
29pnnx.Input input_3 0 1 padding
30pnnx.Input input_4 0 1 dilation
31prim::Constant op_0 0 1 ceil_mode value=%ceil_mode
32aten::max_pool2d op_1 6 1 input kernel_size stride padding dilation ceil_mode out
33pnnx.Output output 1 0 out
34)PNNXIR";
35 }
36
37 const char* type_str() const
38 {
39 return "F.max_pool2d";
40 }
41
42 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
43 {
44 op->params["ceil_mode"] = captured_params.at("ceil_mode");
45 op->params["return_indices"] = false;
46 }
47};
48
49REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool2d, 10)
50
51class F_max_pool2d_2 : public GraphRewriterPass
52{
53public:
54 const char* match_pattern_graph() const
55 {
56 return R"PNNXIR(7767517
578 8
58pnnx.Input input_0 0 1 input
59pnnx.Input input_1 0 1 kernel_size
60pnnx.Input input_2 0 1 stride
61pnnx.Input input_3 0 1 padding
62pnnx.Input input_4 0 1 dilation
63prim::Constant op_0 0 1 ceil_mode value=%ceil_mode
64aten::max_pool2d_with_indices op_1 6 2 input kernel_size stride padding dilation ceil_mode out indices
65pnnx.Output output 2 0 out indices
66)PNNXIR";
67 }
68
69 const char* type_str() const
70 {
71 return "F.max_pool2d";
72 }
73
74 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
75 {
76 op->params["ceil_mode"] = captured_params.at("ceil_mode");
77 op->params["return_indices"] = true;
78 }
79};
80
81REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool2d_2, 10)
82
83class F_max_pool2d_3 : public GraphRewriterPass
84{
85public:
86 const char* match_pattern_graph() const
87 {
88 return R"PNNXIR(7767517
893 3
90pnnx.Input input_0 0 1 input
91aten::max_pool_with_indices_onnx op_1 1 2 input out indices kernel_size=%kernel_size stride=%stride padding=%padding dilation=%dilation ceil_mode=%ceil_mode n_dims_axes=* n_dims_one=* n_dims_zero=* unbatched_rank=*
92pnnx.Output output 2 0 out indices
93)PNNXIR";
94 }
95
96 const char* type_str() const
97 {
98 return "F.max_pool2d";
99 }
100
101 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
102 {
103 std::vector<int> kernel_size = captured_params.at("kernel_size").ai;
104 std::vector<int> dilation = captured_params.at("dilation").ai;
105 std::vector<int> stride = captured_params.at("stride").ai;
106 std::vector<int> padding = captured_params.at("padding").ai;
107 int ceil_mode = captured_params.at("ceil_mode").i;
108
109 if (padding.size() == 4)
110 {
111 padding = {padding[0], padding[1]};
112 }
113
114 op->params["kernel_size"] = kernel_size;
115 op->params["dilation"] = dilation;
116 op->params["stride"] = stride;
117 op->params["padding"] = padding;
118 op->params["ceil_mode"] = (ceil_mode != 0);
119 op->params["return_indices"] = (op->outputs.size() != 1);
120 }
121};
122
123REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool2d_3, 10)
124
125// https://github.com/pytorch/pytorch/blob/c263bd43e8e8502d4726643bc6fd046f0130ac0e/torch/onnx/symbolic_opset9.py#L1496
126static int get_pool_ceil_padding(int w, int ksize, int stride, int pad)
127{
128 if (stride == 1)
129 return 0;
130
131 int ceiled_output_w = int(ceil((w + pad * 2 - ksize) / float(stride))) + 1;
132
133 if ((ceiled_output_w - 1) * stride >= w + pad)
134 ceiled_output_w = ceiled_output_w - 1;
135
136 int ceil_pad = ksize - (w + pad * 2 - ((ceiled_output_w - 1) * stride + 1));
137
138 return ceil_pad;
139}
140
141class F_max_pool2d_onnx : public GraphRewriterPass
142{
143public:
144 const char* match_pattern_graph() const
145 {
146 return R"PNNXIR(7767517
1473 2
148pnnx.Input input 0 1 input
149MaxPool op_0 1 1 input out %*=%*
150pnnx.Output output 1 0 out
151)PNNXIR";
152 }
153
154 const char* type_str() const
155 {
156 return "F.max_pool2d";
157 }
158
159 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
160 {
161 if (captured_params.find("op_0.kernel_shape") == captured_params.end())
162 return false;
163
164 if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 2)
165 return false;
166
167 if (captured_params.find("op_0.dilations") != captured_params.end())
168 {
169 if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 2)
170 return false;
171 }
172
173 if (captured_params.find("op_0.strides") != captured_params.end())
174 {
175 if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 2)
176 return false;
177 }
178
179 if (captured_params.find("op_0.pads") != captured_params.end())
180 {
181 if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 4)
182 return false;
183
184 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
185 const int ceil_mode = captured_params.find("op_0.ceil_mode") != captured_params.end() ? captured_params.at("op_0.ceil_mode").i : 0;
186 if (pads[0] != pads[2] || pads[1] != pads[3])
187 {
188 // ceil_mode for opset9
189 const Operator* avgpool = matched_operators.at("op_0");
190 const std::vector<int>& in_shape = avgpool->inputs[0]->shape;
191 if (in_shape.size() < 2)
192 return false;
193
194 const int inh = in_shape[in_shape.size() - 2];
195 const int inw = in_shape[in_shape.size() - 1];
196 const int kh = captured_params.at("op_0.kernel_shape").ai[0];
197 const int kw = captured_params.at("op_0.kernel_shape").ai[1];
198 const int dh = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[0] : 1;
199 const int dw = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[1] : 1;
200 const int sh = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[0] : 1;
201 const int sw = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[1] : 1;
202
203 const int keh = dh * (kh - 1) + 1;
204 const int kew = dw * (kw - 1) + 1;
205
206 int ceil_padh = get_pool_ceil_padding(inh, keh, sh, pads[0]);
207 int ceil_padw = get_pool_ceil_padding(inw, kew, sw, pads[1]);
208
209 if (ceil_mode == 0 && pads[0] + ceil_padh == pads[2] && pads[1] + ceil_padw == pads[3])
210 {
211 // useless tail padding :D
212 }
213 else
214 {
215 return false;
216 }
217 }
218 }
219
220 return true;
221 }
222
223 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
224 {
225 op->params["kernel_size"] = captured_params.at("op_0.kernel_shape");
226
227 if (captured_params.find("op_0.dilations") != captured_params.end())
228 {
229 op->params["dilation"] = captured_params.at("op_0.dilations");
230 }
231
232 if (captured_params.find("op_0.strides") != captured_params.end())
233 {
234 op->params["stride"] = captured_params.at("op_0.strides");
235 }
236 else
237 {
238 op->params["stride"] = {1, 1};
239 }
240
241 if (captured_params.find("op_0.pads") != captured_params.end())
242 {
243 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
244 op->params["padding"] = {pads[0], pads[1]};
245 }
246 else
247 {
248 op->params["padding"] = {0, 0};
249 }
250
251 if (captured_params.find("op_0.ceil_mode") != captured_params.end())
252 {
253 int ceil_mode = captured_params.at("op_0.ceil_mode").i;
254 op->params["ceil_mode"] = (ceil_mode != 0);
255 }
256 else
257 {
258 op->params["ceil_mode"] = false;
259 }
260
261 // ceil_mode for opset9
262 if (captured_params.find("op_0.pads") != captured_params.end())
263 {
264 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
265 if (pads[0] != pads[2] || pads[1] != pads[3])
266 {
267 op->params["ceil_mode"] = true;
268 }
269 }
270
271 op->params["return_indices"] = false;
272 }
273};
274
275REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool2d_onnx, 10)
276
277class F_max_pool2d_onnx_1 : public F_max_pool2d_onnx
278{
279public:
280 const char* match_pattern_graph() const
281 {
282 return R"PNNXIR(7767517
2833 3
284pnnx.Input input 0 1 input
285MaxPool op_0 1 2 input out indices %*=%*
286pnnx.Output output 2 0 out indices
287)PNNXIR";
288 }
289
290 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
291 {
292 F_max_pool2d_onnx::write(op, captured_params);
293
294 op->params["return_indices"] = true;
295 }
296};
297
298REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool2d_onnx_1, 10)
299
300} // namespace pnnx