部署CogVideoX的方法

1、pip3 install torch==2.0.1+cu117 torchvision==0.15.2+cu117 --index-url https://download.pytorch.org/whl/cu117
2、pip install –upgrade transformers accelerate diffusers imageio-ffmpeg sentencepiece opencv-python

3、安装apex

conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
pip install --upgrade pip setuptools wheel
cd /home/zhaochen/Open-Sora/apex
pip uninstall apex -y
rm -rf build/ dist/ apex.egg-info/
python setup.py build_ext --cpp_ext --cuda_ext
python setup.py install

4、

import torch
from diffusers import CogVideoXPipeline
pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-2b", torch_dtype=torch.float16)

5、

import torch
from diffusers import CogVideoXPipeline
from diffusers.utils import export_to_video

# 加载模型,使用 FP16 精度
pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-2b", torch_dtype=torch.float16)

# 将模型移到 GPU
pipe = pipe.to("cuda")

# 启用显存优化
pipe.enable_model_cpu_offload()  # 将部分计算卸载到 CPU
pipe.vae.enable_slicing()        # 分片处理 VAE
pipe.vae.enable_tiling()         # 平铺处理 VAE

# 设置生成参数
prompt = "A cat playing the piano in a cozy living room"
num_frames = 49  # 帧数(约 6 秒,8fps)
num_inference_steps = 50  # 推理步数

# 生成视频
with torch.no_grad():
    video_frames = pipe(
        prompt=prompt,
        num_frames=num_frames,
        num_inference_steps=num_inference_steps,
        guidance_scale=7.5,
        height=480,  # 降低分辨率以减少显存占用
        width=720
    ).frames[0]

# 导出视频
export_to_video(video_frames, "output.mp4", fps=8)
生成海报

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注