From d222f3ca72218e0ea9f7eb21930af419041c96be Mon Sep 17 00:00:00 2001 From: "yiyi@huggingface.co" Date: Fri, 27 Feb 2026 09:33:15 +0000 Subject: [PATCH] up --- .../modular_pipelines/modular_pipeline.py | 9 ++++++- .../test_modular_pipelines_common.py | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/diffusers/modular_pipelines/modular_pipeline.py b/src/diffusers/modular_pipelines/modular_pipeline.py index 76a850b63c4e..5d6c4064ef96 100644 --- a/src/diffusers/modular_pipelines/modular_pipeline.py +++ b/src/diffusers/modular_pipelines/modular_pipeline.py @@ -1633,7 +1633,14 @@ def __init__( blocks_class_name = self.default_blocks_name if blocks_class_name is not None: diffusers_module = importlib.import_module("diffusers") - blocks_class = getattr(diffusers_module, blocks_class_name) + blocks_class = getattr(diffusers_module, blocks_class_name, None) + # If the blocks_class is not found or is a base class (e.g. SequentialPipelineBlocks saved by from_blocks_dict) with empty block_classes + # fall back to default_blocks_name + if blocks_class is None or not blocks_class.block_classes: + blocks_class_name = self.default_blocks_name + blocks_class = getattr(diffusers_module, blocks_class_name) + + if blocks_class is not None: blocks = blocks_class() else: logger.warning(f"`blocks` is `None`, no default blocks class found for {self.__class__.__name__}") diff --git a/tests/modular_pipelines/test_modular_pipelines_common.py b/tests/modular_pipelines/test_modular_pipelines_common.py index e97b543ff85d..1ebd41faf5cf 100644 --- a/tests/modular_pipelines/test_modular_pipelines_common.py +++ b/tests/modular_pipelines/test_modular_pipelines_common.py @@ -699,3 +699,27 @@ def test_load_components_skips_invalid_pretrained_path(self): # Verify test_component was not loaded assert not hasattr(pipe, "test_component") or pipe.test_component is None + + +class TestModularPipelineInitFallback: + """Test that ModularPipeline.__init__ falls back to default_blocks_name when + _blocks_class_name is a base class (e.g. SequentialPipelineBlocks saved by from_blocks_dict).""" + + def test_init_fallback_when_blocks_class_name_is_base_class(self, tmp_path): + # 1. Load pipeline and get a workflow (returns a base SequentialPipelineBlocks) + pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") + t2i_blocks = pipe.blocks.get_workflow("text2image") + assert t2i_blocks.__class__.__name__ == "SequentialPipelineBlocks" + + # 2. Use init_pipeline to create a new pipeline from the workflow blocks + t2i_pipe = t2i_blocks.init_pipeline("hf-internal-testing/tiny-stable-diffusion-xl-pipe") + + # 3. Save and reload — the saved config will have _blocks_class_name="SequentialPipelineBlocks" + save_dir = str(tmp_path / "pipeline") + t2i_pipe.save_pretrained(save_dir) + loaded_pipe = ModularPipeline.from_pretrained(save_dir) + + # 4. Verify it fell back to default_blocks_name and has correct blocks + assert loaded_pipe.__class__.__name__ == pipe.__class__.__name__ + assert loaded_pipe._blocks.__class__.__name__ == pipe._blocks.__class__.__name__ + assert len(loaded_pipe._blocks.sub_blocks) == len(pipe._blocks.sub_blocks)