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, requires explicit opsets, validates nodes 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:

  • requires an explicitly selected positive opset for the requested domain, raising an exception before adding the node when no version was selected;

  • 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#

import numpy as np

from onnx_light.onnx import TensorProto
from onnx_light.onnx_core.graph_builder import GraphBuilder

g = GraphBuilder("g")
g.set_opset_version("", 18)
x = g.inp("X", TensorProto.FLOAT, [2, 3])
bias = g.init(np.ones((2, 3), dtype=np.float32), name="bias")
y = g.op.Add(x, bias, outputs="Y")
g.out(y, TensorProto.FLOAT, [2, 3])
model = g.to_onnx()

g.inp declares an input, g.init adds an initializer, g.op.<Operator> adds an ONNX node, and g.out declares a graph output. Their explicit counterparts (make_input, make_initializer, make_node, and make_output) remain available for generated code and advanced authoring.

The same builder can be constructed from an existing ModelProto to optimize or extend a model that was produced elsewhere.

Value tags during rewriting#

Initial tag inference uses declared metadata and the graph’s consumers to classify values. For example, an initializer dims used as the second input of Reshape(X, dims) is tagged shape, and Neg(dims) inherits that tag.

Subsequent graph rewriting preserves the existing tags associated with retained result names. Removing a consumer does not invalidate or reclassify those values. If dims and Neg(dims) remain graph outputs while the unused Reshape is removed, both retain their shape tags. This differs from initial inference on the reduced graph without tag metadata, where dims would be inferred as weight and Neg(dims) would inherit weight. A fresh inference on an untagged reduced graph is therefore not the reference for retained tags after a rewrite.

Before replaying a serialized plan, ApplyRewritings rejects reconstruction that changes or drops existing tag assignments. Functions are matched by domain, name and overload across all source scopes, including when a plan hoists a subgraph-owned or function-owned definition to the model root. Diagnostics retain the original source scope path.

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

  • Constant folding replaces subgraphs whose inputs are all constant by their computed value, driven by the runtime kernels described in Runtime.

API reference#

Examples#