onnx_light.tools.dot#
Convert an ONNX model or graph to a Graphviz DOT source string.
The resulting string can be written to a .dot file and then rendered
with dot -Tsvg model.dot -o model.svg or any other Graphviz tool:
from onnx_light.tools import to_dot
with open("model.dot", "w", encoding="utf-8") as f:
f.write(to_dot(model))
The converter is implemented in pure Python and only depends on the
attributes of the standard ONNX message types (ModelProto,
GraphProto, NodeProto, ValueInfoProto, TensorProto and
TensorShapeProto). It therefore works both with messages built by
onnx_light and with messages built by the upstream onnx
package.
- onnx_light.tools.dot.to_dot(model_or_graph: Any, *, direction: str = 'TB', include_initializers: bool = True, include_shapes: bool = True, include_attributes: bool = False, include_inplace: bool = False) str#
Renders an ONNX
ModelProtoorGraphProtoas a Graphviz DOT string.- Parameters:
model_or_graph – A
ModelProtoorGraphProtoinstance. Bothonnx_lightandonnxmessages are accepted.direction – Graphviz layout direction; one of
"TB"(top-to-bottom),"BT"(bottom-to-top),"LR"(left-to-right), or"RL"(right-to-left). Defaults to"TB".include_initializers – When
True, initializers are rendered as separate (cylinder) nodes and connected to the consumers they feed. WhenFalse, initializer tensors are not shown.include_shapes – When
True, tensor type/shape information available in graph inputs, outputs,value_infoand initializers is appended to the corresponding node labels.include_attributes – When
True, node attribute names are listed inside the operator label.include_inplace – When
True, the in-place reuse opportunities recorded in each node’smetadata_props(under theonnx_light.inplace_reusekey) are appended to the operator label, for exampleinplace: out0=in1(equal).
- Returns:
The DOT source as a single
str(newline-separated). Write it to a.dotfile and render it with, for example,dot -Tsvg model.dot -o model.svg.- Raises:
TypeError – If
model_or_graphis neither aModelProtonor aGraphProto.ValueError – If
directionis not a supported Graphviz direction.
The example below builds a small
Abschain and renders the DOT source:digraph onnx { rankdir="TB"; node [fontname="Helvetica", fontsize=10]; edge [fontname="Helvetica", fontsize=9]; "t_X" [label="X\nfloat[3,4]", shape=ellipse, style="filled", fillcolor="#cde4ff", color="#3a6ea5"]; "n_Abs_0" [label="Abs", shape=box, style="filled", fillcolor="#d4ecd4", color="#3a8c3a"]; "t_X" -> "n_Abs_0" [label="float[3,4]"]; "n_Abs_0" -> "t_A"; "n_Abs_1" [label="Abs", shape=box, style="filled", fillcolor="#d4ecd4", color="#3a8c3a"]; "t_A" -> "n_Abs_1"; "n_Abs_1" -> "t_B"; "n_Abs_2" [label="Abs", shape=box, style="filled", fillcolor="#d4ecd4", color="#3a8c3a"]; "t_B" -> "n_Abs_2"; "n_Abs_2" -> "t_Y" [label="float[3,4]"]; "t_Y" [label="Y\nfloat[3,4]", shape=ellipse, style="filled", fillcolor="#ffe1b3", color="#a35a00"]; }
- onnx_light.tools.dot.to_dot_graph(graph: Any, *, direction: str = 'TB', include_initializers: bool = True, include_shapes: bool = True, include_attributes: bool = False, include_inplace: bool = False) str#
Renders a
GraphProtoas a Graphviz DOT string.See
to_dot()for the meaning of every parameter.