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

Limit the use of PreTrainedModel.device #16935

Merged
merged 2 commits into from
Apr 26, 2022
Merged

Limit the use of PreTrainedModel.device #16935

merged 2 commits into from
Apr 26, 2022

Conversation

sgugger
Copy link
Collaborator

@sgugger sgugger commented Apr 25, 2022

What does this PR do?

I'm currently working on solutions to do model parallelism, offload weights to the CPU or the hard drive, and I've encountered some bugs linked to the way we use the PreTrainedModel.device: it grabs the first parameter of the model to infer a device for the whole model. This doesn't work when the model is:

  • split on several devices and the first parameter grabbed happens to be on the wrong one
  • not materialized because its parameters are offloaded on the CPU or the hard-drive.

So whenever it's possible, it would be great to rely on something else if we can, for instance some device where the inputs are. This PR does this for every use of this device attribute in modeling_utils and generation_utils, with the exception of some code where there are no inputs passed so we generate them and have to use something for the device.

If all works well, I plan to add all modeling files that make use of that attribute (when in the dummy_inputs, I'll leave the self.device but outside of it, will grab the device of any inputs we have).

Comment on lines +542 to +543
if device is None:
device = self.device
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Default to self.device here for a 100% backward compatible change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great thanks!

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Apr 25, 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.

LGTM, thanks @sgugger!

@sgugger sgugger merged commit 344b9fb into main Apr 26, 2022
@sgugger sgugger deleted the avoid_self_device branch April 26, 2022 00:58
elusenji pushed a commit to elusenji/transformers that referenced this pull request Jun 12, 2022
* Limit the use of PreTrainedModel.device

* Fix
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.

4 participants