| 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 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | class F_avg_pool2d : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 9 8 |
| 26 | pnnx.Input input_0 0 1 input |
| 27 | pnnx.Input input_1 0 1 kernel_size |
| 28 | pnnx.Input input_2 0 1 stride |
| 29 | pnnx.Input input_3 0 1 padding |
| 30 | prim::Constant op_0 0 1 ceil_mode value=%ceil_mode |
| 31 | prim::Constant op_1 0 1 count_include_pad value=%count_include_pad |
| 32 | prim::Constant op_2 0 1 divisor_override value=%divisor_override |
| 33 | aten::avg_pool2d op_3 7 1 input kernel_size stride padding ceil_mode count_include_pad divisor_override out |
| 34 | pnnx.Output output 1 0 out |
| 35 | )PNNXIR"; |
| 36 | } |
| 37 | |
| 38 | const char* type_str() const |
| 39 | { |
| 40 | return "F.avg_pool2d"; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_avg_pool2d, 10) |
| 45 | |
| 46 | // https://github.com/pytorch/pytorch/blob/c263bd43e8e8502d4726643bc6fd046f0130ac0e/torch/onnx/symbolic_opset9.py#L1496 |
| 47 | static 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 | |
| 62 | class F_avg_pool2d_onnx : public GraphRewriterPass |
| 63 | { |
| 64 | public: |
| 65 | const char* match_pattern_graph() const |
| 66 | { |
| 67 | return R"PNNXIR(7767517 |
| 68 | 3 2 |
| 69 | pnnx.Input input 0 1 input |
| 70 | AveragePool op_0 1 1 input out %*=%* |
| 71 | pnnx.Output output 1 0 out |
| 72 | )PNNXIR"; |
| 73 | } |
| 74 | |
| 75 | const char* type_str() const |
| 76 | { |
| 77 | return "F.avg_pool2d"; |
| 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() != 2) |
| 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() != 2) |
| 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() != 2) |
| 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() != 4) |
| 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[2] || pads[1] != pads[3]) |
| 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 inh = in_shape[in_shape.size() - 2]; |
| 116 | const int inw = in_shape[in_shape.size() - 1]; |
| 117 | const int kh = captured_params.at("op_0.kernel_shape").ai[0]; |
| 118 | const int kw = captured_params.at("op_0.kernel_shape").ai[1]; |
| 119 | const int dh = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[0] : 1; |
| 120 | const int dw = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[1] : 1; |
| 121 | const int sh = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[0] : 1; |
| 122 | const int sw = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[1] : 1; |
| 123 | |
| 124 | const int keh = dh * (kh - 1) + 1; |
| 125 | const int kew = dw * (kw - 1) + 1; |
| 126 | |
| 127 | int ceil_padh = get_pool_ceil_padding(inh, keh, sh, pads[0]); |
| 128 | int ceil_padw = get_pool_ceil_padding(inw, kew, sw, pads[1]); |
| 129 | |
| 130 | if (ceil_mode == 0 && pads[0] + ceil_padh == pads[2] && pads[1] + ceil_padw == pads[3]) |
| 131 | { |
| 132 | // useless tail padding :D |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 145 | { |
| 146 | op->params["kernel_size"] = captured_params.at("op_0.kernel_shape"); |
| 147 | |
| 148 | if (captured_params.find("op_0.dilations") != captured_params.end()) |
| 149 | { |
| 150 | op->params["dilation"] = captured_params.at("op_0.dilations"); |
| 151 | } |
| 152 | |
| 153 | if (captured_params.find("op_0.strides") != captured_params.end()) |
| 154 | { |
| 155 | op->params["stride"] = captured_params.at("op_0.strides"); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | op->params["stride"] = {1, 1}; |
| 160 | } |
| 161 | |
| 162 | if (captured_params.find("op_0.pads") != captured_params.end()) |
| 163 | { |
| 164 | const std::vector<int>& pads = captured_params.at("op_0.pads").ai; |
| 165 | op->params["padding"] = {pads[0], pads[1]}; |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | op->params["padding"] = {0, 0}; |
| 170 | } |
| 171 | |
| 172 | if (captured_params.find("op_0.count_include_pad") != captured_params.end()) |
| 173 | { |
| 174 | int count_include_pad = captured_params.at("op_0.count_include_pad").i; |
| 175 | op->params["count_include_pad"] = (count_include_pad != 0); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | op->params["count_include_pad"] = false; |
| 180 | } |
| 181 | |
| 182 | if (captured_params.find("op_0.ceil_mode") != captured_params.end()) |
| 183 | { |
| 184 | int ceil_mode = captured_params.at("op_0.ceil_mode").i; |
| 185 | op->params["ceil_mode"] = (ceil_mode != 0); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | op->params["ceil_mode"] = false; |
| 190 | } |
| 191 | |
| 192 | // ceil_mode for opset9 |
| 193 | if (captured_params.find("op_0.pads") != captured_params.end()) |
| 194 | { |
| 195 | const std::vector<int>& pads = captured_params.at("op_0.pads").ai; |
| 196 | if (pads[0] != pads[2] || pads[1] != pads[3]) |
| 197 | { |
| 198 | op->params["ceil_mode"] = true; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | op->params["divisor_override"] = Parameter(); |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_avg_pool2d_onnx, 10) |
| 207 | |
| 208 | } // namespace pnnx |