-
Notifications
You must be signed in to change notification settings - Fork 7.3k
ModularPipeline.save_pretrained: change the default overwrite_modular_index to be True
#14659
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
+88
−15
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7117fd0
flipt default for save_pretrained overwrite_modular_index to be True
yiyixuxu 6ffb8b4
update doc
yiyixuxu 6bddc76
make style
yiyixuxu 141eac2
Merge branch 'main' of github.com:huggingface/diffusers into docs-mod…
yiyixuxu 51c129a
Update docs/source/en/modular_diffusers/modular_pipeline.md
yiyixuxu 7785b74
Update docs/source/en/modular_diffusers/modular_pipeline.md
yiyixuxu 8b30dd9
Update docs/source/en/modular_diffusers/modular_pipeline.md
yiyixuxu 028c124
Update docs/source/en/modular_diffusers/modular_pipeline.md
yiyixuxu eebad8c
address review comments
yiyixuxu 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
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import json | ||
| import os | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from diffusers import AutoModel, ControlNetModel, ModularPipeline, UNet2DConditionModel | ||
|
|
@@ -120,16 +121,17 @@ def test_load_components_skips_invalid_pretrained_path(self): | |
|
|
||
|
|
||
| class TestCustomModelSavePretrained: | ||
| def test_save_pretrained_updates_index_for_local_model(self, tmp_path): | ||
| """When a component without _diffusers_load_id (custom/local model) is saved, | ||
| modular_model_index.json should point to the save directory.""" | ||
| @pytest.mark.parametrize("overwrite_modular_index", [True, False]) | ||
| def test_save_pretrained_updates_index_for_local_model(self, tmp_path, overwrite_modular_index): | ||
| """A component without _diffusers_load_id (custom/local model) is rewritten to the save directory in both | ||
| modes; other components' specs follow `overwrite_modular_index`.""" | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
| pipe.load_components(dtype=torch.float32) | ||
|
|
||
| pipe.unet._diffusers_load_id = "null" | ||
|
|
||
| save_dir = str(tmp_path / "my-pipeline") | ||
| pipe.save_pretrained(save_dir) | ||
| pipe.save_pretrained(save_dir, overwrite_modular_index=overwrite_modular_index) | ||
|
|
||
| with open(os.path.join(save_dir, "modular_model_index.json")) as f: | ||
| index = json.load(f) | ||
|
|
@@ -139,7 +141,8 @@ def test_save_pretrained_updates_index_for_local_model(self, tmp_path): | |
| assert unet_spec["subfolder"] == "unet" | ||
|
|
||
| _library, _cls, vae_spec = index["vae"] | ||
| assert vae_spec["pretrained_model_name_or_path"] == "hf-internal-testing/tiny-stable-diffusion-xl-pipe" | ||
| expected_vae = save_dir if overwrite_modular_index else "hf-internal-testing/tiny-stable-diffusion-xl-pipe" | ||
| assert vae_spec["pretrained_model_name_or_path"] == expected_vae | ||
|
|
||
| def test_save_pretrained_roundtrip_with_local_model(self, tmp_path): | ||
| """A pipeline with a custom/local model should be saveable and re-loadable with identical outputs.""" | ||
|
|
@@ -164,9 +167,10 @@ def test_save_pretrained_roundtrip_with_local_model(self, tmp_path): | |
| for key in original_state_dict: | ||
| assert torch.equal(original_state_dict[key], loaded_state_dict[key]), f"Mismatch in {key}" | ||
|
|
||
| def test_save_pretrained_updates_index_for_model_with_no_load_id(self, tmp_path): | ||
| @pytest.mark.parametrize("overwrite_modular_index", [True, False]) | ||
| def test_save_pretrained_updates_index_for_model_with_no_load_id(self, tmp_path, overwrite_modular_index): | ||
| """testing the workflow of update the pipeline with a custom model and save the pipeline, | ||
| the modular_model_index.json should point to the save directory.""" | ||
| the modular_model_index.json should point to the save directory in both modes.""" | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
| pipe.load_components(dtype=torch.float32) | ||
|
|
||
|
|
@@ -177,6 +181,26 @@ def test_save_pretrained_updates_index_for_model_with_no_load_id(self, tmp_path) | |
|
|
||
| pipe.update_components(unet=unet) | ||
|
|
||
| save_dir = str(tmp_path / "my-pipeline") | ||
| pipe.save_pretrained(save_dir, overwrite_modular_index=overwrite_modular_index) | ||
|
|
||
| with open(os.path.join(save_dir, "modular_model_index.json")) as f: | ||
| index = json.load(f) | ||
|
|
||
| _library, _cls, unet_spec = index["unet"] | ||
| assert unet_spec["pretrained_model_name_or_path"] == save_dir | ||
| assert unet_spec["subfolder"] == "unet" | ||
|
|
||
| _library, _cls, vae_spec = index["vae"] | ||
| expected_vae = save_dir if overwrite_modular_index else "hf-internal-testing/tiny-stable-diffusion-xl-pipe" | ||
| assert vae_spec["pretrained_model_name_or_path"] == expected_vae | ||
|
Comment on lines
+187
to
+196
Member
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. Very nice! |
||
|
|
||
| def test_save_pretrained_default_writes_self_contained_local_copy(self, tmp_path): | ||
| """By default the saved index points at the save directory, so the copy reloads offline; a component | ||
| that was never loaded is not saved and keeps its recorded spec.""" | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
| pipe.load_components(names=["unet"], dtype=torch.float32) | ||
|
|
||
| save_dir = str(tmp_path / "my-pipeline") | ||
| pipe.save_pretrained(save_dir) | ||
|
|
||
|
|
@@ -186,10 +210,15 @@ def test_save_pretrained_updates_index_for_model_with_no_load_id(self, tmp_path) | |
| _library, _cls, unet_spec = index["unet"] | ||
| assert unet_spec["pretrained_model_name_or_path"] == save_dir | ||
| assert unet_spec["subfolder"] == "unet" | ||
| assert unet_spec["revision"] is None | ||
|
|
||
| _library, _cls, vae_spec = index["vae"] | ||
| assert vae_spec["pretrained_model_name_or_path"] == "hf-internal-testing/tiny-stable-diffusion-xl-pipe" | ||
|
|
||
| loaded_pipe = ModularPipeline.from_pretrained(save_dir) | ||
| loaded_pipe.load_components(names=["unet"], dtype=torch.float32, local_files_only=True) | ||
| assert loaded_pipe.unet is not None | ||
|
|
||
| def test_save_pretrained_overwrite_modular_index(self, tmp_path): | ||
| """With overwrite_modular_index=True, all component references should point to the save directory.""" | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
|
|
||
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.
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.
But is there any reason to include
revisionhere?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.
just in case the original entry on modular_model_index has a revision and we overwrite, we need to set it to None since it would not apply for the "local compnent"