Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pytorch
GitHub Repository: pytorch/tutorials
Path: blob/main/beginner_source/onnx/intro_onnx.py
1695 views
1
"""
2
**Introduction to ONNX** ||
3
`Exporting a PyTorch model to ONNX <export_simple_model_to_onnx_tutorial.html>`_ ||
4
`Extending the ONNX exporter operator support <onnx_registry_tutorial.html>`_ ||
5
`Export a model with control flow to ONNX <export_control_flow_model_to_onnx_tutorial.html>`_
6
7
Introduction to ONNX
8
====================
9
10
Authors:
11
`Ti-Tai Wang <https://github.com/titaiwangms>`_, `Thiago Crepaldi <https://github.com/thiagocrepaldi>`_.
12
13
`Open Neural Network eXchange (ONNX) <https://onnx.ai/>`_ is an open standard
14
format for representing machine learning models. The ``torch.onnx`` module provides APIs to
15
capture the computation graph from a native PyTorch :class:`torch.nn.Module` model and convert
16
it into an `ONNX graph <https://github.com/onnx/onnx/blob/main/docs/IR.md>`_.
17
18
The exported model can be consumed by any of the many
19
`runtimes that support ONNX <https://onnx.ai/supported-tools.html#deployModel>`_,
20
including Microsoft's `ONNX Runtime <https://www.onnxruntime.ai>`_.
21
22
When setting ``dynamo=True``, the exporter will use `torch.export <https://pytorch.org/docs/stable/export.html>`_ to capture an ``ExportedProgram``,
23
before translating the graph into ONNX representations. This approach is the new and recommended way to export models to ONNX.
24
It works with PyTorch 2.0 features more robustly, has better support for newer ONNX operator sets, and consumes less resources
25
to make exporting larger models possible.
26
27
Dependencies
28
------------
29
30
PyTorch 2.5.0 or newer is required.
31
32
The ONNX exporter depends on extra Python packages:
33
34
- `ONNX <https://onnx.ai>`_ standard library
35
- `ONNX Script <https://onnxscript.ai>`_ library that enables developers to author ONNX operators,
36
functions and models using a subset of Python in an expressive, and yet simple fashion
37
- `ONNX Runtime <https://onnxruntime.ai>`_ accelerated machine learning library.
38
39
They can be installed through `pip <https://pypi.org/project/pip/>`_:
40
41
.. code-block:: bash
42
43
pip install --upgrade onnx onnxscript onnxruntime
44
45
To validate the installation, run the following commands:
46
47
.. code-block:: python
48
49
import torch
50
print(torch.__version__)
51
52
import onnxscript
53
print(onnxscript.__version__)
54
55
import onnxruntime
56
print(onnxruntime.__version__)
57
58
Each `import` must succeed without any errors and the library versions must be printed out.
59
60
Further reading
61
---------------
62
63
The list below refers to tutorials that ranges from basic examples to advanced scenarios,
64
not necessarily in the order they are listed.
65
Feel free to jump directly to specific topics of your interest or
66
sit tight and have fun going through all of them to learn all there is about the ONNX exporter.
67
68
.. include:: /beginner_source/onnx/onnx_toc.txt
69
70
.. toctree::
71
:hidden:
72
73
"""
74