-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Fix compatibility with pipeline when loading model with device_map on single gpu #10390
Open
SunMarc
wants to merge
2
commits into
main
Choose a base branch
from
remove-force-hook-from-model-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -411,6 +411,13 @@ def module_is_offloaded(module): | |
pipeline_is_sequentially_offloaded = any( | ||
module_is_sequentially_offloaded(module) for _, module in self.components.items() | ||
) | ||
|
||
is_pipeline_device_mapped = self.hf_device_map is not None and len(self.hf_device_map) > 1 | ||
if is_pipeline_device_mapped: | ||
raise ValueError( | ||
"It seems like you have activated a device mapping strategy on the pipeline which doesn't allow explicit device placement using `to()`. You can call `reset_device_map()` first and then call `to()`." | ||
) | ||
SunMarc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved up because otherwise it will trigger the |
||
if device and torch.device(device).type == "cuda": | ||
if pipeline_is_sequentially_offloaded and not pipeline_has_bnb: | ||
raise ValueError( | ||
|
@@ -422,12 +429,6 @@ def module_is_offloaded(module): | |
"You are trying to call `.to('cuda')` on a pipeline that has models quantized with `bitsandbytes`. Your current `accelerate` installation does not support it. Please upgrade the installation." | ||
) | ||
|
||
is_pipeline_device_mapped = self.hf_device_map is not None and len(self.hf_device_map) > 1 | ||
if is_pipeline_device_mapped: | ||
raise ValueError( | ||
"It seems like you have activated a device mapping strategy on the pipeline which doesn't allow explicit device placement using `to()`. You can call `reset_device_map()` first and then call `to()`." | ||
) | ||
|
||
# Display a warning in this case (the operation succeeds but the benefits are lost) | ||
pipeline_is_offloaded = any(module_is_offloaded(module) for _, module in self.components.items()) | ||
if pipeline_is_offloaded and device and torch.device(device).type == "cuda": | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These are not needed because we actually removes all hooks and dispatch the model again in the pipeline logic.
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.
If the model were to be used independently of the pipeline, would removing this be sensible?
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.
Only if the user used device_map with only one gpu. Should be fine to be honest. The user actually expect to be able to move the model without any issues if the model in dispatch on only one gpu.