Introduction to ONNX || Exporting a PyTorch model to ONNX || Extending the ONNX Registry

Introduction to ONNX

Authors: 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 many runtimes that support ONNX, including Microsoft’s ONNX Runtime.


Currently, there are two flavors of ONNX exporter APIs, but this tutorial will focus on the torch.onnx.dynamo_export.

The TorchDynamo engine is leveraged to hook into Python’s frame evaluation API and dynamically rewrite its bytecode into an FX graph. The resulting FX Graph is polished before it is finally translated into an ONNX graph.

The main advantage of this approach is that the FX graph is captured using bytecode analysis that preserves the dynamic nature of the model instead of using traditional static tracing techniques.


PyTorch 2.1.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.

They can be installed through pip:

pip install --upgrade onnx onnxscript

To validate the installation, run the following commands:

import torch

import onnxscript

from onnxscript import opset18  # opset 18 is the latest (and only) supported version for now

import onnxruntime

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)

Gallery generated by Sphinx-Gallery

더 궁금하시거나 개선할 내용이 있으신가요? 커뮤니티에 참여해보세요!

이 튜토리얼이 어떠셨나요? 평가해주시면 이후 개선에 참고하겠습니다! :)

© Copyright 2018-2024, PyTorch & 파이토치 한국 사용자 모임(PyTorch Korea User Group).

Built with Sphinx using a theme provided by Read the Docs.

PyTorchKorea @ GitHub

파이토치 한국 사용자 모임을 GitHub에서 만나보세요.

GitHub로 이동

한국어 튜토리얼

한국어로 번역 중인 PyTorch 튜토리얼입니다.

튜토리얼로 이동


다른 사용자들과 의견을 나누고, 도와주세요!

커뮤니티로 이동