|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2024 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 | +import sys |
| 16 | +import unittest |
| 17 | + |
| 18 | +import torch |
| 19 | +from transformers import AutoTokenizer, CLIPTextModel, CLIPTokenizer, T5EncoderModel |
| 20 | + |
| 21 | +from diffusers import FlowMatchEulerDiscreteScheduler, FluxPipeline, FluxTransformer2DModel |
| 22 | +from diffusers.utils.testing_utils import floats_tensor, require_peft_backend |
| 23 | + |
| 24 | + |
| 25 | +sys.path.append(".") |
| 26 | + |
| 27 | +from utils import PeftLoraLoaderMixinTests # noqa: E402 |
| 28 | + |
| 29 | + |
| 30 | +@require_peft_backend |
| 31 | +class FluxLoRATests(unittest.TestCase, PeftLoraLoaderMixinTests): |
| 32 | + pipeline_class = FluxPipeline |
| 33 | + scheduler_cls = FlowMatchEulerDiscreteScheduler() |
| 34 | + scheduler_kwargs = {} |
| 35 | + uses_flow_matching = True |
| 36 | + transformer_kwargs = { |
| 37 | + "patch_size": 1, |
| 38 | + "in_channels": 4, |
| 39 | + "num_layers": 1, |
| 40 | + "num_single_layers": 1, |
| 41 | + "attention_head_dim": 16, |
| 42 | + "num_attention_heads": 2, |
| 43 | + "joint_attention_dim": 32, |
| 44 | + "pooled_projection_dim": 32, |
| 45 | + "axes_dims_rope": [4, 4, 8], |
| 46 | + } |
| 47 | + transformer_cls = FluxTransformer2DModel |
| 48 | + vae_kwargs = { |
| 49 | + "sample_size": 32, |
| 50 | + "in_channels": 3, |
| 51 | + "out_channels": 3, |
| 52 | + "block_out_channels": (4,), |
| 53 | + "layers_per_block": 1, |
| 54 | + "latent_channels": 1, |
| 55 | + "norm_num_groups": 1, |
| 56 | + "use_quant_conv": False, |
| 57 | + "use_post_quant_conv": False, |
| 58 | + "shift_factor": 0.0609, |
| 59 | + "scaling_factor": 1.5035, |
| 60 | + } |
| 61 | + has_two_text_encoders = True |
| 62 | + tokenizer_cls, tokenizer_id = CLIPTokenizer, "peft-internal-testing/tiny-clip-text-2" |
| 63 | + tokenizer_2_cls, tokenizer_2_id = AutoTokenizer, "hf-internal-testing/tiny-random-t5" |
| 64 | + text_encoder_cls, text_encoder_id = CLIPTextModel, "peft-internal-testing/tiny-clip-text-2" |
| 65 | + text_encoder_2_cls, text_encoder_2_id = T5EncoderModel, "hf-internal-testing/tiny-random-t5" |
| 66 | + |
| 67 | + @property |
| 68 | + def output_shape(self): |
| 69 | + return (1, 8, 8, 3) |
| 70 | + |
| 71 | + def get_dummy_inputs(self, with_generator=True): |
| 72 | + batch_size = 1 |
| 73 | + sequence_length = 10 |
| 74 | + num_channels = 4 |
| 75 | + sizes = (32, 32) |
| 76 | + |
| 77 | + generator = torch.manual_seed(0) |
| 78 | + noise = floats_tensor((batch_size, num_channels) + sizes) |
| 79 | + input_ids = torch.randint(1, sequence_length, size=(batch_size, sequence_length), generator=generator) |
| 80 | + |
| 81 | + pipeline_inputs = { |
| 82 | + "prompt": "A painting of a squirrel eating a burger", |
| 83 | + "num_inference_steps": 4, |
| 84 | + "guidance_scale": 0.0, |
| 85 | + "height": 8, |
| 86 | + "width": 8, |
| 87 | + "output_type": "np", |
| 88 | + } |
| 89 | + if with_generator: |
| 90 | + pipeline_inputs.update({"generator": generator}) |
| 91 | + |
| 92 | + return noise, input_ids, pipeline_inputs |
0 commit comments