Skip to content

Conversation

@justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Oct 9, 2024

Stack from ghstack (oldest at bottom):

Move optimization from the export call to the optimize() method in ONNXProgram.

Users can call optimize() before calling save() to save the model. Right now if users set optimize=True in torch.onnx.export it will have the same effect as calling optimize(), but in the future we can evolve the method to be more flexible (e.g. target aware etc.)

Example

onnx_program = torch.onnx.export(..., dynamo=True)
onnx_program.optimize()
onnx_program.save("model.onnx")

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Oct 9, 2024

🔗 Helpful Links

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

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

✅ No Failures

As of commit 9bbfae5 with merge base 7408742 (image):
💚 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 Oct 9, 2024
justinchuby added a commit that referenced this pull request Oct 9, 2024
Move optimization from the export call to the `optimize()` method in ONNXProgram.

Users can call `optimize()` before calling `save()` to save the model. The change is made to allow users to control optimization behavior. It is not always applied in torch.onnx.export so the core export logic can be more robust.

ghstack-source-id: 17ddf6a
Pull Request resolved: #137667
@justinchuby justinchuby changed the title [ONNX] Create an optimize api in ONNXProgram [ONNX] Create an optimize method in ONNXProgram Oct 9, 2024
@justinchuby justinchuby requested a review from xadupre October 9, 2024 23:23
@justinchuby justinchuby added module: onnx Related to torch.onnx topic: new features topic category labels Oct 9, 2024
@titaiwangms
Copy link
Collaborator

Although this is good for developer to maintain and debug, from user perspective, it's a change of usage in a way that now they have to call an extra step to have the same performance as torchscript. I think optimize by default might be a better choice.

@justinchuby
Copy link
Collaborator Author

justinchuby commented Oct 9, 2024

Issue with this is the optimizer may fail sometimes, and it will take a much larger effort to bring it to the point where it will run successfully on all possible dtypes, most models etc. So having an option would be good imo. We could add an optimize=True option in export() too, and default it to true. So users can at least turn it off? The reason I did not do it is that it creates duplicates - one in the export api and the other in the ONNXProgram itself, but that may be the compromise we will have to make.

Move optimization from the export call to the `optimize()` method in ONNXProgram.

Users can call `optimize()` before calling `save()` to save the model. The change is made to allow users to control optimization behavior. It is not always applied in torch.onnx.export so the core export logic can be more robust.

Example

```python
onnx_program = torch.onnx.export(..., dynamo=True)
onnx_program.optimize()
onnx_program.save("model.onnx")
```


[ghstack-poisoned]
justinchuby added a commit that referenced this pull request Oct 10, 2024
Move optimization from the export call to the `optimize()` method in ONNXProgram.

Users can call `optimize()` before calling `save()` to save the model. The change is made to allow users to control optimization behavior. It is not always applied in torch.onnx.export so the core export logic can be more robust.

ghstack-source-id: 70af16b
Pull Request resolved: #137667
@justinchuby
Copy link
Collaborator Author

justinchuby commented Oct 10, 2024

Updated to provide the optimize=False api in torch.onnx.export and make it to call this method. When the optimizer become stable, we can default it to True. Or we can educate users to leverage the ONNXProgram object for the most flexibility. The torch.onnx.export(..., f) call (with path) should just be a transitional thing

@justinchuby
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 10, 2024
@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

@justinchuby
Copy link
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: Command git -C /home/runner/work/pytorch/pytorch cherry-pick -x 6e19ab4cfc28b60a5f6c63f2d6f87fffb0db16b3 returned non-zero exit code 1

Auto-merging torch/onnx/_internal/exporter/_compat.py
CONFLICT (content): Merge conflict in torch/onnx/_internal/exporter/_compat.py
error: could not apply 6e19ab4cfc... [ONNX] Create an optimize api in ONNXProgram
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Details for Dev Infra team Raised by workflow job

@pytorchmergebot
Copy link
Collaborator

The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command
For more information see pytorch-bot wiki.

@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

@github-actions github-actions bot deleted the gh/justinchuby/107/head branch November 10, 2024 02:07
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 topic: new features topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants