Note
Go to the end to download the full example code.
ONNX Graph Visualization with to_mermaid#
to_mermaid converts an
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
NativeShapeInferenceto annotate every edge with its inferred dtype and shape (when available),handles
Scan/Loop/Ifsub-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.
import numpy as np
from yobx._onnx_shim import onnx
import onnx_light.onnx.helper as oh
import onnx_light.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
Build a small model#
The graph performs the following operations:
Add(X, Y)— element-wise sum with shape(batch, seq, d).MatMul(added, W)— project the last dimension tod//2.Relu(Z)— element-wise ReLU activation.
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,
)
Convert to Mermaid#
mermaid_src = to_mermaid(model)
print(mermaid_src)
flowchart TB
t_X(["X<br/>float[batch,seq,4]"]):::onnxInput
t_Y(["Y<br/>float[batch,seq,4]"]):::onnxInput
t_W[("W<br/>float[4,2]")]:::onnxInitializer
n_Add_0["Add"]:::onnxOp
t_X -->|"float[batch,seq,4]"| n_Add_0
t_Y -->|"float[batch,seq,4]"| n_Add_0
n_Add_0 --> t_added
n_MatMul_1["MatMul"]:::onnxOp
t_added --> n_MatMul_1
t_W -->|"float[4,2]"| n_MatMul_1
n_MatMul_1 --> t_mm
n_Relu_2["Relu"]:::onnxOp
t_mm --> n_Relu_2
n_Relu_2 -->|"float[batch,seq,2]"| t_Z
t_Z(["Z<br/>float[batch,seq,2]"]):::onnxOutput
classDef onnxInput fill:#cde4ff,stroke:#3a6ea5,color:#000;
classDef onnxOutput fill:#ffe1b3,stroke:#a35a00,color:#000;
classDef onnxInitializer fill:#eeeeee,stroke:#888,color:#000;
classDef onnxOp fill:#d4ecd4,stroke:#3a8c3a,color:#000;
classDef onnxTagShape fill:#f4d6ff,stroke:#8744a2,color:#000;
classDef onnxTagAxes fill:#ffe9a8,stroke:#9e7a00,color:#000;
classDef onnxTagWeight fill:#e0e0e0,stroke:#666666,color:#000;
classDef onnxTagAmbiguous fill:#ffd9d9,stroke:#a33a3a,color:#000;
Display the graph#
The diagram is rendered to SVG via the mermaid.ink online service (through
mermaid-py) and displayed by wrapping the SVG content in
IPython.display.HTML so that sphinx-gallery captures and embeds it.
HTML(draw_graph_mermaid(model))
Total running time of the script: (0 minutes 5.462 seconds)
Related examples