|
| 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 unittest |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +import torch |
| 20 | + |
| 21 | +from diffusers import ClassifierFreeGuidance |
| 22 | +from diffusers.modular_pipelines import QwenImageAutoBlocks, QwenImageModularPipeline |
| 23 | + |
| 24 | +from ...testing_utils import torch_device |
| 25 | +from ..test_modular_pipelines_common import ModularPipelineTesterMixin |
| 26 | + |
| 27 | + |
| 28 | +class QwenImagexModularTests: |
| 29 | + pipeline_class = QwenImageModularPipeline |
| 30 | + pipeline_blocks_class = QwenImageAutoBlocks |
| 31 | + repo = "hf-internal-testing/tiny-qwenimage-modular" |
| 32 | + |
| 33 | + params = frozenset(["prompt", "height", "width", "negative_prompt", "attention_kwargs", "image", "mask_image"]) |
| 34 | + batch_params = frozenset(["prompt", "negative_prompt", "image", "mask_image"]) |
| 35 | + |
| 36 | + def get_pipeline(self, components_manager=None, torch_dtype=torch.float32): |
| 37 | + pipeline = self.pipeline_blocks_class().init_pipeline(self.repo, components_manager=components_manager) |
| 38 | + pipeline.load_components(torch_dtype=torch_dtype) |
| 39 | + pipeline.set_progress_bar_config(disable=None) |
| 40 | + return pipeline |
| 41 | + |
| 42 | + def get_dummy_inputs(self, device, seed=0): |
| 43 | + if str(device).startswith("mps"): |
| 44 | + generator = torch.manual_seed(seed) |
| 45 | + else: |
| 46 | + generator = torch.Generator(device=device).manual_seed(seed) |
| 47 | + inputs = { |
| 48 | + "prompt": "dance monkey", |
| 49 | + "negative_prompt": "bad quality", |
| 50 | + "generator": generator, |
| 51 | + "num_inference_steps": 2, |
| 52 | + "height": 32, |
| 53 | + "width": 32, |
| 54 | + "max_sequence_length": 16, |
| 55 | + "output_type": "np", |
| 56 | + } |
| 57 | + return inputs |
| 58 | + |
| 59 | + |
| 60 | +class QwenImageModularGuiderTests: |
| 61 | + def test_guider_cfg(self): |
| 62 | + pipe = self.get_pipeline() |
| 63 | + pipe = pipe.to(torch_device) |
| 64 | + |
| 65 | + guider = ClassifierFreeGuidance(guidance_scale=1.0) |
| 66 | + pipe.update_components(guider=guider) |
| 67 | + |
| 68 | + inputs = self.get_dummy_inputs(torch_device) |
| 69 | + out_no_cfg = pipe(**inputs, output="images") |
| 70 | + |
| 71 | + guider = ClassifierFreeGuidance(guidance_scale=7.5) |
| 72 | + pipe.update_components(guider=guider) |
| 73 | + inputs = self.get_dummy_inputs(torch_device) |
| 74 | + out_cfg = pipe(**inputs, output="images") |
| 75 | + |
| 76 | + assert out_cfg.shape == out_no_cfg.shape |
| 77 | + max_diff = np.abs(out_cfg - out_no_cfg).max() |
| 78 | + assert max_diff > 1e-2, "Output with CFG must be different from normal inference" |
| 79 | + |
| 80 | + |
| 81 | +class QwenImageModularPipelineFastTests( |
| 82 | + QwenImagexModularTests, QwenImageModularGuiderTests, ModularPipelineTesterMixin, unittest.TestCase |
| 83 | +): |
| 84 | + def __init__(self, *args, **kwargs): |
| 85 | + super().__init__(*args, **kwargs) |
0 commit comments