| 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 "eliminate_output.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | void eliminate_output(Graph& graph) |
| 22 | { |
| 23 | int output_index = 0; |
| 24 | |
| 25 | for (;;) |
| 26 | { |
| 27 | bool need_eliminate = false; |
| 28 | |
| 29 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 30 | { |
| 31 | Operator* op = graph.ops[i]; |
| 32 | |
| 33 | if (op->type != "pnnx.Output") |
| 34 | continue; |
| 35 | |
| 36 | need_eliminate = true; |
| 37 | |
| 38 | // canonicalize output name |
| 39 | for (int j = 0; j < (int)op->inputs.size(); j++) |
| 40 | { |
| 41 | op->inputs[j]->name = std::string("out") + std::to_string(output_index); |
| 42 | output_index++; |
| 43 | } |
| 44 | |
| 45 | for (Operand* r : op->inputs) |
| 46 | { |
| 47 | r->remove_consumer(op); |
| 48 | } |
| 49 | |
| 50 | op->inputs.clear(); |
| 51 | |
| 52 | for (Operand* r : op->outputs) |
| 53 | { |
| 54 | r->producer = 0; |
| 55 | } |
| 56 | |
| 57 | op->outputs.clear(); |
| 58 | |
| 59 | graph.ops.erase(graph.ops.begin() + i); |
| 60 | delete op; |
| 61 | |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if (!need_eliminate) |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | } // namespace ncnn |
| 71 | |
| 72 | } // namespace pnnx |