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 example18or{"": 18, "ai.onnx.ml": 3}.ir_version – unused; kept for API parity with other builders.
The builder maps string tensor names to
spox.Varobjects and delegates graph construction to spox’s opset modules. Callingto_onnx()runsspox.buildand returns anonnx.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()
- get_shape(name: str) DYNAMIC_SHAPE[source]#
Returns the shape for name as a tuple, or raises
AssertionError.
- make_initializer(name: str, value: Any) str[source]#
Adds an initializer (Constant node) and returns its name.
- Parameters:
name – Name for the initializer. May be
""for auto-naming.value – Initializer data:
numpy.ndarray,int,float, oronnx.TensorProto.
- 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.AttributeProtoinstances.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).
- property op: OpsetProtocol#
Returns the opset helper (
g.op.Relu(x)).
- 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
Nonethe input’s element type is used
- Returns:
Truewhen 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.GraphBuilderexternal_threshold – currently unused; present for API compatibility with
yobx.xbuilder.GraphBuilderinline – inline local functions (this is currently not used)