Skip to content

Instantly share code, notes, and snippets.

@a-r-r-o-w
Created August 13, 2024 13:56
Show Gist options
  • Save a-r-r-o-w/3959a03f15be5c9bd1fe545b09dfcc93 to your computer and use it in GitHub Desktop.
Save a-r-r-o-w/3959a03f15be5c9bd1fe545b09dfcc93 to your computer and use it in GitHub Desktop.
import gc
import torch
from diffusers import CogVideoXPipeline, CogVideoXDDIMScheduler
from diffusers.utils import export_to_video
def reset_memory():
gc.collect()
torch.cuda.empty_cache()
torch.cuda.reset_accumulated_memory_stats()
torch.cuda.reset_peak_memory_stats()
def print_memory():
memory = round(torch.cuda.memory_allocated() / 1024**3, 2)
max_memory = round(torch.cuda.max_memory_allocated() / 1024**3, 2)
max_reserved = round(torch.cuda.max_memory_reserved() / 1024**3, 2)
print(f"{memory=} GB")
print(f"{max_memory=} GB")
print(f"{max_reserved=} GB")
prompt = (
"A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. "
"The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other "
"pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, "
"casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. "
"The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical "
"atmosphere of this unique musical performance."
)
pipe = CogVideoXPipeline.from_pretrained("/raid/aryan/CogVideoX-trial", torch_dtype=torch.float16)
pipe.scheduler = CogVideoXDDIMScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
pipe.enable_model_cpu_offload()
reset_memory()
video = pipe(prompt=prompt, guidance_scale=6, num_inference_steps=50, generator=torch.Generator().manual_seed(42)).frames[0]
print_memory()
export_to_video(video, "output.mp4", fps=8)
pipe.vae.enable_tiling()
reset_memory()
video = pipe(prompt=prompt, guidance_scale=6, num_inference_steps=50, generator=torch.Generator().manual_seed(42)).frames[0]
print_memory()
export_to_video(video, "output_tiling.mp4", fps=8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment