| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "fuse_pad_conv2d.h" |
| 16 | |
| 17 | #include "pass_level2.h" |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | namespace pnnx { |
| 23 | |
| 24 | class fuse_pad_conv2d_pass : public GraphRewriterPass |
| 25 | { |
| 26 | public: |
| 27 | const char* match_pattern_graph() const |
| 28 | { |
| 29 | return R"PNNXIR(7767517 |
| 30 | 4 3 |
| 31 | pnnx.Input input 0 1 input |
| 32 | F.pad op_pad 1 1 input a mode=constant pad=%pad value=%value |
| 33 | nn.Conv2d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=zeros padding=%padding dilation=%dilation groups=%groups bias=%bias @weight @bias |
| 34 | pnnx.Output output 1 0 out |
| 35 | )PNNXIR"; |
| 36 | } |
| 37 | |
| 38 | const char* type_str() const |
| 39 | { |
| 40 | return "nn.Conv2d"; |
| 41 | } |
| 42 | |
| 43 | const char* name_str() const |
| 44 | { |
| 45 | return "padconv2d"; |
| 46 | } |
| 47 | |
| 48 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 49 | { |
| 50 | // constant-0 + zeros |
| 51 | float pad_value = 0.f; |
| 52 | if (captured_params.at("value").type == 2) |
| 53 | pad_value = captured_params.at("value").i; |
| 54 | if (captured_params.at("value").type == 3) |
| 55 | pad_value = captured_params.at("value").f; |
| 56 | |
| 57 | if (pad_value != 0.f) |
| 58 | return false; |
| 59 | |
| 60 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 61 | for (int x : pad) |
| 62 | { |
| 63 | if (x < 0) |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (pad.size() != 2 && pad.size() != 4) |
| 68 | return false; |
| 69 | |
| 70 | if (pad.size() == 2 && pad[0] != pad[1]) |
| 71 | return false; |
| 72 | |
| 73 | if (pad.size() == 4 && (pad[0] != pad[1] || pad[2] != pad[3])) |
| 74 | return false; |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 80 | { |
| 81 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 82 | std::vector<int> padding = captured_params.at("padding").ai; |
| 83 | |
| 84 | if (pad.size() == 2) |
| 85 | { |
| 86 | padding[1] += pad[0]; |
| 87 | } |
| 88 | else if (pad.size() == 4) |
| 89 | { |
| 90 | padding[0] += pad[2]; |
| 91 | padding[1] += pad[0]; |
| 92 | } |
| 93 | |
| 94 | op->params["in_channels"] = captured_params.at("in_channels"); |
| 95 | op->params["out_channels"] = captured_params.at("out_channels"); |
| 96 | op->params["kernel_size"] = captured_params.at("kernel_size"); |
| 97 | op->params["padding_mode"] = "zeros"; |
| 98 | op->params["stride"] = captured_params.at("stride"); |
| 99 | op->params["padding"] = padding; |
| 100 | op->params["dilation"] = captured_params.at("dilation"); |
| 101 | op->params["groups"] = captured_params.at("groups"); |
| 102 | op->params["bias"] = captured_params.at("bias"); |
| 103 | |
| 104 | op->attrs["weight"] = captured_attrs.at("op_0.weight"); |
| 105 | |
| 106 | if (captured_params.at("bias").b) |
| 107 | { |
| 108 | op->attrs["bias"] = captured_attrs.at("op_0.bias"); |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | class fuse_pad_conv2d_pass_1 : public GraphRewriterPass |
| 114 | { |
| 115 | public: |
| 116 | const char* match_pattern_graph() const |
| 117 | { |
| 118 | return R"PNNXIR(7767517 |
| 119 | 4 3 |
| 120 | pnnx.Input input 0 1 input |
| 121 | F.pad op_pad 1 1 input a mode=%mode pad=%pad |
| 122 | nn.Conv2d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=* padding=(0,0) dilation=%dilation groups=%groups bias=%bias @weight @bias |
| 123 | pnnx.Output output 1 0 out |
| 124 | )PNNXIR"; |
| 125 | } |
| 126 | |
| 127 | const char* type_str() const |
| 128 | { |
| 129 | return "nn.Conv2d"; |
| 130 | } |
| 131 | |
| 132 | const char* name_str() const |
| 133 | { |
| 134 | return "padconv2d"; |
| 135 | } |
| 136 | |
| 137 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 138 | { |
| 139 | // reflect/replicate + nopad |
| 140 | if (captured_params.at("mode").s != "reflect" && captured_params.at("mode").s != "replicate") |
| 141 | return false; |
| 142 | |
| 143 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 144 | for (int x : pad) |
| 145 | { |
| 146 | if (x < 0) |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if (pad.size() != 2 && pad.size() != 4) |
| 151 | return false; |
| 152 | |
| 153 | if (pad.size() == 2 && pad[0] != pad[1]) |
| 154 | return false; |
| 155 | |
| 156 | if (pad.size() == 4 && (pad[0] != pad[1] || pad[2] != pad[3])) |
| 157 | return false; |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 163 | { |
| 164 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 165 | std::vector<int> padding(2); |
| 166 | |
| 167 | if (pad.size() == 2) |
| 168 | { |
| 169 | padding[0] = 0; |
| 170 | padding[1] = pad[0]; |
| 171 | } |
| 172 | else if (pad.size() == 4) |
| 173 | { |
| 174 | padding[0] = pad[2]; |
| 175 | padding[1] = pad[0]; |
| 176 | } |
| 177 | |
| 178 | op->params["in_channels"] = captured_params.at("in_channels"); |
| 179 | op->params["out_channels"] = captured_params.at("out_channels"); |
| 180 | op->params["kernel_size"] = captured_params.at("kernel_size"); |
| 181 | op->params["padding_mode"] = captured_params.at("mode"); |
| 182 | op->params["stride"] = captured_params.at("stride"); |
| 183 | op->params["padding"] = padding; |
| 184 | op->params["dilation"] = captured_params.at("dilation"); |
| 185 | op->params["groups"] = captured_params.at("groups"); |
| 186 | op->params["bias"] = captured_params.at("bias"); |
| 187 | |
| 188 | op->attrs["weight"] = captured_attrs.at("op_0.weight"); |
| 189 | |
| 190 | if (captured_params.at("bias").b) |
| 191 | { |
| 192 | op->attrs["bias"] = captured_attrs.at("op_0.bias"); |
| 193 | } |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | class fuse_pad_conv2d_pass_2 : public GraphRewriterPass |
| 198 | { |
| 199 | public: |
| 200 | const char* match_pattern_graph() const |
| 201 | { |
| 202 | return R"PNNXIR(7767517 |
| 203 | 4 3 |
| 204 | pnnx.Input input 0 1 input |
| 205 | nn.ConstantPad2d op_pad 1 1 input a padding=%pad value=%value |
| 206 | nn.Conv2d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=zeros padding=%padding dilation=%dilation groups=%groups bias=%bias @weight @bias |
| 207 | pnnx.Output output 1 0 out |
| 208 | )PNNXIR"; |
| 209 | } |
| 210 | |
| 211 | const char* type_str() const |
| 212 | { |
| 213 | return "nn.Conv2d"; |
| 214 | } |
| 215 | |
| 216 | const char* name_str() const |
| 217 | { |
| 218 | return "padconv2d"; |
| 219 | } |
| 220 | |
| 221 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 222 | { |
| 223 | // constant-0 + zeros |
| 224 | float pad_value = 0.f; |
| 225 | if (captured_params.at("value").type == 2) |
| 226 | pad_value = captured_params.at("value").i; |
| 227 | if (captured_params.at("value").type == 3) |
| 228 | pad_value = captured_params.at("value").f; |
| 229 | |
| 230 | if (pad_value != 0.f) |
| 231 | return false; |
| 232 | |
| 233 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 234 | for (int x : pad) |
| 235 | { |
| 236 | if (x < 0) |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | if (pad.size() != 4) |
| 241 | return false; |
| 242 | |
| 243 | if (pad[0] != pad[1] || pad[2] != pad[3]) |
| 244 | return false; |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 250 | { |
| 251 | std::vector<int> padding = captured_params.at("padding").ai; |
| 252 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 253 | padding[0] += pad[2]; |
| 254 | padding[1] += pad[0]; |
| 255 | |
| 256 | op->params["in_channels"] = captured_params.at("in_channels"); |
| 257 | op->params["out_channels"] = captured_params.at("out_channels"); |
| 258 | op->params["kernel_size"] = captured_params.at("kernel_size"); |
| 259 | op->params["padding_mode"] = "zeros"; |
| 260 | op->params["stride"] = captured_params.at("stride"); |
| 261 | op->params["padding"] = padding; |
| 262 | op->params["dilation"] = captured_params.at("dilation"); |
| 263 | op->params["groups"] = captured_params.at("groups"); |
| 264 | op->params["bias"] = captured_params.at("bias"); |
| 265 | |
| 266 | op->attrs["weight"] = captured_attrs.at("op_0.weight"); |
| 267 | |
| 268 | if (captured_params.at("bias").b) |
| 269 | { |
| 270 | op->attrs["bias"] = captured_attrs.at("op_0.bias"); |
| 271 | } |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | class fuse_pad_conv2d_pass_3 : public GraphRewriterPass |
| 276 | { |
| 277 | public: |
| 278 | const char* match_pattern_graph() const |
| 279 | { |
| 280 | return R"PNNXIR(7767517 |
| 281 | 4 3 |
| 282 | pnnx.Input input 0 1 input |
| 283 | nn.ZeroPad2d op_pad 1 1 input a padding=%pad |
| 284 | nn.Conv2d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=zeros padding=%padding dilation=%dilation groups=%groups bias=%bias @weight @bias |
| 285 | pnnx.Output output 1 0 out |
| 286 | )PNNXIR"; |
| 287 | } |
| 288 | |
| 289 | const char* type_str() const |
| 290 | { |
| 291 | return "nn.Conv2d"; |
| 292 | } |
| 293 | |
| 294 | const char* name_str() const |
| 295 | { |
| 296 | return "padconv2d"; |
| 297 | } |
| 298 | |
| 299 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 300 | { |
| 301 | // constant-0 + zeros |
| 302 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 303 | for (int x : pad) |
| 304 | { |
| 305 | if (x < 0) |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | if (pad.size() != 4) |
| 310 | return false; |
| 311 | |
| 312 | if (pad[0] != pad[1] || pad[2] != pad[3]) |
| 313 | return false; |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 319 | { |
| 320 | std::vector<int> padding = captured_params.at("padding").ai; |
| 321 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 322 | padding[0] += pad[2]; |
| 323 | padding[1] += pad[0]; |
| 324 | |
| 325 | op->params["in_channels"] = captured_params.at("in_channels"); |
| 326 | op->params["out_channels"] = captured_params.at("out_channels"); |
| 327 | op->params["kernel_size"] = captured_params.at("kernel_size"); |
| 328 | op->params["padding_mode"] = "zeros"; |
| 329 | op->params["stride"] = captured_params.at("stride"); |
| 330 | op->params["padding"] = padding; |
| 331 | op->params["dilation"] = captured_params.at("dilation"); |
| 332 | op->params["groups"] = captured_params.at("groups"); |
| 333 | op->params["bias"] = captured_params.at("bias"); |
| 334 | |
| 335 | op->attrs["weight"] = captured_attrs.at("op_0.weight"); |
| 336 | |
| 337 | if (captured_params.at("bias").b) |
| 338 | { |
| 339 | op->attrs["bias"] = captured_attrs.at("op_0.bias"); |
| 340 | } |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | class fuse_pad_conv2d_pass_4 : public GraphRewriterPass |
| 345 | { |
| 346 | public: |
| 347 | const char* match_pattern_graph() const |
| 348 | { |
| 349 | return R"PNNXIR(7767517 |
| 350 | 4 3 |
| 351 | pnnx.Input input 0 1 input |
| 352 | nn.ReplicationPad2d op_pad 1 1 input a padding=%pad |
| 353 | nn.Conv2d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=* padding=(0,0) dilation=%dilation groups=%groups bias=%bias @weight @bias |
| 354 | pnnx.Output output 1 0 out |
| 355 | )PNNXIR"; |
| 356 | } |
| 357 | |
| 358 | const char* type_str() const |
| 359 | { |
| 360 | return "nn.Conv2d"; |
| 361 | } |
| 362 | |
| 363 | const char* name_str() const |
| 364 | { |
| 365 | return "padconv2d"; |
| 366 | } |
| 367 | |
| 368 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 369 | { |
| 370 | // replicate + nopad |
| 371 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 372 | for (int x : pad) |
| 373 | { |
| 374 | if (x < 0) |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | if (pad.size() != 4) |
| 379 | return false; |
| 380 | |
| 381 | if (pad[0] != pad[1] || pad[2] != pad[3]) |
| 382 | return false; |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 388 | { |
| 389 | std::vector<int> padding(2); |
| 390 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 391 | padding[0] = pad[2]; |
| 392 | padding[1] = pad[0]; |
| 393 | |
| 394 | op->params["in_channels"] = captured_params.at("in_channels"); |
| 395 | op->params["out_channels"] = captured_params.at("out_channels"); |
| 396 | op->params["kernel_size"] = captured_params.at("kernel_size"); |
| 397 | op->params["padding_mode"] = "replicate"; |
| 398 | op->params["stride"] = captured_params.at("stride"); |
| 399 | op->params["padding"] = padding; |
| 400 | op->params["dilation"] = captured_params.at("dilation"); |
| 401 | op->params["groups"] = captured_params.at("groups"); |
| 402 | op->params["bias"] = captured_params.at("bias"); |
| 403 | |
| 404 | op->attrs["weight"] = captured_attrs.at("op_0.weight"); |
| 405 | |
| 406 | if (captured_params.at("bias").b) |
| 407 | { |
| 408 | op->attrs["bias"] = captured_attrs.at("op_0.bias"); |
| 409 | } |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | class fuse_pad_conv2d_pass_5 : public GraphRewriterPass |
| 414 | { |
| 415 | public: |
| 416 | const char* match_pattern_graph() const |
| 417 | { |
| 418 | return R"PNNXIR(7767517 |
| 419 | 4 3 |
| 420 | pnnx.Input input 0 1 input |
| 421 | nn.ReflectionPad2d op_pad 1 1 input a padding=%pad |
| 422 | nn.Conv2d op_0 1 1 a out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding_mode=* padding=(0,0) dilation=%dilation groups=%groups bias=%bias @weight @bias |
| 423 | pnnx.Output output 1 0 out |
| 424 | )PNNXIR"; |
| 425 | } |
| 426 | |
| 427 | const char* type_str() const |
| 428 | { |
| 429 | return "nn.Conv2d"; |
| 430 | } |
| 431 | |
| 432 | const char* name_str() const |
| 433 | { |
| 434 | return "padconv2d"; |
| 435 | } |
| 436 | |
| 437 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 438 | { |
| 439 | // reflect + nopad |
| 440 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 441 | for (int x : pad) |
| 442 | { |
| 443 | if (x < 0) |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | if (pad.size() != 4) |
| 448 | return false; |
| 449 | |
| 450 | if (pad[0] != pad[1] || pad[2] != pad[3]) |
| 451 | return false; |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 457 | { |
| 458 | std::vector<int> padding(2); |
| 459 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 460 | padding[0] = pad[2]; |
| 461 | padding[1] = pad[0]; |
| 462 | |
| 463 | op->params["in_channels"] = captured_params.at("in_channels"); |
| 464 | op->params["out_channels"] = captured_params.at("out_channels"); |
| 465 | op->params["kernel_size"] = captured_params.at("kernel_size"); |
| 466 | op->params["padding_mode"] = "reflect"; |
| 467 | op->params["stride"] = captured_params.at("stride"); |
| 468 | op->params["padding"] = padding; |
| 469 | op->params["dilation"] = captured_params.at("dilation"); |
| 470 | op->params["groups"] = captured_params.at("groups"); |
| 471 | op->params["bias"] = captured_params.at("bias"); |
| 472 | |
| 473 | op->attrs["weight"] = captured_attrs.at("op_0.weight"); |
| 474 | |
| 475 | if (captured_params.at("bias").b) |
| 476 | { |
| 477 | op->attrs["bias"] = captured_attrs.at("op_0.bias"); |
| 478 | } |
| 479 | } |
| 480 | }; |
| 481 | |
| 482 | void fuse_pad_conv2d(Graph& graph) |
| 483 | { |
| 484 | fuse_pad_conv2d_pass a; |
| 485 | fuse_pad_conv2d_pass_1 b; |
| 486 | fuse_pad_conv2d_pass_2 c; |
| 487 | fuse_pad_conv2d_pass_3 d; |
| 488 | fuse_pad_conv2d_pass_4 e; |
| 489 | fuse_pad_conv2d_pass_5 f; |
| 490 | int opindex = 0; |
| 491 | |
| 492 | pnnx_graph_rewrite(graph, &a, opindex); |
| 493 | pnnx_graph_rewrite(graph, &b, opindex); |
| 494 | pnnx_graph_rewrite(graph, &c, opindex); |
| 495 | pnnx_graph_rewrite(graph, &d, opindex); |
| 496 | pnnx_graph_rewrite(graph, &e, opindex); |
| 497 | pnnx_graph_rewrite(graph, &f, opindex); |
| 498 | } |
| 499 | |
| 500 | } // namespace pnnx |