-
Notifications
You must be signed in to change notification settings - Fork 26.3k
faster batch sampler #76951
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
faster batch sampler #76951
Conversation
|
Hi @didicout! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
🔗 Helpful links
❌ 1 New FailuresAs of commit 4844bb5 (more details on the Dr. CI page): Expand to see more
🕵️ 1 new failure recognized by patternsThe following CI failures do not appear to be due to upstream breakages
|
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
ejguan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for proactively trying to make batchsampler better. Do you mind listing the performance gain in the PR summary with your testing script? Besides, I also commented about adding a few tests with batchsize 4 or 8 in the issue.
|
Thank you for your reply. I have posted some new test results in the issue. I will list the performance in the PR summary if there's no problem. |
ejguan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ping me when you have PR summary ready! Really appreciate your work.
|
@ejguan Done. :) |
|
@ejguan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
ejguan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@pytorchbot merge this please |
|
Do you mind also adding std to the table in summary? Thank you |
Done. :) |
Summary: Fixes #76950 Improve the performance of iteration on `BatchSampler` , especially when `batch_size` is big. Python 3.6.8: ``` batch_size drop_last speedup ------------ ----------- ------- 4 True -18.07% 4 False 15.92% 8 True 9.43% 8 False 30.90% 64 True 54.99% 64 False 49.64% 640 True 66.26% 640 False 48.32% 6400 True 69.06% 6400 False 45.17% ``` Python 3.8.12: ``` batch_size drop_last speedup ------------ ----------- -------- 4 True -10.50% 4 False -0.78% 8 True 24.40% 8 False 10.20% 64 True 90.96% 64 False 26.09% 640 True 112.88% 640 False 20.09% 6400 True 111.80% 6400 False 18.37% ``` Check the issue page for more details of the tests. Pull Request resolved: #76951 Approved by: https://github.com/ejguan Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/d22d749a0e8436829e564796bf6cd67847199453 Reviewed By: samdow Differential Revision: D36282565 Pulled By: samdow fbshipit-source-id: cbdf554f5a9941a892e10b651c0dc709e25c7a87
Builds upon #76951. Benchmarking code is the same as in #76950. AMD Ryzen Threadripper PRO 3995WX: ``` batch_size drop_last origin new speedup ------------ ----------- -------- ------ --------- 4 True 0.94 0.5706 64.74% 4 False 0.9745 0.9468 2.93% 8 True 0.7423 0.3715 99.82% 8 False 0.7974 0.5666 40.73% 64 True 0.5394 0.2085 158.76% 64 False 0.6083 0.2697 125.51% 640 True 0.5448 0.1985 174.41% 640 False 0.7085 0.2308 206.91% 6400 True 0.5554 0.2028 173.88% 6400 False 0.7711 0.2109 265.60% 64000 True 0.556 0.2091 165.82% 64000 False 0.7803 0.2078 275.58% ``` When `drop_last == True`, it uses `zip` to speed things up. When `drop_last == False`, it uses `itertools` to speed things up. `itertools` was the fastest way I could find that deals with the last batch if it is smaller than `batch_size`. I have a pure python method too, but it is slower when `batch_size` is 4 or 8, so I have committed the `itertools` version for now. Happy to chat further about this change :-) I understand you may not want to introduce the `itertools` package into [sampler.py](https://github.com/pytorch/pytorch/blob/main/torch/utils/data/sampler.py). Pull Request resolved: #137423 Approved by: https://github.com/Skylion007
Fixes #76950
Improve the performance of iteration on
BatchSampler, especially whenbatch_sizeis big.Python 3.6.8:
Python 3.8.12:
Check the issue page for more details of the tests.