Introduction to ONNX — PyTorch Tutorials 2.7.0+cu126 documentation (original) (raw)
beginner/onnx/intro_onnx
Run in Google Colab
Colab
Download Notebook
Notebook
View on GitHub
GitHub
Note
Click hereto 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
Created On: Oct 04, 2023 | Last Updated: Mar 05, 2025 | Last Verified: Nov 05, 2024
Authors:Ti-Tai Wang, Thiago Crepaldi.
Open Neural Network eXchange (ONNX) is an open standard format for representing machine learning models. The torch.onnx
module provides APIs to capture the computation graph from a native PyTorch torch.nn.Module model and convert it into an ONNX graph.
The exported model can be consumed by any of the manyruntimes that support ONNX, including Microsoft’s ONNX Runtime.
Note
Currently, you can choose either through `TorchScript https://pytorch.org/docs/stable/jit.html`_ or`ExportedProgram https://pytorch.org/docs/stable/export.html`_ to export the model to ONNX by the boolean parameter dynamo in torch.onnx.export. In this tutorial, we will focus on the ExportedProgram
approach.
When setting dynamo=True
, the exporter will use torch.export to capture an ExportedProgram
, 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 resources to 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 through pip:
pip install --upgrade onnx onnxscript onnxruntime
To validate the installation, run the following commands:
import torch print(torch.version)
import onnxscript print(onnxscript.version)
import onnxruntime print(onnxruntime.version)
Each import 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 or sit tight and have fun going through all of them to learn all there is about the ONNX exporter.
Total running time of the script: ( 0 minutes 0.000 seconds)