| 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_level2.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | class F_conv1d : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 16 15 |
| 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 | pnnx.Input input_3 0 1 stride |
| 30 | pnnx.Input input_4 0 1 padding |
| 31 | pnnx.Input input_5 0 1 dilation |
| 32 | pnnx.Input input_6 0 1 groups |
| 33 | prim::Constant op_0 0 1 transposed value=False |
| 34 | prim::Constant op_1 0 1 output_padding_w value=0 |
| 35 | prim::ListConstruct op_2 1 1 output_padding_w output_padding |
| 36 | prim::Constant op_3 0 1 benchmark value=* |
| 37 | prim::Constant op_4 0 1 deterministic value=* |
| 38 | prim::Constant op_5 0 1 cudnn_enabled value=* |
| 39 | prim::Constant op_6 0 1 allow_tf32 value=* |
| 40 | aten::_convolution op_7 13 1 input weight bias stride padding dilation transposed output_padding groups benchmark deterministic cudnn_enabled allow_tf32 out |
| 41 | pnnx.Output output 1 0 out |
| 42 | )PNNXIR"; |
| 43 | } |
| 44 | |
| 45 | const char* type_str() const |
| 46 | { |
| 47 | return "F.conv1d"; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv1d, 10) |
| 52 | |
| 53 | class F_convmode : public GraphRewriterPass |
| 54 | { |
| 55 | public: |
| 56 | const char* match_pattern_graph() const |
| 57 | { |
| 58 | return R"PNNXIR(7767517 |
| 59 | 9 8 |
| 60 | pnnx.Input input_0 0 1 input |
| 61 | pnnx.Input input_1 0 1 weight |
| 62 | pnnx.Input input_2 0 1 bias |
| 63 | pnnx.Input input_3 0 1 stride |
| 64 | pnnx.Input input_4 0 1 padding |
| 65 | pnnx.Input input_5 0 1 dilation |
| 66 | pnnx.Input input_6 0 1 groups |
| 67 | aten::_convolution_mode op_0 7 1 input weight bias stride padding dilation groups out |
| 68 | pnnx.Output output 1 0 out |
| 69 | )PNNXIR"; |
| 70 | } |
| 71 | |
| 72 | const char* type_str() const |
| 73 | { |
| 74 | return "F.convmode"; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_convmode, 10) |
| 79 | |
| 80 | class F_conv1d_onnx : 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 0 1 input |
| 88 | pnnx.Input input_1 0 1 weight |
| 89 | pnnx.Input input_2 0 1 bias |
| 90 | Conv op_0 3 1 input weight bias out %*=%* |
| 91 | pnnx.Output output 1 0 out |
| 92 | )PNNXIR"; |
| 93 | } |
| 94 | |
| 95 | const char* type_str() const |
| 96 | { |
| 97 | return "F.conv1d"; |
| 98 | } |
| 99 | |
| 100 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 101 | { |
| 102 | if (captured_params.find("op_0.kernel_shape") != captured_params.end()) |
| 103 | { |
| 104 | if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 1) |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if (captured_params.find("op_0.dilations") != captured_params.end()) |
| 109 | { |
| 110 | if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 1) |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | if (captured_params.find("op_0.strides") != captured_params.end()) |
| 115 | { |
| 116 | if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 1) |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (captured_params.find("op_0.pads") != captured_params.end()) |
| 121 | { |
| 122 | if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 2) |
| 123 | return false; |
| 124 | |
| 125 | const std::vector<int>& pads = captured_params.at("op_0.pads").ai; |
| 126 | if (pads[0] != pads[1]) |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 134 | { |
| 135 | if (captured_params.find("op_0.dilations") != captured_params.end()) |
| 136 | { |
| 137 | op->params["dilation"] = captured_params.at("op_0.dilations"); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | op->params["dilation"] = {1}; |
| 142 | } |
| 143 | |
| 144 | if (captured_params.find("op_0.strides") != captured_params.end()) |
| 145 | { |
| 146 | op->params["stride"] = captured_params.at("op_0.strides"); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | op->params["stride"] = {1}; |
| 151 | } |
| 152 | |
| 153 | if (captured_params.find("op_0.pads") != captured_params.end()) |
| 154 | { |
| 155 | const std::vector<int>& pads = captured_params.at("op_0.pads").ai; |
| 156 | op->params["padding"] = {pads[0]}; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | op->params["padding"] = {0}; |
| 161 | } |
| 162 | |
| 163 | if (captured_params.find("op_0.group") != captured_params.end()) |
| 164 | { |
| 165 | op->params["groups"] = captured_params.at("op_0.group"); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | op->params["groups"] = 1; |
| 170 | } |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv1d_onnx, 10) |
| 175 | |
| 176 | class F_conv1d_onnx_1 : public F_conv1d_onnx |
| 177 | { |
| 178 | public: |
| 179 | const char* match_pattern_graph() const |
| 180 | { |
| 181 | return R"PNNXIR(7767517 |
| 182 | 4 3 |
| 183 | pnnx.Input input_0 0 1 input |
| 184 | pnnx.Input input_1 0 1 weight |
| 185 | Conv op_0 2 1 input weight out %*=%* |
| 186 | pnnx.Output output 1 0 out |
| 187 | )PNNXIR"; |
| 188 | } |
| 189 | |
| 190 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 191 | { |
| 192 | F_conv1d_onnx::write(op, captured_params); |
| 193 | |
| 194 | op->params["bias"] = Parameter(); |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv1d_onnx_1, 10) |
| 199 | |
| 200 | } // namespace pnnx |