Spaces:
Paused
Paused
complete memory management
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import torch
|
|
|
4 |
from diffusers import AutoencoderKLCogVideoX, CogVideoXImageToVideoPipeline, CogVideoXTransformer3DModel
|
5 |
from diffusers.utils import export_to_video, load_image
|
6 |
from transformers import T5EncoderModel, T5Tokenizer
|
@@ -25,17 +26,54 @@ hf_hub_download(
|
|
25 |
local_dir="checkpoints"
|
26 |
)
|
27 |
|
28 |
-
|
29 |
model_id = "THUDM/CogVideoX-5b-I2V"
|
30 |
transformer = CogVideoXTransformer3DModel.from_pretrained(model_id, subfolder="transformer", torch_dtype=torch.float16)
|
31 |
text_encoder = T5EncoderModel.from_pretrained(model_id, subfolder="text_encoder", torch_dtype=torch.float16)
|
32 |
vae = AutoencoderKLCogVideoX.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float16)
|
33 |
tokenizer = T5Tokenizer.from_pretrained(model_id, subfolder="tokenizer")
|
34 |
pipe = CogVideoXImageToVideoPipeline.from_pretrained(model_id, tokenizer=tokenizer, text_encoder=text_encoder, transformer=transformer, vae=vae, torch_dtype=torch.float16)
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def infer(image_path, prompt, orbit_type, progress=gr.Progress(track_tqdm=True)):
|
|
|
39 |
lora_path = "checkpoints/"
|
40 |
adapter_name = None
|
41 |
if orbit_type == "Left":
|
@@ -64,10 +102,14 @@ def infer(image_path, prompt, orbit_type, progress=gr.Progress(track_tqdm=True))
|
|
64 |
use_dynamic_cfg=True,
|
65 |
generator=torch.Generator(device="cpu").manual_seed(seed)
|
66 |
)
|
|
|
|
|
|
|
67 |
|
68 |
# Generate a timestamp for the output filename
|
69 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
70 |
export_to_video(video.frames[0], f"output_{timestamp}.mp4", fps=8)
|
|
|
71 |
return f"output_{timestamp}.mp4"
|
72 |
|
73 |
with gr.Blocks() as demo:
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import torch
|
4 |
+
import gc
|
5 |
from diffusers import AutoencoderKLCogVideoX, CogVideoXImageToVideoPipeline, CogVideoXTransformer3DModel
|
6 |
from diffusers.utils import export_to_video, load_image
|
7 |
from transformers import T5EncoderModel, T5Tokenizer
|
|
|
26 |
local_dir="checkpoints"
|
27 |
)
|
28 |
|
|
|
29 |
model_id = "THUDM/CogVideoX-5b-I2V"
|
30 |
transformer = CogVideoXTransformer3DModel.from_pretrained(model_id, subfolder="transformer", torch_dtype=torch.float16)
|
31 |
text_encoder = T5EncoderModel.from_pretrained(model_id, subfolder="text_encoder", torch_dtype=torch.float16)
|
32 |
vae = AutoencoderKLCogVideoX.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float16)
|
33 |
tokenizer = T5Tokenizer.from_pretrained(model_id, subfolder="tokenizer")
|
34 |
pipe = CogVideoXImageToVideoPipeline.from_pretrained(model_id, tokenizer=tokenizer, text_encoder=text_encoder, transformer=transformer, vae=vae, torch_dtype=torch.float16)
|
35 |
+
|
36 |
+
def find_and_move_object_to_cpu():
|
37 |
+
for obj in gc.get_objects():
|
38 |
+
try:
|
39 |
+
# Check if the object is a PyTorch model
|
40 |
+
if isinstance(obj, torch.nn.Module):
|
41 |
+
# Check if any parameter of the model is on CUDA
|
42 |
+
if any(param.is_cuda for param in obj.parameters()):
|
43 |
+
print(f"Found PyTorch model on CUDA: {type(obj).__name__}")
|
44 |
+
# Move the model to CPU
|
45 |
+
obj.to('cpu')
|
46 |
+
print(f"Moved {type(obj).__name__} to CPU.")
|
47 |
+
|
48 |
+
# Optionally check if buffers are on CUDA
|
49 |
+
if any(buf.is_cuda for buf in obj.buffers()):
|
50 |
+
print(f"Found buffer on CUDA in {type(obj).__name__}")
|
51 |
+
obj.to('cpu')
|
52 |
+
print(f"Moved buffers of {type(obj).__name__} to CPU.")
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
# Handle any exceptions if obj is not a torch model
|
56 |
+
pass
|
57 |
+
|
58 |
+
|
59 |
+
def clear_gpu():
|
60 |
+
"""Clear GPU memory by removing tensors, freeing cache, and moving data to CPU."""
|
61 |
+
# List memory usage before clearing
|
62 |
+
print(f"Memory allocated before clearing: {torch.cuda.memory_allocated() / (1024 ** 2)} MB")
|
63 |
+
print(f"Memory reserved before clearing: {torch.cuda.memory_reserved() / (1024 ** 2)} MB")
|
64 |
+
|
65 |
+
# Move any bound tensors back to CPU if needed
|
66 |
+
if torch.cuda.is_available():
|
67 |
+
torch.cuda.empty_cache()
|
68 |
+
torch.cuda.synchronize() # Ensure that all operations are completed
|
69 |
+
print("GPU memory cleared.")
|
70 |
+
|
71 |
+
print(f"Memory allocated after clearing: {torch.cuda.memory_allocated() / (1024 ** 2)} MB")
|
72 |
+
print(f"Memory reserved after clearing: {torch.cuda.memory_reserved() / (1024 ** 2)} MB")
|
73 |
+
|
74 |
|
75 |
def infer(image_path, prompt, orbit_type, progress=gr.Progress(track_tqdm=True)):
|
76 |
+
|
77 |
lora_path = "checkpoints/"
|
78 |
adapter_name = None
|
79 |
if orbit_type == "Left":
|
|
|
102 |
use_dynamic_cfg=True,
|
103 |
generator=torch.Generator(device="cpu").manual_seed(seed)
|
104 |
)
|
105 |
+
|
106 |
+
find_and_move_object_to_cpu()
|
107 |
+
clear_gpu()
|
108 |
|
109 |
# Generate a timestamp for the output filename
|
110 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
111 |
export_to_video(video.frames[0], f"output_{timestamp}.mp4", fps=8)
|
112 |
+
|
113 |
return f"output_{timestamp}.mp4"
|
114 |
|
115 |
with gr.Blocks() as demo:
|