Skip to content

[BE][Ez]: Prevent copies of std::vector in CUDA ForeachOps#163416

Closed
Skylion007 wants to merge 2 commits intopytorch:mainfrom
Skylion007:skylion007/for-each-binary-op-move-2025-09-20
Closed

[BE][Ez]: Prevent copies of std::vector in CUDA ForeachOps#163416
Skylion007 wants to merge 2 commits intopytorch:mainfrom
Skylion007:skylion007/for-each-binary-op-move-2025-09-20

Conversation

@Skylion007
Copy link
Collaborator

@Skylion007 Skylion007 commented Sep 20, 2025

No need for unnecessary copy of std::vectors. This Tensor list is copied throughout the foreach paths and this code is on a hot path for torch optimizers. Auto move elision will not happen on the return statement since it's a subelement of a vector that needs to be copied out before the std::vector is dtor'd. This should reduce quite a few list copies along this path.

@pytorch-bot
Copy link

pytorch-bot bot commented Sep 20, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit 502e3cf with merge base d70c0ba (image):
💚 Looks good so far! There are no failures yet. 💚

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

@Skylion007 Skylion007 changed the title [BE]: Prevent copy of std::vector in ForeachBinaryOpScalarList [BE]: Prevent copies of std::vector in CUDA ForeachOps Sep 20, 2025
@Skylion007 Skylion007 changed the title [BE]: Prevent copies of std::vector in CUDA ForeachOps [BE][Ez]: Prevent copies of std::vector in CUDA ForeachOps Sep 20, 2025
@Skylion007 Skylion007 added the better-engineering Relatively self-contained tasks for better engineering contributors label Sep 20, 2025
@ezyang
Copy link
Contributor

ezyang commented Sep 21, 2025

@pytorchbot merge

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

mansiag05 pushed a commit to mansiag05/pytorch that referenced this pull request Sep 22, 2025
…63416)

No need for unnecessary copy of std::vectors. This Tensor list is copied throughout the foreach paths and this code is on a hot path for torch optimizers. Auto move elision will not happen on the return statement since it's a subelement of a vector that needs to be copied out before the std::vector is dtor'd. This should reduce quite a few list copies along this path.

Pull Request resolved: pytorch#163416
Approved by: https://github.com/ezyang
cleonard530 pushed a commit to cleonard530/pytorch that referenced this pull request Sep 22, 2025
…63416)

No need for unnecessary copy of std::vectors. This Tensor list is copied throughout the foreach paths and this code is on a hot path for torch optimizers. Auto move elision will not happen on the return statement since it's a subelement of a vector that needs to be copied out before the std::vector is dtor'd. This should reduce quite a few list copies along this path.

Pull Request resolved: pytorch#163416
Approved by: https://github.com/ezyang
pytorchmergebot pushed a commit that referenced this pull request Sep 22, 2025
@ezyang A follow up where I found a few more missing returns of this style in the codebase. Follow up to #163416

Pull Request resolved: #163456
Approved by: https://github.com/cyyever, https://github.com/albanD
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request Sep 26, 2025
…63416)

No need for unnecessary copy of std::vectors. This Tensor list is copied throughout the foreach paths and this code is on a hot path for torch optimizers. Auto move elision will not happen on the return statement since it's a subelement of a vector that needs to be copied out before the std::vector is dtor'd. This should reduce quite a few list copies along this path.

Pull Request resolved: pytorch#163416
Approved by: https://github.com/ezyang
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request Sep 26, 2025
@ezyang A follow up where I found a few more missing returns of this style in the codebase. Follow up to pytorch#163416

Pull Request resolved: pytorch#163456
Approved by: https://github.com/cyyever, https://github.com/albanD
pytorchmergebot pushed a commit that referenced this pull request Nov 2, 2025
pytorch-bot bot pushed a commit that referenced this pull request Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

better-engineering Relatively self-contained tasks for better engineering contributors ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: foreach_frontend release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants