mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
[ONNX] Update images and APIs to onnx_dynamo.rst (#144358)
Update the result image of exporting, and delete the functions/class that belongs to `torch.onnx.dynamo_export` Pull Request resolved: https://github.com/pytorch/pytorch/pull/144358 Approved by: https://github.com/justinchuby, https://github.com/malfet
This commit is contained in:
committed by
PyTorch MergeBot
parent
a5164a2b18
commit
a742859fc2
@ -19,7 +19,6 @@ figures:
|
||||
|
||||
onnx:
|
||||
@$(PYCMD) source/scripts/onnx/build_onnx_torchscript_supported_aten_op_csv_table.py
|
||||
@$(PYCMD) source/scripts/onnx/build_onnx_dynamo_diagnostics_rules_md.py $(SOURCEDIR)/generated/onnx_dynamo_diagnostics_rules
|
||||
|
||||
opset:
|
||||
@$(PYCMD) source/scripts/build_opsets.py
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 26 KiB |
Binary file not shown.
Before Width: | Height: | Size: 11 KiB |
Binary file not shown.
Before Width: | Height: | Size: 5.7 KiB |
@ -23,15 +23,6 @@ bytecode analysis that preserves the dynamic nature of the model instead of usin
|
||||
In addition, during the export process, memory usage is significantly reduced compared to the TorchScript-enabled exporter.
|
||||
See the :doc:`memory usage documentation <onnx_dynamo_memory_usage>` for more information.
|
||||
|
||||
The exporter is designed to be modular and extensible. It is composed of the following components:
|
||||
|
||||
- **ONNX Exporter**: :class:`Exporter` main class that orchestrates the export process.
|
||||
- **ONNX Export Options**: :class:`ExportOptions` has a set of options that control the export process.
|
||||
- **ONNX Registry**: :class:`OnnxRegistry` is the registry of ONNX operators and functions.
|
||||
- **FX Graph Extractor**: :class:`FXGraphExtractor` extracts the FX graph from the PyTorch model.
|
||||
- **Fake Mode**: :class:`ONNXFakeContext` is a context manager that enables fake mode for large scale models.
|
||||
- **ONNX Program**: :class:`ONNXProgram` is the output of the exporter that contains the exported ONNX graph and diagnostics.
|
||||
- **ONNX Diagnostic Options**: :class:`DiagnosticOptions` has a set of options that control the diagnostics emitted by the exporter.
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
@ -85,6 +76,12 @@ See below a demonstration of exporter API in action with a simple Multilayer Per
|
||||
As the code above shows, all you need is to provide :func:`torch.onnx.export` with an instance of the model and its input.
|
||||
The exporter will then return an instance of :class:`torch.onnx.ONNXProgram` that contains the exported ONNX graph along with extra information.
|
||||
|
||||
``onnx_program.optimize()`` can be called to optimize the ONNX graph with constant folding and elimination of redundant operators. The optimization is done in-place.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
onnx_program.optimize()
|
||||
|
||||
The in-memory model available through ``onnx_program.model_proto`` is an ``onnx.ModelProto`` object in compliance with the `ONNX IR spec <https://github.com/onnx/onnx/blob/main/docs/IR.md>`_.
|
||||
The ONNX model may then be serialized into a `Protobuf file <https://protobuf.dev/>`_ using the :meth:`torch.onnx.ONNXProgram.save` API.
|
||||
|
||||
@ -93,12 +90,15 @@ The ONNX model may then be serialized into a `Protobuf file <https://protobuf.de
|
||||
onnx_program.save("mlp.onnx")
|
||||
|
||||
Two functions exist to export the model to ONNX based on TorchDynamo engine.
|
||||
They slightly differ in the way they produce the :class:`ExportedProgram`.
|
||||
They slightly differ in the way they produce the :class:`torch.export.ExportedProgram`.
|
||||
:func:`torch.onnx.dynamo_export` was introduced with PyTorch 2.1 and
|
||||
:func:`torch.onnx.export` was extended with PyTorch 2.5 to easily switch
|
||||
from TorchScript to TorchDynamo. To call the former function,
|
||||
the last line of the previous example can be replaced by the following one.
|
||||
|
||||
.. note::
|
||||
:func:`torch.onnx.dynamo_export` will be deprecated in the future. Please use :func:`torch.onnx.export` with the parameter ``dynamo=True`` instead.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
onnx_program = torch.onnx.dynamo_export(model, tensor_x)
|
||||
@ -112,20 +112,6 @@ You can view the exported model using `Netron <https://netron.app/>`__.
|
||||
:width: 40%
|
||||
:alt: MLP model as viewed using Netron
|
||||
|
||||
Note that each layer is represented in a rectangular box with a *f* icon in the top right corner.
|
||||
|
||||
.. image:: _static/img/onnx/onnx_dynamo_mlp_model_function_highlight.png
|
||||
:width: 40%
|
||||
:alt: ONNX function highlighted on MLP model
|
||||
|
||||
By expanding it, the function body is shown.
|
||||
|
||||
.. image:: _static/img/onnx/onnx_dynamo_mlp_model_function_body.png
|
||||
:width: 50%
|
||||
:alt: ONNX function body
|
||||
|
||||
The function body is a sequence of ONNX operators or other functions.
|
||||
|
||||
When the conversion fails
|
||||
-------------------------
|
||||
|
||||
@ -133,25 +119,6 @@ Function :func:`torch.onnx.export` should called a second time with
|
||||
parameter ``report=True``. A markdown report is generated to help the user
|
||||
to resolve the issue.
|
||||
|
||||
Function :func:`torch.onnx.dynamo_export` generates a report using 'SARIF' format.
|
||||
ONNX diagnostics goes beyond regular logs through the adoption of
|
||||
`Static Analysis Results Interchange Format (aka SARIF) <https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html>`__
|
||||
to help users debug and improve their model using a GUI, such as
|
||||
Visual Studio Code's `SARIF Viewer <https://marketplace.visualstudio.com/items?itemName=MS-SarifVSCode.sarif-viewer>`_.
|
||||
|
||||
The main advantages are:
|
||||
|
||||
- The diagnostics are emitted in machine parseable `Static Analysis Results Interchange Format (SARIF) <https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html>`__.
|
||||
- A new clearer, structured way to add new and keep track of diagnostic rules.
|
||||
- Serve as foundation for more future improvements consuming the diagnostics.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:caption: ONNX Diagnostic SARIF Rules
|
||||
:glob:
|
||||
|
||||
generated/onnx_dynamo_diagnostics_rules/*
|
||||
|
||||
.. toctree::
|
||||
:hidden:
|
||||
|
||||
@ -162,14 +129,14 @@ API Reference
|
||||
|
||||
.. autofunction:: torch.onnx.dynamo_export
|
||||
|
||||
.. autoclass:: torch.onnx.ONNXProgram
|
||||
:members:
|
||||
|
||||
.. autoclass:: torch.onnx.ExportOptions
|
||||
:members:
|
||||
|
||||
.. autofunction:: torch.onnx.enable_fake_mode
|
||||
|
||||
.. autoclass:: torch.onnx.ONNXProgram
|
||||
:members:
|
||||
|
||||
.. autoclass:: torch.onnx.ONNXRuntimeOptions
|
||||
:members:
|
||||
|
||||
|
@ -2,7 +2,7 @@ TorchScript-based ONNX Exporter
|
||||
===============================
|
||||
|
||||
.. note::
|
||||
To export an ONNX model using TorchDynamo instead of TorchScript, see :func:`torch.onnx.dynamo_export`.
|
||||
To export an ONNX model using TorchDynamo instead of TorchScript, please see :doc:`Learn more about the TorchDynamo-based ONNX Exporter <onnx_dynamo>`
|
||||
|
||||
.. contents:: :local:
|
||||
|
||||
|
@ -1,41 +0,0 @@
|
||||
import argparse
|
||||
import os
|
||||
from dataclasses import fields
|
||||
|
||||
from torch.onnx._internal import diagnostics
|
||||
from torch.onnx._internal.diagnostics import infra
|
||||
|
||||
|
||||
def gen_docs(out_dir: str):
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
for field in fields(diagnostics.rules):
|
||||
rule = getattr(diagnostics.rules, field.name)
|
||||
if not isinstance(rule, infra.Rule):
|
||||
continue
|
||||
if not rule.id.startswith("FXE"):
|
||||
# Only generate docs for `dynamo_export` rules. Excluding rules for TorchScript
|
||||
# ONNX exporter.
|
||||
continue
|
||||
title = f"{rule.id}:{rule.name}"
|
||||
full_description_markdown = rule.full_description_markdown
|
||||
assert (
|
||||
full_description_markdown is not None
|
||||
), f"Expected {title} to have a full description in markdown"
|
||||
with open(f"{out_dir}/{title}.md", "w") as f:
|
||||
f.write(f"# {title}\n")
|
||||
f.write(full_description_markdown)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate ONNX diagnostics rules doc in markdown."
|
||||
)
|
||||
parser.add_argument(
|
||||
"out_dir", metavar="OUT_DIR", help="path to output directory for docs"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
gen_docs(args.out_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user