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

[onnx.export] Avoid unnecessary copy of debug_names #123026

Conversation

gustavla
Copy link
Contributor

This PR is part of an effort to speed up torch.onnx.export (#121422).

  • The auto debug_names = infers a copy, where as const auto& debug_names does not.
  • However, this ones requires us to be careful, since calls to setDebugName changes debug_names and invalidates the exist_name iterator. So if we simply change auto to const auto&, then between that line and find we have corrupted the iterator by calling output[i]->setDebugName. This change aims to be functionally equivalent to the original, which is why we first get the Value pointer, then call output[i]->setDebugName, and finally call setDebugName on the found value. It is possible functionally it is OK to simply call output[i]->setDebugName first and then find and the second setDebugName, but this would not be identical to current behavior.
  • Resolves (2) in ONNX export is unnecessarily slow (O(N^2)) #121422.

Copy link

pytorch-bot bot commented Mar 30, 2024

🔗 Helpful Links

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

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

✅ No Failures

As of commit 0f522ce with merge base a91311e (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 Mar 30, 2024
@thiagocrepaldi thiagocrepaldi added module: onnx Related to torch.onnx onnx-triaged triaged by ONNX team labels Apr 1, 2024
@janeyx99 janeyx99 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Apr 1, 2024
@gustavla
Copy link
Contributor Author

@justinchuby Let me know if there is any changes needed to this one, thanks!

Comment on lines 303 to 304
// Note: calls to setDebugName will change debug_names and corrupt
// exist_name
Copy link
Collaborator

Choose a reason for hiding this comment

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

The original comments seem to be clearer, (even though I don't understand the behavior completely). Do you have an idea of what happens here; could you potentially improve the note?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is really subtle, so I have taken a stab at making this even more clear. I'll let the code change speak for itself, so let me know if it's clear now.

@gustavla
Copy link
Contributor Author

@justinchuby Good to merge? (I can't issue that even with approval) Thanks!

@justinchuby
Copy link
Collaborator

@pytorchbot merge

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

@gustavla
Copy link
Contributor Author

These failures look unrelated. I may need to rebase and push again. That seems to do the trick in these situations.

- This ones requires us to be careful, since calls to `setDebugName`
  changes `debug_names` and invalidates the `exist_name` iterator.
  So if we simply change `auto` to `const auto&`, then between
  that line and `find` we have corrupted the iterator by calling
  `output[i]->setDebugName`. This change aims to be functionally
  equivalent to the original, which is why we first get the Value
  pointer, then call `output[i]->setDebugName`, and finally call
  `setDebugName` on the found value. It is possible functionally
  it is OK to simply call `output[i]->setDebugName` first and then
  find and the second `setDebugName`, but this would not be identical
  to current behavior.
- Resolves (2) in pytorch#121422.
@gustavla gustavla force-pushed the dev/gustav/onnx_export_speedup_121422_B_1 branch from d19dd69 to 0f522ce Compare May 15, 2024 17:32
@justinchuby
Copy link
Collaborator

@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

ezyang added a commit that referenced this pull request May 16, 2024
This reverts commit 31d2285.

ghstack-source-id: 8dc497e91ca0bf248c5da5f01baba9dd5d88a092
Pull Request resolved: #126443
ZelboK pushed a commit to ZelboK/pytorch that referenced this pull request May 19, 2024
This PR is part of an effort to speed up torch.onnx.export (pytorch#121422).

- The `auto debug_names = ` infers a copy, where as `const auto& debug_names` does not.
- However, this ones requires us to be careful, since calls to `setDebugName` changes `debug_names` and invalidates the `exist_name` iterator. So if we simply change `auto` to `const auto&`, then between that line and `find` we have corrupted the iterator by calling `output[i]->setDebugName`. This change aims to be functionally equivalent to the original, which is why we first get the Value pointer, then call `output[i]->setDebugName`, and finally call `setDebugName` on the found value. It is possible functionally it is OK to simply call `output[i]->setDebugName` first and then find and the second `setDebugName`, but this would not be identical to current behavior.
- Resolves (2) in pytorch#121422.
Pull Request resolved: pytorch#123026
Approved by: https://github.com/justinchuby
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 onnx-triaged triaged by ONNX team open source release notes: onnx torch.onnx related changes that should show up in the release notes triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

6 participants