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
.tflitefile uses and walks every operator in the requested subgraph. Each operator is converted to its ONNX equivalent by a registered converter (seeyobx.litert.register).- Parameters:
model – path to a
.tflitefile or raw model bytesargs – 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
tupleof numpy arrays oronnx.ValueInfoProtodescriptors. When subgraph_index isNone(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
Sequenceof strings. When subgraph_index isNone: a sequence of per-subgraph name lists (orNoneentries 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 aDict[str, int]mapping domain names to versions.builder_cls – by default the graph builder is a
yobx.xbuilder.GraphBuilderbut any builder can be used as long it implements the Shape and type tracking.verbose – verbosity level (0 = silent).
extra_converters – optional mapping from
BuiltinOperatorint (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
ExportArtifacthas itscontainerattribute set to anExtendedModelContainer.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
Noneto merge all subgraphs into a single ONNX model. Tensor names are automatically prefixed withsg{i}_(where i is the subgraph index) to avoid name collisions across subgraphs.return_optimize_report – if True, the returned
ExportArtifacthas itsreportattribute populated with per-pattern optimization statistics
- Returns:
ExportArtifactwrapping the exported ONNX proto together with anExportReport.
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")