yobx.litert.to_onnx#

yobx.litert.to_onnx(model: str | ~os.PathLike[str] | bytes, args: ~typing.Tuple[~typing.Any, ...] | ~typing.Sequence[~typing.Tuple[~typing.Any, ...]] = (), input_names: ~typing.Sequence[str] | ~typing.Sequence[~typing.Sequence[str] | None] | None = None, dynamic_shapes: ~typing.Tuple[~typing.Dict[int, str], ...] | ~typing.Sequence[~typing.Tuple[~typing.Dict[int, str], ...] | None] | None = None, target_opset: int | ~typing.Dict[str, int] = 21, builder_cls: type | ~typing.Callable = <class 'yobx.xbuilder.graph_builder.GraphBuilder'>, verbose: int = 0, extra_converters: ~typing.Dict[int, ~typing.Callable] | None = None, large_model: bool = False, external_threshold: int = 1024, subgraph_index: int | None = 0, return_optimize_report: bool = False) ExportArtifact[source]#

Convert a TFLite/LiteRT model to ONNX.

The function parses the binary FlatBuffer that every .tflite file uses and walks every operator in the requested subgraph. Each operator is converted to its ONNX equivalent by a registered converter (see yobx.litert.register).

Parameters:
  • model – path to a .tflite file or raw model bytes

  • args – dummy inputs used to determine dtypes and shapes when the model does not carry that information. For a single subgraph (the default) this is a tuple of numpy arrays or onnx.ValueInfoProto descriptors. When subgraph_index is None (merge all subgraphs) this must be a sequence of such tuples, one per subgraph; use an empty sequence () or [] to let the converter infer all types from the model’s own tensor metadata.

  • input_names – optional name(s) for the ONNX input tensors. For a single subgraph: a Sequence of strings. When subgraph_index is None: a sequence of per-subgraph name lists (or None entries to use the TFLite tensor names for that subgraph).

  • dynamic_shapes – optional per-input axis-to-dim-name mappings. When None, axis 0 is treated as a dynamic batch dimension. When subgraph_index is None: a sequence of per-subgraph mappings.

  • target_opset – opset version; either an integer for the default domain ("") or a Dict[str, int] mapping domain names to versions.

  • builder_cls – by default the graph builder is a yobx.xbuilder.GraphBuilder but any builder can be used as long it implements the Shape and type tracking.

  • verbose – verbosity level (0 = silent).

  • extra_converters – optional mapping from BuiltinOperator int (or custom-op name string) to converter function with signature (g, sts, outputs, op). Entries here take priority over the built-in op converters.

  • large_model – if True the returned ExportArtifact has its container attribute set to an ExtendedModelContainer.

  • external_threshold – if large_model is True, every tensor whose element count exceeds this threshold is stored as external data.

  • subgraph_index – index of the subgraph to convert (default: 0). Pass None to merge all subgraphs into a single ONNX model. Tensor names are automatically prefixed with sg{i}_ (where i is the subgraph index) to avoid name collisions across subgraphs.

  • return_optimize_report – if True, the returned ExportArtifact has its report attribute populated with per-pattern optimization statistics

Returns:

ExportArtifact wrapping the exported ONNX proto together with an ExportReport.

Example — single subgraph:

import numpy as np
from yobx.litert import to_onnx

X = np.random.rand(1, 4).astype(np.float32)
artifact = to_onnx("model.tflite", (X,))
proto = artifact.proto
artifact.save("model.onnx")

Example — merge all subgraphs:

artifact = to_onnx("model.tflite", subgraph_index=None)
artifact.save("merged.onnx")