From 2c45daff1c08a22f04eb3bb7a81aa1894a79fb60 Mon Sep 17 00:00:00 2001 From: yiyixuxu Date: Thu, 17 Jul 2025 01:19:08 +0200 Subject: [PATCH] up --- .../modular_pipelines/modular_pipeline.py | 28 ++++++++++++----- src/diffusers/pipelines/pipeline_utils.py | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/diffusers/modular_pipelines/modular_pipeline.py b/src/diffusers/modular_pipelines/modular_pipeline.py index 5bf4730782..601b03dc90 100644 --- a/src/diffusers/modular_pipelines/modular_pipeline.py +++ b/src/diffusers/modular_pipelines/modular_pipeline.py @@ -1876,7 +1876,6 @@ class ModularPipeline(ConfigMixin, PushToHubMixin): # update component_specs and config_specs from modular_repo if pretrained_model_name_or_path is not None: - try: config_dict = self.load_config(pretrained_model_name_or_path, **kwargs) for name, value in config_dict.items(): @@ -1892,8 +1891,9 @@ class ModularPipeline(ConfigMixin, PushToHubMixin): except EnvironmentError as e: logger.debug(e) - logger.debug(f" modular_model_index.json not found in the repo, trying to load from model_index.json") + logger.debug(" modular_model_index.json not found in the repo, trying to load from model_index.json") from diffusers import DiffusionPipeline + config_dict = DiffusionPipeline.load_config(pretrained_model_name_or_path) for name, value in config_dict.items(): if name in self._component_specs and isinstance(value, (tuple, list)) and len(value) == 2: @@ -2435,17 +2435,31 @@ class ModularPipeline(ConfigMixin, PushToHubMixin): for name, component in passed_components.items(): current_component_spec = self._component_specs[name] - # warn if type changed + # log if type changed if current_component_spec.type_hint is not None and not isinstance( component, current_component_spec.type_hint ): - logger.warning( + logger.info( f"ModularPipeline.update_components: adding {name} with new type: {component.__class__.__name__}, previous type: {current_component_spec.type_hint.__name__}" ) # update _component_specs based on the new component - new_component_spec = ComponentSpec.from_component(name, component) - if new_component_spec.default_creation_method != current_component_spec.default_creation_method: + if component is None: + new_component_spec = current_component_spec + if hasattr(self, name) and getattr(self, name) is not None: + logger.warning(f"ModularPipeline.update_components: setting {name} to None (spec unchanged)") + elif current_component_spec.default_creation_method == "from_pretrained" and not ( + hasattr(component, "_diffusers_load_id") and component._diffusers_load_id is not None + ): logger.warning( + f"ModularPipeline.update_components: {name} has no valid _diffusers_load_id. " + f"Updating the component but skipping spec update, use ComponentSpec.load() for proper specs" + ) + new_component_spec = current_component_spec + else: + new_component_spec = ComponentSpec.from_component(name, component) + + if new_component_spec.default_creation_method != current_component_spec.default_creation_method: + logger.info( f"ModularPipeline.update_components: changing the default_creation_method of {name} from {current_component_spec.default_creation_method} to {new_component_spec.default_creation_method}." ) @@ -2466,7 +2480,7 @@ class ModularPipeline(ConfigMixin, PushToHubMixin): if current_component_spec.type_hint is not None and not isinstance( created_components[name], current_component_spec.type_hint ): - logger.warning( + logger.info( f"ModularPipeline.update_components: adding {name} with new type: {created_components[name].__class__.__name__}, previous type: {current_component_spec.type_hint.__name__}" ) # update _component_specs based on the user passed component_spec diff --git a/src/diffusers/pipelines/pipeline_utils.py b/src/diffusers/pipelines/pipeline_utils.py index 6b8ba55941..a5e2f4965a 100644 --- a/src/diffusers/pipelines/pipeline_utils.py +++ b/src/diffusers/pipelines/pipeline_utils.py @@ -1706,6 +1706,36 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin): logger.warning(f"cannot get type annotation for Parameter {k} of {cls}.") return signature_types + @property + def parameters(self) -> Dict[str, Any]: + r""" + The `self.parameters` property can be useful to run different pipelines with the same weights and + configurations without reallocating additional memory. + + Returns (`dict`): + A dictionary containing all the optional parameters needed to initialize the pipeline. + + Examples: + + ```py + >>> from diffusers import ( + ... StableDiffusionPipeline, + ... StableDiffusionImg2ImgPipeline, + ... StableDiffusionInpaintPipeline, + ... ) + + >>> text2img = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5") + >>> img2img = StableDiffusionImg2ImgPipeline(**text2img.components, **text2img.parameters) + >>> inpaint = StableDiffusionInpaintPipeline(**text2img.components, **text2img.parameters) + ``` + """ + expected_modules, optional_parameters = self._get_signature_keys(self) + pipeline_parameters = { + k: self.config[k] for k in self.config.keys() if not k.startswith("_") and k in optional_parameters + } + + return pipeline_parameters + @property def components(self) -> Dict[str, Any]: r"""