| 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 "pass_ncnn.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | class torchvision_DeformConv2d : public GraphRewriterPass |
| 22 | { |
| 23 | public: |
| 24 | const char* match_pattern_graph() const |
| 25 | { |
| 26 | return R"PNNXIR(7767517 |
| 27 | 4 3 |
| 28 | pnnx.Input input_0 0 1 input |
| 29 | pnnx.Input input_1 0 1 offset |
| 30 | torchvision.ops.DeformConv2d op_0 2 1 input offset out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding=%padding dilation=%dilation groups=1 bias=%bias @weight @bias |
| 31 | pnnx.Output output 1 0 out |
| 32 | )PNNXIR"; |
| 33 | } |
| 34 | |
| 35 | const char* type_str() const |
| 36 | { |
| 37 | return "DeformableConv2D"; |
| 38 | } |
| 39 | |
| 40 | const char* name_str() const |
| 41 | { |
| 42 | return "deformconv2d"; |
| 43 | } |
| 44 | |
| 45 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 46 | { |
| 47 | op->params["0"] = captured_params.at("out_channels"); |
| 48 | op->params["1"] = captured_params.at("kernel_size").ai[1]; |
| 49 | op->params["11"] = captured_params.at("kernel_size").ai[0]; |
| 50 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 51 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 52 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 53 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 54 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 55 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 56 | op->params["5"] = captured_params.at("bias").b ? 1 : 0; |
| 57 | op->params["6"] = captured_attrs.at("op_0.weight").elemcount(); |
| 58 | |
| 59 | op->attrs["0"] = Attribute(); |
| 60 | op->attrs["0"].data = {0, 0, 0, 0}; |
| 61 | op->attrs["1"] = captured_attrs.at("op_0.weight"); |
| 62 | if (captured_params.at("bias").b) |
| 63 | op->attrs["2"] = captured_attrs.at("op_0.bias"); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | class torchvision_DeformConv2d_1 : public GraphRewriterPass |
| 68 | { |
| 69 | public: |
| 70 | const char* match_pattern_graph() const |
| 71 | { |
| 72 | return R"PNNXIR(7767517 |
| 73 | 5 4 |
| 74 | pnnx.Input input_0 0 1 input |
| 75 | pnnx.Input input_1 0 1 offset |
| 76 | pnnx.Input input_2 0 1 mask |
| 77 | torchvision.ops.DeformConv2d op_0 3 1 input offset mask out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding=%padding dilation=%dilation groups=1 bias=%bias @weight @bias |
| 78 | pnnx.Output output 1 0 out |
| 79 | )PNNXIR"; |
| 80 | } |
| 81 | |
| 82 | const char* type_str() const |
| 83 | { |
| 84 | return "DeformableConv2D"; |
| 85 | } |
| 86 | |
| 87 | const char* name_str() const |
| 88 | { |
| 89 | return "deformconv2d"; |
| 90 | } |
| 91 | |
| 92 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 93 | { |
| 94 | op->params["0"] = captured_params.at("out_channels"); |
| 95 | op->params["1"] = captured_params.at("kernel_size").ai[1]; |
| 96 | op->params["11"] = captured_params.at("kernel_size").ai[0]; |
| 97 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 98 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 99 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 100 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 101 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 102 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 103 | op->params["5"] = captured_params.at("bias").b ? 1 : 0; |
| 104 | op->params["6"] = captured_attrs.at("op_0.weight").elemcount(); |
| 105 | |
| 106 | op->attrs["0"] = Attribute(); |
| 107 | op->attrs["0"].data = {0, 0, 0, 0}; |
| 108 | op->attrs["1"] = captured_attrs.at("op_0.weight"); |
| 109 | if (captured_params.at("bias").b) |
| 110 | op->attrs["2"] = captured_attrs.at("op_0.bias"); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torchvision_DeformConv2d, 20) |
| 115 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torchvision_DeformConv2d_1, 20) |
| 116 | |
| 117 | } // namespace ncnn |
| 118 | |
| 119 | } // namespace pnnx |