.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples_core/plot_mermaid_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_core_plot_mermaid_graph.py: .. _l-plot-mermaid-graph: ONNX Graph Visualization with to_mermaid ========================================== :func:`to_mermaid ` converts an :class:`onnx.ModelProto` into a `Mermaid `_ ``flowchart TD`` string that can be rendered by any Mermaid-compatible viewer (e.g. GitHub Markdown, the Mermaid live editor, or Sphinx with the ``sphinxcontrib-mermaid`` extension). The function: * assigns different CSS classes to different node kinds (inputs are green, initializers are yellow, operators are light-grey, outputs are light-blue), * 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 Mermaid string; it can be embedded directly in Markdown or saved to a ``.mmd`` file. .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: Python import numpy as np import onnx import onnx.helper as oh import onnx.numpy_helper as onh from IPython.display import HTML from yobx.doc import draw_graph_mermaid from yobx.helpers.mermaid_helper import to_mermaid TFLOAT = onnx.TensorProto.FLOAT .. GENERATED FROM PYTHON SOURCE LINES 39-47 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 47-67 .. 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 68-71 Convert to Mermaid ------------------- .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python mermaid_src = to_mermaid(model) print(mermaid_src) .. rst-class:: sphx-glr-script-out .. code-block:: none flowchart TD I_0["X\nFLOAT(batch,seq,4)"]:::input I_1["Y\nFLOAT(batch,seq,4)"]:::input i_2["W\nFLOAT(4, 2)"]:::init Add_3["Add(., .)"]:::op MatMul_4["MatMul(., .)"]:::op Relu_5["Relu(.)"]:::op I_0 -->|"FLOAT(batch,seq,4)"| Add_3 I_1 -->|"FLOAT(batch,seq,4)"| Add_3 Add_3 -->|"FLOAT(batch,seq,4)"| MatMul_4 i_2 -->|"FLOAT(4, 2)"| MatMul_4 MatMul_4 -->|"FLOAT(batch,seq,2)"| Relu_5 O_6["Z\nFLOAT(batch,seq,2)"]:::output Relu_5 --> O_6 classDef input fill:#aaeeaa,stroke:#00aa00,color:#000 classDef init fill:#cccc00,stroke:#888800,color:#000 classDef op fill:#cccccc,stroke:#666666,color:#000 classDef output fill:#aaaaee,stroke:#0000aa,color:#000 .. GENERATED FROM PYTHON SOURCE LINES 76-82 Display the graph ------------------ The diagram is rendered to SVG via the ``mermaid.ink`` online service (through :epkg:`mermaid-py`) and displayed by wrapping the SVG content in :class:`IPython.display.HTML` so that sphinx-gallery captures and embeds it. .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python HTML(draw_graph_mermaid(model)) .. raw:: html

FLOAT(batch,seq,4)

FLOAT(batch,seq,4)

FLOAT(batch,seq,4)

FLOAT(4, 2)

FLOAT(batch,seq,2)

X\nFLOAT(batch,seq,4)

Y\nFLOAT(batch,seq,4)

W\nFLOAT(4, 2)

Add(., .)

MatMul(., .)

Relu(.)

Z\nFLOAT(batch,seq,2)



.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.803 seconds) .. _sphx_glr_download_auto_examples_core_plot_mermaid_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_mermaid_graph.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mermaid_graph.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mermaid_graph.zip ` .. include:: plot_mermaid_graph.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_