1111from argparse import ArgumentParser , RawTextHelpFormatter , BooleanOptionalAction
1212
1313
14+ def get_parser_dot () -> ArgumentParser :
15+ parser = ArgumentParser (
16+ prog = "dot" ,
17+ description = textwrap .dedent (
18+ """
19+ Converts a model into a dot file dot can draw into a graph.
20+ """
21+ ),
22+ )
23+ parser .add_argument ("input" , type = str , help = "onnx model to lighten" )
24+ parser .add_argument (
25+ "-o" ,
26+ "--output" ,
27+ default = "" ,
28+ type = str ,
29+ required = False ,
30+ help = "dot model to output or empty to print out the result" ,
31+ )
32+ parser .add_argument (
33+ "-v" ,
34+ "--verbose" ,
35+ type = int ,
36+ default = 0 ,
37+ required = False ,
38+ help = "verbosity" ,
39+ )
40+ parser .add_argument (
41+ "-r" ,
42+ "--run" ,
43+ default = "" ,
44+ required = False ,
45+ help = "run dot, in that case, format must be given (svg, png)" ,
46+ )
47+ return parser
48+
49+
50+ def _cmd_dot (argv : List [Any ]):
51+ import subprocess
52+ from .helpers .dot_helper import to_dot
53+
54+ parser = get_parser_dot ()
55+ args = parser .parse_args (argv [1 :])
56+ if args .verbose :
57+ print (f"-- loads { args .input !r} " )
58+ onx = onnx .load (args .input , load_external_data = False )
59+ if args .verbose :
60+ print ("-- converts into dot" )
61+ dot = to_dot (onx )
62+ if args .output :
63+ if args .verbose :
64+ print (f"-- saves into { args .output } " )
65+ with open (args .output , "w" ) as f :
66+ f .write (dot )
67+ else :
68+ print (dot )
69+ if args .run :
70+ assert args .output , "Cannot run dot without an output file."
71+ cmds = ["dot" , f"-T{ args .run } " , args .output , "-o" , f"{ args .output } .{ args .run } " ]
72+ if args .verbose :
73+ print (f"-- run { ' ' .join (cmds )} " )
74+ p = subprocess .Popen (cmds , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
75+ res = p .communicate ()
76+ out , err = res
77+ if out :
78+ print ("--" )
79+ print (out )
80+ if err :
81+ print ("--" )
82+ print (err )
83+
84+
1485def get_parser_lighten () -> ArgumentParser :
1586 parser = ArgumentParser (
1687 prog = "lighten" ,
@@ -1412,6 +1483,7 @@ def get_main_parser() -> ArgumentParser:
14121483
14131484 agg - aggregates statistics from multiple files
14141485 config - prints a configuration for a model id
1486+ dot - converts an onnx model into dot format
14151487 exportsample - produces a code to export a model
14161488 find - find node consuming or producing a result
14171489 lighten - makes an onnx model lighter by removing the weights,
@@ -1428,6 +1500,7 @@ def get_main_parser() -> ArgumentParser:
14281500 choices = [
14291501 "agg" ,
14301502 "config" ,
1503+ "dot" ,
14311504 "exportsample" ,
14321505 "find" ,
14331506 "lighten" ,
@@ -1446,6 +1519,7 @@ def main(argv: Optional[List[Any]] = None):
14461519 fcts = dict (
14471520 agg = _cmd_agg ,
14481521 config = _cmd_config ,
1522+ dot = _cmd_dot ,
14491523 exportsample = _cmd_export_sample ,
14501524 find = _cmd_find ,
14511525 lighten = _cmd_lighten ,
@@ -1470,6 +1544,7 @@ def main(argv: Optional[List[Any]] = None):
14701544 parsers = dict (
14711545 agg = get_parser_agg ,
14721546 config = get_parser_config ,
1547+ dot = get_parser_dot ,
14731548 exportsample = lambda : get_parser_validate ("exportsample" ), # type: ignore[operator]
14741549 find = get_parser_find ,
14751550 lighten = get_parser_lighten ,
0 commit comments