Skip to content

Commit 515fe28

Browse files
committed
update
1 parent 09f9acb commit 515fe28

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Copyright 2025 The HuggingFace Team.
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 gc
16+
import unittest
17+
18+
from PIL import Image
19+
import numpy as np
20+
import torch
21+
from transformers import AutoTokenizer, T5EncoderModel
22+
23+
from diffusers import AutoencoderKLWan, FlowMatchEulerDiscreteScheduler, WanVideoToVideoPipeline, WanTransformer3DModel
24+
from diffusers.utils.testing_utils import (
25+
enable_full_determinism,
26+
require_torch_accelerator,
27+
slow,
28+
)
29+
30+
from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
31+
from ..test_pipelines_common import (
32+
PipelineTesterMixin,
33+
)
34+
35+
36+
enable_full_determinism()
37+
38+
39+
class WanVideoToVideoPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
40+
pipeline_class = WanVideoToVideoPipeline
41+
params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"}
42+
batch_params = TEXT_TO_IMAGE_BATCH_PARAMS
43+
image_params = TEXT_TO_IMAGE_IMAGE_PARAMS
44+
image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS
45+
required_optional_params = frozenset(
46+
[
47+
"num_inference_steps",
48+
"generator",
49+
"latents",
50+
"return_dict",
51+
"callback_on_step_end",
52+
"callback_on_step_end_tensor_inputs",
53+
]
54+
)
55+
test_xformers_attention = False
56+
supports_dduf = False
57+
58+
def get_dummy_components(self):
59+
torch.manual_seed(0)
60+
vae = AutoencoderKLWan(
61+
base_dim=3,
62+
z_dim=16,
63+
dim_mult=[1, 1, 1, 1],
64+
num_res_blocks=1,
65+
temperal_downsample=[False, True, True],
66+
)
67+
68+
torch.manual_seed(0)
69+
# TODO: impl FlowDPMSolverMultistepScheduler
70+
scheduler = FlowMatchEulerDiscreteScheduler(shift=7.0)
71+
text_encoder = T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5")
72+
tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5")
73+
74+
torch.manual_seed(0)
75+
transformer = WanTransformer3DModel(
76+
patch_size=(1, 2, 2),
77+
num_attention_heads=2,
78+
attention_head_dim=12,
79+
in_channels=16,
80+
out_channels=16,
81+
text_dim=32,
82+
freq_dim=256,
83+
ffn_dim=32,
84+
num_layers=2,
85+
cross_attn_norm=True,
86+
qk_norm="rms_norm_across_heads",
87+
rope_max_seq_len=32,
88+
)
89+
90+
components = {
91+
"transformer": transformer,
92+
"vae": vae,
93+
"scheduler": scheduler,
94+
"text_encoder": text_encoder,
95+
"tokenizer": tokenizer,
96+
}
97+
return components
98+
99+
def get_dummy_inputs(self, device, seed=0):
100+
if str(device).startswith("mps"):
101+
generator = torch.manual_seed(seed)
102+
else:
103+
generator = torch.Generator(device=device).manual_seed(seed)
104+
105+
video = [Image.new("RGB", (16, 16))] * 19
106+
inputs = {
107+
"video": video,
108+
"prompt": "dance monkey",
109+
"negative_prompt": "negative", # TODO
110+
"generator": generator,
111+
"num_inference_steps": 2,
112+
"guidance_scale": 6.0,
113+
"height": 16,
114+
"width": 16,
115+
"num_frames": 9,
116+
"max_sequence_length": 16,
117+
"output_type": "pt",
118+
}
119+
return inputs
120+
121+
def test_inference(self):
122+
device = "cpu"
123+
124+
components = self.get_dummy_components()
125+
pipe = self.pipeline_class(**components)
126+
pipe.to(device)
127+
pipe.set_progress_bar_config(disable=None)
128+
129+
inputs = self.get_dummy_inputs(device)
130+
video = pipe(**inputs).frames
131+
generated_video = video[0]
132+
133+
self.assertEqual(generated_video.shape, (9, 3, 16, 16))
134+
expected_video = torch.randn(9, 3, 16, 16)
135+
max_diff = np.abs(generated_video - expected_video).max()
136+
self.assertLessEqual(max_diff, 1e10)
137+
138+
@unittest.skip("Test not supported")
139+
def test_attention_slicing_forward_pass(self):
140+
pass
141+
142+
143+
@slow
144+
@require_torch_accelerator
145+
class WanPipelineIntegrationTests(unittest.TestCase):
146+
prompt = "A painting of a squirrel eating a burger."
147+
148+
def setUp(self):
149+
super().setUp()
150+
gc.collect()
151+
torch.cuda.empty_cache()
152+
153+
def tearDown(self):
154+
super().tearDown()
155+
gc.collect()
156+
torch.cuda.empty_cache()
157+
158+
@unittest.skip("TODO: test needs to be implemented")
159+
def test_Wanx(self):
160+
pass

0 commit comments

Comments
 (0)