| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "fuse_static_convtranspose.h" |
| 16 | |
| 17 | #include "pass_level2.h" |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | namespace pnnx { |
| 23 | |
| 24 | class fuse_static_Fconvtranspose1d_pass : public GraphRewriterPass |
| 25 | { |
| 26 | public: |
| 27 | const char* match_pattern_graph() const |
| 28 | { |
| 29 | return R"PNNXIR(7767517 |
| 30 | 4 3 |
| 31 | pnnx.Input input 0 1 input |
| 32 | pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kw)f32 |
| 33 | F.conv_transpose1d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups |
| 34 | pnnx.Output output 1 0 out |
| 35 | )PNNXIR"; |
| 36 | } |
| 37 | |
| 38 | const char* replace_pattern_graph() const |
| 39 | { |
| 40 | return R"PNNXIR(7767517 |
| 41 | 3 2 |
| 42 | pnnx.Input input 0 1 input |
| 43 | nn.ConvTranspose1d conv_transpose1d 1 1 input out in_channels=%in_channels kernel_size=(%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data |
| 44 | pnnx.Output output 1 0 out |
| 45 | )PNNXIR"; |
| 46 | } |
| 47 | |
| 48 | void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 49 | { |
| 50 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 51 | |
| 52 | const int out_channels_per_group = captured_params.at("out_channels_per_group").i; |
| 53 | const int groups = captured_params.at("groups").i; |
| 54 | |
| 55 | ops.at("conv_transpose1d")->params["out_channels"] = out_channels_per_group * groups; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | class fuse_static_Fconvtranspose1d_pass_2 : public GraphRewriterPass |
| 60 | { |
| 61 | public: |
| 62 | const char* match_pattern_graph() const |
| 63 | { |
| 64 | return R"PNNXIR(7767517 |
| 65 | 5 4 |
| 66 | pnnx.Input input 0 1 input |
| 67 | pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kw)f32 |
| 68 | pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32 |
| 69 | F.conv_transpose1d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups |
| 70 | pnnx.Output output 1 0 out |
| 71 | )PNNXIR"; |
| 72 | } |
| 73 | |
| 74 | const char* replace_pattern_graph() const |
| 75 | { |
| 76 | return R"PNNXIR(7767517 |
| 77 | 3 2 |
| 78 | pnnx.Input input 0 1 input |
| 79 | nn.ConvTranspose1d conv_transpose1d 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=(%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data |
| 80 | pnnx.Output output 1 0 out |
| 81 | )PNNXIR"; |
| 82 | } |
| 83 | |
| 84 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 85 | { |
| 86 | const int out_channels_per_group = captured_params.at("out_channels_per_group").i; |
| 87 | const int out_channels = captured_params.at("out_channels").i; |
| 88 | const int groups = captured_params.at("groups").i; |
| 89 | |
| 90 | if (out_channels != out_channels_per_group * groups) |
| 91 | return false; |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | class fuse_static_Fconvtranspose2d_pass : public GraphRewriterPass |
| 98 | { |
| 99 | public: |
| 100 | const char* match_pattern_graph() const |
| 101 | { |
| 102 | return R"PNNXIR(7767517 |
| 103 | 4 3 |
| 104 | pnnx.Input input 0 1 input |
| 105 | pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kh,%kw)f32 |
| 106 | F.conv_transpose2d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups |
| 107 | pnnx.Output output 1 0 out |
| 108 | )PNNXIR"; |
| 109 | } |
| 110 | |
| 111 | const char* replace_pattern_graph() const |
| 112 | { |
| 113 | return R"PNNXIR(7767517 |
| 114 | 3 2 |
| 115 | pnnx.Input input 0 1 input |
| 116 | nn.ConvTranspose2d conv_transpose2d 1 1 input out in_channels=%in_channels kernel_size=(%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data |
| 117 | pnnx.Output output 1 0 out |
| 118 | )PNNXIR"; |
| 119 | } |
| 120 | |
| 121 | void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 122 | { |
| 123 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 124 | |
| 125 | const int out_channels_per_group = captured_params.at("out_channels_per_group").i; |
| 126 | const int groups = captured_params.at("groups").i; |
| 127 | |
| 128 | ops.at("conv_transpose2d")->params["out_channels"] = out_channels_per_group * groups; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | class fuse_static_Fconvtranspose2d_pass_2 : public GraphRewriterPass |
| 133 | { |
| 134 | public: |
| 135 | const char* match_pattern_graph() const |
| 136 | { |
| 137 | return R"PNNXIR(7767517 |
| 138 | 5 4 |
| 139 | pnnx.Input input 0 1 input |
| 140 | pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kh,%kw)f32 |
| 141 | pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32 |
| 142 | F.conv_transpose2d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups |
| 143 | pnnx.Output output 1 0 out |
| 144 | )PNNXIR"; |
| 145 | } |
| 146 | |
| 147 | const char* replace_pattern_graph() const |
| 148 | { |
| 149 | return R"PNNXIR(7767517 |
| 150 | 3 2 |
| 151 | pnnx.Input input 0 1 input |
| 152 | nn.ConvTranspose2d conv_transpose2d 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=(%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data |
| 153 | pnnx.Output output 1 0 out |
| 154 | )PNNXIR"; |
| 155 | } |
| 156 | |
| 157 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 158 | { |
| 159 | const int out_channels_per_group = captured_params.at("out_channels_per_group").i; |
| 160 | const int out_channels = captured_params.at("out_channels").i; |
| 161 | const int groups = captured_params.at("groups").i; |
| 162 | |
| 163 | if (out_channels != out_channels_per_group * groups) |
| 164 | return false; |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | class fuse_static_Fconvtranspose3d_pass : public GraphRewriterPass |
| 171 | { |
| 172 | public: |
| 173 | const char* match_pattern_graph() const |
| 174 | { |
| 175 | return R"PNNXIR(7767517 |
| 176 | 4 3 |
| 177 | pnnx.Input input 0 1 input |
| 178 | pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kd,%kh,%kw)f32 |
| 179 | F.conv_transpose3d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups |
| 180 | pnnx.Output output 1 0 out |
| 181 | )PNNXIR"; |
| 182 | } |
| 183 | |
| 184 | const char* replace_pattern_graph() const |
| 185 | { |
| 186 | return R"PNNXIR(7767517 |
| 187 | 3 2 |
| 188 | pnnx.Input input 0 1 input |
| 189 | nn.ConvTranspose3d conv_transpose3d 1 1 input out in_channels=%in_channels kernel_size=(%kd,%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data |
| 190 | pnnx.Output output 1 0 out |
| 191 | )PNNXIR"; |
| 192 | } |
| 193 | |
| 194 | void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 195 | { |
| 196 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 197 | |
| 198 | const int out_channels_per_group = captured_params.at("out_channels_per_group").i; |
| 199 | const int groups = captured_params.at("groups").i; |
| 200 | |
| 201 | ops.at("conv_transpose3d")->params["out_channels"] = out_channels_per_group * groups; |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | class fuse_static_Fconvtranspose3d_pass_2 : public GraphRewriterPass |
| 206 | { |
| 207 | public: |
| 208 | const char* match_pattern_graph() const |
| 209 | { |
| 210 | return R"PNNXIR(7767517 |
| 211 | 5 4 |
| 212 | pnnx.Input input 0 1 input |
| 213 | pnnx.Attribute op_weight 0 1 weight @data=(%in_channels,%out_channels_per_group,%kd,%kh,%kw)f32 |
| 214 | pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32 |
| 215 | F.conv_transpose3d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups |
| 216 | pnnx.Output output 1 0 out |
| 217 | )PNNXIR"; |
| 218 | } |
| 219 | |
| 220 | const char* replace_pattern_graph() const |
| 221 | { |
| 222 | return R"PNNXIR(7767517 |
| 223 | 3 2 |
| 224 | pnnx.Input input 0 1 input |
| 225 | nn.ConvTranspose3d conv_transpose3d 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=(%kd,%kh,%kw) stride=%stride padding=%padding output_padding=%output_padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data |
| 226 | pnnx.Output output 1 0 out |
| 227 | )PNNXIR"; |
| 228 | } |
| 229 | |
| 230 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 231 | { |
| 232 | const int out_channels_per_group = captured_params.at("out_channels_per_group").i; |
| 233 | const int out_channels = captured_params.at("out_channels").i; |
| 234 | const int groups = captured_params.at("groups").i; |
| 235 | |
| 236 | if (out_channels != out_channels_per_group * groups) |
| 237 | return false; |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | void fuse_static_convtranspose(Graph& graph) |
| 244 | { |
| 245 | fuse_static_Fconvtranspose1d_pass a; |
| 246 | fuse_static_Fconvtranspose1d_pass_2 b; |
| 247 | fuse_static_Fconvtranspose2d_pass c; |
| 248 | fuse_static_Fconvtranspose2d_pass_2 d; |
| 249 | fuse_static_Fconvtranspose3d_pass e; |
| 250 | fuse_static_Fconvtranspose3d_pass_2 f; |
| 251 | int opindex = 0; |
| 252 | |
| 253 | pnnx_graph_rewrite(graph, &a, opindex); |
| 254 | pnnx_graph_rewrite(graph, &b, opindex); |
| 255 | pnnx_graph_rewrite(graph, &c, opindex); |
| 256 | pnnx_graph_rewrite(graph, &d, opindex); |
| 257 | pnnx_graph_rewrite(graph, &e, opindex); |
| 258 | pnnx_graph_rewrite(graph, &f, opindex); |
| 259 | } |
| 260 | |
| 261 | } // namespace pnnx |