Skip to content

Commit 6809572

Browse files
committed
add tests
1 parent 307b797 commit 6809572

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Copyright 2024 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 numpy as np
19+
import pytest
20+
import torch
21+
from transformers import AutoTokenizer, T5EncoderModel
22+
23+
from diffusers import (
24+
AutoencoderKLLTXVideo,
25+
FlowMatchEulerDiscreteScheduler,
26+
LTXPipeline,
27+
LTXVideoTransformer3DModel,
28+
)
29+
from diffusers.utils.testing_utils import (
30+
floats_tensor,
31+
is_peft_available,
32+
is_torch_version,
33+
require_peft_backend,
34+
skip_mps,
35+
torch_device,
36+
)
37+
38+
39+
if is_peft_available():
40+
pass
41+
42+
sys.path.append(".")
43+
44+
from utils import PeftLoraLoaderMixinTests, check_if_lora_correctly_set # noqa: E402
45+
46+
47+
@require_peft_backend
48+
class LTXVideoLoRATests(unittest.TestCase, PeftLoraLoaderMixinTests):
49+
pipeline_class = LTXPipeline
50+
scheduler_cls = FlowMatchEulerDiscreteScheduler
51+
scheduler_classes = [FlowMatchEulerDiscreteScheduler]
52+
scheduler_kwargs = {}
53+
54+
transformer_kwargs = {
55+
"in_channels": 8,
56+
"out_channels": 8,
57+
"patch_size": 1,
58+
"patch_size_t": 1,
59+
"num_attention_heads": 4,
60+
"attention_head_dim": 8,
61+
"cross_attention_dim": 32,
62+
"num_layers": 1,
63+
"caption_channels": 32,
64+
}
65+
transformer_cls = LTXVideoTransformer3DModel
66+
vae_kwargs = {
67+
"latent_channels": 8,
68+
"block_out_channels": (8, 8, 8, 8),
69+
"spatio_temporal_scaling": (True, True, False, False),
70+
"layers_per_block": (1, 1, 1, 1, 1),
71+
"patch_size": 1,
72+
"patch_size_t": 1,
73+
"encoder_causal": True,
74+
"decoder_causal": False,
75+
}
76+
vae_cls = AutoencoderKLLTXVideo
77+
tokenizer_cls, tokenizer_id = AutoTokenizer, "hf-internal-testing/tiny-random-t5"
78+
text_encoder_cls, text_encoder_id = T5EncoderModel, "hf-internal-testing/tiny-random-t5"
79+
80+
text_encoder_target_modules = ["q", "k", "v", "o"]
81+
82+
@property
83+
def output_shape(self):
84+
return (1, 9, 32, 32, 3)
85+
86+
def get_dummy_inputs(self, with_generator=True):
87+
batch_size = 1
88+
sequence_length = 16
89+
num_channels = 8
90+
num_frames = 9
91+
num_latent_frames = 3 # (num_frames - 1) // temporal_compression_ratio + 1
92+
latent_height = 8
93+
latent_width = 8
94+
95+
generator = torch.manual_seed(0)
96+
noise = floats_tensor((batch_size, num_latent_frames, num_channels, latent_height, latent_width))
97+
input_ids = torch.randint(1, sequence_length, size=(batch_size, sequence_length), generator=generator)
98+
99+
pipeline_inputs = {
100+
"prompt": "dance monkey",
101+
"num_frames": num_frames,
102+
"num_inference_steps": 4,
103+
"guidance_scale": 6.0,
104+
"height": 32,
105+
"width": 32,
106+
"max_sequence_length": sequence_length,
107+
"output_type": "np",
108+
}
109+
if with_generator:
110+
pipeline_inputs.update({"generator": generator})
111+
112+
return noise, input_ids, pipeline_inputs
113+
114+
@skip_mps
115+
@pytest.mark.xfail(
116+
condition=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
117+
reason="Test currently fails on CPU and PyTorch 2.5.1 but not on PyTorch 2.4.1.",
118+
strict=True,
119+
)
120+
def test_lora_fuse_nan(self):
121+
for scheduler_cls in self.scheduler_classes:
122+
components, text_lora_config, denoiser_lora_config = self.get_dummy_components(scheduler_cls)
123+
pipe = self.pipeline_class(**components)
124+
pipe = pipe.to(torch_device)
125+
pipe.set_progress_bar_config(disable=None)
126+
_, _, inputs = self.get_dummy_inputs(with_generator=False)
127+
128+
pipe.transformer.add_adapter(denoiser_lora_config, "adapter-1")
129+
130+
self.assertTrue(check_if_lora_correctly_set(pipe.transformer), "Lora not correctly set in denoiser")
131+
132+
# corrupt one LoRA weight with `inf` values
133+
with torch.no_grad():
134+
pipe.transformer.transformer_blocks[0].attn1.to_q.lora_A["adapter-1"].weight += float("inf")
135+
136+
# with `safe_fusing=True` we should see an Error
137+
with self.assertRaises(ValueError):
138+
pipe.fuse_lora(components=self.pipeline_class._lora_loadable_modules, safe_fusing=True)
139+
140+
# without we should not see an error, but every image will be black
141+
pipe.fuse_lora(components=self.pipeline_class._lora_loadable_modules, safe_fusing=False)
142+
143+
out = pipe(
144+
"test", num_inference_steps=2, max_sequence_length=inputs["max_sequence_length"], output_type="np"
145+
)[0]
146+
147+
self.assertTrue(np.isnan(out).all())
148+
149+
def test_simple_inference_with_text_lora_denoiser_fused_multi(self):
150+
super().test_simple_inference_with_text_lora_denoiser_fused_multi(expected_atol=9e-3)
151+
152+
def test_simple_inference_with_text_denoiser_lora_unfused(self):
153+
super().test_simple_inference_with_text_denoiser_lora_unfused(expected_atol=9e-3)
154+
155+
@unittest.skip("Not supported in LTXVideo.")
156+
def test_simple_inference_with_text_denoiser_block_scale(self):
157+
pass
158+
159+
@unittest.skip("Not supported in LTXVideo.")
160+
def test_simple_inference_with_text_denoiser_block_scale_for_all_dict_options(self):
161+
pass
162+
163+
@unittest.skip("Not supported in LTXVideo.")
164+
def test_modify_padding_mode(self):
165+
pass
166+
167+
@unittest.skip("Text encoder LoRA is not supported in LTXVideo.")
168+
def test_simple_inference_with_partial_text_lora(self):
169+
pass
170+
171+
@unittest.skip("Text encoder LoRA is not supported in LTXVideo.")
172+
def test_simple_inference_with_text_lora(self):
173+
pass
174+
175+
@unittest.skip("Text encoder LoRA is not supported in LTXVideo.")
176+
def test_simple_inference_with_text_lora_and_scale(self):
177+
pass
178+
179+
@unittest.skip("Text encoder LoRA is not supported in LTXVideo.")
180+
def test_simple_inference_with_text_lora_fused(self):
181+
pass
182+
183+
@unittest.skip("Text encoder LoRA is not supported in LTXVideo.")
184+
def test_simple_inference_with_text_lora_save_load(self):
185+
pass

0 commit comments

Comments
 (0)