yobx.xbuilder.graph_builder#

Exposes the native graph builder at the historical public import path.

Graph construction, shape inference and pattern optimization use onnx-light. The former Python graph engine is deliberately not available as a fallback. Unsupported legacy constructor options raise rather than selecting another engine.

class yobx.xbuilder.graph_builder.FunctionOptions(export_as_function: bool = False, name: str = '', domain: str = '', external_threshold: int = 33554432, move_initializer_to_constant: bool = False, return_initializer: bool = False, inline: bool = False, merge_allowed: bool = False, rename_allowed: bool = False)[source]#

Defines how local functions must behave.

Parameters:
  • name – function name

  • domain – function domain

  • export_as_function – export the onnx as functions or keep local function

  • external_threshold – whether or not keep initializer as input for the function or move them as constant of the function

  • move_initializer_to_constant – move initializers as constant first before creating the function proto, that depends on the size defined by external_threshold

  • return_initializer – return the remaining initializer and add them as input to the function

  • inline – inline functions

  • rename_allowed – allow to rename the function if a duplicate is detected

  • merge_allowed – allow to merge a function in case the same code is detected

class yobx.xbuilder.graph_builder.GraphBuilderTorchProtocol(*args, **kwargs)[source]#

Protocol for graph builders that support the full torch-exporter API.

This protocol extends GraphBuilderExtendedProtocol with the additional methods and attributes used by FxGraphInterpreter (the torch exporter) when translating a torch.fx graph into ONNX.

The extra surface covers:

add_dynamic_object(key: str, value: Any, name: str | None = None, dim: int | None = None, parse: bool = False, check_tokens: bool = True) → None[source]#

Registers value as the dynamic dimension named key.

Parameters:
  • key – symbolic dimension name

  • value – torch.SymInt, int, or similar

  • name – tensor input this dimension originates from

  • dim – axis of name this dimension corresponds to

  • parse – also register sub-expressions of value

  • check_tokens – verify sub-tokens are registered first

add_stat(kind: str, name: str) → None[source]#

Records a conversion statistic (no-op in most implementations).

Parameters:
  • kind – statistic category

  • name – statistic name

property anyop: OpsetProtocol#

An opset helper that allows operators from any domain.

Identical to op but configured to accept unknown domains. Used by the torch exporter to emit custom-domain operators such as ai.onnx.complex.

Returns:

an OpsetProtocol-compatible object

extract_input_names_from_args(args: Any) → List[str][source]#

Extracts all known tensor names from a (possibly nested) args structure.

Parameters:

args – flat or nested sequence of values; strings that are known graph names are collected

Returns:

deduplicated list of input tensor names

get_device(name: str) → int[source]#

Returns the device index for tensor name.

The convention is -1 for CPU and a non-negative index for a CUDA device.

Parameters:

name – tensor name

Returns:

device index

get_input_dynamic_shape(name: str | None, input_index: int, example_shape: Any, dynamic_shapes: Any | None = None, example_value: Any | None = None) → Any[source]#

Returns the dynamic shape specification for a graph input.

Parameters:
  • name – input name

  • input_index – positional index of the input

  • example_shape – concrete shape of an example input

  • dynamic_shapes – override the builder’s dynamic_shapes

  • example_value – one example tensor value

Returns:

shape tuple (mixing int for static dimensions and str for dynamic ones)

get_rank(name: str) → int[source]#

Returns the rank (number of dimensions) of tensor name.

Parameters:

name – tensor name

Returns:

non-negative integer rank

get_type_known(name: str, exc: bool = False) → int | None[source]#

Returns the ONNX element type inferred by torch for name, or None when unavailable.

Parameters:
  • name – tensor name

  • exc – raise an exception when the value is malformed

Returns:

ONNX element type integer or None

has_device(name: str) → bool[source]#

Returns True when the device of tensor name is known.

Parameters:

name – tensor name

has_rank(name: str) → bool[source]#

Returns True when the rank of tensor name is known.

Parameters:

name – tensor name

is_dynamic_shape(shape: Any, verify: bool = True, allow_none: bool = False, allow_new_dynamic_dimension: bool = False) → bool[source]#

Returns True when shape contains at least one dynamic (non-integer) dimension.

Parameters:
  • shape – shape tuple to check

  • verify – verify that symbolic names are registered

  • allow_none – treat None entries as dynamic

  • allow_new_dynamic_dimension – allow unregistered symbolic dims

property last_added_node: Any | None#

The most recently appended ONNX node, or None when the graph is still empty.

Returns:

an NodeProto or None

make_dynamic_object(name: str, value: Any, shape_as_input: bool = False, input_name: str | None = None, axis: int | None = None) → str | None[source]#

