yet-another-onnx-builder documentation#
yet-another-onnx-builder on GitHub
yet-another-onnx-builder (yobx) proposes a unique API and a unique function
yobx.to_onnx() to convert machine learning models
to ONNX format and manipulating ONNX graphs programmatically.
It can export from many libraries. Each converters relies on a common GraphBuilder API
(GraphBuilderExtendedProtocol)
to build the final ONNX model. One default implementation is provided but
it can also be replaced by any implementation of your own.
Any user can implement its own. You can see
OnnxScriptGraphBuilder
or SpoxGraphBuilder for a reference.
These API are close to onnx API, using NodeProto for nodes
and strings for names. This is on purpose: what this API produces is
what you see in the final ONNX model. You can add your own metadata,
choose your own names.
yobx.to_onnx() is the single entry point for all supported frameworks.
It inspects the type of model at runtime and automatically delegates to
the appropriate backend-specific converter:
a
torch.nn.Moduleortorch.fx.GraphModule→yobx.torch.to_onnx()a
tf.Module(including Keras models) →yobx.tensorflow.to_onnx()raw
.tflitebytes or a path ending in".tflite"→yobx.litert.to_onnx()a SQL string, a Python callable, or a polars.LazyFrame →
yobx.sql.to_onnx()
All extra keyword arguments are forwarded verbatim to the selected converter,
so the backend-specific parameters (export_options, function_options,
extra_converters, …) remain fully accessible through the top-level function.
standard machine learning
data manipulations
This is work in progress.
Many packages produce SQL queries. It starts by converting a SQL
query into ONNX. A lightweight DataFrame function tracer
(dataframe_to_onnx()) records pandas-inspired
operations on a DataFrame and compiles them to ONNX directly.
sql |
||
deep learning
The package is built upon a single graph builder API for constructing and optimizing ONNX graphs with built-in shape inference with can also linked to spox or onnxscript/ir-py. Its unique API:
# the model is called
from yobx import to_onnx
expected = model(*args, **kwargs)
onnx_model = to_onnx(model, args, kwargs, dynamic_shapes, **options)
The function returns an ExportArtifact that
wraps the exported ONNX proto together with an
ExportReport. The ONNX model can be retrieved
with artifact.model_proto and saved to disk via artifact.save(path).
options are different across the libraries producing the model even they share some of them. The common parameters accepted by all backends are:
target_opset— an integer or a dict mapping ONNX domain names to their opset version (e.g.{"": 18, "com.microsoft": 1}). Adding"com.microsoft"will trigger operator fusions specific to onnxruntime such as fused attention and layer normalization.large_model— whenTruethe weights are stored in a separate.onnx_datafile next to the model (ONNX external-data format). This is required for models whose size exceeds the 2 GB protobuf limit.external_threshold— size in bytes above which individual initializers are stored externally whenlarge_model=True(default: 1024).input_names— an explicit list of names for the ONNX graph input tensors. When omitted, names are derived automatically.dynamic_shapes— declares which tensor dimensions are symbolic (variable-length). The exact format depends on the backend: torch followstorch.export.export()conventions while the other backends, the default is different is different for every library but it is usually empty for pytorch or tensorflow (so static shape), first dimension is batch dimension for scikit-learn. use a tuple of{axis: dim_name}dicts.verbose— verbosity level (integer, 0 = silent).return_optimize_report— whenTrue, the returned artifact has itsreportattribute populated with per-pattern optimization statistics.
Oother options are specific to every converter and control the way a model is captured or converted. It is possible to output the decision path for trees or ensembles in scikit-learn.
This package was initially started using vibe coding. AI is able to translate an existing code into another one such as ONNX but it tends sometime to favor ugly functions definition not friendly to the users.