部署cogvideox视频生成模型

      CogVideoX是智谱AI开源的一款视频生成模型,其核心在于3D变分自编码器和3D旋转位置编码(3D RoPE)技术。这些技术使得CogVideoX能够在保持视频帧间连贯性的同时,极大地降低计算资源需求,并生成流畅、连贯的视频序列。此外,CogVideoX还具备强大的文本理解能力,能够处理超长且复杂的文本提示,生成与用户输入高度相关的视频内容.

查看显存

nvidia-smi

在线创建Python虚拟环境

 python3.10 -m venv python310

激活Python虚拟环境

 #source /usr/local/bin/python310/bin/activate

拉取CogVideo代码

#cd /home

git clone https://github.com/THUDM/CogVideo.git

安装CogVideo依赖

pip install -r requirements.txt  

modelscope下载cogvideox-2b模型

pip install modelscope

modelscope download --model zhipuai/cogvideox-2b

模型具体位置:

/home/admin/workspace/.cache/modelscope/hub/zhipuai/cogvideox-2b

创建test.py

vi test.py

import torch

from diffusers import CogVideoXPipeline

from diffusers.utils import export_to_video

import os

os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'



# prompt里写自定义想要生成的视频内容

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(

    "/home/admin/workspace/.cache/modelscope/hub/zhipuai/cogvideox-2b", # 这里填CogVideo模型存放的位置,此处是放在了数据盘中

    torch_dtype=torch.float16

).to("cuda")



# 参数do_classifier_free_guidance设置为True可以启用无分类器指导,增强生成内容一致性和多样性

# num_videos_per_prompt控制每个prompt想要生成的视频数量

# max_sequence_length控制输入序列的最大长度

prompt_embeds, _ = pipe.encode_prompt(

    prompt=prompt,

    do_classifier_free_guidance=True,

    num_videos_per_prompt=1,

    max_sequence_length=226,

    device="cuda",

    dtype=torch.float16,

)

# 释放不需要的张量

del _

torch.cuda.empty_cache()



# 生成视频

video = pipe(

    num_inference_steps=10,  # 减少推理步骤

    guidance_scale=4,  # 降低指导比例

    prompt_embeds=prompt_embeds,

).frames[0]

#使用较少的推理步骤和较低的指导比例来减少显存使用。

export_to_video(video, "output.mp4", fps=8)

运行test.py

#python test.py

模型对比

模型名

CogVideoX-2B (本仓库)

CogVideoX-5B

CogVideoX-5B-I2V

模型介绍

入门级模型,兼顾兼容性。运行,二次开发成本低。

视频生成质量更高,视觉效果更好的更大尺寸模型。

CogVideoX-5B 图生视频版本。

推理精度

FP16*(推荐), BF16, FP32,FP8*,INT8,不支持INT4

BF16(推荐), FP16, FP32,FP8*,INT8,不支持INT4

单GPU显存消耗

SAT FP16: 18GB
diffusers FP16: 4GB起*
diffusers INT8(torchao): 3.6G起*

SAT BF16: 26GB
diffusers BF16 : 5GB起*
diffusers INT8(torchao): 4.4G起*

多GPU推理显存消耗

FP16: 10GB* using diffusers

BF16: 15GB* using diffusers

推理速度
(Step = 50, FP/BF16)

单卡A100: ~90秒
单卡H100: ~45秒

单卡A100: ~180秒
单卡H100: ~90秒

微调精度

FP16

BF16

微调显存消耗

47 GB (bs=1, LORA)
61 GB (bs=2, LORA)
62GB (bs=1, SFT)

63 GB (bs=1, LORA)
80 GB (bs=2, LORA)
75GB (bs=1, SFT)

78 GB (bs=1, LORA)
75GB (bs=1, SFT, 16GPU)

提示词语言

English*

提示词长度上限

226 Tokens

视频长度

6 秒

帧率

8 帧 / 秒

视频分辨率

720 * 480,不支持其他分辨率(含微调)

位置编码

3d_sincos_pos_embed

3d_rope_pos_embed

3d_rope_pos_embed + learnable_pos_embed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值