| 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_conv2d_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.conv2d op_0 2 1 input weight out bias=None stride=%stride 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 "Convolution"; |
| 38 | } |
| 39 | |
| 40 | const char* name_str() const |
| 41 | { |
| 42 | return "conv2d"; |
| 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[0]; |
| 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 | if (captured_params.at("padding").type == 4) |
| 61 | { |
| 62 | if (captured_params.at("padding").s == "same") |
| 63 | op->params["4"] = -233; |
| 64 | else if (captured_params.at("padding").s == "valid") |
| 65 | op->params["4"] = 0; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 70 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 71 | } |
| 72 | op->params["5"] = 0; |
| 73 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 74 | op->params["19"] = 1; // dynamic weight |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv2d_4, 22) |
| 79 | |
| 80 | class F_conv2d_5 : public GraphRewriterPass |
| 81 | { |
| 82 | public: |
| 83 | const char* match_pattern_graph() const |
| 84 | { |
| 85 | return R"PNNXIR(7767517 |
| 86 | 5 4 |
| 87 | pnnx.Input input 0 1 input |
| 88 | pnnx.Input weight 0 1 weight |
| 89 | pnnx.Input bias 0 1 bias |
| 90 | F.conv2d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation groups=1 |
| 91 | pnnx.Output output 1 0 out |
| 92 | )PNNXIR"; |
| 93 | } |
| 94 | |
| 95 | const char* type_str() const |
| 96 | { |
| 97 | return "Convolution"; |
| 98 | } |
| 99 | |
| 100 | const char* name_str() const |
| 101 | { |
| 102 | return "conv2d"; |
| 103 | } |
| 104 | |
| 105 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 106 | { |
| 107 | std::vector<int> weight_shape = op->inputs[1]->shape; |
| 108 | if (weight_shape.empty()) |
| 109 | { |
| 110 | weight_shape = {0, 0, 0, 0}; |
| 111 | } |
| 112 | |
| 113 | op->params["0"] = weight_shape[0]; |
| 114 | op->params["1"] = weight_shape[3]; |
| 115 | op->params["11"] = weight_shape[2]; |
| 116 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 117 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 118 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 119 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 120 | if (captured_params.at("padding").type == 4) |
| 121 | { |
| 122 | if (captured_params.at("padding").s == "same") |
| 123 | op->params["4"] = -233; |
| 124 | else if (captured_params.at("padding").s == "valid") |
| 125 | op->params["4"] = 0; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 130 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 131 | } |
| 132 | op->params["5"] = 1; |
| 133 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 134 | op->params["19"] = 1; // dynamic weight |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv2d_5, 22) |
| 139 | |
| 140 | class F_conv2d_6 : public GraphRewriterPass |
| 141 | { |
| 142 | public: |
| 143 | const char* match_pattern_graph() const |
| 144 | { |
| 145 | return R"PNNXIR(7767517 |
| 146 | 4 3 |
| 147 | pnnx.Input input 0 1 input |
| 148 | pnnx.Input weight 0 1 weight |
| 149 | F.conv2d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 150 | pnnx.Output output 1 0 out |
| 151 | )PNNXIR"; |
| 152 | } |
| 153 | |
| 154 | const char* type_str() const |
| 155 | { |
| 156 | return "ConvolutionDepthWise"; |
| 157 | } |
| 158 | |
| 159 | const char* name_str() const |
| 160 | { |
| 161 | return "convdw2d"; |
| 162 | } |
| 163 | |
| 164 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 165 | { |
| 166 | std::vector<int> weight_shape = op->inputs[1]->shape; |
| 167 | if (weight_shape.empty()) |
| 168 | { |
| 169 | weight_shape = {0, 0, 0, 0}; |
| 170 | } |
| 171 | |
| 172 | op->params["0"] = weight_shape[0]; |
| 173 | op->params["1"] = weight_shape[3]; |
| 174 | op->params["11"] = weight_shape[2]; |
| 175 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 176 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 177 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 178 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 179 | if (captured_params.at("padding").type == 4) |
| 180 | { |
| 181 | if (captured_params.at("padding").s == "same") |
| 182 | op->params["4"] = -233; |
| 183 | else if (captured_params.at("padding").s == "valid") |
| 184 | op->params["4"] = 0; |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 189 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 190 | } |
| 191 | op->params["5"] = 0; |
| 192 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 193 | op->params["7"] = captured_params.at("groups"); |
| 194 | op->params["19"] = 1; // dynamic weight |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv2d_6, 23) |
| 199 | |
| 200 | class F_conv2d_7 : public GraphRewriterPass |
| 201 | { |
| 202 | public: |
| 203 | const char* match_pattern_graph() const |
| 204 | { |
| 205 | return R"PNNXIR(7767517 |
| 206 | 5 4 |
| 207 | pnnx.Input input 0 1 input |
| 208 | pnnx.Input weight 0 1 weight |
| 209 | pnnx.Input bias 0 1 bias |
| 210 | F.conv2d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 211 | pnnx.Output output 1 0 out |
| 212 | )PNNXIR"; |
| 213 | } |
| 214 | |
| 215 | const char* type_str() const |
| 216 | { |
| 217 | return "ConvolutionDepthWise"; |
| 218 | } |
| 219 | |
| 220 | const char* name_str() const |
| 221 | { |
| 222 | return "convdw2d"; |
| 223 | } |
| 224 | |
| 225 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 226 | { |
| 227 | std::vector<int> weight_shape = op->inputs[1]->shape; |
| 228 | if (weight_shape.empty()) |
| 229 | { |
| 230 | weight_shape = {0, 0, 0, 0}; |
| 231 | } |
| 232 | |
| 233 | op->params["0"] = weight_shape[0]; |
| 234 | op->params["1"] = weight_shape[3]; |
| 235 | op->params["11"] = weight_shape[2]; |
| 236 | op->params["2"] = captured_params.at("dilation").ai[1]; |
| 237 | op->params["12"] = captured_params.at("dilation").ai[0]; |
| 238 | op->params["3"] = captured_params.at("stride").ai[1]; |
| 239 | op->params["13"] = captured_params.at("stride").ai[0]; |
| 240 | if (captured_params.at("padding").type == 4) |
| 241 | { |
| 242 | if (captured_params.at("padding").s == "same") |
| 243 | op->params["4"] = -233; |
| 244 | else if (captured_params.at("padding").s == "valid") |
| 245 | op->params["4"] = 0; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | op->params["4"] = captured_params.at("padding").ai[1]; |
| 250 | op->params["14"] = captured_params.at("padding").ai[0]; |
| 251 | } |
| 252 | op->params["5"] = 1; |
| 253 | op->params["6"] = (int)(weight_shape[0] * weight_shape[1] * weight_shape[2] * weight_shape[3]); |
| 254 | op->params["7"] = captured_params.at("groups"); |
| 255 | op->params["19"] = 1; // dynamic weight |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_conv2d_7, 23) |
| 260 | |
| 261 | } // namespace ncnn |
| 262 | |
| 263 | } // namespace pnnx |