Elffuss BraidHub

kikocis/ncnn public

master · 6.4 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 "chain_multi_output.h"
16
17#include <algorithm>
18
19namespace pnnx {
20
21namespace ncnn {
22
23void chain_multi_output(Graph& graph)
24{
25 for (;;)
26 {
27 bool need_eliminate = false;
28
29 for (int i = (int)graph.ops.size() - 1; i >= 0; i--)
30 {
31 Operator* op = graph.ops[i];
32
33 if (op->type != "pnnx.Output")
34 continue;
35
36 // prim::TupleConstruct pnnx_791 2 1 a b out
37 // pnnx.Expression pnnx_expr_0 3 1 a b c out expr=[@0,@1,@2]
38 // pnnx.Output pnnx_output_0 1 0 out
39 bool match_tuple_expr_output = false;
40 for (int j = 0; j < (int)op->inputs.size(); j++)
41 {
42 Operand* r = op->inputs[j];
43
44 if (r->consumers.size() != 1)
45 continue;
46
47 Operator* op0 = r->producer;
48
49 if (op0->type == "prim::TupleConstruct")
50 {
51 match_tuple_expr_output = true;
52 }
53 else if (op0->type == "pnnx.Expression")
54 {
55 const int op_expr_input_count = (int)op0->inputs.size();
56 const std::string& expr = op0->params.at("expr").s;
57
58 std::string pattern_expr = "[";
59 for (int k = 0; k < op_expr_input_count; k++)
60 {
61 pattern_expr += std::string("@") + std::to_string(k);
62
63 if (k != op_expr_input_count - 1)
64 pattern_expr += ",";
65 }
66 pattern_expr += "]";
67
68 if (expr == pattern_expr)
69 {
70 match_tuple_expr_output = true;
71 }
72 }
73
74 if (!match_tuple_expr_output)
75 continue;
76
77 // chain op0 as output and delete op0
78 std::vector<Operand*> new_inputs;
79 for (int k = 0; k < j; k++)
80 {
81 new_inputs.push_back(op->inputs[k]);
82 }
83
84 for (Operand* r : op0->inputs)
85 {
86 r->remove_consumer(op0);
87 r->consumers.push_back(op);
88 new_inputs.push_back(r);
89 }
90
91 for (int k = j + 1; k < (int)op->inputs.size(); k++)
92 {
93 new_inputs.push_back(op->inputs[k]);
94 }
95
96 op->inputs = new_inputs;
97
98 Operand* op0_out = op0->outputs[0];
99 op0_out->producer = 0;
100 op0_out->consumers.clear();
101
102 graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), op0_out));
103 delete op0_out;
104
105 op0->inputs.clear();
106 op0->outputs.clear();
107
108 graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op0));
109 delete op0;
110
111 break;
112 }
113
114 if (match_tuple_expr_output)
115 need_eliminate = true;
116
117 break;
118 }
119
120 if (!need_eliminate)
121 break;
122 }
123
124 for (;;)
125 {
126 bool need_eliminate = false;
127
128 for (int i = (int)graph.ops.size() - 1; i >= 0; i--)
129 {
130 Operator* op = graph.ops[i];
131
132 if (op->type != "pnnx.Output")
133 continue;
134
135 // prim::DictConstruct pnnx_68 4 1 key0 out0 key1 out1 out
136 // pnnx.Output pnnx_output_0 1 0 out
137
138 bool match_dict_output = false;
139 for (int j = 0; j < (int)op->inputs.size(); j++)
140 {
141 Operand* r = op->inputs[j];
142
143 if (r->consumers.size() != 1)
144 continue;
145
146 Operator* op0 = r->producer;
147
148 if (op0->type == "prim::DictConstruct")
149 {
150 match_dict_output = true;
151 }
152
153 if (!match_dict_output)
154 continue;
155
156 // chain op0 odd ones as output and delete op0
157 std::vector<Operand*> new_inputs;
158 for (int k = 0; k < j; k++)
159 {
160 new_inputs.push_back(op->inputs[k]);
161 }
162
163 for (int k = 0; k < (int)op0->inputs.size(); k++)
164 {
165 Operand* r = op0->inputs[k];
166
167 if (k % 2 == 0)
168 {
169 // ignore key
170 r->remove_consumer(op0);
171 }
172 else
173 {
174 r->remove_consumer(op0);
175 r->consumers.push_back(op);
176 new_inputs.push_back(r);
177 }
178 }
179
180 for (int k = j + 1; k < (int)op->inputs.size(); k++)
181 {
182 new_inputs.push_back(op->inputs[k]);
183 }
184
185 op->inputs = new_inputs;
186
187 Operand* op0_out = op0->outputs[0];
188 op0_out->producer = 0;
189 op0_out->consumers.clear();
190
191 graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), op0_out));
192 delete op0_out;
193
194 op0->inputs.clear();
195 op0->outputs.clear();
196
197 graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op0));
198 delete op0;
199
200 break;
201 }
202
203 if (match_dict_output)
204 need_eliminate = true;
205
206 break;
207 }
208
209 if (!need_eliminate)
210 break;
211 }
212}
213
214} // namespace ncnn
215
216} // namespace pnnx