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