Diffusers 文档

Latte

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Latte

latte text-to-video

Latte: 用于视频生成的潜在扩散 Transformer 来自莫纳什大学、上海人工智能实验室、南京大学和南洋理工大学。

论文摘要如下:

我们提出了一种新颖的潜在扩散 Transformer,名为 Latte,用于视频生成。Latte 首先从输入视频中提取时空 tokens,然后采用一系列 Transformer 块在潜在空间中对视频分布进行建模。为了对从视频中提取的大量 tokens 进行建模,从分解输入视频的空间和时间维度的角度引入了四种高效变体。为了提高生成视频的质量,我们通过严格的实验分析确定了 Latte 的最佳实践,包括视频剪辑 patch 嵌入、模型变体、时间步长-类别信息注入、时间位置嵌入和学习策略。我们全面的评估表明,Latte 在四个标准视频生成数据集(即 FaceForensics、SkyTimelapse、UCF101 和 Taichi-HD)上实现了最先进的性能。此外,我们将 Latte 扩展到文本到视频生成 (T2V) 任务,与最新的 T2V 模型相比,Latte 取得了相当的结果。我们坚信,Latte 为未来将 Transformers 纳入用于视频生成的扩散模型的研究提供了有价值的见解。

亮点:Latte 是一种潜在扩散 transformer,被提议作为对不同模态进行建模的骨干网络(此处针对文本到视频生成进行了训练)。它在四个标准视频基准测试 - FaceForensics, SkyTimelapse, UCF101Taichi-HD 上实现了最先进的性能。要准备和下载用于评估的数据集,请参阅 此链接

此 pipeline 由 maxin-cn 贡献。原始代码库可以在 这里 找到。原始权重可以在 hf.co/maxin-cn 下找到。

请务必查看 Schedulers 指南,了解如何探索 scheduler 速度和质量之间的权衡,并查看 跨 pipelines 重用组件 部分,了解如何有效地将相同组件加载到多个 pipelines 中。

推理

使用 torch.compile 减少推理延迟。

首先,加载 pipeline

import torch
from diffusers import LattePipeline

pipeline = LattePipeline.from_pretrained(
	"maxin-cn/Latte-1", torch_dtype=torch.float16
).to("cuda")

然后将 pipelines 的 transformervae 组件的内存布局更改为 torch.channels-last

pipeline.transformer.to(memory_format=torch.channels_last)
pipeline.vae.to(memory_format=torch.channels_last)

最后,编译组件并运行推理

pipeline.transformer = torch.compile(pipeline.transformer)
pipeline.vae.decode = torch.compile(pipeline.vae.decode)

video = pipeline(prompt="A dog wearing sunglasses floating in space, surreal, nebulae in background").frames[0]

在 80GB A100 机器上的基准测试结果如下:

Without torch.compile(): Average inference time: 16.246 seconds.
With torch.compile(): Average inference time: 14.573 seconds.

LattePipeline

diffusers.LattePipeline

< >

( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKL transformer: LatteTransformer3DModel scheduler: KarrasDiffusionSchedulers )

参数

  • vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将视频编码和解码为潜在表示形式。
  • text_encoder (T5EncoderModel) — 冻结的文本编码器。Latte 使用 T5,特别是 t5-v1_1-xxl 变体。
  • tokenizer (T5Tokenizer) — 类 T5Tokenizer 的分词器。
  • transformer (LatteTransformer3DModel) — 文本条件化的 LatteTransformer3DModel,用于对编码的视频潜在表示进行去噪。
  • scheduler (SchedulerMixin) — 一个调度器,与 transformer 结合使用,以对编码的视频潜在表示进行去噪。

使用 Latte 的文本到视频生成流程。

此模型继承自 DiffusionPipeline。有关库为所有流程实现的通用方法(例如下载或保存,在特定设备上运行等),请查看超类文档。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None negative_prompt: str = '' num_inference_steps: int = 50 timesteps: typing.Optional[typing.List[int]] = None guidance_scale: float = 7.5 num_images_per_prompt: int = 1 video_length: int = 16 height: int = 512 width: int = 512 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.FloatTensor] = None prompt_embeds: typing.Optional[torch.FloatTensor] = None negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None output_type: str = 'pil' return_dict: bool = True callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] clean_caption: bool = True mask_feature: bool = True enable_temporal_attentions: bool = True decode_chunk_size: typing.Optional[int] = None ) LattePipelineOutput or tuple

