Elffuss BraidHub

kikocis/ncnn public

master · 10.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2024 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 "dead_code_elimination.h"
16
17#include <string>
18#include <unordered_set>
19
20namespace pnnx {
21
22namespace onnx2pnnx {
23
24static void collect_dead_nodes(const onnx::GraphProto& graph, std::vector<std::string>& dead_outputs, std::vector<int>& dead_node_indexes, std::unordered_set<std::string>& live_inputs)
25{
26 for (int i = 0; i < graph.output_size(); i++)
27 {
28 live_inputs.insert(graph.output(i).name());
29 }
30
31 for (int i = graph.node_size() - 1; i >= 0; i--)
32 {
33 const onnx::NodeProto& node = graph.node(i);
34
35 bool is_outputs_live = false;
36 for (int j = 0; j < node.output_size(); j++)
37 {
38 if (live_inputs.find(node.output(j)) != live_inputs.end())
39 {
40 is_outputs_live = true;
41 break;
42 }
43 }
44
45 if (is_outputs_live)
46 {
47 for (int j = node.output_size() - 1; j >= 0; j--)
48 {
49 if (live_inputs.find(node.output(j)) == live_inputs.end())
50 {
51 dead_outputs.push_back(node.output(j));
52 }
53 else
54 {
55 // leading outputs cannot be optional
56 break;
57 }
58 }
59
60 for (int j = 0; j < node.input_size(); j++)
61 {
62 live_inputs.insert(node.input(j));
63 }
64 }
65 else
66 {
67 dead_node_indexes.push_back(i);
68 }
69
70 if (is_outputs_live)
71 {
72 for (int j = 0; j < node.attribute_size(); j++)
73 {
74 const onnx::AttributeProto& attr = node.attribute(j);
75
76 if (attr.type() == onnx::AttributeProto::GRAPH)
77 {
78 const onnx::GraphProto& sg = attr.g();
79
80 std::vector<std::string> sg_dead_outputs;
81 std::vector<int> sg_dead_node_indexes;
82 collect_dead_nodes(sg, sg_dead_outputs, sg_dead_node_indexes, live_inputs);
83 }
84 if (attr.type() == onnx::AttributeProto::GRAPHS)
85 {
86 for (int k = 0; k < attr.graphs().size(); k++)
87 {
88 const onnx::GraphProto& sg = attr.graphs().at(k);
89
90 std::vector<std::string> sg_dead_outputs;
91 std::vector<int> sg_dead_node_indexes;
92 collect_dead_nodes(sg, sg_dead_outputs, sg_dead_node_indexes, live_inputs);
93 }
94 }
95 }
96 }
97 }
98}
99
100void dead_code_elimination(onnx::ModelProto& model)
101{
102 // collect all nodes that have no links with graph outputs
103 std::vector<std::string> dead_outputs;
104 std::vector<int> dead_node_indexes;
105 {
106 const onnx::GraphProto& graph = model.graph();
107
108 std::unordered_set<std::string> live_inputs;
109 collect_dead_nodes(graph, dead_outputs, dead_node_indexes, live_inputs);
110 }
111
112 // eliminate dead nodes
113 {
114 onnx::GraphProto* graph = model.mutable_graph();
115
116 for (size_t i = 0; i < dead_node_indexes.size(); i++)
117 {
118 const int dead_node_index = dead_node_indexes[i];
119
120 // ..... dni .......
121 const int graph_node_size = graph->node_size();
122 for (int j = dead_node_index; j < graph_node_size - 1; j++)
123 {
124 graph->mutable_node()->SwapElements(j, j + 1);
125 }
126
127 // ..... ....... dni
128 graph->mutable_node()->RemoveLast();
129 }
130 }
131
132 // eliminate dead value info
133 {
134 onnx::GraphProto* graph = model.mutable_graph();
135
136 for (size_t i = 0; i < dead_outputs.size(); i++)
137 {
138 const std::string& dead_output = dead_outputs[i];
139
140 for (int j = 0; j < graph->value_info_size(); j++)
141 {
142 if (graph->value_info(j).name() == dead_output)
143 {
144 // ..... j .......
145 const int graph_value_info_size = graph->value_info_size();
146 for (int k = j; k < graph_value_info_size - 1; k++)
147 {
148 graph->mutable_value_info()->SwapElements(k, k + 1);
149 }
150
151 // ..... ....... j
152 graph->mutable_value_info()->RemoveLast();
153
154 break;
155 }
156 }
157 }
158
159 for (int i = 0; i < graph->node_size(); i++)
160 {
161 onnx::NodeProto* node = graph->mutable_node(i);
162
163 for (int j = 0; j < node->output_size(); j++)
164 {
165 bool has_dead_output = false;
166 for (auto x : dead_outputs)
167 {
168 if (x == node->output(j))
169 {
170 has_dead_output = true;
171 break;
172 }
173 }
174
175 if (has_dead_output)
176 {
177 // drop optional unused outputs
178 const int node_output_size = node->output_size();
179 for (int k = j; k < node_output_size; k++)
180 {
181 node->mutable_output()->RemoveLast();
182 }
183
184 break;
185 }
186 }
187 }
188 }
189
190 // collect all dead functions
191 std::vector<int> dead_function_indexes;
192 {
193 const onnx::GraphProto& graph = model.graph();
194
195 std::unordered_set<int> live_function_indexes;
196 for (int i = 0; i < graph.node_size(); i++)
197 {
198 const std::string& op_type = graph.node(i).op_type();
199
200 for (int j = 0; j < model.functions_size(); j++)
201 {
202 const onnx::FunctionProto& function = model.functions(j);
203
204 if (function.name() == op_type)
205 {
206 live_function_indexes.insert(j);
207 break;
208 }
209 }
210 }
211
212 // find nested live functions
213 while (1)
214 {
215 bool new_nested_live_function = false;
216
217 for (int i = 0; i < model.functions_size(); i++)
218 {
219 if (live_function_indexes.find(i) == live_function_indexes.end())
220 continue;
221
222 const onnx::FunctionProto& function = model.functions(i);
223
224 for (int j = 0; j < function.node_size(); j++)
225 {
226 const std::string& op_type = function.node(j).op_type();
227
228 for (int k = 0; k < model.functions_size(); k++)
229 {
230 const onnx::FunctionProto& nested_function = model.functions(k);
231
232 if (nested_function.name() == op_type && live_function_indexes.find(k) == live_function_indexes.end())
233 {
234 // nested live function added
235 live_function_indexes.insert(k);
236
237 new_nested_live_function = true;
238 }
239 }
240 }
241 }
242
243 if (!new_nested_live_function)
244 break;
245 }
246
247 for (int i = model.functions_size() - 1; i >= 0; i--)
248 {
249 if (live_function_indexes.find(i) == live_function_indexes.end())
250 {
251 dead_function_indexes.push_back(i);
252 }
253 }
254 }
255
256 // eliminate dead funtions
257 {
258 for (size_t i = 0; i < dead_function_indexes.size(); i++)
259 {
260 const int dead_function_index = dead_function_indexes[i];
261
262 // ..... dfi .......
263 const int model_functions_size = model.functions_size();
264 for (int j = dead_function_index; j < model_functions_size - 1; j++)
265 {
266 model.mutable_functions()->SwapElements(j, j + 1);
267 }
268
269 // ..... ....... dfi
270 model.mutable_functions()->RemoveLast();
271 }
272 }
273
274 // eliminate dead initializers
275 {
276 onnx::GraphProto* graph = model.mutable_graph();
277
278 std::unordered_set<std::string> live_inputs;
279 for (int i = 0; i < graph->node_size(); i++)
280 {
281 const onnx::NodeProto& node = graph->node(i);
282
283 for (int j = 0; j < node.input_size(); j++)
284 {
285 live_inputs.insert(node.input(j));
286 }
287 }
288
289 // find live inputs in functions
290 for (int i = 0; i < model.functions_size(); i++)
291 {
292 const onnx::FunctionProto& function = model.functions(i);
293
294 for (int j = 0; j < function.node_size(); j++)
295 {
296 const onnx::NodeProto& node = function.node(j);
297
298 for (int k = 0; k < node.input_size(); k++)
299 {
300 live_inputs.insert(node.input(k));
301 }
302 }
303 }
304
305 std::vector<int> dead_initializer_indexes;
306 for (int i = graph->initializer_size() - 1; i >= 0; i--)
307 {
308 if (live_inputs.find(graph->initializer(i).name()) == live_inputs.end())
309 {
310 dead_initializer_indexes.push_back(i);
311 }
312 }
313
314 for (size_t i = 0; i < dead_initializer_indexes.size(); i++)
315 {
316 const int dead_initializer_index = dead_initializer_indexes[i];
317
318 // ..... dii .......
319 const int graph_initializer_size = graph->initializer_size();
320 for (int j = dead_initializer_index; j < graph_initializer_size - 1; j++)
321 {
322 graph->mutable_initializer()->SwapElements(j, j + 1);
323 }
324
325 // ..... ....... dii
326 graph->mutable_initializer()->RemoveLast();
327 }
328 }
329}
330
331} // namespace onnx2pnnx
332
333} // namespace pnnx