Command Line: python -m yobx print#

This example builds a small ONNX model, saves it to a temporary file, and then demonstrates the different output formats produced by the python -m yobx print command.

The same result can be achieved from the terminal with:

python -m yobx print pretty   model.onnx
python -m yobx print printer  model.onnx
python -m yobx print dot      model.onnx
python -m yobx print mermaid  model.onnx
import os
import tempfile
from yobx._onnx_shim import onnx
import onnx_light.onnx.helper as oh
from yobx._command_lines_parser import _cmd_print

TFLOAT = onnx.TensorProto.FLOAT

Build a small ONNX model#

The graph computes Z = Relu(X + Y) with static shapes (2, 3).

model = oh.make_model(
    oh.make_graph(
        [oh.make_node("Add", ["X", "Y"], ["added"]), oh.make_node("Relu", ["added"], ["Z"])],
        "add_relu",
        [
            oh.make_tensor_value_info("X", TFLOAT, [2, 3]),
            oh.make_tensor_value_info("Y", TFLOAT, [2, 3]),
        ],
        [oh.make_tensor_value_info("Z", TFLOAT, [2, 3])],
    ),
    opset_imports=[oh.make_opsetid("", 18)],
    ir_version=10,
)

# Save to a temporary file so the CLI helpers can load it.
fd, tmp = tempfile.mkstemp(suffix=".onnx")
os.close(fd)
onnx.save(model, tmp)

pretty format#

The default format produced by yobx.helpers.onnx_helper.pretty_onnx(). It shows opset, inputs/outputs, and every node in a compact, human-readable layout.

print("python -m yobx print pretty model.onnx")
print("-" * 40)
_cmd_print(["print", "pretty", tmp])
python -m yobx print pretty model.onnx
----------------------------------------
opset: domain='' version=18
input: name='X' type=dtype('float32') shape=[2, 3]
input: name='Y' type=dtype('float32') shape=[2, 3]
Add(X, Y) -> added
  Relu(added) -> Z
output: name='Z' type=dtype('float32') shape=[2, 3]

printer format#

Uses the built-in onnx.printer.to_text renderer which produces the official ONNX text representation.

print("python -m yobx print printer model.onnx")
print("-" * 40)
_cmd_print(["print", "printer", tmp])
python -m yobx print printer model.onnx
----------------------------------------
<
   ir_version: 10,
   opset_import: ["" : 18]
>
add_relu (float[2,3] X, float[2,3] Y) => (float[2,3] Z) {
   added = Add (X, Y)
   Z = Relu (added)
}

dot format#

Dumps the DOT graph source. Pipe the output to dot -Tsvg to get a visual representation of the graph (see also yobx.helpers.dot_helper.to_dot()).

print("python -m yobx print dot model.onnx")
print("-" * 40)
_cmd_print(["print", "dot", tmp])
python -m yobx print dot model.onnx
----------------------------------------
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(2,3)", fillcolor="#aaeeaa"];
  I_1 [label="Y\nFLOAT(2,3)", fillcolor="#aaeeaa"];
  Add_2 [label="Add(., .)", fillcolor="#cccccc"];
  Relu_3 [label="Relu(.)", fillcolor="#cccccc"];
  I_0 -> Add_2 [label="FLOAT(2,3)"];
  I_1 -> Add_2 [label="FLOAT(2,3)"];
  Add_2 -> Relu_3 [label="FLOAT(2,3)"];
  O_4 [label="Z\nFLOAT(2,3)", fillcolor="#aaaaee"];
  Relu_3 -> O_4;
}

mermaid format#

Dumps a Mermaid flowchart TD diagram. Paste the output into the Mermaid live editor or any Markdown renderer that supports Mermaid fenced code blocks to get an interactive graph visualisation (see also onnx_light.tools.to_mermaid()).

print("python -m yobx print mermaid model.onnx")
print("-" * 40)
_cmd_print(["print", "mermaid", tmp])
python -m yobx print mermaid model.onnx
----------------------------------------
flowchart TB
    t_X(["X<br/>float[2,3]"]):::onnxInput
    t_Y(["Y<br/>float[2,3]"]):::onnxInput
    n_Add_0["Add"]:::onnxOp
    t_X -->|"float[2,3]"| n_Add_0
    t_Y -->|"float[2,3]"| n_Add_0
    n_Add_0 --> t_added
    n_Relu_1["Relu"]:::onnxOp
    t_added --> n_Relu_1
    n_Relu_1 -->|"float[2,3]"| t_Z
    t_Z(["Z<br/>float[2,3]"]):::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;

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

Related examples

ONNX Graph Visualization with to_dot

ONNX Graph Visualization with to_dot

ONNX Graph Visualization with to_mermaid

ONNX Graph Visualization with to_mermaid

ExtendedModelContainer: large-initializer ONNX models

ExtendedModelContainer: large-initializer ONNX models

Gallery generated by Sphinx-Gallery