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