onnx_light.onnx_core.graph_builder#

Full builds load the compiled kernel registry when this module is imported, so constant folding does not depend on importing ReferenceEvaluator first. Reduced builds without the kernels extension still support graph authoring. Required shape constant folding in such builds raises an error identifying the missing operator kernel; importing the builder does not suppress extension loading failures when the extension is present.

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.

init(value: ndarray, name: str | None = None, *, copy: bool = True) → str#

Adds a NumPy initializer and returns its final name.

By default, copies the payload. With copy=False, borrows C-contiguous, dtype-aligned, little-endian storage without conversion. Supports bool, 8/16/32/64-bit integers, float16/32/64 and complex64/128; rejects other dtypes and layouts. Retains the array until the last borrowed payload owner releases it, including models exported from this builder.

Writable arrays remain writable and mutations are visible to all owners. The caller must finish mutations before optimization or creating an execution session, which may cache derived values, and must not resize or reallocate storage while borrowed. Use copy=True for an independent snapshot; setting an array read-only does not freeze its aliases.

inp(name: str, elem_type: int, shape: list[str | int | None]) → str#

Declares and returns a compact graph input.

property op: _OperatorProxy#

Returns the cached compact operator proxy.

out(name: str, elem_type: int | None = None, shape: list[str | int | None] | None = None) → str#

Declares and returns a compact graph output.

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.

set_opset_version(domain: str, version: int) → None#

Records an explicit opset version.

unregister_pattern(name: str) → bool#

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