|
6 | 6 | import torch |
7 | 7 |
|
8 | 8 | import diffusers |
9 | | -from diffusers import ComponentsManager, ModularPipeline, ModularPipelineBlocks |
| 9 | +from diffusers import AutoModel, ComponentsManager, ModularPipeline, ModularPipelineBlocks |
10 | 10 | from diffusers.guiders import ClassifierFreeGuidance |
11 | 11 | from diffusers.modular_pipelines.modular_pipeline_utils import ( |
12 | 12 | ComponentSpec, |
@@ -598,3 +598,68 @@ def test_model_description_includes_block_count(self): |
598 | 598 | content = generate_modular_model_card_content(blocks) |
599 | 599 |
|
600 | 600 | assert "5-block architecture" in content["model_description"] |
| 601 | + |
| 602 | + |
| 603 | +class TestAutoModelLoadIdTagging: |
| 604 | + def test_automodel_tags_load_id(self): |
| 605 | + model = AutoModel.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet") |
| 606 | + |
| 607 | + assert hasattr(model, "_diffusers_load_id"), "Model should have _diffusers_load_id attribute" |
| 608 | + assert model._diffusers_load_id != "null", "_diffusers_load_id should not be 'null'" |
| 609 | + |
| 610 | + # Verify load_id contains the expected fields |
| 611 | + load_id = model._diffusers_load_id |
| 612 | + assert "hf-internal-testing/tiny-stable-diffusion-xl-pipe" in load_id |
| 613 | + assert "unet" in load_id |
| 614 | + |
| 615 | + def test_automodel_update_components(self): |
| 616 | + pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") |
| 617 | + pipe.load_components(torch_dtype=torch.float32) |
| 618 | + |
| 619 | + auto_model = AutoModel.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet") |
| 620 | + |
| 621 | + pipe.update_components(unet=auto_model) |
| 622 | + |
| 623 | + assert pipe.unet is auto_model |
| 624 | + |
| 625 | + assert "unet" in pipe._component_specs |
| 626 | + spec = pipe._component_specs["unet"] |
| 627 | + assert spec.pretrained_model_name_or_path == "hf-internal-testing/tiny-stable-diffusion-xl-pipe" |
| 628 | + assert spec.subfolder == "unet" |
| 629 | + |
| 630 | + |
| 631 | +class TestLoadComponentsSkipBehavior: |
| 632 | + def test_load_components_skips_already_loaded(self): |
| 633 | + pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") |
| 634 | + pipe.load_components(torch_dtype=torch.float32) |
| 635 | + |
| 636 | + original_unet = pipe.unet |
| 637 | + |
| 638 | + pipe.load_components() |
| 639 | + |
| 640 | + # Verify that the unet is the same object (not reloaded) |
| 641 | + assert pipe.unet is original_unet, "load_components should skip already loaded components" |
| 642 | + |
| 643 | + def test_load_components_selective_loading(self): |
| 644 | + pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") |
| 645 | + |
| 646 | + pipe.load_components(names="unet", torch_dtype=torch.float32) |
| 647 | + |
| 648 | + # Verify only requested component was loaded. |
| 649 | + assert hasattr(pipe, "unet") |
| 650 | + assert pipe.unet is not None |
| 651 | + assert getattr(pipe, "vae", None) is None |
| 652 | + |
| 653 | + def test_load_components_skips_invalid_pretrained_path(self): |
| 654 | + pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") |
| 655 | + |
| 656 | + pipe._component_specs["test_component"] = ComponentSpec( |
| 657 | + name="test_component", |
| 658 | + type_hint=torch.nn.Module, |
| 659 | + pretrained_model_name_or_path=None, |
| 660 | + default_creation_method="from_pretrained", |
| 661 | + ) |
| 662 | + pipe.load_components(torch_dtype=torch.float32) |
| 663 | + |
| 664 | + # Verify test_component was not loaded |
| 665 | + assert not hasattr(pipe, "test_component") or pipe.test_component is None |
0 commit comments