参数

  • prompt (strList[str], *可选*) — 用于引导视频生成的提示或提示列表。如果未定义,则必须传递 prompt_embeds
  • negative_prompt (strList[str], *可选*) — 不用于引导视频生成的提示或提示列表。如果未定义,则必须传递 negative_prompt_embeds。不使用 guidance 时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • num_inference_steps (int, *可选*, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的视频,但代价是推理速度较慢。
  • timesteps (List[int], *可选*) — 用于去噪过程的自定义时间步长。如果未定义,则使用等间距的 num_inference_steps 时间步长。必须按降序排列。
  • guidance_scale (float, *可选*, 默认为 7.0) — Guidance scale,定义于 Classifier-Free Diffusion Guidanceguidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的视频,但通常以较低的视频质量为代价。
  • video_length (int, *可选*, 默认为 16) — 生成的视频帧数。默认为 16 帧,即每秒 8 帧。
  • num_images_per_prompt (int, *可选*, 默认为 1) — 每个 prompt 生成的视频数量。
  • height (int, *可选*, 默认为 self.unet.config.sample_size) — 生成视频的高度,以像素为单位。
  • width (int, *可选*, 默认为 self.unet.config.sample_size) — 生成视频的宽度,以像素为单位。
  • eta (float, *可选*, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η): https://arxiv.org/abs/2010.02502。仅适用于 schedulers.DDIMScheduler,对其他调度器将被忽略。
  • generator (torch.GeneratorList[torch.Generator], *可选*) — 用于使生成具有确定性的一个或一组 torch generator(s)
  • latents (torch.FloatTensor, *可选*) — 预生成的噪声潜在表示,从高斯分布中采样,用作视频生成的输入。可用于使用不同的 prompt 调整相同的生成。如果未提供,将使用提供的随机 generator 采样生成潜在张量。
  • prompt_embeds (torch.FloatTensor, *可选*) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 prompt 加权。如果未提供,将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.FloatTensor, *可选*) — 预生成的负文本嵌入。对于 Latte,此负面 prompt 应为 ""。如果未提供,negative_prompt_embeds 将从 negative_prompt 输入参数生成。
  • output_type (str, *可选*, 默认为 "pil") — 生成视频的输出格式。在 PIL: PIL.Image.Imagenp.array 之间选择。
  • return_dict (bool, *可选*, 默认为 True) — 是否返回 ~pipelines.stable_diffusion.IFPipelineOutput 而不是普通元组。
  • callback_on_step_end (Callable[[int, int, Dict], None], PipelineCallback, MultiPipelineCallbacks, *可选*) — 在每个去噪步骤结束时调用的回调函数或回调函数列表。
  • callback_on_step_end_tensor_inputs (List[str], optional) — 应该传递给回调函数的张量输入列表。如果未定义,则将传递所有张量输入。
  • clean_caption (bool, optional, defaults to True) — 是否在创建嵌入之前清理 caption。需要安装 beautifulsoup4ftfy。如果未安装依赖项,则将从原始 prompt 创建嵌入。
  • mask_feature (bool defaults to True) — 如果设置为 True,则将屏蔽文本嵌入。
  • enable_temporal_attentions (bool, optional, defaults to True) — 是否启用时间注意力机制
  • decode_chunk_size (int, optional) — 一次解码的帧数。较高的 chunk size 会带来更好的时间一致性,但会增加内存使用量。默认情况下,解码器一次解码所有帧以获得最佳质量。为了降低内存使用量,请减小 decode_chunk_size

返回

LattePipelineOutputtuple

如果 return_dictTrue,则返回 LattePipelineOutput,否则返回 tuple,其中第一个元素是包含生成的图像的列表

调用 pipeline 进行生成时调用的函数。

示例

>>> import torch
>>> from diffusers import LattePipeline
>>> from diffusers.utils import export_to_gif

>>> # You can replace the checkpoint id with "maxin-cn/Latte-1" too.
>>> pipe = LattePipeline.from_pretrained("maxin-cn/Latte-1", torch_dtype=torch.float16)
>>> # Enable memory optimizations.
>>> pipe.enable_model_cpu_offload()

>>> prompt = "A small cactus with a happy face in the Sahara desert."
>>> videos = pipe(prompt).frames[0]
>>> export_to_gif(videos, "latte.gif")

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] do_classifier_free_guidance: bool = True negative_prompt: str = '' num_images_per_prompt: int = 1 device: typing.Optional[torch.device] = None prompt_embeds: typing.Optional[torch.FloatTensor] = None negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None clean_caption: bool = False mask_feature: bool = True dtype = None )

参数

  • prompt (str or List[str], optional) — 要编码的 prompt
  • negative_prompt (str or List[str], optional) — 不用于引导视频生成的 prompt。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时(即,如果 guidance_scale 小于 1),则忽略此参数。对于 Latte,这应该为 ""。
  • do_classifier_free_guidance (bool, optional, defaults to True) — 是否使用无分类器引导
  • num_images_per_prompt (int, optional, defaults to 1) — 每个 prompt 应生成的视频数量
  • device — (torch.device, optional): 用于放置结果嵌入的 torch 设备
  • prompt_embeds (torch.FloatTensor, optional) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.FloatTensor, optional) — 预生成的负面文本嵌入。对于 Latte,它应该是 "" 字符串的嵌入。
  • clean_caption (bool, defaults to False) — 如果为 True,该函数将在编码之前预处理和清理提供的 caption。
  • mask_feature — (bool, defaults to True): 如果为 True,该函数将屏蔽文本嵌入。

将 prompt 编码为文本编码器隐藏状态。

< > Update on GitHub