.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_dot_graph.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_dot_graph.py: .. _l-plot-dot-graph: ONNX Graph Visualization with to_dot ====================================== :func:`to_dot ` converts an :class:`onnx.ModelProto` into a `DOT `_ string that can be rendered by `Graphviz `_. The function: * assigns different fill colors to well-known op-types (``Shape``, ``MatMul``, ``Reshape``, …), * inlines small scalar constants and 1-D initializers whose length is ≤ 9 directly onto the node label so the graph stays compact, * uses :class:`BasicShapeBuilder ` to annotate every edge with its inferred dtype and shape (when available), * handles ``Scan`` / ``Loop`` / ``If`` sub-graphs by drawing dotted edges for outer-scope values consumed by the sub-graph. The output is a plain DOT string; it can be saved to a ``.dot`` file or passed to any graphviz renderer (``dot -Tsvg``, ``dot -Tpng``, …). .. GENERATED FROM PYTHON SOURCE LINES 25-34 .. code-block:: Python import numpy as np import onnx import onnx.helper as oh import onnx.numpy_helper as onh from yobx.helpers.dot_helper import to_dot TFLOAT = onnx.TensorProto.FLOAT .. GENERATED FROM PYTHON SOURCE LINES 35-43 Build a small model -------------------- The graph performs the following operations: 1. ``Add(X, Y)`` — element-wise sum with shape ``(batch, seq, d)``. 2. ``MatMul(added, W)`` — project the last dimension to ``d//2``. 3. ``Relu(Z)`` — element-wise ReLU activation. .. GENERATED FROM PYTHON SOURCE LINES 43-68 .. code-block:: Python model = oh.make_model( oh.make_graph( [ oh.make_node("Add", ["X", "Y"], ["added"]), oh.make_node("MatMul", ["added", "W"], ["mm"]), oh.make_node("Relu", ["mm"], ["Z"]), ], "add_matmul_relu", [ oh.make_tensor_value_info("X", TFLOAT, ["batch", "seq", 4]), oh.make_tensor_value_info("Y", TFLOAT, ["batch", "seq", 4]), ], [oh.make_tensor_value_info("Z", TFLOAT, ["batch", "seq", 2])], [ onh.from_array( np.random.randn(4, 2).astype(np.float32), name="W", ) ], ), opset_imports=[oh.make_opsetid("", 18)], ir_version=10, ) .. GENERATED FROM PYTHON SOURCE LINES 69-75 Convert to DOT --------------- :func:`to_dot ` returns the DOT source as a plain string. You can write it to a file and render it with ``dot -Tsvg graph.dot > graph.svg``. .. GENERATED FROM PYTHON SOURCE LINES 75-79 .. code-block:: Python dot_src = to_dot(model) print(dot_src) .. rst-class:: sphx-glr-script-out .. code-block:: none digraph { graph [rankdir=TB, splines=true, overlap=false, nodesep=0.2, ranksep=0.2, fontsize=8]; node [style="rounded,filled", color="#888888", fontcolor="#222222", shape=box]; edge [arrowhead=vee, fontsize=7, labeldistance=-5, labelangle=0]; I_0 [label="X\nFLOAT(batch,seq,4)", fillcolor="#aaeeaa"]; I_1 [label="Y\nFLOAT(batch,seq,4)", fillcolor="#aaeeaa"]; i_2 [label="W\nFLOAT(4, 2)", fillcolor="#cccc00"]; Add_3 [label="Add(., .)", fillcolor="#cccccc"]; MatMul_4 [label="MatMul(., .)", fillcolor="#ee9999"]; Relu_5 [label="Relu(.)", fillcolor="#cccccc"]; I_0 -> Add_3 [label="FLOAT(batch,seq,4)"]; I_1 -> Add_3 [label="FLOAT(batch,seq,4)"]; Add_3 -> MatMul_4 [label="FLOAT(batch,seq,4)"]; i_2 -> MatMul_4 [label="FLOAT(4, 2)"]; MatMul_4 -> Relu_5 [label="FLOAT(batch,seq,2)"]; O_6 [label="Z\nFLOAT(batch,seq,2)", fillcolor="#aaaaee"]; Relu_5 -> O_6; } .. GENERATED FROM PYTHON SOURCE LINES 80-121 Display the graph ------------------ The DOT source produced above describes the following graph. .. gdot:: :script: DOT-SECTION :process: import numpy as np import onnx import onnx.helper as oh import onnx.numpy_helper as onh from yobx.helpers.dot_helper import to_dot TFLOAT = onnx.TensorProto.FLOAT model = oh.make_model( oh.make_graph( [ oh.make_node("Add", ["X", "Y"], ["added"]), oh.make_node("MatMul", ["added", "W"], ["mm"]), oh.make_node("Relu", ["mm"], ["Z"]), ], "add_matmul_relu", [ oh.make_tensor_value_info("X", TFLOAT, ["batch", "seq", 4]), oh.make_tensor_value_info("Y", TFLOAT, ["batch", "seq", 4]), ], [oh.make_tensor_value_info("Z", TFLOAT, ["batch", "seq", 2])], [ onh.from_array( np.random.randn(4, 2).astype(np.float32), name="W", ) ], ), opset_imports=[oh.make_opsetid("", 18)], ir_version=10, ) dot = to_dot(model) print("DOT-SECTION", dot) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.033 seconds) .. _sphx_glr_download_auto_examples_plot_dot_graph.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dot_graph.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dot_graph.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_dot_graph.zip ` .. include:: plot_dot_graph.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_