Rate this Page

Note

Go to the endto download the full example code.

Introduction to ONNX ||Exporting a PyTorch model to ONNX ||Extending the ONNX exporter operator support ||Export a model with control flow to ONNX

Introduction to ONNX#

Created On: Oct 04, 2023 | Last Updated: Jul 11, 2025 | Last Verified: Nov 05, 2024

Authors:Ti-Tai Wang,Thiago Crepaldi.

Open Neural Network eXchange (ONNX) is an open standardformat for representing machine learning models. Thetorch.onnx module provides APIs tocapture the computation graph from a native PyTorchtorch.nn.Module model and convertit into anONNX graph.

The exported model can be consumed by any of the manyruntimes that support ONNX,including Microsoft’sONNX Runtime.

When settingdynamo=True, the exporter will usetorch.export to capture anExportedProgram,before translating the graph into ONNX representations. This approach is the new and recommended way to export models to ONNX.It works with PyTorch 2.0 features more robustly, has better support for newer ONNX operator sets, and consumes less resourcesto make exporting larger models possible.

Dependencies#

PyTorch 2.5.0 or newer is required.

The ONNX exporter depends on extra Python packages:

  • ONNX standard library

  • ONNX Script library that enables developers to author ONNX operators,functions and models using a subset of Python in an expressive, and yet simple fashion

  • ONNX Runtime accelerated machine learning library.

They can be installed throughpip:

pipinstall--upgradeonnxonnxscriptonnxruntime

To validate the installation, run the following commands:

importtorchprint(torch.__version__)importonnxscriptprint(onnxscript.__version__)importonnxruntimeprint(onnxruntime.__version__)

Eachimport must succeed without any errors and the library versions must be printed out.

Further reading#

The list below refers to tutorials that ranges from basic examples to advanced scenarios,not necessarily in the order they are listed.Feel free to jump directly to specific topics of your interest orsit tight and have fun going through all of them to learn all there is about the ONNX exporter.