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.exportand 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 byyobx.torch.interpreter.native_export; unsupported requests raiseNotImplementedErrorat 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.epraise_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.Dispatcherlarge_model – stores weights in a native
ExtendedModelContaineron the returned artifactexternal_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.reporton the returnedExportArtifactfilename – 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:
ExportArtifactwrapping the exported ONNX proto and anExportReport. The native builder is retained onartifact.builderrather 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")