Elffuss BraidHub

kikocis/ncnn public

master · 2.0 KiBRawPermalink
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_noop.h"
16
17#include <algorithm>
18
19namespace pnnx {
20
21namespace ncnn {
22
23void eliminate_noop(Graph& graph)
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 != "Noop" && op->type != "Tensor.clone")
34 continue;
35
36 need_eliminate = true;
37
38 Operand* op_in = op->inputs[0];
39 Operand* op_out = op->outputs[0];
40
41 op_in->remove_consumer(op);
42
43 op_in->params = op_out->params;
44
45 for (auto& x : op_out->consumers)
46 {
47 for (size_t j = 0; j < x->inputs.size(); j++)
48 {
49 if (x->inputs[j] == op_out)
50 x->inputs[j] = op_in;
51 }
52
53 op_in->consumers.push_back(x);
54 }
55
56 op_out->producer = 0;
57 op_out->consumers.clear();
58
59 graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), op_out));
60 delete op_out;
61
62 op->inputs.clear();
63 op->outputs.clear();
64
65 graph.ops.erase(graph.ops.begin() + i);
66 delete op;
67
68 break;
69 }
70
71 if (!need_eliminate)
72 break;
73 }
74}
75
76} // namespace ncnn
77
78} // namespace pnnx