| 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_pad : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 5 4 |
| 26 | pnnx.Input input_0 0 1 input |
| 27 | pnnx.Input input_1 0 1 pad |
| 28 | pnnx.Input input_2 0 1 value |
| 29 | aten::constant_pad_nd op_0 3 1 input pad value out |
| 30 | pnnx.Output output 1 0 out |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* type_str() const |
| 35 | { |
| 36 | return "F.pad"; |
| 37 | } |
| 38 | |
| 39 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 40 | { |
| 41 | op->params["mode"] = "constant"; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad, 10) |
| 46 | |
| 47 | class F_pad_01 : public GraphRewriterPass |
| 48 | { |
| 49 | public: |
| 50 | const char* match_pattern_graph() const |
| 51 | { |
| 52 | return R"PNNXIR(7767517 |
| 53 | 4 3 |
| 54 | pnnx.Input input_0 0 1 input |
| 55 | pnnx.Input input_1 0 1 pad |
| 56 | aten::constant_pad_nd op_0 2 1 input pad out value=%value |
| 57 | pnnx.Output output 1 0 out |
| 58 | )PNNXIR"; |
| 59 | } |
| 60 | |
| 61 | const char* type_str() const |
| 62 | { |
| 63 | return "F.pad"; |
| 64 | } |
| 65 | |
| 66 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 67 | { |
| 68 | op->params["mode"] = "constant"; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_01, 10) |
| 73 | |
| 74 | class F_pad_1 : public GraphRewriterPass |
| 75 | { |
| 76 | public: |
| 77 | const char* match_pattern_graph() const |
| 78 | { |
| 79 | return R"PNNXIR(7767517 |
| 80 | 4 3 |
| 81 | pnnx.Input input_0 0 1 input |
| 82 | pnnx.Input input_1 0 1 pad |
| 83 | aten::reflection_pad1d op_0 2 1 input pad out |
| 84 | pnnx.Output output 1 0 out |
| 85 | )PNNXIR"; |
| 86 | } |
| 87 | |
| 88 | const char* type_str() const |
| 89 | { |
| 90 | return "F.pad"; |
| 91 | } |
| 92 | |
| 93 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 94 | { |
| 95 | op->params["mode"] = "reflect"; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_1, 10) |
| 100 | |
| 101 | class F_pad_2 : public GraphRewriterPass |
| 102 | { |
| 103 | public: |
| 104 | const char* match_pattern_graph() const |
| 105 | { |
| 106 | return R"PNNXIR(7767517 |
| 107 | 4 3 |
| 108 | pnnx.Input input_0 0 1 input |
| 109 | pnnx.Input input_1 0 1 pad |
| 110 | aten::replication_pad1d op_0 2 1 input pad out |
| 111 | pnnx.Output output 1 0 out |
| 112 | )PNNXIR"; |
| 113 | } |
| 114 | |
| 115 | const char* type_str() const |
| 116 | { |
| 117 | return "F.pad"; |
| 118 | } |
| 119 | |
| 120 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 121 | { |
| 122 | op->params["mode"] = "replicate"; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_2, 10) |
| 127 | |
| 128 | class F_pad_3 : public GraphRewriterPass |
| 129 | { |
| 130 | public: |
| 131 | const char* match_pattern_graph() const |
| 132 | { |
| 133 | return R"PNNXIR(7767517 |
| 134 | 4 3 |
| 135 | pnnx.Input input_0 0 1 input |
| 136 | pnnx.Input input_1 0 1 pad |
| 137 | aten::reflection_pad2d op_0 2 1 input pad out |
| 138 | pnnx.Output output 1 0 out |
| 139 | )PNNXIR"; |
| 140 | } |
| 141 | |
| 142 | const char* type_str() const |
| 143 | { |
| 144 | return "F.pad"; |
| 145 | } |
| 146 | |
| 147 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 148 | { |
| 149 | op->params["mode"] = "reflect"; |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_3, 10) |
| 154 | |
| 155 | class F_pad_4 : public GraphRewriterPass |
| 156 | { |
| 157 | public: |
| 158 | const char* match_pattern_graph() const |
| 159 | { |
| 160 | return R"PNNXIR(7767517 |
| 161 | 4 3 |
| 162 | pnnx.Input input_0 0 1 input |
| 163 | pnnx.Input input_1 0 1 pad |
| 164 | aten::replication_pad2d op_0 2 1 input pad out |
| 165 | pnnx.Output output 1 0 out |
| 166 | )PNNXIR"; |
| 167 | } |
| 168 | |
| 169 | const char* type_str() const |
| 170 | { |
| 171 | return "F.pad"; |
| 172 | } |
| 173 | |
| 174 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 175 | { |
| 176 | op->params["mode"] = "replicate"; |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_4, 10) |
| 181 | |
| 182 | class F_pad_6 : public GraphRewriterPass |
| 183 | { |
| 184 | public: |
| 185 | const char* match_pattern_graph() const |
| 186 | { |
| 187 | return R"PNNXIR(7767517 |
| 188 | 4 3 |
| 189 | pnnx.Input input_0 0 1 input |
| 190 | pnnx.Input input_1 0 1 pad |
| 191 | aten::replication_pad3d op_0 2 1 input pad out |
| 192 | pnnx.Output output 1 0 out |
| 193 | )PNNXIR"; |
| 194 | } |
| 195 | |
| 196 | const char* type_str() const |
| 197 | { |
| 198 | return "F.pad"; |
| 199 | } |
| 200 | |
| 201 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 202 | { |
| 203 | op->params["mode"] = "replicate"; |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_6, 10) |
| 208 | |
| 209 | class F_pad_7 : public GraphRewriterPass |
| 210 | { |
| 211 | public: |
| 212 | const char* match_pattern_graph() const |
| 213 | { |
| 214 | return R"PNNXIR(7767517 |
| 215 | 6 5 |
| 216 | pnnx.Input input_0 0 1 input |
| 217 | pnnx.Input input_1 0 1 pad |
| 218 | pnnx.Input input_2 0 1 value |
| 219 | prim::Constant op_0 0 1 mode value=constant |
| 220 | aten::pad op_1 4 1 input pad mode value out |
| 221 | pnnx.Output output 1 0 out |
| 222 | )PNNXIR"; |
| 223 | } |
| 224 | |
| 225 | const char* type_str() const |
| 226 | { |
| 227 | return "F.pad"; |
| 228 | } |
| 229 | |
| 230 | void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const |
| 231 | { |
| 232 | op->params["mode"] = "constant"; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_7, 10) |
| 237 | |
| 238 | class F_pad_8 : public GraphRewriterPass |
| 239 | { |
| 240 | public: |
| 241 | const char* match_pattern_graph() const |
| 242 | { |
| 243 | return R"PNNXIR(7767517 |
| 244 | 6 5 |
| 245 | pnnx.Input input_0 0 1 input |
| 246 | pnnx.Input input_1 0 1 pad |
| 247 | pnnx.Input input_2 0 1 mode |
| 248 | prim::Constant op_0 0 1 value value=* |
| 249 | aten::pad op_1 4 1 input pad mode value out |
| 250 | pnnx.Output output 1 0 out |
| 251 | )PNNXIR"; |
| 252 | } |
| 253 | |
| 254 | const char* type_str() const |
| 255 | { |
| 256 | return "F.pad"; |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_8, 10) |
| 261 | |
| 262 | class F_pad_onnx : public GraphRewriterPass |
| 263 | { |
| 264 | public: |
| 265 | const char* match_pattern_graph() const |
| 266 | { |
| 267 | return R"PNNXIR(7767517 |
| 268 | 3 2 |
| 269 | pnnx.Input input 0 1 input |
| 270 | Pad op_0 1 1 input out mode=%mode pads=%pads |
| 271 | pnnx.Output output 1 0 out |
| 272 | )PNNXIR"; |
| 273 | } |
| 274 | |
| 275 | const char* type_str() const |
| 276 | { |
| 277 | return "F.pad"; |
| 278 | } |
| 279 | |
| 280 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 281 | { |
| 282 | const std::string& mode = captured_params.at("mode").s; |
| 283 | if (mode == "constant") op->params["mode"] = "constant"; |
| 284 | if (mode == "reflect") op->params["mode"] = "reflect"; |
| 285 | if (mode == "edge") op->params["mode"] = "replicate"; |
| 286 | if (mode == "wrap") op->params["mode"] = "circular"; |
| 287 | |
| 288 | const std::vector<int>& pads = captured_params.at("pads").ai; |
| 289 | |
| 290 | if (pads.size() == 2) |
| 291 | op->params["pad"] = pads; |
| 292 | |
| 293 | if (pads.size() == 4) |
| 294 | { |
| 295 | if (pads[0] == 0 && pads[2] == 0) |
| 296 | op->params["pad"] = std::vector<int>{pads[1], pads[3]}; |
| 297 | else |
| 298 | op->params["pad"] = std::vector<int>{pads[1], pads[3], pads[0], pads[2]}; |
| 299 | } |
| 300 | |
| 301 | if (pads.size() == 6) |
| 302 | { |
| 303 | if (pads[1] == 0 && pads[4] == 0 && pads[0] == 0 && pads[3] == 0) |
| 304 | op->params["pad"] = std::vector<int>{pads[2], pads[5]}; |
| 305 | else if (pads[0] == 0 && pads[3] == 0) |
| 306 | op->params["pad"] = std::vector<int>{pads[2], pads[5], pads[1], pads[4]}; |
| 307 | else |
| 308 | op->params["pad"] = std::vector<int>{pads[2], pads[5], pads[1], pads[4], pads[0], pads[3]}; |
| 309 | } |
| 310 | |
| 311 | if (pads.size() == 8) |
| 312 | { |
| 313 | if (pads[1] == 0 && pads[5] == 0 && pads[0] == 0 && pads[4] == 0) |
| 314 | op->params["pad"] = std::vector<int>{pads[3], pads[7], pads[2], pads[6]}; |
| 315 | else if (pads[0] == 0 && pads[4] == 0) |
| 316 | op->params["pad"] = std::vector<int>{pads[3], pads[7], pads[2], pads[6], pads[1], pads[5]}; |
| 317 | else |
| 318 | op->params["pad"] = std::vector<int>{pads[3], pads[7], pads[2], pads[6], pads[1], pads[5], pads[0], pads[4]}; |
| 319 | } |
| 320 | |
| 321 | if (pads.size() == 10) |
| 322 | { |
| 323 | if (pads[1] == 0 && pads[6] == 0 && pads[0] == 0 && pads[5] == 0) |
| 324 | op->params["pad"] = std::vector<int>{pads[4], pads[9], pads[3], pads[8], pads[2], pads[7]}; |
| 325 | else if (pads[0] == 0 && pads[5] == 0) |
| 326 | op->params["pad"] = std::vector<int>{pads[4], pads[9], pads[3], pads[8], pads[2], pads[7], pads[1], pads[6]}; |
| 327 | else |
| 328 | op->params["pad"] = std::vector<int>{pads[4], pads[9], pads[3], pads[8], pads[2], pads[7], pads[1], pads[6], pads[0], pads[5]}; |
| 329 | } |
| 330 | } |
| 331 | }; |
| 332 | |
| 333 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_onnx, 10) |
| 334 | |
| 335 | class F_pad_onnx_1 : public F_pad_onnx |
| 336 | { |
| 337 | public: |
| 338 | const char* match_pattern_graph() const |
| 339 | { |
| 340 | return R"PNNXIR(7767517 |
| 341 | 3 2 |
| 342 | pnnx.Input input 0 1 input |
| 343 | Pad op_0 1 1 input out mode=%mode pads=%pads value=%value |
| 344 | pnnx.Output output 1 0 out |
| 345 | )PNNXIR"; |
| 346 | } |
| 347 | |
| 348 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 349 | { |
| 350 | F_pad_onnx::write(op, captured_params); |
| 351 | |
| 352 | op->params["value"] = captured_params.at("value"); |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_pad_onnx_1, 10) |
| 357 | |
| 358 | } // namespace pnnx |