onnx_light.onnx_core.graph_builder#

Incremental ONNX graph builder backed by a C++ library.

This module exposes GraphBuilder, an incremental builder for ONNX graphs, models and functions. A builder starts empty, holds a compute context and records every value name it hands out so a name can never be reused. Nodes are added with GraphBuilder.make_node(), which resolves the operator opset, validates the node against the built-in ONNX operator schemas, assigns output names when the caller leaves them empty and runs incremental shape inference. GraphBuilder.to_onnx() finalises the accumulated graph into a model (default), a graph or a function, writing 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 module is exposed as onnx_light.onnx_core.graph_builder.

class onnx_light.onnx_core.graph_builder.ConstantFoldingOptions(*args, **kwargs)#

Options controlling GraphBuilder.constant_fold.

property enabled#

when False constant_fold is a no-op and returns 0 without touching the graph.

Type:

Master switch

property excluded_ops#

Set of (domain, op_type) tuples that must never be folded. An empty domain matches every domain and an empty op_type matches every operator, so an empty-empty pair disables folding for every node.

property fold_weights#

Controls whether nodes whose results are tagged "weight" (or untagged) are folded. Shape-tagged results are always foldable; when False only shape-tagged results are folded, so a caller can fold shapes early and defer weight folding to a final pass.

property max_element_count#

Skips folding a node when any of its outputs would hold strictly more than this many elements. A negative value (the default) means no limit.

property raise_on_missing_weight_kernel#

When True a weight/untagged node for which no runtime kernel is registered raises instead of being left untouched. Shape-tagged results always raise when their kernel is missing, regardless of this flag.

class onnx_light.onnx_core.graph_builder.GraphBuilder(name: str = 'graph', schema_lookup: ~collections.abc.Callable[[str], list[~onnx_light.onnx_py._onnxpyprotoop.onnx_op.LightOpSchema]] | None = <function _default_schema_lookup>)#

Incrementally builds an ONNX graph, model or function.

See onnx_light.onnx_core.graph_builder for details. By default the builder validates nodes and resolves opsets using the built-in ONNX operator schemas; pass schema_lookup=None to disable this, or a custom op_type -> list[LightOpSchema] callable to use different schemas.

clear_registered_patterns() None#

Removes every builder-local pattern.

register_pattern(pattern: PatternOptimization) None#

Registers or replaces a pattern for this builder.

registered_pattern_names() tuple[str, ...]#

Returns builder-local pattern names in registration order.

registered_patterns() tuple[PatternOptimization, ...]#

Returns builder-local patterns in registration order.

unregister_pattern(name: str) bool#

Removes a builder-local pattern and returns whether it existed.