Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce torch.onnx.dynamo_export API #97920

Closed
wants to merge 2 commits into from

Conversation

abock
Copy link
Contributor

@abock abock commented Mar 29, 2023

This is the first phase of the new ONNX exporter API for exporting from TorchDynamo and FX, and represents the beginning of a new era for exporting ONNX from PyTorch.

The API here is a starting point upon which we will layer more capability and expressiveness in subsequent phases. This first phase introduces the following into torch.onnx:

dynamo_export(
    model: torch.nn.Module,
    /,
    *model_args,
    export_options: Optional[ExportOptions] = None,
    **model_kwargs,
) -> ExportOutput:
    ...

class ExportOptions:
    opset_version: Optional[int] = None
    dynamic_shapes: Optional[bool] = None
    logger: Optional[logging.Logger] = None

class ExportOutputSerializer(Protocol):
    def serialize(
        self,
        export_output: ExportOutput,
        destination: io.BufferedIOBase,
    ) -> None:
        ...

class ExportOutput:
    model_proto: onnx.ModelProto

    def save(
        self,
        destination: Union[str, io.BufferedIOBase],
        *,
        serializer: Optional[ExportOutputSerializer] = None,
    ) -> None:
        ...

In addition to the API in the first commit on this PR, we have a few experiments for exporting Dynamo and FX to ONNX that this PR rationalizes through the new Exporter API and adjusts tests to use the new API.

  • A base FXGraphModuleExporter exporter from which all derive:
    • DynamoExportExporter: uses dynamo.export to acquire FX graph
    • DynamoOptimizeExporter: uses dynamo.optimize to acquire FX graph
    • FXSymbolicTraceExporter: uses FX symbolic tracing

The dynamo_export API currently uses DynamoOptimizeExporter.

Next Steps (subsequent PRs):

  • Combine DynamoExportExporter and DynamoOptimizeExporter into a single DynamoExporter.
  • Make it easy to test FXSymbolicTraceExporter through the same API; eventually FXSymbolicTraceExporter goes away entirely when the Dynamo approach works for large models. We want to keep FXSymbolicTraceExporter around for now for experimenting and internal use.
  • Parameterize (on ExportOptions) and consolidate Dynamo exporter tests.
    • This PR intentionally leaves the existing tests unchanged as much as possible except for the necessary plumbing.
  • Subsequent API phases:
    • Diagnostics
    • Registry, dispatcher, and Custom Ops
    • Passes
    • Dynamic shapes

Fixes #94774

@abock abock added the module: onnx Related to torch.onnx label Mar 29, 2023
@abock abock self-assigned this Mar 29, 2023
@abock abock requested a review from BowenBao as a code owner March 29, 2023 22:25
@pytorch-bot
Copy link

pytorch-bot bot commented Mar 29, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/97920

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit f8ed7cc:
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the release notes: onnx torch.onnx related changes that should show up in the release notes label Mar 29, 2023
torch/onnx/_internal/exporter_impl.py Outdated Show resolved Hide resolved
torch/onnx/_internal/exporter.py Outdated Show resolved Hide resolved
torch/onnx/errors.py Outdated Show resolved Hide resolved
@abock abock force-pushed the abock/new-onnx-exporter-api-phase1 branch from 3a01a1a to 518ab7f Compare March 29, 2023 22:35
Copy link
Collaborator

@titaiwangms titaiwangms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work

torch/onnx/_internal/exporter.py Outdated Show resolved Hide resolved
torch/onnx/_internal/exporter.py Outdated Show resolved Hide resolved
torch/onnx/_internal/exporter.py Show resolved Hide resolved
torch/onnx/errors.py Outdated Show resolved Hide resolved
@abock abock force-pushed the abock/new-onnx-exporter-api-phase1 branch from 518ab7f to acc95db Compare March 30, 2023 17:32
@abock abock requested a review from jeffdaily as a code owner March 30, 2023 20:42
@abock abock force-pushed the abock/new-onnx-exporter-api-phase1 branch 2 times, most recently from 882c198 to b2b4d83 Compare March 30, 2023 21:23
@abock abock removed the request for review from jeffdaily March 30, 2023 21:23
@abock abock force-pushed the abock/new-onnx-exporter-api-phase1 branch 2 times, most recently from 11fdb46 to 4962704 Compare March 30, 2023 23:40
Copy link
Collaborator

@shubhambhokare1 shubhambhokare1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@abock abock force-pushed the abock/new-onnx-exporter-api-phase1 branch 2 times, most recently from eba5d6c to 388b918 Compare March 31, 2023 20:15
@abock
Copy link
Contributor Author

abock commented Apr 3, 2023

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 3, 2023
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-bionic-py3.8-clang9-slow / filter

Details for Dev Infra team Raised by workflow job

