.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "beginner/onnx/intro_onnx.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_beginner_onnx_intro_onnx.py: **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 :class:`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 `_. .. note:: 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. Dependencies ------------ 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 `_: .. code-block:: bash pip install --upgrade onnx onnxscript To validate the installation, run the following commands: .. code-block:: python import torch print(torch.__version__) import onnxscript print(onnxscript.__version__) from onnxscript import opset18 # opset 18 is the latest (and only) supported version for now 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. .. include:: /beginner_source/onnx/onnx_toc.txt .. toctree:: :hidden: .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_beginner_onnx_intro_onnx.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: intro_onnx.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: intro_onnx.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_