Elffuss BraidHub

kikocis/ncnn public

master · 4.3 KiBRawPermalink
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_convert_shufflechannel_slice.h"
16
17#include "pass_level2.h"
18
19namespace pnnx {
20
21namespace ncnn {
22
23// def channel_shuffle(self, x):
24// batchsize, num_channels, height, width = x.data.size()
25// assert (num_channels % 4 == 0)
26// x = x.reshape(batchsize * num_channels // 2, 2, height * width)
27// x = x.permute(1, 0, 2)
28// x = x.reshape(2, -1, num_channels // 2, height, width)
29// return x[0], x[1]
30
31class fuse_shufflechannel_slice_pass : public GraphRewriterPass
32{
33public:
34 const char* match_pattern_graph() const
35 {
36 return R"PNNXIR(7767517
376 6
38pnnx.Input input 0 1 input #input=(%batch,%c,%h,%w)f32
39Tensor.reshape op_0 1 1 input a shape=(%batch_mul_ch_per_group,%groups,%h_mul_w)
40Tensor.permute op_1 1 1 a b dims=(1,0,2)
41Tensor.reshape op_2 1 1 b c shape=(%groups,%batch,%ch_per_group,%h,%w)
42torch.unbind op_3 1 2 c out0 out1 dim=0
43pnnx.Output output 2 0 out0 out1
44)PNNXIR";
45 }
46
47 const char* replace_pattern_graph() const
48 {
49 return R"PNNXIR(7767517
504 4
51pnnx.Input input 0 1 input
52ShuffleChannel shufflechannel 1 1 input a 0=%groups 1=1 #a=(%batch,%c,%h,%w)f32
53Slice slice 1 2 a out0 out1 0=(-233,-233) 1=0
54pnnx.Output output 2 0 out0 out1
55)PNNXIR";
56 }
57
58 bool match(const std::map<std::string, Parameter>& captured_params) const
59 {
60 const int groups = captured_params.at("groups").i;
61 const int batch = captured_params.at("batch").i;
62 const int batch_mul_ch_per_group = captured_params.at("batch_mul_ch_per_group").i;
63 const int ch_per_group = captured_params.at("ch_per_group").i;
64 const int h_mul_w = captured_params.at("h_mul_w").i;
65 const int c = captured_params.at("c").i;
66 const int h = captured_params.at("h").i;
67 const int w = captured_params.at("w").i;
68
69 if (groups != 2 || groups * ch_per_group != c)
70 return false;
71
72 if (batch_mul_ch_per_group != batch * ch_per_group)
73 return false;
74
75 if (h_mul_w != h * w)
76 return false;
77
78 return true;
79 }
80
81 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
82 {
83 GraphRewriterPass::write(ops, captured_params, captured_attrs);
84
85 const int batch_index = ops.at("shufflechannel")->inputs[0]->params["__batch_index"].i;
86
87 ops.at("slice")->inputs[0]->params["__batch_index"] = batch_index;
88 ops.at("slice")->outputs[0]->params["__batch_index"] = batch_index;
89 ops.at("slice")->outputs[1]->params["__batch_index"] = batch_index;
90 }
91};
92
93class fuse_shufflechannel_slice_pass_1 : public fuse_shufflechannel_slice_pass
94{
95public:
96 const char* match_pattern_graph() const
97 {
98 return R"PNNXIR(7767517
996 6
100pnnx.Input input 0 1 input #input=(%batch,%c,%h,%w)f32
101Tensor.reshape op_0 1 1 input a shape=(%batch_mul_ch_per_group,%groups,%h_mul_w)
102Tensor.permute op_1 1 1 a b dims=(1,0,2)
103Tensor.reshape op_2 1 1 b c shape=(%groups,%batch,%ch_per_group,%h,%w)
104torch.unbind op_3 1 2 c out0 out1 dim=0
105pnnx.Output output 2 0 out0 out1
106)PNNXIR";
107 }
108};
109
110void fuse_convert_shufflechannel_slice(Graph& graph)
111{
112 fuse_shufflechannel_slice_pass a;
113 fuse_shufflechannel_slice_pass_1 b;
114 int opindex = 0;
115
116 pnnx_graph_rewrite(graph, &a, opindex);
117 pnnx_graph_rewrite(graph, &b, opindex);
118}
119
120} // namespace ncnn
121
122} // namespace pnnx