Elffuss BraidHub

kikocis/ncnn public

master · 3.2 KiBRawPermalink
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 "fuse_channel_shuffle.h"
16
17#include "pass_level2.h"
18
19namespace pnnx {
20
21class fuse_channel_shuffle_pass : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
277 6
28pnnx.Input input 0 1 input
29pnnx.Expression op_0 1 1 input 13 expr=[int(size(@0,0)),2,int(floor_divide(size(@0,1),%groups)),int(size(@0,2)),int(size(@0,3))]
30Tensor.view op_1 2 1 input 13 14
31pnnx.Expression op_2 1 1 input 15 expr=[int(size(@0,0)),-1,int(size(@0,2)),int(size(@0,3))]
32torch.transpose op_3 1 1 14 16 dim0=1 dim1=2
33Tensor.reshape op_4 2 1 16 15 out
34pnnx.Output output 1 0 out
35)PNNXIR";
36 }
37
38 const char* type_str() const
39 {
40 return "nn.ChannelShuffle";
41 }
42
43 const char* name_str() const
44 {
45 return "channelshuffle";
46 }
47};
48
49class fuse_channel_shuffle_pass_1 : public GraphRewriterPass
50{
51public:
52 const char* match_pattern_graph() const
53 {
54 return R"PNNXIR(7767517
555 4
56pnnx.Input input 0 1 input
57Tensor.view op_0 1 1 input 13 shape=(%batch,%groups,%channels_per_group,%h,%w)
58torch.transpose op_1 1 1 13 14 dim0=1 dim1=2
59Tensor.reshape op_2 1 1 14 out shape=(%batch,%channels,%h,%w)
60pnnx.Output output 1 0 out
61)PNNXIR";
62 }
63
64 const char* type_str() const
65 {
66 return "nn.ChannelShuffle";
67 }
68
69 const char* name_str() const
70 {
71 return "channelshuffle";
72 }
73
74 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
75 {
76 op->params["groups"] = captured_params.at("groups");
77 }
78};
79
80class fuse_channel_shuffle_pass_2 : public fuse_channel_shuffle_pass_1
81{
82public:
83 const char* match_pattern_graph() const
84 {
85 return R"PNNXIR(7767517
865 4
87pnnx.Input input 0 1 input
88Tensor.reshape op_0 1 1 input 13 shape=(%batch,%groups,%channels_per_group,%h,%w)
89Tensor.permute op_1 1 1 13 14 dims=(0,2,1,3,4)
90Tensor.reshape op_2 1 1 14 out shape=(%batch,%channels,%h,%w)
91pnnx.Output output 1 0 out
92)PNNXIR";
93 }
94};
95
96void fuse_channel_shuffle(Graph& graph)
97{
98 fuse_channel_shuffle_pass a;
99 fuse_channel_shuffle_pass_1 b;
100 fuse_channel_shuffle_pass_2 c;
101 int opindex = 0;
102
103 pnnx_graph_rewrite(graph, &a, opindex);
104 pnnx_graph_rewrite(graph, &b, opindex);
105 pnnx_graph_rewrite(graph, &c, opindex);
106}
107
108} // namespace pnnx