Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/flashpack/integrations/diffusers/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
57 changes: 57 additions & 0 deletions tests/test_diffusers_integration.py
Original file line number Diff line number Diff line change
@@ -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