yobx.torch.to_onnx#

yobx.torch.to_onnx(mod: torch.nn.Module | torch.fx.GraphModule, args: Sequence[torch.Tensor] | None = None, kwargs: Dict[str, torch.Tensor] | None = None, input_names: Sequence[str] | None = None, target_opset: int | Dict[str, int] | None = None, as_function: bool = False, options: OnnxLightOptimizationOptions | None = None, verbose: int = 0, return_builder: bool = False, raise_list: Set[str] | None = None, dynamic_shapes: Dict[str, Any] | Tuple[Any] | None = None, optimize: bool = True, dispatcher: Dispatcher | None = None, large_model: bool = False, external_threshold: int = 1024, export_options: str | ExportOptions | None = None, return_optimize_report: bool = False, filename: str | None = None, inline: bool = True, export_modules_as_functions: bool | Set[type[torch.nn.Module]] = False, function_options: FunctionOptions | None = None, output_names: List[str] | None = None, output_dynamic_shapes: Dict[str, Any] | Tuple[Any] | None = None, validate_onnx: bool | float = False, return_ep: bool = False) → ExportArtifact[source]#

Exports a torch model through torch.export and the native onnx-light builder, shape context and pattern optimizer. There is no legacy graph engine fallback. Supported ATen operations, conditional branches and standalone functions are implemented by yobx.torch.interpreter.native_export; unsupported requests raise NotImplementedError at the relevant option or FX node.

Parameters:
  • mod – torch module

  • args – input arguments

  • kwargs – keyword attributes

  • input_names – input names

  • target_opset – targeted opset or targeted opsets as a dictionary

  • as_function – export as a native ModelProto or standalone FunctionProto

  • options – optimization options

  • verbose – verbosity level

  • return_builder – retains the native builder on the artifact

  • return_ep – retains the ExportedProgram on artifact.ep

  • raise_list – the builder stops any time a name falls into that list, this is a debugging tool

  • dynamic_shapes – see torch.export.export

  • optimize – optimize the model before exporting into onnx

  • dispatcher – see yobx.torch.interpreter.Dispatcher

  • large_model – stores weights in a native ExtendedModelContainer on the returned artifact

  • external_threshold – if large_model is True, every tensor above this limit is stored as external

  • return_optimize_report – returns statistics on the optimization as well; statistics are also available via artifact.report on the returned ExportArtifact

  • filename – if specified, stores the model into that file

  • inline – inline the model before converting to onnx, this is done before any optimization takes place

  • export_options – to apply different options before to get the exported program

  • export_modules_as_functions – currently unsupported; module-boundary preservation raises explicitly

  • function_options – standalone function name, domain and whether to promote initializer values to additional function inputs

  • output_names – to rename the output names

  • output_dynamic_shapes – currently unsupported; output shapes are inferred natively

  • validate_onnx – if a float or True, validates the onnx model against the model with the input used to export, if True, the tolerance is 1e-5

Returns:

ExportArtifact wrapping the exported ONNX proto and an ExportReport. The native builder is retained on artifact.builder rather than returned in a tuple.

Example:

import torch
from yobx.torch.interpreter import to_onnx

class Neuron(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = torch.nn.Linear(4, 2)

    def forward(self, x):
        return torch.relu(self.linear(x))

x = torch.randn(3, 4)
artifact = to_onnx(Neuron(), (x,))
artifact.save("model.onnx")