Elffuss BraidHub

kikocis/ncnn public

master · 9.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_level2.h"
16
17namespace pnnx {
18
19class F_max_pool3d : 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_pool3d 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_pool3d";
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_pool3d, 10)
50
51class F_max_pool3d_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_pool3d_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_pool3d";
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_pool3d_2, 10)
82
83// https://github.com/pytorch/pytorch/blob/c263bd43e8e8502d4726643bc6fd046f0130ac0e/torch/onnx/symbolic_opset9.py#L1496
84static int get_pool_ceil_padding(int w, int ksize, int stride, int pad)
85{
86 if (stride == 1)
87 return 0;
88
89 int ceiled_output_w = int(ceil((w + pad * 2 - ksize) / float(stride))) + 1;
90
91 if ((ceiled_output_w - 1) * stride >= w + pad)
92 ceiled_output_w = ceiled_output_w - 1;
93
94 int ceil_pad = ksize - (w + pad * 2 - ((ceiled_output_w - 1) * stride + 1));
95
96 return ceil_pad;
97}
98
99class F_max_pool3d_onnx : public GraphRewriterPass
100{
101public:
102 const char* match_pattern_graph() const
103 {
104 return R"PNNXIR(7767517
1053 2
106pnnx.Input input 0 1 input
107MaxPool op_0 1 1 input out %*=%*
108pnnx.Output output 1 0 out
109)PNNXIR";
110 }
111
112 const char* type_str() const
113 {
114 return "F.max_pool3d";
115 }
116
117 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
118 {
119 if (captured_params.find("op_0.kernel_shape") == captured_params.end())
120 return false;
121
122 if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 3)
123 return false;
124
125 if (captured_params.find("op_0.dilations") != captured_params.end())
126 {
127 if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 3)
128 return false;
129 }
130
131 if (captured_params.find("op_0.strides") != captured_params.end())
132 {
133 if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 3)
134 return false;
135 }
136
137 if (captured_params.find("op_0.pads") != captured_params.end())
138 {
139 if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 6)
140 return false;
141
142 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
143 const int ceil_mode = captured_params.find("op_0.ceil_mode") != captured_params.end() ? captured_params.at("op_0.ceil_mode").i : 0;
144 if (pads[0] != pads[3] || pads[1] != pads[4] || pads[2] != pads[5])
145 {
146 // ceil_mode for opset9
147 const Operator* avgpool = matched_operators.at("op_0");
148 const std::vector<int>& in_shape = avgpool->inputs[0]->shape;
149 if (in_shape.size() < 2)
150 return false;
151
152 const int ind = in_shape[in_shape.size() - 3];
153 const int inh = in_shape[in_shape.size() - 2];
154 const int inw = in_shape[in_shape.size() - 1];
155 const int kd = captured_params.at("op_0.kernel_shape").ai[0];
156 const int kh = captured_params.at("op_0.kernel_shape").ai[1];
157 const int kw = captured_params.at("op_0.kernel_shape").ai[2];
158 const int dd = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[0] : 1;
159 const int dh = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[1] : 1;
160 const int dw = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[2] : 1;
161 const int sd = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[0] : 1;
162 const int sh = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[1] : 1;
163 const int sw = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[2] : 1;
164
165 const int ked = dd * (kd - 1) + 1;
166 const int keh = dh * (kh - 1) + 1;
167 const int kew = dw * (kw - 1) + 1;
168
169 int ceil_padd = get_pool_ceil_padding(ind, ked, sd, pads[0]);
170 int ceil_padh = get_pool_ceil_padding(inh, keh, sh, pads[1]);
171 int ceil_padw = get_pool_ceil_padding(inw, kew, sw, pads[2]);
172
173 if (ceil_mode == 0 && pads[0] + ceil_padd == pads[3] && pads[1] + ceil_padh == pads[4] && pads[2] + ceil_padw == pads[5])
174 {
175 // useless tail padding :D
176 }
177 else
178 {
179 return false;
180 }
181 }
182 }
183
184 return true;
185 }
186
187 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
188 {
189 op->params["kernel_size"] = captured_params.at("op_0.kernel_shape");
190
191 if (captured_params.find("op_0.dilations") != captured_params.end())
192 {
193 op->params["dilation"] = captured_params.at("op_0.dilations");
194 }
195
196 if (captured_params.find("op_0.strides") != captured_params.end())
197 {
198 op->params["stride"] = captured_params.at("op_0.strides");
199 }
200 else
201 {
202 op->params["stride"] = {1, 1, 1};
203 }
204
205 if (captured_params.find("op_0.pads") != captured_params.end())
206 {
207 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
208 op->params["padding"] = {pads[0], pads[1], pads[2]};
209 }
210 else
211 {
212 op->params["padding"] = {0, 0, 0};
213 }
214
215 if (captured_params.find("op_0.ceil_mode") != captured_params.end())
216 {
217 int ceil_mode = captured_params.at("op_0.ceil_mode").i;
218 op->params["ceil_mode"] = (ceil_mode != 0);
219 }
220 else
221 {
222 op->params["ceil_mode"] = false;
223 }
224
225 // ceil_mode for opset9
226 if (captured_params.find("op_0.pads") != captured_params.end())
227 {
228 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
229 if (pads[0] != pads[3] || pads[1] != pads[4] || pads[2] != pads[5])
230 {
231 op->params["ceil_mode"] = true;
232 }
233 }
234
235 op->params["return_indices"] = false;
236 }
237};
238
239REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool3d_onnx, 10)
240
241class F_max_pool3d_onnx_1 : public F_max_pool3d_onnx
242{
243public:
244 const char* match_pattern_graph() const
245 {
246 return R"PNNXIR(7767517
2473 3
248pnnx.Input input 0 1 input
249MaxPool op_0 1 2 input out indices %*=%*
250pnnx.Output output 2 0 out indices
251)PNNXIR";
252 }
253
254 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
255 {
256 F_max_pool3d_onnx::write(op, captured_params);
257
258 op->params["return_indices"] = true;
259 }
260};
261
262REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool3d_onnx_1, 10)
263
264} // namespace pnnx