Elffuss BraidHub

kikocis/ncnn public

master · 3.5 KiBRawPermalink
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 "eliminate_identity_operator.h"
16
17#include <algorithm>
18#include "pass_level2.h"
19
20namespace pnnx {
21
22void eliminate_identity_operator(Graph& graph)
23{
24 while (1)
25 {
26 bool matched = false;
27
28 for (size_t i = 0; i < graph.ops.size(); i++)
29 {
30 Operator* op0 = graph.ops[i];
31
32 if (op0->type == "pnnx.Input" || op0->type == "pnnx.Output" || op0->type == "pnnx.Attribute" || op0->type == "torch.clone")
33 continue;
34
35 Operator* op1 = 0;
36
37 for (size_t j = i + 1; j < graph.ops.size(); j++)
38 {
39 op1 = graph.ops[j];
40
41 if (op1->type == "pnnx.Input" || op1->type == "pnnx.Output" || op0->type == "pnnx.Attribute" || op1->type == "torch.clone")
42 continue;
43
44 if (op0->type != op1->type)
45 continue;
46
47 if (op0->inputs != op1->inputs)
48 continue;
49
50 if (op0->outputs.size() != op1->outputs.size())
51 continue;
52
53 if (op0->params != op1->params)
54 continue;
55
56 if (op0->attrs != op1->attrs)
57 continue;
58
59 // we find same operator with same inputs
60 matched = true;
61 break;
62 }
63
64 if (!matched)
65 continue;
66
67 // fprintf(stderr, "eliminate_identity_operator %s %s %s\n", op0->type.c_str(), op0->name.c_str(), op1->name.c_str());
68
69 int input_count = (int)op0->inputs.size();
70 for (int j = 0; j < input_count; j++)
71 {
72 Operand* in0 = op0->inputs[j];
73
74 in0->remove_consumer(op1);
75 }
76
77 int output_count = (int)op0->outputs.size();
78 for (int j = 0; j < output_count; j++)
79 {
80 Operand* out0 = op0->outputs[j];
81 Operand* out1 = op1->outputs[j];
82
83 for (auto x : out1->consumers)
84 {
85 for (size_t k = 0; k < x->inputs.size(); k++)
86 {
87 if (x->inputs[k] == out1)
88 x->inputs[k] = out0;
89 }
90
91 out0->consumers.push_back(x);
92 }
93
94 out1->consumers.clear();
95 }
96
97 // delete op1 and its output operands
98 for (int j = 0; j < output_count; j++)
99 {
100 graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), op1->outputs[j]));
101 delete op1->outputs[j];
102 }
103
104 op1->inputs.clear();
105 op1->outputs.clear();
106
107 graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op1));
108 delete op1;
109
110 break;
111 }
112
113 if (!matched)
114 break;
115 }
116}
117
118} // namespace pnnx