| 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 | |
| 19 | namespace pnnx { |
| 20 | |
| 21 | namespace 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 | |
| 31 | class fuse_shufflechannel_slice_pass : public GraphRewriterPass |
| 32 | { |
| 33 | public: |
| 34 | const char* match_pattern_graph() const |
| 35 | { |
| 36 | return R"PNNXIR(7767517 |
| 37 | 6 6 |
| 38 | pnnx.Input input 0 1 input #input=(%batch,%c,%h,%w)f32 |
| 39 | Tensor.reshape op_0 1 1 input a shape=(%batch_mul_ch_per_group,%groups,%h_mul_w) |
| 40 | Tensor.permute op_1 1 1 a b dims=(1,0,2) |
| 41 | Tensor.reshape op_2 1 1 b c shape=(%groups,%batch,%ch_per_group,%h,%w) |
| 42 | torch.unbind op_3 1 2 c out0 out1 dim=0 |
| 43 | pnnx.Output output 2 0 out0 out1 |
| 44 | )PNNXIR"; |
| 45 | } |
| 46 | |
| 47 | const char* replace_pattern_graph() const |
| 48 | { |
| 49 | return R"PNNXIR(7767517 |
| 50 | 4 4 |
| 51 | pnnx.Input input 0 1 input |
| 52 | ShuffleChannel shufflechannel 1 1 input a 0=%groups 1=1 #a=(%batch,%c,%h,%w)f32 |
| 53 | Slice slice 1 2 a out0 out1 0=(-233,-233) 1=0 |
| 54 | pnnx.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 | |
| 93 | class fuse_shufflechannel_slice_pass_1 : public fuse_shufflechannel_slice_pass |
| 94 | { |
| 95 | public: |
| 96 | const char* match_pattern_graph() const |
| 97 | { |
| 98 | return R"PNNXIR(7767517 |
| 99 | 6 6 |
| 100 | pnnx.Input input 0 1 input #input=(%batch,%c,%h,%w)f32 |
| 101 | Tensor.reshape op_0 1 1 input a shape=(%batch_mul_ch_per_group,%groups,%h_mul_w) |
| 102 | Tensor.permute op_1 1 1 a b dims=(1,0,2) |
| 103 | Tensor.reshape op_2 1 1 b c shape=(%groups,%batch,%ch_per_group,%h,%w) |
| 104 | torch.unbind op_3 1 2 c out0 out1 dim=0 |
| 105 | pnnx.Output output 2 0 out0 out1 |
| 106 | )PNNXIR"; |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | void 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 |