@abock abock force-pushed the abock/new-onnx-exporter-api-phase1 branch 4 times, most recently from 521f11c to 4e87a6a Compare April 4, 2023 01:30
Thiago Crepaldi and others added 2 commits April 4, 2023 14:01
This is the first phase of the new ONNX exporter API for exporting from
TorchDynamo and FX, and represents the beginning of a new era for exporting
ONNX from PyTorch.

The API here is a starting point upon which we will layer more capability
and expressiveness in subsequent phases. This first phase introduces the
following into `torch.onnx`:

dynamo_export(
    model: torch.nn.Module,
    /,
    *model_args,
    export_options: Optional[ExportOptions] = None,
    **model_kwargs,
) -> ExportOutput

class ExportOptions:
    opset_version: Optional[int] = None
    dynamic_shapes: Optional[bool] = None
    logger: Optional[logging.Logger] = None

class ExportOutputSerializer(Protocol):
    def serialize(
        self,
        export_output: ExportOutput,
        destination: io.BufferedIOBase
    ) -> None:
        ...

class ExportOutput:
    export_options: ExportOptions
    model_proto: onnx.ModelProto

    def save(
        self,
        destination: Union[str, io.BufferedIOBase],
        *,
        serializer: Optional[ExportOutputSerializer] = None
    ) -> None:
        ...

Co-authored-by: Bowen Bao <bowbao@microsoft.com>
Co-authored-by: Aaron Bockover <abock@microsoft.com>
We have a few experiments for exporting Dynamo and FX to ONNX
that this PR rationalizes through the new Exporter API and
adjusts tests to use the new API.

- A base FXGraphModuleExporter exporter from which all derive:
- DynamoExportExporter: uses dynamo.export to acquire FX graph
- DynamoOptimizeExporter: uses dynamo.optimize to acquire FX graph
- FXSymbolicTraceExporter: uses FX symbolic tracing

The dynamo_export API currently uses DynamoOptimizeExporter.
@abock abock force-pushed the abock/new-onnx-exporter-api-phase1 branch from 4e87a6a to f8ed7cc Compare April 4, 2023 14:01
@abock
Copy link
Contributor Author

abock commented Apr 4, 2023

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

ZainRizvi pushed a commit that referenced this pull request Apr 19, 2023
This is the first phase of the new ONNX exporter API for exporting from TorchDynamo and FX, and represents the beginning of a new era for exporting ONNX from PyTorch.

The API here is a starting point upon which we will layer more capability and expressiveness in subsequent phases. This first phase introduces the following into `torch.onnx`:

```python
dynamo_export(
    model: torch.nn.Module,
    /,
    *model_args,
    export_options: Optional[ExportOptions] = None,
    **model_kwargs,
) -> ExportOutput:
    ...

class ExportOptions:
    opset_version: Optional[int] = None
    dynamic_shapes: Optional[bool] = None
    logger: Optional[logging.Logger] = None

class ExportOutputSerializer(Protocol):
    def serialize(
        self,
        export_output: ExportOutput,
        destination: io.BufferedIOBase,
    ) -> None:
        ...

class ExportOutput:
    model_proto: onnx.ModelProto

    def save(
        self,
        destination: Union[str, io.BufferedIOBase],
        *,
        serializer: Optional[ExportOutputSerializer] = None,
    ) -> None:
        ...
```

In addition to the API in the first commit on this PR, we have a few experiments for exporting Dynamo and FX to ONNX that this PR rationalizes through the new Exporter API and adjusts tests to use the new API.

- A base `FXGraphModuleExporter` exporter from which all derive:
  - `DynamoExportExporter`: uses dynamo.export to acquire FX graph
  - `DynamoOptimizeExporter`: uses dynamo.optimize to acquire FX graph
  - `FXSymbolicTraceExporter`: uses FX symbolic tracing

The `dynamo_export` API currently uses `DynamoOptimizeExporter`.

### Next Steps (subsequent PRs):

* Combine `DynamoExportExporter` and `DynamoOptimizeExporter` into a single `DynamoExporter`.
* Make it easy to test `FXSymbolicTraceExporter` through the same API; eventually `FXSymbolicTraceExporter` goes away entirely when the Dynamo approach works for large models. We want to keep `FXSymbolicTraceExporter` around for now for experimenting and internal use.
* Parameterize (on `ExportOptions`) and consolidate Dynamo exporter tests.
  - This PR intentionally leaves the existing tests unchanged as much as possible except for the necessary plumbing.
* Subsequent API phases:
  - Diagnostics
  - Registry, dispatcher, and Custom Ops
  - Passes
  - Dynamic shapes

Fixes #94774

Pull Request resolved: #97920
Approved by: https://github.com/justinchuby, https://github.com/titaiwangms, https://github.com/thiagocrepaldi, https://github.com/shubhambhokare1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged module: onnx Related to torch.onnx open source release notes: onnx torch.onnx related changes that should show up in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ONNX] Reduce/Integrate multiple exporting APIs under FX-ONNX exporter
8 participants