yobx.convert#

Export discovery from a Python file#

yobx.guess_export executes a trusted Python file and finds an existing model and example inputs. It returns keyword arguments for yobx.to_onnx, which selects the appropriate backend. For example, a file model.py may contain:

import numpy as np
from sklearn.linear_model import LinearRegression

X = np.arange(12, dtype=np.float32).reshape(4, 3)
y = X.sum(axis=1)
reg = LinearRegression().fit(X, y)

Export it with:

from yobx import guess_export, to_onnx

export_args = guess_export("model.py")
artifact = to_onnx(**export_args, target_opset=18)

Use model_name and args_name to resolve ambiguous files or select unconventionally named variables. Discovery does not fit models, instantiate classes, invoke factories, or invent example inputs. Code inside an if __name__ == "__main__": block is not executed. Imports must be resolvable in the current Python environment. Errors from the file are not suppressed.

The file is executed outside a package context. Explicit relative imports such as from .foo import bar are not supported; use absolute imports from packages available in the current Python environment instead.

Exporting a pickled model#

to_onnx also accepts a path ending in .pkl or .pickle and loads the pickled model before selecting its backend. The example inputs must still be supplied:

artifact = to_onnx("model.pkl", (X,))

The pickle must be compatible with the current Python environment, including the packages and class definitions needed to deserialize it. Pickle loading can execute arbitrary code, so only load trusted files. Checkpoint files that contain only model weights (rather than a complete model object) cannot be converted without first recreating the corresponding model architecture and loading the weights into it.

Warning

guess_export is not thread-safe. Its use of runpy.run_path temporarily modifies process-global state, including sys.argv[0] and sys.path[0]. Call it sequentially, not concurrently with other executions or changes to import state.

Top-level dispatcher that routes a model to the appropriate backend converter.

yobx.convert.DEFAULT_TARGET_OPSET = 29#

Default ONNX opset version targeted by all converters.

yobx.convert.guess_export(filename: str | PathLike, model_name: str | None = None, args_name: str | None = None) → Dict[str, Any][source]#

Guesses arguments for to_onnx() from a trusted Python file.

Executes the file with runpy.run_path() (not as __main__). Only module-level objects are inspected; model classes and factory functions are never instantiated or called. The file must therefore create its model and example inputs outside a __main__ guard. The file is executed outside a package context: explicit relative imports such as from .foo import bar are not supported. Use absolute imports resolvable in the current Python environment instead.

Prefers a variable named model. Otherwise selects the single supported framework instance or locally defined callable, ignoring private names, imported functions, and classes. SQL strings and TFLite paths/bytes require model or an explicit model_name.

Looks for inputs named args, example_inputs, or inputs, in that order, then a single X or x. A single input is wrapped in a tuple; sequences and mappings are preserved. Module-level input_names and dynamic_shapes are also included when present.

Warning

This function executes arbitrary Python code. Only use trusted files. It is not a sandbox, and execution errors propagate unchanged. It is not thread-safe: runpy.run_path temporarily modifies process-global state, including sys.argv[0] and import-related state such as sys.path[0]. Call it sequentially, not concurrently with other executions or changes to import state.

Parameters:
  • filename – path to a Python file.

  • model_name – explicit name of the model variable.

  • args_name – explicit name of the example-input variable.

Raises:
  • ValueError – if a model or inputs cannot be identified unambiguously.

  • TypeError – if the selected model is not supported.

Returns:

A dictionary usable as to_onnx(**guess_export(filename)). Backend selection remains the responsibility of to_onnx().