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

Add balanced strategies for device_map in from_pretrained #18349

Merged
merged 4 commits into from
Aug 1, 2022

Conversation

sgugger
Copy link
Collaborator

@sgugger sgugger commented Jul 28, 2022

What does this PR do?

This PR brings to Transformers the functionality introduced in huggingface/accelerate#534 .
Basically device_map can now take several options:

  • "sequential" which corresponds to the current auto: fill each GPU sequentially (and if the user has lots of GPU spaces, some are not used at all)
  • "balanced" which will split the model evenly across GPUs
  • "balanced_low_0" which will split the model evenly across GPUs while leaving the most available memory on GPU 0, since that GPU might have more tensors on it when the outputs are used for some form of post-processing (generate and use_cache for instance)
  • "auto" which now defaults to "balanced".

When the user does not have enough GPU memory to accommodate the model, all the options are equivalent.

@sgugger sgugger requested a review from LysandreJik July 28, 2022 19:33
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jul 28, 2022

The documentation is not available anymore as the PR was closed or merged.

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Very clean implementation! Thanks for porting the work to transformers.

Comment on lines +88 to +91
if version.parse(accelerate_version) > version.parse("0.11.0"):
from accelerate.utils import get_balanced_memory
else:
get_balanced_memory = None
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

src/transformers/modeling_utils.py Outdated Show resolved Hide resolved
sgugger and others added 2 commits August 1, 2022 07:59
Co-authored-by: Lysandre Debut <lysandre.debut@reseau.eseo.fr>
@sgugger sgugger merged commit e0bc4c7 into main Aug 1, 2022
@sgugger sgugger deleted the balanced_device_map branch August 1, 2022 14:28
barry-jin pushed a commit to barry-jin/transformers that referenced this pull request Sep 8, 2022
…e#18349)

* Add balanced strategies for device_map in from_pretrained

* Add safeguards for Accelerate version

* Update src/transformers/modeling_utils.py

Co-authored-by: Lysandre Debut <lysandre.debut@reseau.eseo.fr>

* Style

Co-authored-by: Lysandre Debut <lysandre.debut@reseau.eseo.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants