-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[modular] add tests for robust model loading. #13120
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 all commits
Commits
Show all changes
4 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 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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| import torch | ||
|
|
||
| import diffusers | ||
| from diffusers import ComponentsManager, ModularPipeline, ModularPipelineBlocks | ||
| from diffusers import AutoModel, ComponentsManager, ModularPipeline, ModularPipelineBlocks | ||
| from diffusers.guiders import ClassifierFreeGuidance | ||
| from diffusers.modular_pipelines.modular_pipeline_utils import ( | ||
| ComponentSpec, | ||
|
|
@@ -598,3 +598,68 @@ def test_model_description_includes_block_count(self): | |
| content = generate_modular_model_card_content(blocks) | ||
|
|
||
| assert "5-block architecture" in content["model_description"] | ||
|
|
||
|
|
||
| class TestAutoModelLoadIdTagging: | ||
| def test_automodel_tags_load_id(self): | ||
| model = AutoModel.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet") | ||
|
|
||
| assert hasattr(model, "_diffusers_load_id"), "Model should have _diffusers_load_id attribute" | ||
| assert model._diffusers_load_id != "null", "_diffusers_load_id should not be 'null'" | ||
|
|
||
| # Verify load_id contains the expected fields | ||
| load_id = model._diffusers_load_id | ||
| assert "hf-internal-testing/tiny-stable-diffusion-xl-pipe" in load_id | ||
| assert "unet" in load_id | ||
|
|
||
| def test_automodel_update_components(self): | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
| pipe.load_components(torch_dtype=torch.float32) | ||
|
|
||
| auto_model = AutoModel.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet") | ||
|
|
||
| pipe.update_components(unet=auto_model) | ||
|
|
||
| assert pipe.unet is auto_model | ||
|
|
||
| assert "unet" in pipe._component_specs | ||
| spec = pipe._component_specs["unet"] | ||
| assert spec.pretrained_model_name_or_path == "hf-internal-testing/tiny-stable-diffusion-xl-pipe" | ||
| assert spec.subfolder == "unet" | ||
|
|
||
|
|
||
| class TestLoadComponentsSkipBehavior: | ||
|
Member
Author
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. Same as above. |
||
| def test_load_components_skips_already_loaded(self): | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
| pipe.load_components(torch_dtype=torch.float32) | ||
|
|
||
| original_unet = pipe.unet | ||
|
|
||
| pipe.load_components() | ||
|
|
||
| # Verify that the unet is the same object (not reloaded) | ||
| assert pipe.unet is original_unet, "load_components should skip already loaded components" | ||
|
|
||
| def test_load_components_selective_loading(self): | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
|
|
||
| pipe.load_components(names="unet", torch_dtype=torch.float32) | ||
|
|
||
| # Verify only requested component was loaded. | ||
| assert hasattr(pipe, "unet") | ||
| assert pipe.unet is not None | ||
| assert getattr(pipe, "vae", None) is None | ||
|
|
||
| def test_load_components_skips_invalid_pretrained_path(self): | ||
| pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") | ||
|
|
||
| pipe._component_specs["test_component"] = ComponentSpec( | ||
| name="test_component", | ||
| type_hint=torch.nn.Module, | ||
| pretrained_model_name_or_path=None, | ||
| default_creation_method="from_pretrained", | ||
| ) | ||
| pipe.load_components(torch_dtype=torch.float32) | ||
|
|
||
| # Verify test_component was not loaded | ||
| assert not hasattr(pipe, "test_component") or pipe.test_component is None | ||
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.