| 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_ncnn.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | class F_conv_transpose2d_4 : 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 1 input |
| 29 | pnnx.Input weight 0 1 weight |
| 30 | F.conv_transpose2d op_0 2 1 input weight out bias=None stride=%stride output_padding=%output_padding padding=%padding dilation=%dilation groups=1 |
| 31 | pnnx.Output output 1 0 out |
| 32 | )PNNXIR"; |
| 33 | } |
| 34 | |
| 35 | const char* type_str() const |
| 36 | { |
| 37 | return "Deconvolution"; |
| 38 | } |
| 39 | |
| 40 | const char* name_str() const |
| 41 | { |
| 42 | return "deconv2d"; |
| 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 | std::vector<int> weight_shape = op->inputs[1]->shape; |
| 48 | if (weight_shape.empty()) |
| 49 | { |
| 50 | weight_shape = {0, 0, 0, 0}; |
| 51 | } |
| 52 | |
| 53 | op->params["0"] = weight_shape[1]; |
| 54 | op->params["1"] = weight_shape[3]; |
| 55 | op->params["11"] = weight_shape[2]; |
| 56 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 57 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 58 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 59 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 60 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 61 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 62 | op->params["18"] = captured_params.at("output_padding").ai[1]; |
| 63 | op->params["19"] = captured_params.at("output_padding").ai[0]; |
| 64 | op->params["5"] = 0; |
| 65 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 66 | op->params["28"] = 1; // dynamic weight |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv_transpose2d_4, 22) |
| 71 | |
| 72 | class F_conv_transpose2d_5 : public GraphRewriterPass |
| 73 | { |
| 74 | public: |
| 75 | const char* match_pattern_graph() const |
| 76 | { |
| 77 | return R"PNNXIR(7767517 |
| 78 | 5 4 |
| 79 | pnnx.Input input 0 1 input |
| 80 | pnnx.Input weight 0 1 weight |
| 81 | pnnx.Input bias 0 1 bias |
| 82 | F.conv_transpose2d op_0 3 1 input weight bias out stride=%stride output_padding=%output_padding padding=%padding dilation=%dilation groups=1 |
| 83 | pnnx.Output output 1 0 out |
| 84 | )PNNXIR"; |
| 85 | } |
| 86 | |
| 87 | const char* type_str() const |
| 88 | { |
| 89 | return "Deconvolution"; |
| 90 | } |
| 91 | |
| 92 | const char* name_str() const |
| 93 | { |
| 94 | return "deconv2d"; |
| 95 | } |
| 96 | |
| 97 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 98 | { |
| 99 | std::vector<int> weight_shape = op->inputs[1]->shape; |
| 100 | if (weight_shape.empty()) |
| 101 | { |
| 102 | weight_shape = {0, 0, 0, 0}; |
| 103 | } |
| 104 | |
| 105 | op->params["0"] = weight_shape[1]; |
| 106 | op->params["1"] = weight_shape[3]; |
| 107 | op->params["11"] = weight_shape[2]; |
| 108 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 109 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 110 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 111 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 112 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 113 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 114 | op->params["18"] = captured_params.at("output_padding").ai[1]; |
| 115 | op->params["19"] = captured_params.at("output_padding").ai[0]; |
| 116 | op->params["5"] = 1; |
| 117 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 118 | op->params["28"] = 1; // dynamic weight |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv_transpose2d_5, 22) |
| 123 | |
| 124 | class F_conv_transpose2d_6 : public GraphRewriterPass |
| 125 | { |
| 126 | public: |
| 127 | const char* match_pattern_graph() const |
| 128 | { |
| 129 | return R"PNNXIR(7767517 |
| 130 | 4 3 |
| 131 | pnnx.Input input 0 1 input |
| 132 | pnnx.Input weight 0 1 weight |
| 133 | F.conv_transpose2d op_0 2 1 input weight out bias=None stride=%stride output_padding=%output_padding padding=%padding dilation=%dilation groups=%groups |
| 134 | pnnx.Output output 1 0 out |
| 135 | )PNNXIR"; |
| 136 | } |
| 137 | |
| 138 | const char* type_str() const |
| 139 | { |
| 140 | return "DeconvolutionDepthWise"; |
| 141 | } |
| 142 | |
| 143 | const char* name_str() const |
| 144 | { |
| 145 | return "deconvdw2d"; |
| 146 | } |
| 147 | |
| 148 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 149 | { |
| 150 | std::vector<int> weight_shape = op->inputs[1]->shape; |
| 151 | if (weight_shape.empty()) |
| 152 | { |
| 153 | weight_shape = {0, 0, 0, 0}; |
| 154 | } |
| 155 | |
| 156 | op->params["0"] = weight_shape[1] * captured_params.at("groups").i; |
| 157 | op->params["1"] = weight_shape[3]; |
| 158 | op->params["11"] = weight_shape[2]; |
| 159 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 160 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 161 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 162 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 163 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 164 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 165 | op->params["18"] = captured_params.at("output_padding").ai[1]; |
| 166 | op->params["19"] = captured_params.at("output_padding").ai[0]; |
| 167 | op->params["5"] = 0; |
| 168 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 169 | op->params["7"] = captured_params.at("groups"); |
| 170 | op->params["28"] = 1; // dynamic weight |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv_transpose2d_6, 23) |
| 175 | |
| 176 | class F_conv_transpose2d_7 : public GraphRewriterPass |
| 177 | { |
| 178 | public: |
| 179 | const char* match_pattern_graph() const |
| 180 | { |
| 181 | return R"PNNXIR(7767517 |
| 182 | 5 4 |
| 183 | pnnx.Input input 0 1 input |
| 184 | pnnx.Input weight 0 1 weight |
| 185 | pnnx.Input bias 0 1 bias |
| 186 | F.conv_transpose2d op_0 3 1 input weight bias out stride=%stride output_padding=%output_padding padding=%padding dilation=%dilation groups=%groups |
| 187 | pnnx.Output output 1 0 out |
| 188 | )PNNXIR"; |
| 189 | } |
| 190 | |
| 191 | const char* type_str() const |
| 192 | { |
| 193 | return "DeconvolutionDepthWise"; |
| 194 | } |
| 195 | |
| 196 | const char* name_str() const |
| 197 | { |
| 198 | return "deconvdw2d"; |
| 199 | } |
| 200 | |
| 201 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 202 | { |
| 203 | std::vector<int> weight_shape = op->inputs[1]->shape; |
| 204 | if (weight_shape.empty()) |
| 205 | { |
| 206 | weight_shape = {0, 0, 0, 0}; |
| 207 | } |
| 208 | |
| 209 | op->params["0"] = weight_shape[1] * captured_params.at("groups").i; |
| 210 | op->params["1"] = weight_shape[3]; |
| 211 | op->params["11"] = weight_shape[2]; |
| 212 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 213 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 214 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 215 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 216 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 217 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 218 | op->params["18"] = captured_params.at("output_padding").ai[1]; |
| 219 | op->params["19"] = captured_params.at("output_padding").ai[0]; |
| 220 | op->params["5"] = 1; |
| 221 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 222 | op->params["7"] = captured_params.at("groups"); |
| 223 | op->params["28"] = 1; // dynamic weight |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv_transpose2d_7, 23) |
| 228 | |
| 229 | } // namespace ncnn |
| 230 | |
| 231 | } // namespace pnnx |