Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
[refactor] DiffusionPipeline.download#9557
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d57f79
update
a-r-r-o-w 5db3506
make style
a-r-r-o-w 87805a5
fix import
a-r-r-o-w 20c08f5
update test
a-r-r-o-w e821552
Merge branch 'main' into refactor-download
a-r-r-o-w a152332
Merge branch 'main' into refactor-download
sayakpaul 41ccf7c
Merge branch 'main' into refactor-download
DN6 09fa592
Update src/diffusers/pipelines/pipeline_utils.py
a-r-r-o-w 8d1314c
apply suggestions from review
a-r-r-o-w e19e800
fix import
a-r-r-o-w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -71,15 +71,16 @@ | ||
| CUSTOM_PIPELINE_FILE_NAME, | ||
| LOADABLE_CLASSES, | ||
| _fetch_class_library_tuple, | ||
| _get_custom_components_and_folders, | ||
| _get_custom_pipeline_class, | ||
| _get_final_device_map, | ||
| _get_ignore_patterns, | ||
| _get_pipeline_class, | ||
| _identify_model_variants, | ||
| _maybe_raise_warning_for_inpainting, | ||
| _resolve_custom_pipeline_and_cls, | ||
| _unwrap_model, | ||
| _update_init_kwargs_with_connected_pipeline, | ||
| is_safetensors_compatible, | ||
| load_sub_model, | ||
| maybe_raise_or_warn, | ||
| variant_compatible_siblings, | ||
| @@ -1298,44 +1299,18 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]: | ||
| config_dict = cls._dict_from_json_file(config_file) | ||
| ignore_filenames = config_dict.pop("_ignore_files", []) | ||
| # retrieve all folder_names that contain relevant files | ||
| folder_names = [k for k, v in config_dict.items() if isinstance(v, list) and k != "_class_name"] | ||
| diffusers_module = importlib.import_module(__name__.split(".")[0]) | ||
| pipelines = getattr(diffusers_module, "pipelines") | ||
| # optionally create a custom component <> custom file mapping | ||
| custom_components = {} | ||
| for component in folder_names: | ||
| module_candidate = config_dict[component][0] | ||
| if module_candidate is None or not isinstance(module_candidate, str): | ||
| continue | ||
| # We compute candidate file path on the Hub. Do not use `os.path.join`. | ||
| candidate_file = f"{component}/{module_candidate}.py" | ||
| if candidate_file in filenames: | ||
| custom_components[component] = module_candidate | ||
| elif module_candidate not in LOADABLE_CLASSES and not hasattr(pipelines, module_candidate): | ||
| raise ValueError( | ||
| f"{candidate_file} as defined in `model_index.json` does not exist in {pretrained_model_name} and is not a module in 'diffusers/pipelines'." | ||
| ) | ||
| if len(variant_filenames) == 0 and variant is not None: | ||
| error_message = f"You are trying to load the model files of the `variant={variant}`, but no such modeling files are available." | ||
| raise ValueError(error_message) | ||
| # remove ignored filenames | ||
| model_filenames = set(model_filenames) - set(ignore_filenames) | ||
| variant_filenames = set(variant_filenames) - set(ignore_filenames) | ||
| # if the whole pipeline is cached we don't have to ping the Hub | ||
a-r-r-o-w marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if revision in DEPRECATED_REVISION_ARGS and version.parse( | ||
| version.parse(__version__).base_version | ||
| ) >= version.parse("0.22.0"): | ||
| warn_deprecated_model_variant(pretrained_model_name, token, variant, revision, model_filenames) | ||
| custom_components, folder_names = _get_custom_components_and_folders( | ||
| pretrained_model_name, config_dict, filenames, variant_filenames, variant | ||
| ) | ||
| model_folder_names = {os.path.split(f)[0] for f in model_filenames if os.path.split(f)[0] in folder_names} | ||
| custom_class_name = None | ||
| @@ -1395,49 +1370,19 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]: | ||
| expected_components, _ = cls._get_signature_keys(pipeline_class) | ||
| passed_components = [k for k in expected_components if k in kwargs] | ||
| if ( | ||
| use_safetensors | ||
| and not allow_pickle | ||
| and not is_safetensors_compatible( | ||
| model_filenames, passed_components=passed_components, folder_names=model_folder_names | ||
| ) | ||
| ): | ||
| raise EnvironmentError( | ||
| f"Could not find the necessary `safetensors` weights in {model_filenames} (variant={variant})" | ||
| ) | ||
| if from_flax: | ||
| ignore_patterns = ["*.bin", "*.safetensors", "*.onnx", "*.pb"] | ||
| elif use_safetensors and is_safetensors_compatible( | ||
| model_filenames, passed_components=passed_components, folder_names=model_folder_names | ||
| ): | ||
| ignore_patterns = ["*.bin", "*.msgpack"] | ||
| use_onnx = use_onnx if use_onnx is not None else pipeline_class._is_onnx | ||
| if not use_onnx: | ||
| ignore_patterns += ["*.onnx", "*.pb"] | ||
| safetensors_variant_filenames = {f for f in variant_filenames if f.endswith(".safetensors")} | ||
| safetensors_model_filenames = {f for f in model_filenames if f.endswith(".safetensors")} | ||
| if ( | ||
| len(safetensors_variant_filenames) > 0 | ||
| and safetensors_model_filenames != safetensors_variant_filenames | ||
| ): | ||
| logger.warning( | ||
| f"\nA mixture of {variant} and non-{variant} filenames will be loaded.\nLoaded {variant} filenames:\n[{', '.join(safetensors_variant_filenames)}]\nLoaded non-{variant} filenames:\n[{', '.join(safetensors_model_filenames - safetensors_variant_filenames)}\nIf this behavior is not expected, please check your folder structure." | ||
| ) | ||
| else: | ||
| ignore_patterns = ["*.safetensors", "*.msgpack"] | ||
| use_onnx = use_onnx if use_onnx is not None else pipeline_class._is_onnx | ||
| if not use_onnx: | ||
| ignore_patterns += ["*.onnx", "*.pb"] | ||
| bin_variant_filenames = {f for f in variant_filenames if f.endswith(".bin")} | ||
| bin_model_filenames = {f for f in model_filenames if f.endswith(".bin")} | ||
| if len(bin_variant_filenames) > 0 and bin_model_filenames != bin_variant_filenames: | ||
| logger.warning( | ||
| f"\nA mixture of {variant} and non-{variant} filenames will be loaded.\nLoaded {variant} filenames:\n[{', '.join(bin_variant_filenames)}]\nLoaded non-{variant} filenames:\n[{', '.join(bin_model_filenames - bin_variant_filenames)}\nIf this behavior is not expected, please check your folder structure." | ||
| ) | ||
| # retrieve all patterns that should not be downloaded and error out when needed | ||
| ignore_patterns = _get_ignore_patterns( | ||
| passed_components, | ||
| model_folder_names, | ||
| model_filenames, | ||
| variant_filenames, | ||
| use_safetensors, | ||
| from_flax, | ||
| allow_pickle, | ||
| use_onnx, | ||
| pipeline_class._is_onnx, | ||
| variant, | ||
| ) | ||
| # Don't download any objects that are passed | ||
| allow_patterns = [ | ||
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
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.
Very nice!