| 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_max_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 | pnnx.Input input_4 0 1 dilation |
| 31 | prim::Constant op_0 0 1 ceil_mode value=%ceil_mode |
| 32 | aten::max_pool1d op_1 6 1 input kernel_size stride padding dilation ceil_mode out |
| 33 | pnnx.Output output 1 0 out |
| 34 | )PNNXIR"; |
| 35 | } |
| 36 | |
| 37 | const char* type_str() const |
| 38 | { |
| 39 | return "F.max_pool1d"; |
| 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 | |
| 49 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool1d, 10) |
| 50 | |
| 51 | class F_max_pool1d_2 : public GraphRewriterPass |
| 52 | { |
| 53 | public: |
| 54 | const char* match_pattern_graph() const |
| 55 | { |
| 56 | return R"PNNXIR(7767517 |
| 57 | 8 8 |
| 58 | pnnx.Input input_0 0 1 input |
| 59 | pnnx.Input input_1 0 1 kernel_size |
| 60 | pnnx.Input input_2 0 1 stride |
| 61 | pnnx.Input input_3 0 1 padding |
| 62 | pnnx.Input input_4 0 1 dilation |
| 63 | prim::Constant op_0 0 1 ceil_mode value=%ceil_mode |
| 64 | aten::max_pool1d_with_indices op_1 6 2 input kernel_size stride padding dilation ceil_mode out indices |
| 65 | pnnx.Output output 2 0 out indices |
| 66 | )PNNXIR"; |
| 67 | } |
| 68 | |
| 69 | const char* type_str() const |
| 70 | { |
| 71 | return "F.max_pool1d"; |
| 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 | |
| 81 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool1d_2, 10) |
| 82 | |
| 83 | // https://github.com/pytorch/pytorch/blob/c263bd43e8e8502d4726643bc6fd046f0130ac0e/torch/onnx/symbolic_opset9.py#L1496 |
| 84 | static 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 | |
| 99 | class F_max_pool1d_onnx : public GraphRewriterPass |
| 100 | { |
| 101 | public: |
| 102 | const char* match_pattern_graph() const |
| 103 | { |
| 104 | return R"PNNXIR(7767517 |
| 105 | 3 2 |
| 106 | pnnx.Input input 0 1 input |
| 107 | MaxPool op_0 1 1 input out %*=%* |
| 108 | pnnx.Output output 1 0 out |
| 109 | )PNNXIR"; |
| 110 | } |
| 111 | |
| 112 | const char* type_str() const |
| 113 | { |
| 114 | return "F.max_pool1d"; |
| 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() != 1) |
| 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() != 1) |
| 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() != 1) |
| 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() != 2) |
| 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[1]) |
| 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() < 1) |
| 150 | return false; |
| 151 | |
| 152 | const int inw = in_shape[in_shape.size() - 1]; |
| 153 | const int kw = captured_params.at("op_0.kernel_shape").ai[0]; |
| 154 | const int dw = captured_params.find("op_0.dilations") != captured_params.end() ? captured_params.at("op_0.dilations").ai[0] : 1; |
| 155 | const int sw = captured_params.find("op_0.strides") != captured_params.end() ? captured_params.at("op_0.strides").ai[0] : 1; |
| 156 | |
| 157 | const int kew = dw * (kw - 1) + 1; |
| 158 | |
| 159 | int ceil_padw = get_pool_ceil_padding(inw, kew, sw, pads[0]); |
| 160 | |
| 161 | if (ceil_mode == 0 && pads[0] + ceil_padw == pads[1]) |
| 162 | { |
| 163 | // useless tail padding :D |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 176 | { |
| 177 | op->params["kernel_size"] = captured_params.at("op_0.kernel_shape"); |
| 178 | |
| 179 | if (captured_params.find("op_0.dilations") != captured_params.end()) |
| 180 | { |
| 181 | op->params["dilation"] = captured_params.at("op_0.dilations"); |
| 182 | } |
| 183 | |
| 184 | if (captured_params.find("op_0.strides") != captured_params.end()) |
| 185 | { |
| 186 | op->params["stride"] = captured_params.at("op_0.strides"); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | op->params["stride"] = {1}; |
| 191 | } |
| 192 | |
| 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 | op->params["padding"] = {pads[0]}; |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | op->params["padding"] = {0}; |
| 201 | } |
| 202 | |
| 203 | if (captured_params.find("op_0.ceil_mode") != captured_params.end()) |
| 204 | { |
| 205 | int ceil_mode = captured_params.at("op_0.ceil_mode").i; |
| 206 | op->params["ceil_mode"] = (ceil_mode != 0); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | op->params["ceil_mode"] = false; |
| 211 | } |
| 212 | |
| 213 | // ceil_mode for opset9 |
| 214 | if (captured_params.find("op_0.pads") != captured_params.end()) |
| 215 | { |
| 216 | const std::vector<int>& pads = captured_params.at("op_0.pads").ai; |
| 217 | if (pads[0] != pads[1]) |
| 218 | { |
| 219 | op->params["ceil_mode"] = true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | op->params["return_indices"] = false; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool1d_onnx, 10) |
| 228 | |
| 229 | class F_max_pool1d_onnx_1 : public F_max_pool1d_onnx |
| 230 | { |
| 231 | public: |
| 232 | const char* match_pattern_graph() const |
| 233 | { |
| 234 | return R"PNNXIR(7767517 |
| 235 | 3 3 |
| 236 | pnnx.Input input 0 1 input |
| 237 | MaxPool op_0 1 2 input out indices %*=%* |
| 238 | pnnx.Output output 2 0 out indices |
| 239 | )PNNXIR"; |
| 240 | } |
| 241 | |
| 242 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 243 | { |
| 244 | F_max_pool1d_onnx::write(op, captured_params); |
| 245 | |
| 246 | op->params["return_indices"] = true; |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_max_pool1d_onnx_1, 10) |
| 251 | |
| 252 | } // namespace pnnx |