|
| 1 | +# Copyright 2025 HuggingFace Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +import unittest |
| 17 | + |
| 18 | +import torch |
| 19 | +from transformers import AutoTokenizer, T5EncoderModel |
| 20 | + |
| 21 | +from diffusers import AutoencoderKLWan, FlowMatchEulerDiscreteScheduler, WanPipeline, WanVACETransformer3DModel |
| 22 | +from diffusers.utils.testing_utils import floats_tensor, require_peft_backend, skip_mps |
| 23 | + |
| 24 | + |
| 25 | +sys.path.append(".") |
| 26 | + |
| 27 | +from utils import PeftLoraLoaderMixinTests # noqa: E402 |
| 28 | + |
| 29 | + |
| 30 | +@require_peft_backend |
| 31 | +@skip_mps |
| 32 | +class WanLoRATests(unittest.TestCase, PeftLoraLoaderMixinTests): |
| 33 | + pipeline_class = WanPipeline |
| 34 | + scheduler_cls = FlowMatchEulerDiscreteScheduler |
| 35 | + scheduler_classes = [FlowMatchEulerDiscreteScheduler] |
| 36 | + scheduler_kwargs = {} |
| 37 | + |
| 38 | + transformer_kwargs = { |
| 39 | + "patch_size": (1, 2, 2), |
| 40 | + "num_attention_heads": 2, |
| 41 | + "attention_head_dim": 12, |
| 42 | + "in_channels": 16, |
| 43 | + "out_channels": 16, |
| 44 | + "text_dim": 32, |
| 45 | + "freq_dim": 256, |
| 46 | + "ffn_dim": 32, |
| 47 | + "num_layers": 2, |
| 48 | + "cross_attn_norm": True, |
| 49 | + "qk_norm": "rms_norm_across_heads", |
| 50 | + "rope_max_seq_len": 32, |
| 51 | + } |
| 52 | + transformer_cls = WanVACETransformer3DModel |
| 53 | + vae_kwargs = { |
| 54 | + "base_dim": 3, |
| 55 | + "z_dim": 16, |
| 56 | + "dim_mult": [1, 1, 1, 1], |
| 57 | + "num_res_blocks": 1, |
| 58 | + "temperal_downsample": [False, True, True], |
| 59 | + } |
| 60 | + vae_cls = AutoencoderKLWan |
| 61 | + has_two_text_encoders = True |
| 62 | + tokenizer_cls, tokenizer_id = AutoTokenizer, "hf-internal-testing/tiny-random-t5" |
| 63 | + text_encoder_cls, text_encoder_id = T5EncoderModel, "hf-internal-testing/tiny-random-t5" |
| 64 | + |
| 65 | + text_encoder_target_modules = ["q", "k", "v", "o"] |
| 66 | + |
| 67 | + @property |
| 68 | + def output_shape(self): |
| 69 | + return (1, 9, 32, 32, 3) |
| 70 | + |
| 71 | + def get_dummy_inputs(self, with_generator=True): |
| 72 | + batch_size = 1 |
| 73 | + sequence_length = 16 |
| 74 | + num_channels = 4 |
| 75 | + num_frames = 9 |
| 76 | + num_latent_frames = 3 # (num_frames - 1) // temporal_compression_ratio + 1 |
| 77 | + sizes = (4, 4) |
| 78 | + |
| 79 | + generator = torch.manual_seed(0) |
| 80 | + noise = floats_tensor((batch_size, num_latent_frames, num_channels) + sizes) |
| 81 | + input_ids = torch.randint(1, sequence_length, size=(batch_size, sequence_length), generator=generator) |
| 82 | + |
| 83 | + pipeline_inputs = { |
| 84 | + "prompt": "", |
| 85 | + "num_frames": num_frames, |
| 86 | + "num_inference_steps": 1, |
| 87 | + "guidance_scale": 6.0, |
| 88 | + "height": 32, |
| 89 | + "width": 32, |
| 90 | + "max_sequence_length": sequence_length, |
| 91 | + "output_type": "np", |
| 92 | + } |
| 93 | + if with_generator: |
| 94 | + pipeline_inputs.update({"generator": generator}) |
| 95 | + |
| 96 | + return noise, input_ids, pipeline_inputs |
| 97 | + |
| 98 | + def test_simple_inference_with_text_lora_denoiser_fused_multi(self): |
| 99 | + super().test_simple_inference_with_text_lora_denoiser_fused_multi(expected_atol=9e-3) |
| 100 | + |
| 101 | + def test_simple_inference_with_text_denoiser_lora_unfused(self): |
| 102 | + super().test_simple_inference_with_text_denoiser_lora_unfused(expected_atol=9e-3) |
| 103 | + |
| 104 | + @unittest.skip("Not supported in Wan.") |
| 105 | + def test_simple_inference_with_text_denoiser_block_scale(self): |
| 106 | + pass |
| 107 | + |
| 108 | + @unittest.skip("Not supported in Wan.") |
| 109 | + def test_simple_inference_with_text_denoiser_block_scale_for_all_dict_options(self): |
| 110 | + pass |
| 111 | + |
| 112 | + @unittest.skip("Not supported in Wan.") |
| 113 | + def test_modify_padding_mode(self): |
| 114 | + pass |
| 115 | + |
| 116 | + @unittest.skip("Text encoder LoRA is not supported in Wan.") |
| 117 | + def test_simple_inference_with_partial_text_lora(self): |
| 118 | + pass |
| 119 | + |
| 120 | + @unittest.skip("Text encoder LoRA is not supported in Wan.") |
| 121 | + def test_simple_inference_with_text_lora(self): |
| 122 | + pass |
| 123 | + |
| 124 | + @unittest.skip("Text encoder LoRA is not supported in Wan.") |
| 125 | + def test_simple_inference_with_text_lora_and_scale(self): |
| 126 | + pass |
| 127 | + |
| 128 | + @unittest.skip("Text encoder LoRA is not supported in Wan.") |
| 129 | + def test_simple_inference_with_text_lora_fused(self): |
| 130 | + pass |
| 131 | + |
| 132 | + @unittest.skip("Text encoder LoRA is not supported in Wan.") |
| 133 | + def test_simple_inference_with_text_lora_save_load(self): |
| 134 | + pass |
0 commit comments