GraphBuilder#
onnx-light builds ONNX graphs, models and functions incrementally
through core::builder::GraphBuilder (C++) and its Python wrapper
onnx_light.onnx_core.graph_builder.GraphBuilder. The builder is
the entry point of the core pipeline: it accumulates nodes, resolves
their opsets, validates them against the built-in operator schemas, runs
incremental shape inference and finalises everything into a proto.
Overview#
A builder starts empty and holds a compute context. It records every
value name it hands out so a name is never reused, which keeps the
successor and predecessor maps valid while the graph grows and while
patterns rewrite it (see Pattern optimization). Nodes are added
with make_node(),
which:
resolves the operator opset for the requested domain;
validates the node against the built-in ONNX operator schemas;
assigns output names when the caller leaves them empty;
runs incremental shape inference so every value has an inferred type and shape as soon as it is created.
to_onnx()
finalises the accumulated graph into a model (default), a graph or a
function, writing back the inferred shapes, the in-place /
release-after metadata, the value tags and the peak-memory estimates.
Typical usage#
from onnx_light.onnx_core.graph_builder import GraphBuilder
from onnx_light.onnx_proto import TensorProto
builder = GraphBuilder("g")
builder.make_input("x", TensorProto.FLOAT, [2, 3])
builder.make_input("y", TensorProto.FLOAT, [2, 3])
(z,) = builder.make_node("Add", ["x", "y"])
builder.make_output(z)
model = builder.to_onnx("model")
The same builder can be constructed from an existing ModelProto to
optimize or extend a model that was produced elsewhere.
Relation to the rest of the core pipeline#
The builder is the shared foundation of the other core components:
Pattern optimization rewrites the graph held by a builder; the optimizer reuses the builder’s shape and type inference, its constant knowledge and its cleanup passes instead of duplicating them. See Pattern optimization.
Shape inference is the same engine the builder invokes incrementally; the standalone entry point is described in Shape inference.
Constant folding replaces subgraphs whose inputs are all constant by their computed value, driven by the runtime kernels described in Runtime Design.
API reference#
Python API:
onnx_light.onnx_core.graph_builder.GraphBuilder.C++ API: builder.
Examples#
pretty_onnx: shape info, shape tags, inplace and release annotations inspects a model built and rendered through the builder.
ComputeContext memory expressions and Statistics on the weights of an ONNX model use the builder to report the peak-memory and initializer statistics it estimates.