onnx_light.tools.svg#

Convert an ONNX model or graph to a standalone SVG image.

The resulting string is a self-contained <svg> document that can be written to a .svg file or embedded directly in an HTML page:

from onnx_light.tools import to_svg

with open("model.svg", "w", encoding="utf-8") as f:
    f.write(to_svg(model))

Unlike onnx_light.tools.to_mermaid(), which produces source that still needs a Mermaid renderer, to_svg() performs a simple layered layout itself and emits ready-to-display SVG. The converter is implemented in pure Python and only depends on the attributes of the standard ONNX message types (ModelProto, GraphProto, NodeProto, ValueInfoProto, TensorProto and TensorShapeProto). It therefore works both with messages built by onnx_light and with messages built by the upstream onnx package.

onnx_light.tools.svg.to_svg(model_or_graph: Any, *, direction: str = 'TB', include_initializers: bool = True, include_shapes: bool = True, include_attributes: bool = False, include_inplace: bool = False, include_release: bool = False) str#

Renders an ONNX ModelProto or GraphProto as an SVG image.

Parameters:
  • model_or_graph – A ModelProto or GraphProto instance. Both onnx_light and onnx messages are accepted.

  • direction – Layout direction; "TB" (or "TD") lays the graph out top-to-bottom and "LR" left-to-right. Defaults to "TB".

  • include_initializers – When True, initializers are rendered as separate (dashed) boxes connected to their consumers. When False, initializer tensors are not shown.

  • include_shapes – When True, tensor type/shape information available in graph inputs, outputs, value_info and initializers is appended to the corresponding box labels.

  • include_attributes – When True, node attribute names are listed inside the operator label.

  • include_inplace – When True, the in-place reuse opportunities recorded in each node’s metadata_props (under the onnx_light.inplace_reuse key) are appended to the operator label, for example inplace: out0=in1(equal).

  • include_release – When True, the post-execution release hints recorded in each node’s metadata_props (under the onnx_light.release_after key) are appended to the operator label, for example release: A, B.

Returns:

A self-contained SVG document as a single str.

Raises:
  • TypeError – If model_or_graph is neither a ModelProto nor a GraphProto.

  • ValueError – If direction is not a supported direction.

The example below builds a small Abs chain, runs shape inference and records the in-place reuse opportunities into the graph metadata with onnx_light.onnx_optim.shape_inference.write_inplace_reuse_to_metadata(), then renders the annotated diagram with include_inplace=True:

X · float[3,4] A B Y · float[3,4] Xfloat[3,4] Abs Absinplace: out0=in0(equal) Absinplace: out0=in0(equal) float[3,4]
onnx_light.tools.svg.to_svg_graph(graph: Any, *, direction: str = 'TB', include_initializers: bool = True, include_shapes: bool = True, include_attributes: bool = False, include_inplace: bool = False, include_release: bool = False) str#

Renders a GraphProto as an SVG image.

See to_svg() for the meaning of every parameter.

Example#

The following snippet builds a small model and renders it with onnx_light.tools.to_svg(). The resulting image is embedded below the code so it can be inspected directly in the documentation.

from onnx_light.onnx_lib import TensorProto
from onnx_light.onnx.helper import (
    make_model,
    make_node,
    make_graph,
    make_tensor_value_info,
)
from onnx_light.tools import to_svg

X = make_tensor_value_info("X", TensorProto.FLOAT, [None, None])
A = make_tensor_value_info("A", TensorProto.FLOAT, [None, None])
B = make_tensor_value_info("B", TensorProto.FLOAT, [None, None])
Y = make_tensor_value_info("Y", TensorProto.FLOAT, [None])

node1 = make_node("MatMul", ["X", "A"], ["XA"])
node2 = make_node("Add", ["XA", "B"], ["Y"])
graph = make_graph([node1, node2], "example", [X, A, B], [Y])
model = make_model(graph)

svg = to_svg(model)
X · float[?,?] A · float[?,?] XA B · float[?,?] Y · float[?] Xfloat[?,?] Afloat[?,?] Bfloat[?,?] MatMul Add float[?]