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
ModelProtoorGraphProtoas an SVG image.- Parameters:
model_or_graph – A
ModelProtoorGraphProtoinstance. Bothonnx_lightandonnxmessages 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. WhenFalse, initializer tensors are not shown.include_shapes – When
True, tensor type/shape information available in graph inputs, outputs,value_infoand 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’smetadata_props(under theonnx_light.inplace_reusekey) are appended to the operator label, for exampleinplace: out0=in1(equal).include_release – When
True, the post-execution release hints recorded in each node’smetadata_props(under theonnx_light.release_afterkey) are appended to the operator label, for examplerelease: A, B.
- Returns:
A self-contained SVG document as a single
str.- Raises:
TypeError – If
model_or_graphis neither aModelProtonor aGraphProto.ValueError – If
directionis not a supported direction.
The example below builds a small
Abschain, runs shape inference and records the in-place reuse opportunities into the graph metadata withonnx_light.onnx_optim.shape_inference.write_inplace_reuse_to_metadata(), then renders the annotated diagram withinclude_inplace=True:
- 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
GraphProtoas 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)