yobx.builder.spox#

SpoxGraphBuilder#

class yobx.builder.spox.SpoxGraphBuilder(target_opset_or_opsets: int | Dict[str, int], ir_version: int | None = None)[source]#

Bridge builder that exposes a yobx-compatible API over spox.

Parameters:
  • target_opset_or_opsets – Either a single opset version (int) or a mapping {domain: version} (Dict[str, int]). For example 18 or {"": 18, "ai.onnx.ml": 3}.

  • ir_version – unused; kept for API parity with other builders.

The builder maps string tensor names to spox.Var objects and delegates graph construction to spox’s opset modules. Calling to_onnx() runs spox.build and returns an onnx.ModelProto.

Typical usage:

from yobx.builder.spox import SpoxGraphBuilder
from onnx import TensorProto

g = SpoxGraphBuilder(18)
g.make_tensor_input("X", TensorProto.FLOAT, (None, 4))
w = g.make_initializer("W", np.eye(4, dtype=np.float32))
(y,) = g.make_node("MatMul", ["X", w], 1)
g.make_tensor_output(y)
proto = g.to_onnx()
add_domain(domain: str, version: int = 1) None[source]#

Deprecated alias for set_opset().

get_debug_msg() str[source]#

Returns a short debug message listing known names.

get_opset(domain: str, exc: bool = True) int[source]#

Returns the opset version for domain.

get_shape(name: str) DYNAMIC_SHAPE[source]#

Returns the shape for name as a tuple, or raises AssertionError.

get_type(name: str) int[source]#

Returns the ONNX element type integer for name, or 0 if unknown.

has_device(name: str) bool[source]#

Always returns False — device tracking is not supported.

has_name(name: str) bool[source]#

Returns True when name is known in this graph.

has_opset(domain: str) int[source]#

Returns the opset version for domain, or 0 if not registered.

has_shape(name: str) bool[source]#

Returns True when name has a known shape.

has_type(name: str) bool[source]#

Returns True when name has a known element type.

property input_names: List[str]#

Returns the list of graph input names.

property main_opset: int#

Returns the opset version for the main (""/ONNX) domain.

make_initializer(name: str, value: Any) str[source]#

Adds an initializer (Constant node) and returns its name.

Parameters:
Returns:

The registered name.

make_node(op_type: str, inputs: str | List[str], outputs: int | str | List[str] = 1, domain: str = '', attributes: List[AttributeProto] | None = None, name: str | None = None, **kwargs: Any) str | Tuple[str, ...][source]#

Creates an ONNX node and returns its output name(s).

Parameters:
  • op_type – ONNX operator type.

  • inputs – Input tensor name(s).

  • outputs – Number of outputs (int), a single name (str), or a list of names.

  • domain – Operator domain.

  • attributes – Additional onnx.AttributeProto instances.

  • name – Optional node name (ignored by spox).

  • kwargs – Operator attributes.

Returns:

Single output name or tuple of names.

make_tensor_input(name: str, elem_type: int | None = None, shape: Sequence[int | str | None] | None = None, device: int | None = None) str[source]#

Adds a graph input and returns its name.

Parameters:
  • name – Input tensor name.

  • elem_type – ONNX element type (e.g. TensorProto.FLOAT).

  • shape – Tensor shape.

  • device – unused.

Returns:

The registered name (same as name).

make_tensor_output(name: str | List[str], elem_type: int | None = None, shape: Sequence[int | str | None] | None = None, indexed: bool = False, allow_untyped_output: bool = False) str | List[str][source]#

Registers an existing value as a graph output and returns its name.

Parameters:
  • name – Name (or list of names) of the tensor(s) to mark as output(s).

  • elem_type – Optional element type hint.

  • shape – Optional shape hint.

  • indexed – unused.

  • allow_untyped_output – allow output with no type/shape.

Returns:

The name (or list of names).

onnx_dtype_to_np_dtype(itype: int) dtype[source]#

See yobx.helpers.onnx_helper.tensor_dtype_to_np_dtype().

property op: OpsetProtocol#

Returns the opset helper (g.op.Relu(x)).

property output_names: List[str]#

Returns the list of graph output names.

prefix_name_context(prefix: str) Generator[source]#

Context manager that pushes prefix onto the prefix stack.

While active, unique_name() prepends the joined stack to every requested name so that all tensor names produced inside the block are scoped to the current context. The prefix is removed when the block exits, even if an exception is raised.

Parameters:

prefix – scope prefix to push (e.g. a pipeline step name)

set_opset(domain: str, version: int = 1) None[source]#

Registers version for domain, or no-ops if already set to the same value.

set_shape(name: str, shape: DYNAMIC_SHAPE, allow_zero: bool = False) None[source]#

Stores shape in the side-channel shape map for name.

set_type(name: str, itype: int) None[source]#

Overrides the element type for name in the side-channel type map.

set_type_shape_unary_op(name: str, input_name: str, itype: int | None = None) bool[source]#

Propagates type and shape from input_name to name for a unary op.

This is a convenience helper used when emitting elementwise unary operators (Abs, Exp, Relu, etc.) where the output has the same type and shape as the input.

Parameters:
  • name – output tensor name whose type/shape should be set

  • input_name – input tensor name to copy type/shape from

  • itype – override element type; if None the input’s element type is used

Returns:

True when shape information was available and set, None/falsy when it could not be determined

to_onnx(large_model: bool = False, external_threshold: int = 1024, inline: bool = True) ExportArtifact[source]#

Exports the accumulated graph as an onnx.ModelProto.

Parameters:
  • large_model – currently unused; present for API compatibility with yobx.xbuilder.GraphBuilder

  • external_threshold – currently unused; present for API compatibility with yobx.xbuilder.GraphBuilder

  • inline – inline local functions (this is currently not used)

unique_name(prefix: str) str[source]#

Returns a unique name derived from prefix.