Elffuss BraidHub

kikocis/ncnn public

master · 8.2 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_avg_pool3d : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
259 8
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
30prim::Constant op_0 0 1 ceil_mode value=%ceil_mode
31prim::Constant op_1 0 1 count_include_pad value=%count_include_pad
32prim::Constant op_2 0 1 divisor_override value=%divisor_override
33aten::avg_pool3d op_3 7 1 input kernel_size stride padding ceil_mode count_include_pad divisor_override out
34pnnx.Output output 1 0 out
35)PNNXIR";
36 }
37
38 const char* type_str() const
39 {
40 return "F.avg_pool3d";
41 }
42};
43
44REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_avg_pool3d, 10)
45
46// https://github.com/pytorch/pytorch/blob/c263bd43e8e8502d4726643bc6fd046f0130ac0e/torch/onnx/symbolic_opset9.py#L1496
47static int get_pool_ceil_padding(int w, int ksize, int stride, int pad)
48{
49 if (stride == 1)
50 return 0;
51
52 int ceiled_output_w = int(ceil((w + pad * 2 - ksize) / float(stride))) + 1;
53
54 if ((ceiled_output_w - 1) * stride >= w + pad)
55 ceiled_output_w = ceiled_output_w - 1;
56
57 int ceil_pad = ksize - (w + pad * 2 - ((ceiled_output_w - 1) * stride + 1));
58
59 return ceil_pad;
60}
61
62class F_avg_pool3d_onnx : public GraphRewriterPass
63{
64public:
65 const char* match_pattern_graph() const
66 {
67 return R"PNNXIR(7767517
683 2
69pnnx.Input input 0 1 input
70AveragePool op_0 1 1 input out %*=%*
71pnnx.Output output 1 0 out
72)PNNXIR";
73 }
74
75 const char* type_str() const
76 {
77 return "F.avg_pool3d";
78 }
79
80 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
81 {
82 if (captured_params.find("op_0.kernel_shape") == captured_params.end())
83 return false;
84
85 if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 3)
86 return false;
87
88 if (captured_params.find("op_0.dilations") != captured_params.end())
89 {
90 if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 3)
91 return false;
92 }
93
94 if (captured_params.find("op_0.strides") != captured_params.end())
95 {
96 if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 3)
97 return false;
98 }
99
100 if (captured_params.find("op_0.pads") != captured_params.end())
101 {
102 if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 6)
103 return false;
104
105 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
106 const int ceil_mode = captured_params.find("op_0.ceil_mode") != captured_params.end() ? captured_params.at("op_0.ceil_mode").i : 0;
107 if (pads[0] != pads[3] || pads[1] != pads[4] || pads[2] != pads[5])
108 {
109 // ceil_mode for opset9
110 const Operator* avgpool = matched_operators.at("op_0");
111 const std::vector<int>& in_shape = avgpool->inputs[0]->shape;
112 if (in_shape.size() < 2)
113 return false;
114
115 const int ind = in_shape[in_shape.size() - 3];
116 const int inh = in_shape[in_shape.size() - 2];
117 const int inw = in_shape[in_shape.size() - 1];
118 const int kd = captured_params.at("op_0.kernel_shape").ai[0];
119 const int kh = captured_params.at("op_0.kernel_shape").ai[1];
120 const int kw = captured_params.at("op_0.kernel_shape").ai[2];
121 const int dd = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[0] : 1;
122 const int dh = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[1] : 1;
123 const int dw = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[2] : 1;
124 const int sd = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[0] : 1;
125 const int sh = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[1] : 1;
126 const int sw = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[2] : 1;
127
128 const int ked = dd * (kd - 1) + 1;
129 const int keh = dh * (kh - 1) + 1;
130 const int kew = dw * (kw - 1) + 1;
131
132 int ceil_padd = get_pool_ceil_padding(ind, ked, sd, pads[0]);
133 int ceil_padh = get_pool_ceil_padding(inh, keh, sh, pads[1]);
134 int ceil_padw = get_pool_ceil_padding(inw, kew, sw, pads[2]);
135
136 if (ceil_mode == 0 && pads[0] + ceil_padd == pads[3] && pads[1] + ceil_padh == pads[4] && pads[2] + ceil_padw == pads[5])
137 {
138 // useless tail padding :D
139 }
140 else
141 {
142 return false;
143 }
144 }
145 }
146
147 return true;
148 }
149
150 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
151 {
152 op->params["kernel_size"] = captured_params.at("op_0.kernel_shape");
153
154 if (captured_params.find("op_0.dilations") != captured_params.end())
155 {
156 op->params["dilation"] = captured_params.at("op_0.dilations");
157 }
158
159 if (captured_params.find("op_0.strides") != captured_params.end())
160 {
161 op->params["stride"] = captured_params.at("op_0.strides");
162 }
163 else
164 {
165 op->params["stride"] = {1, 1, 1};
166 }
167
168 if (captured_params.find("op_0.pads") != captured_params.end())
169 {
170 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
171 op->params["padding"] = {pads[0], pads[1], pads[2]};
172 }
173 else
174 {
175 op->params["padding"] = {0, 0, 0};
176 }
177
178 if (captured_params.find("op_0.count_include_pad") != captured_params.end())
179 {
180 int count_include_pad = captured_params.at("op_0.count_include_pad").i;
181 op->params["count_include_pad"] = (count_include_pad != 0);
182 }
183 else
184 {
185 op->params["count_include_pad"] = false;
186 }
187
188 if (captured_params.find("op_0.ceil_mode") != captured_params.end())
189 {
190 int ceil_mode = captured_params.at("op_0.ceil_mode").i;
191 op->params["ceil_mode"] = (ceil_mode != 0);
192 }
193 else
194 {
195 op->params["ceil_mode"] = false;
196 }
197
198 // ceil_mode for opset9
199 if (captured_params.find("op_0.pads") != captured_params.end())
200 {
201 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
202 if (pads[0] != pads[3] || pads[1] != pads[4] || pads[2] != pads[5])
203 {
204 op->params["ceil_mode"] = true;
205 }
206 }
207
208 op->params["divisor_override"] = Parameter();
209 }
210};
211
212REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_avg_pool3d_onnx, 10)
213
214} // namespace pnnx