【视频生成大模型】 视频生成大模型 THUDM/CogVideoX-2b

CogVideoX-2b 模型介绍

CogVideoX是 清影 同源的开源版本视频生成模型。

基础信息:

在这里插入图片描述

发布时间

2024年8月份

模型测试生成的demo视频

https://github.com/THUDM/CogVideo/raw/main/resources/videos/1.mp4

视频生成1

https://github.com/THUDM/CogVideo/raw/main/resources/videos/2.mp4

视频生成2

生成视频限制

  • 提示词语言 English*
  • 提示词长度上限 226 Tokens
  • 视频长度 6 秒
  • 帧率 8 帧 / 秒
  • 视频分辨率 720 * 480,不支持其他分辨率(含微调)

运行环境安装

# diffusers>=0.30.1
# transformers>=0.44.0
# accelerate>=0.33.0 (suggest install from source)
# imageio-ffmpeg>=0.5.1
pip install --upgrade transformers accelerate diffusers imageio-ffmpeg 

运行模型

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

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(
    "THUDM/CogVideoX-2b",
    torch_dtype=torch.float16
)

pipe.enable_model_cpu_offload()
pipe.enable_sequential_cpu_offload()
pipe.vae.enable_slicing()
pipe.vae.enable_tiling()
video = pipe(
    prompt=prompt,
    num_videos_per_prompt=1,
    num_inference_steps=50,
    num_frames=49,
    guidance_scale=6,
    generator=torch.Generator(device="cuda").manual_seed(42),
).frames[0]

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

PytorchAO 和 Optimum-quanto 可以用于对文本编码器、Transformer 和 VAE 模块进行量化,从而降低 CogVideoX 的内存需求。这使得在免费的 T4 Colab 或较小 VRAM 的 GPU 上运行该模型成为可能!值得注意的是,TorchAO 量化与 torch.compile 完全兼容,这可以显著加快推理速度。

# To get started, PytorchAO needs to be installed from the GitHub source and PyTorch Nightly.
# Source and nightly installation is only required until next release.

import torch
from diffusers import AutoencoderKLCogVideoX, CogVideoXTransformer3DModel, CogVideoXPipeline
from diffusers.utils import export_to_video
from transformers import T5EncoderModel
from torchao.quantization import quantize_, int8_weight_only, int8_dynamic_activation_int8_weight

quantization = int8_weight_only

text_encoder = T5EncoderModel.from_pretrained("THUDM/CogVideoX-2b", subfolder="text_encoder", torch_dtype=torch.bfloat16)
quantize_(text_encoder, quantization())

transformer = CogVideoXTransformer3DModel.from_pretrained("THUDM/CogVideoX-5b", subfolder="transformer", torch_dtype=torch.bfloat16)
quantize_(transformer, quantization())

vae = AutoencoderKLCogVideoX.from_pretrained("THUDM/CogVideoX-2b", subfolder="vae", torch_dtype=torch.bfloat16)
quantize_(vae, quantization())

# Create pipeline and run inference
pipe = CogVideoXPipeline.from_pretrained(
    "THUDM/CogVideoX-2b",
   text_encoder=text_encoder,
   transformer=transformer,
   vae=vae,
    torch_dtype=torch.bfloat16,
)
pipe.enable_model_cpu_offload()
pipe.vae.enable_tiling()

# 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."

video = pipe(
    prompt=prompt,
    num_videos_per_prompt=1,
    num_inference_steps=50,
    num_frames=49,
    guidance_scale=6,
    generator=torch.Generator(device="cuda").manual_seed(42),
).frames[0]

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

下载

model_id: THUDM/CogVideoX-2b
下载地址:https://hf-mirror.com/THUDM/CogVideoX-2b 不需要翻墙

开源协议

License: apache-2.0

参考

### Qwen2-VL-2B-Instruct 大模型蒸馏方法和技术 #### 什么是模型蒸馏? 模型蒸馏是一种通过较小的学生模型来模仿较大的教师模型的技术,从而减少计算成本并提高效率。这种方法通常涉及将大型复杂模型的知识转移到更简单的模型中。 对于Qwen2-VL-2B-Instruct这样的多模态大模型,其蒸馏过程可以分为以下几个方面: #### 数据准备 为了有效地进行蒸馏,需要准备好高质量的数据集。这些数据可以从原始训练集中提取,或者通过对已有模型生成的结果重新标注得到。如果采用对话形式的模型,则可参考ChatML格式构建指令调优数据集[^3]。 #### 教师模型的选择 在此场景下,Qwen2-VL-2B-Instruct充当教师角色。它具有强大的视觉和语言处理能力,能够提供精确的预测结果用于指导学生模型的学习。 #### 学生模型设计 学生模型的设计应考虑目标应用场景的需求以及硬件限制条件等因素。一般而言,学生模型会比教师模型参数量少得多,结构也更加简单紧凑。 #### 蒸馏损失函数定义 常见的做法是结合软标签(Soft Labels)与硬标签(Hard Labels)。其中软标签来源于温度调整后的概率分布;而硬标签则是真实类别标记。总的目标是最小化两者之间的差异。 \[ L_{distill} = \alpha * KL(Teacher\_Output, Student\_Output) + (1-\alpha)*CrossEntropy(GroundTruthLabel,Student\_Output)\] 这里 \(KL\) 表示Kullback-Leibler散度,用来衡量两个分布间的距离;\( CrossEntropy \) 是交叉熵损失项;\(\alpha\) 控制两种损失权重平衡。 #### 训练策略优化 除了基本的前向传播外,还可以引入一些高级技巧提升效果,比如: - **自适应学习率调度器**:动态调节每轮迭代中的步长大小; - **梯度累积机制**:当批量尺寸受限时允许积累多次更新后再执行一次完整的反向传递操作; - **混合精度训练**:利用半浮点数表示加速运算同时节省内存占用空间。 ```python import torch.nn as nn class DistillationLoss(nn.Module): def __init__(self,alpha=0.5,temp=4): super(DistillationLoss,self).__init__() self.alpha = alpha self.temp = temp def forward(self,output_student,output_teacher,target): loss_kl = F.kl_div(F.log_softmax(output_student/self.temp,dim=-1), F.softmax(output_teacher/self.temp,dim=-1),reduction='batchmean')*self.temp*self.temp loss_ce = F.cross_entropy(output_student,target) total_loss = self.alpha * loss_kl + (1-self.alpha)*loss_ce return total_loss ``` 上述代码片段展示了如何实现一个融合了KL散度和交叉熵的标准蒸馏损失类[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值