-
Notifications
You must be signed in to change notification settings - Fork 989
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
Reduce memory by using all_gather_into_tensor
#1968
Conversation
The documentation is not available anymore as the PR was closed or merged. |
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.
Nice improvement. Before proceeding, I have a few questions, please take a look. Also some minor suggestions, but those are not blockers.
src/accelerate/utils/operations.py
Outdated
state = PartialState() | ||
|
||
if state.backend is not None and state.backend != "gloo": | ||
output_tensors = torch.zeros( |
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.
Why use torch.zeros
instead of torch.empty_like
as previously?
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.
We're doing something slightly different here with the new API, where this gather works using a different tensor dim than before which is more efficient.
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.
Added 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.
Not sure, but won't this allocate more memory than previously?
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.
It's actually the opposite, hence what we're doing here.
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 @muellerzr for working on this, I have the same comments as Benjamin.
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.
In general looks good, thanks for adding support for this feature. I have only one question, but feel free to merge.
src/accelerate/utils/operations.py
Outdated
state = PartialState() | ||
|
||
if state.backend is not None and state.backend != "gloo": | ||
output_tensors = torch.zeros( |
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.
Not sure, but won't this allocate more memory than previously?
What does this PR do?
torch 1.13 introduced publicly
all_gather_into_tensor
(before this was_all_gather_base
) which is a much more memory efficient version of gather. One thing to note mentioned in the PR here is they did not have this be the base ofgather
, since it handles uneven inputs automatically. Since Accelerate does this separately and checks, we can safely use this API. Original DeepSpeed PR I discovered showing thisFor a general idea of just how much memory can be stored, I ran a small simple test:
The results can be summarized as such:
Before:
Total CUDA memory allocated: 3.83gb
After:
Total CUDA memory allocated: 2.3gb
Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@BenjaminBossan @LysandreJik