From b7648557d4ffb64ca7ebf4160d3124835b107149 Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 10 Mar 2026 16:09:02 +0530 Subject: [PATCH 1/5] test load_components. --- .../test_modular_pipelines_common.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/modular_pipelines/test_modular_pipelines_common.py b/tests/modular_pipelines/test_modular_pipelines_common.py index d897ed793376..e42e353e8259 100644 --- a/tests/modular_pipelines/test_modular_pipelines_common.py +++ b/tests/modular_pipelines/test_modular_pipelines_common.py @@ -5,6 +5,7 @@ import pytest import torch +from huggingface_hub import hf_hub_download import diffusers from diffusers import AutoModel, ComponentsManager, ModularPipeline, ModularPipelineBlocks @@ -32,6 +33,30 @@ ) +def _get_specified_components(path_or_repo_id, cache_dir=None): + if os.path.isdir(path_or_repo_id): + config_path = os.path.join(path_or_repo_id, "modular_model_index.json") + else: + config_path = hf_hub_download( + repo_id=path_or_repo_id, + filename="modular_model_index.json", + local_dir=cache_dir, + ) + + with open(config_path) as f: + config = json.load(f) + + components = set() + for k, v in config.items(): + if isinstance(v, str): + continue + for entry in v: + if isinstance(entry, dict) and (entry.get("repo") or entry.get("pretrained_model_name_or_path")): + components.add(k) + break + return components + + class ModularPipelineTesterMixin: """ It provides a set of common tests for each modular pipeline, @@ -360,6 +385,36 @@ def test_save_from_pretrained(self, tmp_path): assert torch.abs(image_slices[0] - image_slices[1]).max() < 1e-3 + def test_load_expected_components_from_pretrained(self, tmp_path): + pipe = self.get_pipeline() + expected = _get_specified_components(self.pretrained_model_name_or_path, cache_dir=tmp_path) + actual = { + name + for name in pipe.components + if getattr(pipe, name, None) is not None + and getattr(getattr(pipe, name), "_diffusers_load_id", None) != "null" + } + assert expected == actual, f"Component mismatch: missing={expected - actual}, unexpected={actual - expected}" + + def test_load_expected_components_from_save_pretrained(self, tmp_path): + pipe = self.get_pipeline() + save_dir = str(tmp_path / "saved-pipeline") + pipe.save_pretrained(save_dir) + + expected = _get_specified_components(save_dir) + loaded_pipe = ModularPipeline.from_pretrained(save_dir) + loaded_pipe.load_components(torch_dtype=torch.float32) + + actual = { + name + for name in loaded_pipe.components + if getattr(loaded_pipe, name, None) is not None + and getattr(getattr(loaded_pipe, name), "_diffusers_load_id", None) != "null" + } + assert expected == actual, ( + f"Component mismatch after save/load: missing={expected - actual}, unexpected={actual - expected}" + ) + def test_modular_index_consistency(self, tmp_path): pipe = self.get_pipeline() components_spec = pipe._component_specs From 7673ab1757a462bcb22aabf4e94a06c2523e53a9 Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 10 Mar 2026 16:50:27 +0530 Subject: [PATCH 2/5] fix --- tests/modular_pipelines/test_modular_pipelines_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modular_pipelines/test_modular_pipelines_common.py b/tests/modular_pipelines/test_modular_pipelines_common.py index e42e353e8259..d7a4f91f21c9 100644 --- a/tests/modular_pipelines/test_modular_pipelines_common.py +++ b/tests/modular_pipelines/test_modular_pipelines_common.py @@ -48,7 +48,7 @@ def _get_specified_components(path_or_repo_id, cache_dir=None): components = set() for k, v in config.items(): - if isinstance(v, str): + if isinstance(v, (str, int, float, bool)): continue for entry in v: if isinstance(entry, dict) and (entry.get("repo") or entry.get("pretrained_model_name_or_path")): From 78a86e85cf83fb2b87a6a1c37701642bb586f718 Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 10 Mar 2026 17:46:55 +0530 Subject: [PATCH 3/5] fix --- tests/modular_pipelines/test_modular_pipelines_common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/modular_pipelines/test_modular_pipelines_common.py b/tests/modular_pipelines/test_modular_pipelines_common.py index d7a4f91f21c9..ab01cf7aa085 100644 --- a/tests/modular_pipelines/test_modular_pipelines_common.py +++ b/tests/modular_pipelines/test_modular_pipelines_common.py @@ -392,7 +392,7 @@ def test_load_expected_components_from_pretrained(self, tmp_path): name for name in pipe.components if getattr(pipe, name, None) is not None - and getattr(getattr(pipe, name), "_diffusers_load_id", None) != "null" + and getattr(getattr(pipe, name), "_diffusers_load_id", None) not in (None, "null") } assert expected == actual, f"Component mismatch: missing={expected - actual}, unexpected={actual - expected}" @@ -409,7 +409,7 @@ def test_load_expected_components_from_save_pretrained(self, tmp_path): name for name in loaded_pipe.components if getattr(loaded_pipe, name, None) is not None - and getattr(getattr(loaded_pipe, name), "_diffusers_load_id", None) != "null" + and getattr(getattr(loaded_pipe, name), "_diffusers_load_id", None) not in (None, "null") } assert expected == actual, ( f"Component mismatch after save/load: missing={expected - actual}, unexpected={actual - expected}" From bf846f722c909f5d1a5c0189ad4fce7944c055dd Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 10 Mar 2026 17:49:51 +0530 Subject: [PATCH 4/5] u[ --- .../test_modular_pipelines_common.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/modular_pipelines/test_modular_pipelines_common.py b/tests/modular_pipelines/test_modular_pipelines_common.py index ab01cf7aa085..6d69e2f8f7bb 100644 --- a/tests/modular_pipelines/test_modular_pipelines_common.py +++ b/tests/modular_pipelines/test_modular_pipelines_common.py @@ -6,6 +6,7 @@ import pytest import torch from huggingface_hub import hf_hub_download +from huggingface_hub.errors import RemoteEntryNotFoundError import diffusers from diffusers import AutoModel, ComponentsManager, ModularPipeline, ModularPipelineBlocks @@ -37,11 +38,14 @@ def _get_specified_components(path_or_repo_id, cache_dir=None): if os.path.isdir(path_or_repo_id): config_path = os.path.join(path_or_repo_id, "modular_model_index.json") else: - config_path = hf_hub_download( - repo_id=path_or_repo_id, - filename="modular_model_index.json", - local_dir=cache_dir, - ) + try: + config_path = hf_hub_download( + repo_id=path_or_repo_id, + filename="modular_model_index.json", + local_dir=cache_dir, + ) + except RemoteEntryNotFoundError: + return None with open(config_path) as f: config = json.load(f) @@ -388,6 +392,9 @@ def test_save_from_pretrained(self, tmp_path): def test_load_expected_components_from_pretrained(self, tmp_path): pipe = self.get_pipeline() expected = _get_specified_components(self.pretrained_model_name_or_path, cache_dir=tmp_path) + if not expected: + pytest.skip("Skipping test as we couldn't fetch the expected components.") + actual = { name for name in pipe.components From a1f63a398cda22dc42ee396811a33a1d7ec073c5 Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 10 Mar 2026 17:55:08 +0530 Subject: [PATCH 5/5] up --- tests/modular_pipelines/test_modular_pipelines_common.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/modular_pipelines/test_modular_pipelines_common.py b/tests/modular_pipelines/test_modular_pipelines_common.py index 6d69e2f8f7bb..8a65999b2006 100644 --- a/tests/modular_pipelines/test_modular_pipelines_common.py +++ b/tests/modular_pipelines/test_modular_pipelines_common.py @@ -6,7 +6,6 @@ import pytest import torch from huggingface_hub import hf_hub_download -from huggingface_hub.errors import RemoteEntryNotFoundError import diffusers from diffusers import AutoModel, ComponentsManager, ModularPipeline, ModularPipelineBlocks @@ -44,7 +43,7 @@ def _get_specified_components(path_or_repo_id, cache_dir=None): filename="modular_model_index.json", local_dir=cache_dir, ) - except RemoteEntryNotFoundError: + except Exception: return None with open(config_path) as f: