Open-Sora,高效复现类Sora视频生成方案开源!魔搭社区最佳实践教程来啦!

本文涉及的产品
交互式建模 PAI-DSW,5000CU*H 3个月
简介: 近期,HPC-AI Tech团队在GitHub上正式公开了Open-Sora项目

技术解读

近期,HPC-AI Tech团队在GitHub上正式公开了Open-Sora项目(https://github.com/hpcaitech/Open-Sora),该项目致力于复现OpenAI的Sora模型核心技术,并已取得实质性进展。作为开源社区内的开创性工作,Open-Sora率先提供了全球首个类Sora视频生成方案。魔搭社区也迅速跟进并深入学习了这一研究成果,以期促进技术交流与应用落地。

Open-Sora的工作主要分为如下几个部分:

1、变分自编码器(VAE)

为了降低计算成本,Open-Sora使用VAE名将视频从原始像素空间映射至潜在空间(latent space)。Sora的技术报告中,采用了时空VAE来减少时间维度,Open-Sora项目组通过研究和实践,发现目前尚无开源的高品质时空VAE(3D-VAE)模型。Google研究项目的MAGVIT所使用的4x4x4 VAE并未开放源代码,而VideoGPT的2x4x4 VAE在实验中表现出较低的质量。因此,在Open-Sora v1版本中,使用来自Stability-AI的2D VAE(sd-vae-ft-mse)。

2、Diffusion Transformers - STDiT

在处理视频训练时,涉及到大量token。对于每分钟24帧的视频,共有1440帧。经过VAE 4倍下采样和patch尺寸2倍下采样后,大约得到1440x1024≈150万token。对这150万个token进行全注意力操作会导致巨大的计算开销。因此,Open-Sora项目借鉴Latte项目,采用时空注意力机制来降低成本。


如图所示,在STDiT(空间-时间)架构中每个空间注意力模块之后插入一个时间注意力模块。这一设计与Latte论文中的变体3相似,但在参数数量上未做严格控制。Open-Sora在16x256x256分辨率视频上的实验表明,在相同迭代次数下,性能排序为:DiT(全注意力)> STDiT(顺序执行)> STDiT(并行执行)≈ Latte。出于效率考虑,Open-Sora本次选择了STDiT(顺序执行)。

Open-Sora专注视频生成任务,在PixArt-α一个强大的图像生成模型基础上训练模型。这项研究,采用了T5-conditioned DiT结构。Open-Sora以PixArt-α为基础初始化模型,并将插入的时间注意力层初始化为零值。这样的初始化方式确保了模型从一开始就能保持图像生成能力,文本编码器采用的则是T5模型。插入的时间注意力层使得参数量从5.8亿增加到了7.24亿。

受PixArt-α和稳定视频扩散技术成功的启发,Open-Sora采取了逐步训练策略:首先在36.6万预训练数据集上以16x256x256分辨率训练,然后在2万数据集上分别以16x256x256、16x512x512以及64x512x512分辨率继续训练。借助缩放position embedding,显著降低了计算成本。

3、Patch Embedding

Open-Sora尝试在DiT中使用三维patch embedding,但由于在时间维度上进行2倍下采样,生成的视频质量较低。因此,在下一版本中,Open-Sora把下采样的任务留给时空VAE,在V1中按照每3帧采样(16帧训练)和每2帧采样(64帧训练)的方式进行训练。

4、Video caption

Open-Sora使用LLaVA-1.6-Yi-34B(一款图像描述生成模型)为视频进行标注,该标注基于三个连续的帧以及一个精心设计的提示语。借助这个精心设计的提示语,LLaVA能够生成高质量的视频描述。

总结来说,Open-Sora项目V1版本非常完整的复刻了基于Transformers的视频生成的Pipeline:

  • 比较多种STDiT的方式,采用了STDiT(顺序执行),并验证了结果
  • 借助position embedding,实现了不同分辨率和不同时长的视频生成。

在试验的过程中也遇到了一些困难,比如我们注意到,

  • Open-Sora项目一开始采用的是VideoGPT的时空VAE,验证效果不佳后,依然选择了Stable Diffusion的2D的VAE。
  • 同时,三维patch embedding,由于在时间维度上进行2倍下采样,生成的视频质量较低。在V1版本中依然采用了按帧采样的方式。

Open-Sora也借助了开源项目和模型的力量,包括但不限于:

  • LLaVA-1.6-Yi-34B的多模态LLM来实现Video-Caption,生成高质量的视频文本对。
  • 受PixArt-α和稳定视频扩散技术成功的启发,采用了T5 conditioned DiT结构。

Open-Sora项目通过其强大的工程能力,快速的搭建和验证了Sora的技术链路,推动了开源视频生成的发展,同时我们也期待V2版本中,对时空VAE等难题的进一步解决。

魔搭最佳实践

第一步:下载代码并安装:

# install flash attention (optional)
pip install packaging ninja
pip install flash-attn --no-build-isolation
# install apex (optional)
pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" git+https://github.com/NVIDIA/apex.git
# install xformers
pip3 install -U xformers --index-url https://download.pytorch.org/whl/cu121
git clone https://github.com/hpcaitech/Open-Sora
cd Open-Sora
pip install -v .

第二步:下载模型并放到对应的文件夹

cd Open-Sora/opensora/models
# 下载VAE模型
git clone https://www.modelscope.cn/AI-ModelScope/sd-vae-ft-ema.git
# 下载ST-dit模型
git clone https://www.modelscope.cn/AI-ModelScope/Open-Sora.git
# 下载text-encoder模型
cd text-encoder
git clone https://www.modelscope.cn/AI-ModelScope/t5-v1_1-xxl.git

第三步:修改config文件/mnt/workspace/Open-Sora/configs/opensora/inference/16x256x256.py

num_frames = 16
fps = 24 // 3
image_size = (256, 256)
# Define model
model = dict(
    type="STDiT-XL/2",
    space_scale=0.5,
    time_scale=1.0,
    enable_flashattn=True,
    enable_layernorm_kernel=True,
    from_pretrained="/mnt/workspace/Open-Sora/opensora/models/stdit/OpenSora-v1-HQ-16x256x256.pth",
)
vae = dict(
    type="VideoAutoencoderKL",
    from_pretrained="/mnt/workspace/Open-Sora/opensora/models/sd-vae-ft-ema",
)
text_encoder = dict(
    type="t5",
    from_pretrained="/mnt/workspace/Open-Sora/opensora/models/text_encoder",
    model_max_length=120,
)
scheduler = dict(
    type="iddpm",
    num_sampling_steps=100,
    cfg_scale=7.0,
)
dtype = "fp16"
# Others
batch_size = 2
seed = 42
prompt_path = "./assets/texts/t2v_samples.txt"
save_dir = "./outputs/samples/"

运行推理代码:

torchrun --standalone --nproc_per_node 1 scripts/inference.py configs/opensora/inference/16x256x256.py

显存峰值约30G,值得一提的,大部分的显存使用是t5-v1_1-xxl,我们切换成t5-v1_1-large,会报conditioned tensor shape不对,后续魔搭社区也会继续尝试切换,目标是可以让开发者在一张消费级显卡上使用。

使用官方提供的prompt的生成效果:

sample_4 00_00_00-00_00_30.gif

prompt:A serene underwater scene featuring a sea turtle swimming through a coral reef. The turtle, with its greenish-brown shell, is the main focus of the video, swimming gracefully towards the right side of the frame. The coral reef, teeming with life, is visible in the background, providing a vibrant and colorful backdrop to the turtle's journey. Several small fish, darting around the turtle, add a sense of movement and dynamism to the scene. The video is shot from a slightly elevated angle, providing a comprehensive view of the turtle's surroundings. The overall style of the video is calm and peaceful, capturing the beauty and tranquility of the underwater world.

sample_8 00_00_00-00_00_30.gif

prompt:The dynamic movement of tall, wispy grasses swaying in the wind. The sky above is filled with clouds, creating a dramatic backdrop. The sunlight pierces through the clouds, casting a warm glow on the scene. The grasses are a mix of green and brown, indicating a change in seasons. The overall style of the video is naturalistic, capturing the beauty of the landscape in a realistic manner. The focus is on the grasses and their movement, with the sky serving as a secondary element. The video does not contain any human or animal elements.

sample_9 (1) 00_00_00-00_00_30.gif

prompt:A serene night scene in a forested area. The first frame shows a tranquil lake reflecting the star-filled sky above. The second frame reveals a beautiful sunset, casting a warm glow over the landscape. The third frame showcases the night sky, filled with stars and a vibrant Milky Way galaxy. The video is a time-lapse, capturing the transition from day to night, with the lake and forest serving as a constant backdrop. The style of the video is naturalistic, emphasizing the beauty of the night sky and the peacefulness of the forest.

sample_0 (2) 00_00_00-00_00_30.gif

prompt:The Grandeur of a Venetian Canal at Twilight.As evening descends upon Venice, the Grand Canal shimmers with the reflections of historic palazzos and gently bobbing gondolas. The fading light turns the water to liquid gold, while the sky transitions from a soft lavender to the deep blue of the coming night. Lanterns begin to glow from wrought iron posts, casting a warm light on the faces of lovers walking hand in hand along the cobblestone paths. The gentle lapping of the water against the stone walls of the canal carries the soft melodies of a distant accordion, evoking a timeless romance only Venice can offer.

sample_3 (2) 00_00_00-00_00_30.gif

prompt:A Rustic Vineyard at Harvest Time.Nestled in the rolling hills of the countryside, the vineyard is a patchwork of vines laden with clusters of ripe, plump grapes. Workers move between the rows, baskets in hand, as they gather the fruits of their labor under the autumn sun. The vines are a cascade of golden leaves, ready to fall at the slightest touch. In the distance, a stone farmhouse rests, its walls aged by time, with smoke rising from the chimney into the clear blue sky. The air is filled with the sweet scent of grapes and the earthy aroma of the soil, inviting a sense of home and the promise of a bountiful harvest.

64帧

脚本换为/configs/opensora/inference/64x512x512.py

注意修改模型为OpenSora-v1-HQ-16x512x512.pth

推理时显存占用约44G

保存视频时占用更高,约60G

以上同样的prompt的生成效果:

sample_4 (2) 00_00_00-00_00_30.gif

prompt:A serene underwater scene featuring a sea turtle swimming through a coral reef. The turtle, with its greenish-brown shell, is the main focus of the video, swimming gracefully towards the right side of the frame. The coral reef, teeming with life, is visible in the background, providing a vibrant and colorful backdrop to the turtle's journey. Several small fish, darting around the turtle, add a sense of movement and dynamism to the scene. The video is shot from a slightly elevated angle, providing a comprehensive view of the turtle's surroundings. The overall style of the video is calm and peaceful, capturing the beauty and tranquility of the underwater world.

sample_8 (1) 00_00_00-00_00_30.gif

prompt:The dynamic movement of tall, wispy grasses swaying in the wind. The sky above is filled with clouds, creating a dramatic backdrop. The sunlight pierces through the clouds, casting a warm glow on the scene. The grasses are a mix of green and brown, indicating a change in seasons. The overall style of the video is naturalistic, capturing the beauty of the landscape in a realistic manner. The focus is on the grasses and their movement, with the sky serving as a secondary element. The video does not contain any human or animal elements.

sample_9 (1) 00_00_00-00_00_30.gif

prompt:A serene night scene in a forested area. The first frame shows a tranquil lake reflecting the star-filled sky above. The second frame reveals a beautiful sunset, casting a warm glow over the landscape. The third frame showcases the night sky, filled with stars and a vibrant Milky Way galaxy. The video is a time-lapse, capturing the transition from day to night, with the lake and forest serving as a constant backdrop. The style of the video is naturalistic, emphasizing the beauty of the night sky and the peacefulness of the forest.

点击直达Open-Sora开源链接:

GitHub - hpcaitech/Open-Sora: Open-Sora: Democratizing Efficient Video Production for All

相关文章
|
1月前
|
数据采集 存储 人工智能
TripoSR开源!从单个图像快速生成 3D 对象!(附魔搭社区推理实战教程)
近期,VAST团队和Stability AI团队合作发布了TripoSR,可在一秒内从单个图像生成高质量3D对象。
|
3月前
|
数据可视化 物联网 Swift
谷歌发布开源LLM Gemma,魔搭社区评测+最佳实践教程来啦!
Gemma是由Google推出的一系列轻量级、先进的开源模型,他们是基于 Google Gemini 模型的研究和技术而构建。
|
4月前
|
数据采集 自然语言处理 前端开发
社区供稿 | 猎户星空百亿参数大模型 Orion-14B系列开源,一张3060就能跑(附魔搭社区推理微调最佳实践)
1月21日,傅盛在猎户星空大模型发布会上宣布,“为企业应用而生” 的开源百亿参数猎户星空大模型正式发布。猎户星空大模型(Orion-14B)是由猎户星空研发的预训练多语言大语言模型,以其140亿参数规模展现出了卓越的性能。
|
9月前
|
数据安全/隐私保护 开发者 异构计算
阿里巴巴开源可控视频生成框架VideoComposer!(内含体验、推理实践干货)
8月16日,时间、空间可控的视频生成模型 VideoComposer 在魔搭社区开源。
阿里巴巴开源可控视频生成框架VideoComposer!(内含体验、推理实践干货)
|
3月前
|
人工智能 自然语言处理 搜索推荐
魔搭ModelScope社区作为一个AI模型开源平台,提供了丰富的模型资源和便捷的服务
【2月更文挑战第9天】魔搭ModelScope社区作为一个AI模型开源平台,提供了丰富的模型资源和便捷的服务
199 3
|
12天前
|
机器学习/深度学习 人工智能 分布式计算
人工智能平台PAI 操作报错合集之机器学习PAI deeprec中的sok该怎么使用
阿里云人工智能平台PAI (Platform for Artificial Intelligence) 是阿里云推出的一套全面、易用的机器学习和深度学习平台,旨在帮助企业、开发者和数据科学家快速构建、训练、部署和管理人工智能模型。在使用阿里云人工智能平台PAI进行操作时,可能会遇到各种类型的错误。以下列举了一些常见的报错情况及其可能的原因和解决方法。
|
4月前
|
自然语言处理 算法 Swift
浪潮信息开源 源2.0 基础大模型,魔搭社区最佳实践教程来啦!
浪潮信息发布源2.0基础大模型,参数量包括1026亿、518亿、21亿三个大模型,从源2.0的论文上可以看到,源2.0不仅在数理逻辑、数学计算、代码生成能力上,再次超强进化,还在算法、数据、算力方面,提出了三项创新。
|
6月前
|
人工智能 自然语言处理 IDE
人手一个编程助手!北大代码大模型CodeShell-7B开源,魔搭社区最佳实践来了!
CodeShell是北京大学知识计算实验室联合四川天府银行AI团队研发的多语言代码大模型基座。
|
10月前
|
人工智能 自然语言处理 程序员
编程助手 | CodeGeeX2-6B模型发布及魔搭最佳实践
编程助手 | CodeGeeX2-6B模型发布及魔搭最佳实践
|
机器学习/深度学习 存储 文字识别
图解来啦!机器学习工业部署最佳实践!10分钟上手机器学习部署与大规模扩展 ⛵
如何快速部署机器学习模型?本文是机器学习工业部署的 best practice(最佳实践)!详细讲解了如何操作机器学习开源框架 BentoML,帮助研发团队轻松打包机器学习模型,并重现该模型以用于生产。
1572 4
图解来啦!机器学习工业部署最佳实践!10分钟上手机器学习部署与大规模扩展 ⛵

热门文章

最新文章