| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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_pixel_unshuffle.h" |
| 16 | |
| 17 | #include "pass_level2.h" |
| 18 | |
| 19 | namespace pnnx { |
| 20 | |
| 21 | class fuse_pixel_unshuffle_pass : public GraphRewriterPass |
| 22 | { |
| 23 | public: |
| 24 | const char* match_pattern_graph() const |
| 25 | { |
| 26 | return R"PNNXIR(7767517 |
| 27 | 5 4 |
| 28 | pnnx.Input input 0 1 input |
| 29 | Tensor.reshape op_1 1 1 input 1 shape=%shape |
| 30 | Tensor.permute op_2 1 1 1 2 dims=%dims |
| 31 | Tensor.reshape op_3 1 1 2 out shape=%shape2 |
| 32 | pnnx.Output output 1 0 out |
| 33 | )PNNXIR"; |
| 34 | } |
| 35 | |
| 36 | const char* type_str() const |
| 37 | { |
| 38 | return "nn.PixelUnshuffle"; |
| 39 | } |
| 40 | |
| 41 | const char* name_str() const |
| 42 | { |
| 43 | return "pixelunshuffle"; |
| 44 | } |
| 45 | |
| 46 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 47 | { |
| 48 | const std::vector<int>& shape = captured_params.at("shape").ai; |
| 49 | const std::vector<int>& shape2 = captured_params.at("shape2").ai; |
| 50 | const std::vector<int>& dims = captured_params.at("dims").ai; |
| 51 | int size = shape.size(); |
| 52 | int size2 = shape2.size(); |
| 53 | |
| 54 | if (shape.size() < 3 || shape2.size() < 3 || shape.size() != shape2.size() + 2) return false; |
| 55 | |
| 56 | int downscale_factor = shape[size - 1]; |
| 57 | |
| 58 | bool match_reshape = downscale_factor == shape[size - 3] && shape[size - 2] == shape2[size2 - 1] && shape[size - 4] == shape2[size2 - 2] |
| 59 | && shape2[size2 - 3] == downscale_factor * downscale_factor * shape[size - 5]; |
| 60 | bool match_permute = dims == std::vector<int>{0, 1, 3, 5, 2, 4}; |
| 61 | |
| 62 | return match_reshape & match_permute; |
| 63 | } |
| 64 | |
| 65 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 66 | { |
| 67 | const std::vector<int>& shape = captured_params.at("shape").ai; |
| 68 | |
| 69 | int downscale_factor = shape.back(); |
| 70 | |
| 71 | op->params["downscale_factor"] = downscale_factor; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | void fuse_pixel_unshuffle(Graph& graph) |
| 76 | { |
| 77 | fuse_pixel_unshuffle_pass a; |
| 78 | int opindex = 0; |
| 79 | |
| 80 | pnnx_graph_rewrite(graph, &a, opindex); |
| 81 | } |
| 82 | |
| 83 | } // namespace pnnx |