| 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_conv.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_Fconv1d_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=(%out_channels,%in_channels_per_group,%kw)f32 |
| 33 | F.conv1d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation 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.Conv1d conv1d 1 1 input out out_channels=%out_channels kernel_size=(%kw) padding_mode=zeros stride=%stride padding=%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 in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 53 | const int groups = captured_params.at("groups").i; |
| 54 | |
| 55 | ops.at("conv1d")->params["in_channels"] = in_channels_per_group * groups; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | class fuse_static_Fconv1d_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=(%out_channels,%in_channels_per_group,%kw)f32 |
| 68 | pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32 |
| 69 | F.conv1d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation 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.Conv1d conv1d 1 1 input out out_channels=%out_channels kernel_size=(%kw) padding_mode=zeros stride=%stride padding=%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 | 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 |
| 85 | { |
| 86 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 87 | |
| 88 | const int in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 89 | const int groups = captured_params.at("groups").i; |
| 90 | |
| 91 | ops.at("conv1d")->params["in_channels"] = in_channels_per_group * groups; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | class fuse_static_Fconv1d_pass_3 : public GraphRewriterPass |
| 96 | { |
| 97 | public: |
| 98 | const char* match_pattern_graph() const |
| 99 | { |
| 100 | return R"PNNXIR(7767517 |
| 101 | 6 5 |
| 102 | pnnx.Input input 0 1 input |
| 103 | pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kw)f32 |
| 104 | pnnx.Attribute op_bias 0 1 bias @data=(1,%out_channels,1)f32 |
| 105 | F.conv1d op_0 2 1 input weight a bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 106 | pnnx.Expression op_1 2 1 a bias out expr=add(@0,@1) |
| 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.Conv1d conv1d 1 1 input out out_channels=%out_channels kernel_size=(%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.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 in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 126 | const int groups = captured_params.at("groups").i; |
| 127 | |
| 128 | ops.at("conv1d")->params["in_channels"] = in_channels_per_group * groups; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | class fuse_static_Fconv2d_pass : public GraphRewriterPass |
| 133 | { |
| 134 | public: |
| 135 | const char* match_pattern_graph() const |
| 136 | { |
| 137 | return R"PNNXIR(7767517 |
| 138 | 4 3 |
| 139 | pnnx.Input input 0 1 input |
| 140 | pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kh,%kw)f32 |
| 141 | F.conv2d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 142 | pnnx.Output output 1 0 out |
| 143 | )PNNXIR"; |
| 144 | } |
| 145 | |
| 146 | const char* replace_pattern_graph() const |
| 147 | { |
| 148 | return R"PNNXIR(7767517 |
| 149 | 3 2 |
| 150 | pnnx.Input input 0 1 input |
| 151 | nn.Conv2d conv2d 1 1 input out out_channels=%out_channels kernel_size=(%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data |
| 152 | pnnx.Output output 1 0 out |
| 153 | )PNNXIR"; |
| 154 | } |
| 155 | |
| 156 | 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 |
| 157 | { |
| 158 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 159 | |
| 160 | const int in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 161 | const int groups = captured_params.at("groups").i; |
| 162 | |
| 163 | ops.at("conv2d")->params["in_channels"] = in_channels_per_group * groups; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | class fuse_static_Fconv2d_pass_2 : public GraphRewriterPass |
| 168 | { |
| 169 | public: |
| 170 | const char* match_pattern_graph() const |
| 171 | { |
| 172 | return R"PNNXIR(7767517 |
| 173 | 5 4 |
| 174 | pnnx.Input input 0 1 input |
| 175 | pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kh,%kw)f32 |
| 176 | pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32 |
| 177 | F.conv2d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 178 | pnnx.Output output 1 0 out |
| 179 | )PNNXIR"; |
| 180 | } |
| 181 | |
| 182 | const char* replace_pattern_graph() const |
| 183 | { |
| 184 | return R"PNNXIR(7767517 |
| 185 | 3 2 |
| 186 | pnnx.Input input 0 1 input |
| 187 | nn.Conv2d conv2d 1 1 input out out_channels=%out_channels kernel_size=(%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data |
| 188 | pnnx.Output output 1 0 out |
| 189 | )PNNXIR"; |
| 190 | } |
| 191 | |
| 192 | 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 |
| 193 | { |
| 194 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 195 | |
| 196 | const int in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 197 | const int groups = captured_params.at("groups").i; |
| 198 | |
| 199 | ops.at("conv2d")->params["in_channels"] = in_channels_per_group * groups; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | class fuse_static_Fconv2d_pass_3 : public GraphRewriterPass |
| 204 | { |
| 205 | public: |
| 206 | const char* match_pattern_graph() const |
| 207 | { |
| 208 | return R"PNNXIR(7767517 |
| 209 | 6 5 |
| 210 | pnnx.Input input 0 1 input |
| 211 | pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kh,%kw)f32 |
| 212 | pnnx.Attribute op_bias 0 1 bias @data=(1,%out_channels,1,1)f32 |
| 213 | F.conv2d op_0 2 1 input weight a bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 214 | pnnx.Expression op_1 2 1 a bias out expr=add(@0,@1) |
| 215 | pnnx.Output output 1 0 out |
| 216 | )PNNXIR"; |
| 217 | } |
| 218 | |
| 219 | const char* replace_pattern_graph() const |
| 220 | { |
| 221 | return R"PNNXIR(7767517 |
| 222 | 3 2 |
| 223 | pnnx.Input input 0 1 input |
| 224 | nn.Conv2d conv2d 1 1 input out out_channels=%out_channels kernel_size=(%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data |
| 225 | pnnx.Output output 1 0 out |
| 226 | )PNNXIR"; |
| 227 | } |
| 228 | |
| 229 | 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 |
| 230 | { |
| 231 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 232 | |
| 233 | const int in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 234 | const int groups = captured_params.at("groups").i; |
| 235 | |
| 236 | ops.at("conv2d")->params["in_channels"] = in_channels_per_group * groups; |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | class fuse_static_Fconv3d_pass : public GraphRewriterPass |
| 241 | { |
| 242 | public: |
| 243 | const char* match_pattern_graph() const |
| 244 | { |
| 245 | return R"PNNXIR(7767517 |
| 246 | 4 3 |
| 247 | pnnx.Input input 0 1 input |
| 248 | pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kd,%kh,%kw)f32 |
| 249 | F.conv3d op_0 2 1 input weight out bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 250 | pnnx.Output output 1 0 out |
| 251 | )PNNXIR"; |
| 252 | } |
| 253 | |
| 254 | const char* replace_pattern_graph() const |
| 255 | { |
| 256 | return R"PNNXIR(7767517 |
| 257 | 3 2 |
| 258 | pnnx.Input input 0 1 input |
| 259 | nn.Conv3d conv3d 1 1 input out out_channels=%out_channels kernel_size=(%kd,%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=False @weight=%op_weight.data |
| 260 | pnnx.Output output 1 0 out |
| 261 | )PNNXIR"; |
| 262 | } |
| 263 | |
| 264 | 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 |
| 265 | { |
| 266 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 267 | |
| 268 | const int in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 269 | const int groups = captured_params.at("groups").i; |
| 270 | |
| 271 | ops.at("conv3d")->params["in_channels"] = in_channels_per_group * groups; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | class fuse_static_Fconv3d_pass_2 : public GraphRewriterPass |
| 276 | { |
| 277 | public: |
| 278 | const char* match_pattern_graph() const |
| 279 | { |
| 280 | return R"PNNXIR(7767517 |
| 281 | 5 4 |
| 282 | pnnx.Input input 0 1 input |
| 283 | pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kd,%kh,%kw)f32 |
| 284 | pnnx.Attribute op_bias 0 1 bias @data=(%out_channels)f32 |
| 285 | F.conv3d op_0 3 1 input weight bias out stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 286 | pnnx.Output output 1 0 out |
| 287 | )PNNXIR"; |
| 288 | } |
| 289 | |
| 290 | const char* replace_pattern_graph() const |
| 291 | { |
| 292 | return R"PNNXIR(7767517 |
| 293 | 3 2 |
| 294 | pnnx.Input input 0 1 input |
| 295 | nn.Conv3d conv3d 1 1 input out out_channels=%out_channels kernel_size=(%kd,%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data |
| 296 | pnnx.Output output 1 0 out |
| 297 | )PNNXIR"; |
| 298 | } |
| 299 | |
| 300 | 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 |
| 301 | { |
| 302 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 303 | |
| 304 | const int in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 305 | const int groups = captured_params.at("groups").i; |
| 306 | |
| 307 | ops.at("conv3d")->params["in_channels"] = in_channels_per_group * groups; |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | class fuse_static_Fconv3d_pass_3 : public GraphRewriterPass |
| 312 | { |
| 313 | public: |
| 314 | const char* match_pattern_graph() const |
| 315 | { |
| 316 | return R"PNNXIR(7767517 |
| 317 | 6 5 |
| 318 | pnnx.Input input 0 1 input |
| 319 | pnnx.Attribute op_weight 0 1 weight @data=(%out_channels,%in_channels_per_group,%kd,%kh,%kw)f32 |
| 320 | pnnx.Attribute op_bias 0 1 bias @data=(1,%out_channels,1,1,1)f32 |
| 321 | F.conv3d op_0 2 1 input weight a bias=None stride=%stride padding=%padding dilation=%dilation groups=%groups |
| 322 | pnnx.Expression op_1 2 1 a bias out expr=add(@0,@1) |
| 323 | pnnx.Output output 1 0 out |
| 324 | )PNNXIR"; |
| 325 | } |
| 326 | |
| 327 | const char* replace_pattern_graph() const |
| 328 | { |
| 329 | return R"PNNXIR(7767517 |
| 330 | 3 2 |
| 331 | pnnx.Input input 0 1 input |
| 332 | nn.Conv3d conv3d 1 1 input out out_channels=%out_channels kernel_size=(%kd,%kh,%kw) padding_mode=zeros stride=%stride padding=%padding dilation=%dilation groups=%groups bias=True @weight=%op_weight.data @bias=%op_bias.data |
| 333 | pnnx.Output output 1 0 out |
| 334 | )PNNXIR"; |
| 335 | } |
| 336 | |
| 337 | 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 |
| 338 | { |
| 339 | GraphRewriterPass::write(ops, captured_params, captured_attrs); |
| 340 | |
| 341 | const int in_channels_per_group = captured_params.at("in_channels_per_group").i; |
| 342 | const int groups = captured_params.at("groups").i; |
| 343 | |
| 344 | ops.at("conv3d")->params["in_channels"] = in_channels_per_group * groups; |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | void fuse_static_conv(Graph& graph) |
| 349 | { |
| 350 | fuse_static_Fconv1d_pass_3 a3; |
| 351 | fuse_static_Fconv2d_pass_3 a4; |
| 352 | fuse_static_Fconv3d_pass_3 a5; |
| 353 | |
| 354 | fuse_static_Fconv1d_pass a; |
| 355 | fuse_static_Fconv1d_pass_2 b; |
| 356 | fuse_static_Fconv2d_pass c; |
| 357 | fuse_static_Fconv2d_pass_2 d; |
| 358 | fuse_static_Fconv3d_pass e; |
| 359 | fuse_static_Fconv3d_pass_2 f; |
| 360 | int opindex = 0; |
| 361 | |
| 362 | pnnx_graph_rewrite(graph, &a3, opindex); |
| 363 | pnnx_graph_rewrite(graph, &a4, opindex); |
| 364 | pnnx_graph_rewrite(graph, &a5, opindex); |
| 365 | |
| 366 | pnnx_graph_rewrite(graph, &a, opindex); |
| 367 | pnnx_graph_rewrite(graph, &b, opindex); |
| 368 | pnnx_graph_rewrite(graph, &c, opindex); |
| 369 | pnnx_graph_rewrite(graph, &d, opindex); |
| 370 | pnnx_graph_rewrite(graph, &e, opindex); |
| 371 | pnnx_graph_rewrite(graph, &f, opindex); |
| 372 | } |
| 373 | |
| 374 | } // namespace pnnx |