| 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_adjacent_reshape.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include "pass_level2.h" |
| 19 | |
| 20 | namespace pnnx { |
| 21 | |
| 22 | void fuse_adjacent_reshape(Graph& graph) |
| 23 | { |
| 24 | while (1) |
| 25 | { |
| 26 | bool matched = false; |
| 27 | |
| 28 | for (int i = (int)graph.ops.size() - 1; i > 0; i--) |
| 29 | { |
| 30 | Operator* op = graph.ops[i]; |
| 31 | |
| 32 | // look for Tensor.view / Tensor.reshape / torch.squeeze / torch.unsqueeze chain |
| 33 | if (op->type != "Tensor.view" && op->type != "Tensor.reshape" && op->type != "torch.squeeze" && op->type != "torch.unsqueeze") |
| 34 | continue; |
| 35 | |
| 36 | if ((op->type == "torch.squeeze" || op->type == "torch.unsqueeze") && op->outputs[0]->shape.empty()) |
| 37 | continue; |
| 38 | |
| 39 | std::vector<Operator*> reshapes_to_delete; |
| 40 | const Operand* in0 = op->inputs[0]; |
| 41 | while (in0->consumers.size() == 1 && (in0->producer->type == "Tensor.view" || in0->producer->type == "Tensor.reshape" || in0->producer->type == "torch.squeeze" || in0->producer->type == "torch.unsqueeze")) |
| 42 | { |
| 43 | reshapes_to_delete.push_back(in0->producer); |
| 44 | in0 = in0->producer->inputs[0]; |
| 45 | } |
| 46 | |
| 47 | if (reshapes_to_delete.empty()) |
| 48 | continue; |
| 49 | |
| 50 | // keep the last reshape only |
| 51 | matched = true; |
| 52 | |
| 53 | op->type = "Tensor.reshape"; |
| 54 | |
| 55 | if (!op->outputs[0]->shape.empty()) |
| 56 | { |
| 57 | op->params.clear(); |
| 58 | op->params["shape"] = op->outputs[0]->shape; |
| 59 | } |
| 60 | |
| 61 | for (auto& op0 : reshapes_to_delete) |
| 62 | { |
| 63 | for (auto& x : op0->inputs) |
| 64 | { |
| 65 | x->remove_consumer(op0); |
| 66 | } |
| 67 | |
| 68 | Operand* op0_in = op0->inputs[0]; |
| 69 | Operand* op0_out = op0->outputs[0]; |
| 70 | |
| 71 | for (auto& x : op0_out->consumers) |
| 72 | { |
| 73 | for (size_t j = 0; j < x->inputs.size(); j++) |
| 74 | { |
| 75 | if (x->inputs[j] == op0_out) |
| 76 | x->inputs[j] = op0_in; |
| 77 | } |
| 78 | |
| 79 | op0_in->consumers.push_back(x); |
| 80 | } |
| 81 | |
| 82 | op0_in->name = op0_out->name; |
| 83 | |
| 84 | op0_out->producer = 0; |
| 85 | op0_out->consumers.clear(); |
| 86 | |
| 87 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), op0_out)); |
| 88 | delete op0_out; |
| 89 | |
| 90 | op0->inputs.clear(); |
| 91 | op0->outputs.clear(); |
| 92 | |
| 93 | graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op0)); |
| 94 | delete op0; |
| 95 | } |
| 96 | |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | if (!matched) |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } // namespace pnnx |