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 NativeShapeInference 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.

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:

  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.

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#

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))

float[batch,seq,4]

float[batch,seq,4]

float[4,2]

float[batch,seq,2]

X
float[batch,seq,4]

Y
float[batch,seq,4]

W
float[4,2]

Add

t_added

MatMul

t_mm

Relu

Z
float[batch,seq,2]



Total running time of the script: (0 minutes 5.462 seconds)

Related examples

ONNX Graph Visualization with to_dot

ONNX Graph Visualization with to_dot

Translating ONNX models to source code

Translating ONNX models to source code

Command Line: python -m yobx print

Command Line: python -m yobx print

Gallery generated by Sphinx-Gallery