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 ModelProto or GraphProto as a Graphviz DOT string.

Parameters:
  • model_or_graph – A ModelProto or GraphProto instance. Both onnx_light and onnx messages 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. When False, initializer tensors are not shown.

  • include_shapes – When True, tensor type/shape information available in graph inputs, outputs, value_info and 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’s metadata_props (under the onnx_light.inplace_reuse key) are appended to the operator label, for example inplace: out0=in1(equal).

Returns:

The DOT source as a single str (newline-separated). Write it to a .dot file and render it with, for example, dot -Tsvg model.dot -o model.svg.

Raises:
  • TypeError – If model_or_graph is neither a ModelProto nor a GraphProto.

  • ValueError – If direction is not a supported Graphviz direction.

The example below builds a small Abs chain 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 GraphProto as a Graphviz DOT string.

See to_dot() for the meaning of every parameter.