Elffuss BraidHub

kikocis/ncnn public

master · 21.4 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 "model_stat.h"
16
17namespace pnnx {
18
19namespace onnx2pnnx {
20
21static bool string_starts_with(const std::string& s, const std::string& s2)
22{
23 return strncmp(s.c_str(), s2.c_str(), s2.size()) == 0;
24}
25
26ModelStat get_model_stat(const onnx::ModelProto& model)
27{
28 ModelStat stat;
29
30 const onnx::GraphProto& graph = model.graph();
31
32 stat.node_size = graph.node_size();
33 for (int i = 0; i < model.functions_size(); i++)
34 {
35 stat.node_size += model.functions(i).node_size();
36 }
37
38 stat.initializer_size = graph.initializer_size();
39 stat.functions_size = model.functions_size();
40
41 for (int i = 0; i < graph.node_size(); i++)
42 {
43 const onnx::NodeProto& node = graph.node(i);
44
45 std::string op_type = node.op_type();
46
47 // drop |folded_N suffix
48 if (op_type.size() > 8)
49 {
50 size_t folded_N_index = op_type.rfind("|folded_");
51 if (folded_N_index != std::string::npos)
52 {
53 op_type = op_type.substr(0, folded_N_index);
54 }
55 }
56
57 if (node.domain().empty())
58 {
59 // native onnx op
60 stat.onnx_count += 1;
61
62 if (stat.onnx_op_count.find(op_type) == stat.onnx_op_count.end())
63 {
64 stat.onnx_op_count[op_type] = 1;
65 }
66 else
67 {
68 stat.onnx_op_count[op_type] = stat.onnx_op_count[op_type] + 1;
69 }
70 continue;
71 }
72
73 if (string_starts_with(op_type, "aten_") || string_starts_with(op_type, "_aten_"))
74 {
75 // aten_view
76 stat.aten_count += 1;
77
78 std::string simname = op_type;
79 if (simname[0] == '_')
80 simname = simname.substr(1);
81 simname[4] = '.';
82
83 if (stat.aten_op_count.find(simname) == stat.aten_op_count.end())
84 {
85 stat.aten_op_count[simname] = 1;
86 }
87 else
88 {
89 stat.aten_op_count[simname] = stat.aten_op_count[simname] + 1;
90 }
91 continue;
92 }
93
94 if (string_starts_with(op_type, "prims_"))
95 {
96 // prims_convert_element_type
97 stat.prims_count += 1;
98
99 std::string simname = op_type;
100 simname[5] = '.';
101
102 if (stat.prims_op_count.find(simname) == stat.prims_op_count.end())
103 {
104 stat.prims_op_count[simname] = 1;
105 }
106 else
107 {
108 stat.prims_op_count[simname] = stat.prims_op_count[simname] + 1;
109 }
110 continue;
111 }
112
113 if (string_starts_with(op_type, "torch_nn_modules_") || string_starts_with(op_type, "nn_"))
114 {
115 // torch_nn_modules_conv_Conv2d _conv1_1
116 stat.nn_module_count += 1;
117
118 std::string simname;
119 if (string_starts_with(op_type, "nn_"))
120 {
121 // nn_Conv2d_i -> nn.Conv2d
122 simname = op_type;
123 simname[2] = '.';
124 if (simname.find_first_of('_') != std::string::npos)
125 simname = simname.substr(0, simname.find_first_of('_'));
126 }
127 else
128 {
129 // torch_nn_modules_conv_Conv2d_xyz -> nn.Conv2d
130 char nn_type[256];
131 sscanf(op_type.c_str() + sizeof("torch_nn_modules_") - 1, "%*[^_]_%255[^_]", nn_type);
132 simname = std::string("nn.") + nn_type;
133 }
134
135 if (stat.nn_module_op_count.find(simname) == stat.nn_module_op_count.end())
136 {
137 stat.nn_module_op_count[simname] = 1;
138 }
139 else
140 {
141 stat.nn_module_op_count[simname] = stat.nn_module_op_count[simname] + 1;
142 }
143 continue;
144 }
145
146 // custom module op
147 stat.custom_module_count += 1;
148 }
149
150 // collect called functions
151 std::unordered_set<std::string> called_functions;
152 {
153 for (int i = 0; i < graph.node_size(); i++)
154 {
155 const onnx::NodeProto& node = graph.node(i);
156
157 std::string op_type = node.op_type();
158
159 // drop |folded_N suffix
160 if (op_type.size() > 8)
161 {
162 size_t folded_N_index = op_type.rfind("|folded_");
163 if (folded_N_index != std::string::npos)
164 {
165 op_type = op_type.substr(0, folded_N_index);
166 }
167 }
168
169 if (node.domain().empty())
170 {
171 // native onnx op
172 continue;
173 }
174
175 if (string_starts_with(op_type, "aten_") || string_starts_with(op_type, "_aten_"))
176 {
177 // aten_view
178 continue;
179 }
180
181 if (string_starts_with(op_type, "prims_"))
182 {
183 // prims_convert_element_type
184 continue;
185 }
186
187 if ((string_starts_with(op_type, "torch_nn_modules_") && !string_starts_with(op_type, "torch_nn_modules_container_")) || string_starts_with(op_type, "nn_"))
188 {
189 // torch_nn_modules_conv_Conv2d _conv1_1
190 continue;
191 }
192
193 called_functions.insert(op_type);
194 }
195
196 while (1)
197 {
198 bool new_called_function = false;
199
200 for (int i = 0; i < model.functions_size(); i++)
201 {
202 const onnx::FunctionProto& function = model.functions(i);
203
204 if (called_functions.find(function.name()) == called_functions.end())
205 continue;
206
207 for (int j = 0; j < function.node_size(); j++)
208 {
209 const onnx::NodeProto& node = function.node(j);
210
211 std::string op_type = node.op_type();
212
213 // drop |folded_N suffix
214 if (op_type.size() > 8)
215 {
216 size_t folded_N_index = op_type.rfind("|folded_");
217 if (folded_N_index != std::string::npos)
218 {
219 op_type = op_type.substr(0, folded_N_index);
220 }
221 }
222
223 if (node.domain().empty())
224 {
225 // native onnx op
226 continue;
227 }
228
229 if (string_starts_with(op_type, "aten_") || string_starts_with(op_type, "_aten_"))
230 {
231 // aten_view
232 continue;
233 }
234
235 if (string_starts_with(op_type, "prims_"))
236 {
237 // prims_convert_element_type
238 continue;
239 }
240
241 if ((string_starts_with(op_type, "torch_nn_modules_") && !string_starts_with(op_type, "torch_nn_modules_container_")) || string_starts_with(op_type, "nn_"))
242 {
243 // torch_nn_modules_conv_Conv2d _conv1_1
244 continue;
245 }
246
247 if (called_functions.find(op_type) == called_functions.end())
248 {
249 called_functions.insert(op_type);
250 new_called_function = true;
251 }
252 }
253 }
254
255 if (!new_called_function)
256 break;
257 }
258 }
259
260 for (int i = 0; i < model.functions_size(); i++)
261 {
262 const onnx::FunctionProto& function = model.functions(i);
263
264 if (called_functions.find(function.name()) == called_functions.end())
265 continue;
266
267 for (int j = 0; j < function.node_size(); j++)
268 {
269 const onnx::NodeProto& node = function.node(j);
270
271 std::string op_type = node.op_type();
272
273 // drop |folded_N suffix
274 if (op_type.size() > 8)
275 {
276 size_t folded_N_index = op_type.rfind("|folded_");
277 if (folded_N_index != std::string::npos)
278 {
279 op_type = op_type.substr(0, folded_N_index);
280 }
281 }
282
283 if (node.domain().empty())
284 {
285 // native onnx op
286 stat.onnx_count += 1;
287
288 if (stat.onnx_op_count.find(op_type) == stat.onnx_op_count.end())
289 {
290 stat.onnx_op_count[op_type] = 1;
291 }
292 else
293 {
294 stat.onnx_op_count[op_type] = stat.onnx_op_count[op_type] + 1;
295 }
296 continue;
297 }
298
299 if (string_starts_with(op_type, "aten_") || string_starts_with(op_type, "_aten_"))
300 {
301 // aten_view
302 stat.aten_count += 1;
303
304 std::string simname = op_type;
305 if (simname[0] == '_')
306 simname = simname.substr(1);
307 simname[4] = '.';
308
309 if (stat.aten_op_count.find(simname) == stat.aten_op_count.end())
310 {
311 stat.aten_op_count[simname] = 1;
312 }
313 else
314 {
315 stat.aten_op_count[simname] = stat.aten_op_count[simname] + 1;
316 }
317 continue;
318 }
319
320 if (string_starts_with(op_type, "prims_"))
321 {
322 // prims_convert_element_type
323 stat.prims_count += 1;
324
325 std::string simname = op_type;
326 simname[5] = '.';
327
328 if (stat.prims_op_count.find(simname) == stat.prims_op_count.end())
329 {
330 stat.prims_op_count[simname] = 1;
331 }
332 else
333 {
334 stat.prims_op_count[simname] = stat.prims_op_count[simname] + 1;
335 }
336 continue;
337 }
338
339 if (string_starts_with(op_type, "torch_nn_modules_") || string_starts_with(op_type, "nn_"))
340 {
341 // torch_nn_modules_conv_Conv2d _conv1_1
342 stat.nn_module_count += 1;
343
344 std::string simname;
345 if (string_starts_with(op_type, "nn_"))
346 {
347 simname = op_type;
348 simname[2] = '.';
349 if (simname.find_first_of('_') != std::string::npos)
350 simname = simname.substr(0, simname.find_first_of('_'));
351 }
352 else
353 {
354 // torch_nn_modules_conv_Conv2d_xyz -> nn_Conv2d_i
355 char nn_type[256];
356 sscanf(op_type.c_str() + sizeof("torch_nn_modules_") - 1, "%*[^_]_%255[^_]", nn_type);
357 simname = std::string("nn.") + nn_type;
358 }
359
360 if (stat.nn_module_op_count.find(simname) == stat.nn_module_op_count.end())
361 {
362 stat.nn_module_op_count[simname] = 1;
363 }
364 else
365 {
366 stat.nn_module_op_count[simname] = stat.nn_module_op_count[simname] + 1;
367 }
368 continue;
369 }
370
371 // custom module op
372 stat.custom_module_count += 1;
373 }
374 }
375
376 return stat;
377}
378
379void print_model_stat(const ModelStat& oldstat, const ModelStat& newstat)
380{
381 std::set<std::string> nn_module_op_count;
382 std::set<std::string> aten_op_count;
383 std::set<std::string> prims_op_count;
384 std::set<std::string> onnx_op_count;
385 {
386 for (auto& x : oldstat.nn_module_op_count)
387 {
388 nn_module_op_count.insert(x.first);
389 }
390 for (auto& x : newstat.nn_module_op_count)
391 {
392 nn_module_op_count.insert(x.first);
393 }
394
395 for (auto& x : oldstat.aten_op_count)
396 {
397 aten_op_count.insert(x.first);
398 }
399 for (auto& x : newstat.aten_op_count)
400 {
401 aten_op_count.insert(x.first);
402 }
403
404 for (auto& x : oldstat.prims_op_count)
405 {
406 prims_op_count.insert(x.first);
407 }
408 for (auto& x : newstat.prims_op_count)
409 {
410 prims_op_count.insert(x.first);
411 }
412
413 for (auto& x : oldstat.onnx_op_count)
414 {
415 onnx_op_count.insert(x.first);
416 }
417 for (auto& x : newstat.onnx_op_count)
418 {
419 onnx_op_count.insert(x.first);
420 }
421 }
422
423 // resolve longest text
424 int max_op_name_length = 16;
425 for (auto& x : nn_module_op_count)
426 {
427 max_op_name_length = std::max(max_op_name_length, (int)x.size());
428 }
429 for (auto& x : aten_op_count)
430 {
431 max_op_name_length = std::max(max_op_name_length, (int)x.size());
432 }
433 for (auto& x : prims_op_count)
434 {
435 max_op_name_length = std::max(max_op_name_length, (int)x.size());
436 }
437 for (auto& x : onnx_op_count)
438 {
439 max_op_name_length = std::max(max_op_name_length, (int)x.size());
440 }
441
442 fprintf(stderr, "┌─");
443 for (int i = 0; i < max_op_name_length; i++)
444 fprintf(stderr, "─");
445 fprintf(stderr, "─┬──────────┬──────────┐\n");
446
447 fprintf(stderr, "│ %-*s │ orig │ opt │\n", max_op_name_length, "");
448
449 fprintf(stderr, "├─");
450 for (int i = 0; i < max_op_name_length; i++)
451 fprintf(stderr, "─");
452 fprintf(stderr, "─┼──────────┼──────────┤\n");
453
454 if (newstat.node_size < oldstat.node_size)
455 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, "node", oldstat.node_size, newstat.node_size);
456 else
457 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "node", oldstat.node_size, newstat.node_size);
458
459 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "initializer", oldstat.initializer_size, newstat.initializer_size);
460
461 if (newstat.functions_size < oldstat.functions_size)
462 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, "functions", oldstat.functions_size, newstat.functions_size);
463 else
464 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "functions", oldstat.functions_size, newstat.functions_size);
465
466 fprintf(stderr, "├─");
467 for (int i = 0; i < max_op_name_length; i++)
468 fprintf(stderr, "─");
469 fprintf(stderr, "─┼──────────┼──────────┤\n");
470
471 if (newstat.nn_module_count < oldstat.nn_module_count)
472 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, "nn module op", oldstat.nn_module_count, newstat.nn_module_count);
473 else
474 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "nn module op", oldstat.nn_module_count, newstat.nn_module_count);
475
476 if (newstat.custom_module_count < oldstat.custom_module_count)
477 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, "custom module op", oldstat.custom_module_count, newstat.custom_module_count);
478 else
479 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "custom module op", oldstat.custom_module_count, newstat.custom_module_count);
480
481 if (newstat.aten_count < oldstat.aten_count)
482 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, "aten op", oldstat.aten_count, newstat.aten_count);
483 else
484 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "aten op", oldstat.aten_count, newstat.aten_count);
485
486 if (newstat.prims_count < oldstat.prims_count)
487 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, "prims op", oldstat.prims_count, newstat.prims_count);
488 else
489 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "prims op", oldstat.prims_count, newstat.prims_count);
490
491 if (newstat.onnx_count < oldstat.onnx_count)
492 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, "onnx native op", oldstat.onnx_count, newstat.onnx_count);
493 else
494 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, "onnx native op", oldstat.onnx_count, newstat.onnx_count);
495
496 fprintf(stderr, "├─");
497 for (int i = 0; i < max_op_name_length; i++)
498 fprintf(stderr, "─");
499 fprintf(stderr, "─┼──────────┼──────────┤\n");
500
501 // merge nn_module_op_count
502 {
503 for (auto x : nn_module_op_count)
504 {
505 int oldcount = 0;
506 int newcount = 0;
507 if (oldstat.nn_module_op_count.find(x) != oldstat.nn_module_op_count.end())
508 {
509 oldcount = oldstat.nn_module_op_count.at(x);
510 }
511 if (newstat.nn_module_op_count.find(x) != newstat.nn_module_op_count.end())
512 {
513 newcount = newstat.nn_module_op_count.at(x);
514 }
515
516 if (newcount < oldcount)
517 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, x.c_str(), oldcount, newcount);
518 else
519 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, x.c_str(), oldcount, newcount);
520 }
521
522 if (!nn_module_op_count.empty())
523 {
524 fprintf(stderr, "├─");
525 for (int i = 0; i < max_op_name_length; i++)
526 fprintf(stderr, "─");
527 fprintf(stderr, "─┼──────────┼──────────┤\n");
528 }
529 }
530
531 // merge aten_op_count
532 {
533 for (auto x : aten_op_count)
534 {
535 int oldcount = 0;
536 int newcount = 0;
537 if (oldstat.aten_op_count.find(x) != oldstat.aten_op_count.end())
538 {
539 oldcount = oldstat.aten_op_count.at(x);
540 }
541 if (newstat.aten_op_count.find(x) != newstat.aten_op_count.end())
542 {
543 newcount = newstat.aten_op_count.at(x);
544 }
545
546 if (newcount < oldcount)
547 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, x.c_str(), oldcount, newcount);
548 else
549 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, x.c_str(), oldcount, newcount);
550 }
551
552 if (!aten_op_count.empty())
553 {
554 fprintf(stderr, "├─");
555 for (int i = 0; i < max_op_name_length; i++)
556 fprintf(stderr, "─");
557 fprintf(stderr, "─┼──────────┼──────────┤\n");
558 }
559 }
560
561 // merge prims_op_count
562 {
563 for (auto x : prims_op_count)
564 {
565 int oldcount = 0;
566 int newcount = 0;
567 if (oldstat.prims_op_count.find(x) != oldstat.prims_op_count.end())
568 {
569 oldcount = oldstat.prims_op_count.at(x);
570 }
571 if (newstat.prims_op_count.find(x) != newstat.prims_op_count.end())
572 {
573 newcount = newstat.prims_op_count.at(x);
574 }
575
576 if (newcount < oldcount)
577 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, x.c_str(), oldcount, newcount);
578 else
579 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, x.c_str(), oldcount, newcount);
580 }
581
582 if (!prims_op_count.empty())
583 {
584 fprintf(stderr, "├─");
585 for (int i = 0; i < max_op_name_length; i++)
586 fprintf(stderr, "─");
587 fprintf(stderr, "─┼──────────┼──────────┤\n");
588 }
589 }
590
591 // merge onnx_op_count
592 {
593 for (auto x : onnx_op_count)
594 {
595 int oldcount = 0;
596 int newcount = 0;
597 if (oldstat.onnx_op_count.find(x) != oldstat.onnx_op_count.end())
598 {
599 oldcount = oldstat.onnx_op_count.at(x);
600 }
601 if (newstat.onnx_op_count.find(x) != newstat.onnx_op_count.end())
602 {
603 newcount = newstat.onnx_op_count.at(x);
604 }
605
606 if (newcount < oldcount)
607 fprintf(stderr, "│ %-*s │ %-8d │ \033[32m%-8d\033[0m │\n", max_op_name_length, x.c_str(), oldcount, newcount);
608 else
609 fprintf(stderr, "│ %-*s │ %-8d │ %-8d │\n", max_op_name_length, x.c_str(), oldcount, newcount);
610 }
611 }
612
613 fprintf(stderr, "└─");
614 for (int i = 0; i < max_op_name_length; i++)
615 fprintf(stderr, "─");
616 fprintf(stderr, "─┴──────────┴──────────┘\n");
617}
618
619} // namespace onnx2pnnx
620
621} // namespace pnnx