forked from huggingface/diffusers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_latte.py
59 lines (48 loc) · 1.37 KB
/
test_latte.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gc
import torch
from diffusers import LattePipeline
from diffusers.utils import export_to_gif
from transformers import T5EncoderModel, BitsAndBytesConfig
def flush():
gc.collect()
torch.cuda.empty_cache()
def bytes_to_giga_bytes(bytes):
return bytes / 1024 / 1024 / 1024
model_id = "maxin-cn/Latte-1"
text_encoder = T5EncoderModel.from_pretrained(
model_id,
subfolder="text_encoder",
quantization_config=BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16),
device_map="auto",
)
pipe = LattePipeline.from_pretrained(
model_id,
text_encoder=text_encoder,
transformer=None,
vae=None,
device_map="balanced",
)
with torch.no_grad():
prompt = "A dog in astronaut suit and sunglasses floating in space"
prompt_embeds, negative_prompt_embeds = pipe.encode_prompt(prompt)
del text_encoder
del pipe
flush()
pipe = LattePipeline.from_pretrained(
model_id,
text_encoder=None,
torch_dtype=torch.float16,
).to("cuda")
# pipe.enable_vae_tiling()
# pipe.enable_vae_slicing()
video = pipe(
video_length=16,
num_inference_steps=25,
negative_prompt=None,
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_prompt_embeds,
).frames[0]
print(
f"Max memory allocated: {bytes_to_giga_bytes(torch.cuda.max_memory_allocated())} GB"
)
export_to_gif(video, "latte_memory_optimized.gif")