Creates and registers a dynamic dimension object.

Parameters:
  • name – name for the dynamic dimension

  • value – torch.SymInt or similar symbolic value

  • shape_as_input – add the dimension as a scalar graph input

  • input_name – the tensor input this dimension originates from

  • axis – the axis of input_name this dimension corresponds to

Returns:

the registered name, or None

make_local_function(builder: Any, function_options: Any, optimize: bool = False, metadata_props: Dict[str, str] | None = None) → Tuple[List[str], Tuple[str, str]][source]#

Converts builder into a local ONNX function and registers it.

Parameters:
  • builder – source GraphBuilder for the function body

  • function_options – controls naming, inlining, and weight handling

  • optimize – run optimizations on the function body

  • metadata_props – extra key/value metadata for the function

Returns:

(added_initializers, (domain, name))

make_new_dynamic_shape(rank: int, prefix: str = 'd') → Tuple[Any, ...][source]#

Creates a dynamic shape of the given rank with fresh symbolic dimensions.

Parameters:
  • rank – number of dimensions

  • prefix – prefix for the generated dimension names

Returns:

tuple of torch.SymInt objects

make_nodes(builder: Any, input_names: List[str], output_names: List[str], prefix: str = '', function_options: Any | None = None, optimize: bool = False, force_rename_with_prefix: str | None = None) → Any[source]#

Appends all nodes and initializers from builder into this graph.

Parameters:
  • builder – source GraphBuilder

  • input_names – names of the inputs that correspond to builder’s inputs in the current graph

  • output_names – desired output names in the current graph

  • prefix – prefix applied to every result name from builder when function_options is None

  • function_options – when set the sub-graph is exported as a local ONNX function

  • optimize – run optimizations on the appended sub-graph

  • force_rename_with_prefix – force this prefix regardless of function_options

Returns:

output name(s) in the current graph

make_subset_builder(input_names: List[str], name: str, domain: str, add_local_functions: bool = False) → Any[source]#

Creates a reduced copy of this builder that covers only the tensors reachable from input_names.

Parameters:
  • input_names – graph inputs for the subset

  • name – local-function name for the subset

  • domain – local-function domain for the subset

  • add_local_functions – copy local functions into the subset

Returns:

new GraphBuilder instance

make_tensor_sequence_input(name: str, elem_type: Any, shape: Any, marker: str = '') → str[source]#

Registers a tensor-sequence graph input and returns its name.

Parameters:
  • name – desired input name

  • elem_type – ONNX element type for the sequence elements

  • shape – shape of each element

  • marker – optional label for debugging / provenance

Returns:

the registered input name

pretty_text(add_fx_graph: bool = False, recursive: bool = True) → str[source]#

Returns a human-readable multi-line description of the graph.

Parameters:
  • add_fx_graph – include the original FX graph text

  • recursive – include local functions

Returns:

formatted string

register_dynamic_objects_from_shape(shape: Any) → None[source]#

Registers all dynamic-dimension objects found in shape.

Parameters:

shape – shape tuple potentially containing symbolic dimensions

register_users(name: str, users: Iterable[str]) → None[source]#

Registers the consumers of tensor name (used for validation).

Parameters:
  • name – producer tensor name

  • users – iterable of consumer tensor names

set_device(name: str, device: Any, exc: bool = True, keep_this_device: bool = False) → None[source]#

Records the device for tensor name.

Parameters:
  • name – tensor name

  • device – device identifier — an int index, a torch.device, or a constant name

  • exc – raise an exception on inconsistency

  • keep_this_device – overwrite an already-known device

set_rank(name: str, value: int) → None[source]#

Records the rank for tensor name.

Parameters:
  • name – tensor name

  • value – rank (number of dimensions)

set_shapes_types(name: Any, where: str, value: Any) → None[source]#

Records a torch-side (where, value) annotation for name.

The annotation is later consulted by get_type_known() to resolve type mismatches between ONNX and torch.

Parameters:
  • name – tensor name (or a torch.fx.Node)

  • where – source label (e.g. "run_node")

  • value – annotation value (a tuple describing type / shape)

verify_dynamic_shape(shape: Any, name: str | None = None, add: bool = True) → Any | None[source]#

Normalises shape, replacing symbolic dimensions with their registered string names.

Parameters:
  • shape – raw shape (may contain torch.SymInt)

  • name – tensor name for context messages

  • add – register newly encountered symbolic dims automatically

Returns:

normalised shape tuple or None if shape is None

yobx.xbuilder.graph_builder.OptimizationOptions[source]#

alias of OnnxLightOptimizationOptions