|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 HuggingFace Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import random |
| 17 | +import tempfile |
| 18 | +import unittest |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import PIL |
| 22 | +import torch |
| 23 | + |
| 24 | +from diffusers.image_processor import VaeImageProcessor |
| 25 | +from diffusers.modular_pipelines import ( |
| 26 | + FluxAutoBlocks, |
| 27 | + FluxKontextAutoBlocks, |
| 28 | + FluxKontextModularPipeline, |
| 29 | + FluxModularPipeline, |
| 30 | + ModularPipeline, |
| 31 | +) |
| 32 | + |
| 33 | +from ...testing_utils import floats_tensor, torch_device |
| 34 | +from ..test_modular_pipelines_common import ModularPipelineTesterMixin |
| 35 | + |
| 36 | + |
| 37 | +class FluxModularTests: |
| 38 | + pipeline_class = FluxModularPipeline |
| 39 | + pipeline_blocks_class = FluxAutoBlocks |
| 40 | + repo = "hf-internal-testing/tiny-flux-modular" |
| 41 | + |
| 42 | + def get_pipeline(self, components_manager=None, torch_dtype=torch.float32): |
| 43 | + pipeline = self.pipeline_blocks_class().init_pipeline(self.repo, components_manager=components_manager) |
| 44 | + pipeline.load_components(torch_dtype=torch_dtype) |
| 45 | + return pipeline |
| 46 | + |
| 47 | + def get_dummy_inputs(self, device, seed=0): |
| 48 | + if str(device).startswith("mps"): |
| 49 | + generator = torch.manual_seed(seed) |
| 50 | + else: |
| 51 | + generator = torch.Generator(device=device).manual_seed(seed) |
| 52 | + inputs = { |
| 53 | + "prompt": "A painting of a squirrel eating a burger", |
| 54 | + "generator": generator, |
| 55 | + "num_inference_steps": 2, |
| 56 | + "guidance_scale": 5.0, |
| 57 | + "height": 8, |
| 58 | + "width": 8, |
| 59 | + "max_sequence_length": 48, |
| 60 | + "output_type": "np", |
| 61 | + } |
| 62 | + return inputs |
| 63 | + |
| 64 | + |
| 65 | +class FluxModularPipelineFastTests(FluxModularTests, ModularPipelineTesterMixin, unittest.TestCase): |
| 66 | + params = frozenset(["prompt", "height", "width", "guidance_scale"]) |
| 67 | + batch_params = frozenset(["prompt"]) |
| 68 | + |
| 69 | + |
| 70 | +class FluxImg2ImgModularPipelineFastTests(FluxModularTests, ModularPipelineTesterMixin, unittest.TestCase): |
| 71 | + params = frozenset(["prompt", "height", "width", "guidance_scale", "image"]) |
| 72 | + batch_params = frozenset(["prompt", "image"]) |
| 73 | + |
| 74 | + def get_pipeline(self, components_manager=None, torch_dtype=torch.float32): |
| 75 | + pipeline = super().get_pipeline(components_manager, torch_dtype) |
| 76 | + # Override `vae_scale_factor` here as currently, `image_processor` is initialized with |
| 77 | + # fixed constants instead of |
| 78 | + # https://github.com/huggingface/diffusers/blob/d54622c2679d700b425ad61abce9b80fc36212c0/src/diffusers/pipelines/flux/pipeline_flux_img2img.py#L230C9-L232C10 |
| 79 | + pipeline.image_processor = VaeImageProcessor(vae_scale_factor=2) |
| 80 | + return pipeline |
| 81 | + |
| 82 | + def get_dummy_inputs(self, device, seed=0): |
| 83 | + inputs = super().get_dummy_inputs(device, seed) |
| 84 | + image = floats_tensor((1, 3, 32, 32), rng=random.Random(seed)).to(device) |
| 85 | + image = image / 2 + 0.5 |
| 86 | + inputs["image"] = image |
| 87 | + inputs["strength"] = 0.8 |
| 88 | + inputs["height"] = 8 |
| 89 | + inputs["width"] = 8 |
| 90 | + return inputs |
| 91 | + |
| 92 | + def test_save_from_pretrained(self): |
| 93 | + pipes = [] |
| 94 | + base_pipe = self.get_pipeline().to(torch_device) |
| 95 | + pipes.append(base_pipe) |
| 96 | + |
| 97 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 98 | + base_pipe.save_pretrained(tmpdirname) |
| 99 | + pipe = ModularPipeline.from_pretrained(tmpdirname).to(torch_device) |
| 100 | + pipe.load_components(torch_dtype=torch.float32) |
| 101 | + pipe.to(torch_device) |
| 102 | + pipe.image_processor = VaeImageProcessor(vae_scale_factor=2) |
| 103 | + |
| 104 | + pipes.append(pipe) |
| 105 | + |
| 106 | + image_slices = [] |
| 107 | + for pipe in pipes: |
| 108 | + inputs = self.get_dummy_inputs(torch_device) |
| 109 | + image = pipe(**inputs, output="images") |
| 110 | + |
| 111 | + image_slices.append(image[0, -3:, -3:, -1].flatten()) |
| 112 | + |
| 113 | + assert np.abs(image_slices[0] - image_slices[1]).max() < 1e-3 |
| 114 | + |
| 115 | + |
| 116 | +class FluxKontextModularPipelineFastTests(FluxImg2ImgModularPipelineFastTests): |
| 117 | + pipeline_class = FluxKontextModularPipeline |
| 118 | + pipeline_blocks_class = FluxKontextAutoBlocks |
| 119 | + repo = "hf-internal-testing/tiny-flux-kontext-pipe" |
| 120 | + |
| 121 | + def get_dummy_inputs(self, device, seed=0): |
| 122 | + inputs = super().get_dummy_inputs(device, seed) |
| 123 | + image = PIL.Image.new("RGB", (32, 32), 0) |
| 124 | + _ = inputs.pop("strength") |
| 125 | + inputs["image"] = image |
| 126 | + inputs["height"] = 8 |
| 127 | + inputs["width"] = 8 |
| 128 | + inputs["max_area"] = 8 * 8 |
| 129 | + inputs["_auto_resize"] = False |
| 130 | + return inputs |
0 commit comments