| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 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_conv_transpose1d_onnx : 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 weight |
| 28 | pnnx.Input input_2 0 1 bias |
| 29 | ConvTranspose op_0 3 1 input weight bias out %*=%* |
| 30 | pnnx.Output output 1 0 out |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* type_str() const |
| 35 | { |
| 36 | return "F.conv_transpose1d"; |
| 37 | } |
| 38 | |
| 39 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 40 | { |
| 41 | if (captured_params.find("op_0.kernel_shape") != captured_params.end()) |
| 42 | { |
| 43 | if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 1) |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | if (captured_params.find("op_0.dilations") != captured_params.end()) |
| 48 | { |
| 49 | if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 1) |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | if (captured_params.find("op_0.strides") != captured_params.end()) |
| 54 | { |
| 55 | if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 1) |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | if (captured_params.find("op_0.output_padding") != captured_params.end()) |
| 60 | { |
| 61 | if (captured_params.at("op_0.output_padding").type != 5 || captured_params.at("op_0.output_padding").ai.size() != 1) |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (captured_params.find("op_0.pads") != captured_params.end()) |
| 66 | { |
| 67 | if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 2) |
| 68 | return false; |
| 69 | |
| 70 | const std::vector<int>& pads = captured_params.at("op_0.pads").ai; |
| 71 | if (pads[0] != pads[1]) |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 79 | { |
| 80 | if (captured_params.find("op_0.dilations") != captured_params.end()) |
| 81 | { |
| 82 | op->params["dilation"] = captured_params.at("op_0.dilations"); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | op->params["dilation"] = {1}; |
| 87 | } |
| 88 | |
| 89 | if (captured_params.find("op_0.strides") != captured_params.end()) |
| 90 | { |
| 91 | op->params["stride"] = captured_params.at("op_0.strides"); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | op->params["stride"] = {1}; |
| 96 | } |
| 97 | |
| 98 | if (captured_params.find("op_0.output_padding") != captured_params.end()) |
| 99 | { |
| 100 | op->params["output_padding"] = captured_params.at("op_0.output_padding"); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | op->params["output_padding"] = {0}; |
| 105 | } |
| 106 | |
| 107 | if (captured_params.find("op_0.pads") != captured_params.end()) |
| 108 | { |
| 109 | const std::vector<int>& pads = captured_params.at("op_0.pads").ai; |
| 110 | op->params["padding"] = {pads[0]}; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | op->params["padding"] = {0}; |
| 115 | } |
| 116 | |
| 117 | if (captured_params.find("op_0.group") != captured_params.end()) |
| 118 | { |
| 119 | op->params["groups"] = captured_params.at("op_0.group"); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | op->params["groups"] = 1; |
| 124 | } |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv_transpose1d_onnx, 10) |
| 129 | |
| 130 | class F_conv_transpose1d_onnx_1 : public F_conv_transpose1d_onnx |
| 131 | { |
| 132 | public: |
| 133 | const char* match_pattern_graph() const |
| 134 | { |
| 135 | return R"PNNXIR(7767517 |
| 136 | 4 3 |
| 137 | pnnx.Input input_0 0 1 input |
| 138 | pnnx.Input input_1 0 1 weight |
| 139 | ConvTranspose op_0 2 1 input weight out %*=%* |
| 140 | pnnx.Output output 1 0 out |
| 141 | )PNNXIR"; |
| 142 | } |
| 143 | |
| 144 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 145 | { |
| 146 | F_conv_transpose1d_onnx::write(op, captured_params); |
| 147 | |
| 148 | op->params["bias"] = Parameter(); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv_transpose1d_onnx_1, 10) |
| 153 | |
| 154 | } // namespace pnnx |