-
Notifications
You must be signed in to change notification settings - Fork 6k
allow models to run with a user-provided dtype map instead of a single dtype #10301
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3643246
allow models to run with a user-provided dtype map instead of a singl…
hlky db77006
make style
hlky 2c58c64
Add warning, change `_` to `default`
hlky 2adba04
make style
hlky 156e6db
Merge branch 'main' into dtype-map
hlky 6de7479
Merge branch 'main' into dtype-map
sayakpaul e8ac2dd
Merge branch 'main' into dtype-map
hlky 70ae4b6
Merge branch 'main' into dtype-map
hlky b1237f7
Merge branch 'main' into dtype-map
hlky 1dc755c
Merge remote-tracking branch 'upstream/main' into dtype-map
hlky ec53008
add test
hlky e8aa61b
handle shared tensors
hlky 8f71311
remove warning
hlky 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -552,9 +552,12 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P | |
saved using | ||
[`~DiffusionPipeline.save_pretrained`]. | ||
- A path to a *directory* (for example `./my_pipeline_directory/`) containing a dduf file | ||
torch_dtype (`str` or `torch.dtype`, *optional*): | ||
torch_dtype (`str` or `torch.dtype` or `dict[str, Union[str, torch.dtype]]`, *optional*): | ||
Override the default `torch.dtype` and load the model with another dtype. If "auto" is passed, the | ||
dtype is automatically derived from the model's weights. | ||
dtype is automatically derived from the model's weights. To load submodels with different dtype pass a | ||
`dict` (for example `{'transformer': torch.bfloat16, 'vae': torch.float16}`). Set the default dtype for | ||
unspecified components with `default` (for example `{'transformer': torch.bfloat16, 'default': | ||
torch.float16}`). If a component is not specified and no default is set, `torch.float32` is used. | ||
custom_pipeline (`str`, *optional*): | ||
|
||
<Tip warning={true}> | ||
|
@@ -703,7 +706,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P | |
use_onnx = kwargs.pop("use_onnx", None) | ||
load_connected_pipeline = kwargs.pop("load_connected_pipeline", False) | ||
|
||
if torch_dtype is not None and not isinstance(torch_dtype, torch.dtype): | ||
if torch_dtype is not None and not isinstance(torch_dtype, dict) and not isinstance(torch_dtype, torch.dtype): | ||
torch_dtype = torch.float32 | ||
logger.warning( | ||
f"Passed `torch_dtype` {torch_dtype} is not a `torch.dtype`. Defaulting to `torch.float32`." | ||
|
@@ -884,6 +887,20 @@ def load_module(name, value): | |
|
||
init_dict = {k: v for k, v in init_dict.items() if load_module(k, v)} | ||
|
||
# Check `torch_dtype` map for unused keys | ||
if isinstance(torch_dtype, dict): | ||
extra_keys_dtype = set(torch_dtype.keys()) - set(passed_class_obj.keys()) | ||
extra_keys_obj = set(passed_class_obj.keys()) - set(torch_dtype.keys()) | ||
if len(extra_keys_dtype) > 0: | ||
logger.warning( | ||
f"Expected `{list(passed_class_obj.keys())}`, got extra `torch_dtype` keys `{extra_keys_dtype}`." | ||
) | ||
if len(extra_keys_obj) > 0: | ||
logger.warning( | ||
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. I don't think we need this warning. I think the expectation of passed class objects is that their dtype is already set and if it isn't it happens at the model level where a |
||
f"Expected `{list(passed_class_obj.keys())}`, missing `torch_dtype` keys `{extra_keys_dtype}`." | ||
" using `default` or `torch.float32`." | ||
) | ||
|
||
# Special case: safety_checker must be loaded separately when using `from_flax` | ||
if from_flax and "safety_checker" in init_dict and "safety_checker" not in passed_class_obj: | ||
raise NotImplementedError( | ||
|
@@ -950,14 +967,19 @@ def load_module(name, value): | |
loaded_sub_model = passed_class_obj[name] | ||
else: | ||
# load sub model | ||
sub_model_dtype = ( | ||
DN6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
torch_dtype.get(name, torch_dtype.get("default", torch.float32)) | ||
if isinstance(torch_dtype, dict) | ||
else torch_dtype | ||
) | ||
loaded_sub_model = load_sub_model( | ||
library_name=library_name, | ||
class_name=class_name, | ||
importable_classes=importable_classes, | ||
pipelines=pipelines, | ||
is_pipeline_module=is_pipeline_module, | ||
pipeline_class=pipeline_class, | ||
torch_dtype=torch_dtype, | ||
torch_dtype=sub_model_dtype, | ||
provider=provider, | ||
sess_options=sess_options, | ||
device_map=current_device_map, | ||
|
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.