From 027c5ea5218cb3a57f4dc65bef50bdb50416b824 Mon Sep 17 00:00:00 2001 From: Sharif Parish Date: Thu, 5 Nov 2026 19:21:22 -0500 Subject: [PATCH] fix(integrations): support diffusers 0.41 pipeline imports diffusers 0.41 removed _get_load_method and _maybe_raise_error_for_incorrect_transformers from pipeline_loading_utils. Import them when present and fall back to equivalent local helpers otherwise. Add a test that imports the integration and round-trips a small DDPM pipeline. --- .../integrations/diffusers/pipeline.py | 18 +++++- tests/test_diffusers_integration.py | 57 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/test_diffusers_integration.py diff --git a/src/flashpack/integrations/diffusers/pipeline.py b/src/flashpack/integrations/diffusers/pipeline.py index cc4456a..60c1366 100644 --- a/src/flashpack/integrations/diffusers/pipeline.py +++ b/src/flashpack/integrations/diffusers/pipeline.py @@ -48,10 +48,8 @@ LOADABLE_CLASSES, TRANSFORMERS_DUMMY_MODULES_FOLDER, _get_final_device_map, - _get_load_method, _get_pipeline_class, _identify_model_variants, - _maybe_raise_error_for_incorrect_transformers, _maybe_raise_warning_for_inpainting, _maybe_warn_for_wrong_component_in_quant_config, _resolve_custom_pipeline_and_cls, @@ -64,6 +62,22 @@ ) from diffusers.pipelines.pipeline_utils import DiffusionPipeline +try: + # diffusers < 0.41: DDUF-aware resolution of a component's loader + from diffusers.pipelines.pipeline_loading_utils import ( + _get_load_method, + _maybe_raise_error_for_incorrect_transformers, + ) +except ImportError: + # diffusers >= 0.41 dropped DDUF loading and inlined both helpers + + def _get_load_method(class_obj: Any, load_method_name: str, is_dduf: bool) -> Any: + return getattr(class_obj, load_method_name) + + def _maybe_raise_error_for_incorrect_transformers(config_dict: dict) -> None: + return None + + if is_accelerate_available(): pass diff --git a/tests/test_diffusers_integration.py b/tests/test_diffusers_integration.py new file mode 100644 index 0000000..16c435c --- /dev/null +++ b/tests/test_diffusers_integration.py @@ -0,0 +1,57 @@ +"""The diffusers integration must import, and load a pipeline, on every +diffusers release flashpack supports. No network: the pipeline is a tiny +randomly initialised DDPM built in the test. + +Skipped when diffusers is not installed (the CI tier), so run it locally or in +the gpu/network tier.""" + +import pytest + +pytest.importorskip("diffusers") + +import torch # noqa: E402 +from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel # noqa: E402 +from flashpack.integrations.diffusers import ( # noqa: E402 + FlashPackDiffusersModelMixin, + FlashPackDiffusionPipeline, +) + + +# Module level: the pipeline loader imports component classes by module path +# and name from ``model_index.json`` +class FPUNet(UNet2DModel, FlashPackDiffusersModelMixin): + pass + + +class FPDDPM(DDPMPipeline, FlashPackDiffusionPipeline): + def __init__(self, unet: FPUNet, scheduler: DDPMScheduler) -> None: + super().__init__(unet=unet, scheduler=scheduler) + + +def test_cli_module_imports() -> None: + """``flashpack.__main__`` patches the integrations at import time, so a + broken diffusers import takes every CLI command down with it.""" + import flashpack.__main__ # noqa: F401 + + +def test_local_pipeline_roundtrip(tmp_path) -> None: + torch.manual_seed(0) + unet = FPUNet( + sample_size=8, + in_channels=3, + out_channels=3, + layers_per_block=1, + block_out_channels=(8, 8), + down_block_types=("DownBlock2D", "DownBlock2D"), + up_block_types=("UpBlock2D", "UpBlock2D"), + norm_num_groups=8, + ) + pipeline = FPDDPM(unet=unet, scheduler=DDPMScheduler(num_train_timesteps=10)) + pipeline.save_pretrained_flashpack(str(tmp_path)) + + loaded = FPDDPM.from_pretrained_flashpack(str(tmp_path)) + + assert isinstance(loaded.unet, UNet2DModel) + assert type(loaded.unet).__name__ == "FPUNet" + for name, tensor in unet.state_dict().items(): + assert torch.equal(loaded.unet.state_dict()[name], tensor), name