Diffusers 文档

Latte

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Latte

latte text-to-video

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

论文摘要如下:

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

亮点:Latte 是一种潜在扩散 Transformer,被提议作为建模不同模态的骨干(此处用于文本到视频生成)。它在四个标准视频基准数据集上取得了最先进的性能——FaceForensicsSkyTimelapseUCF101Taichi-HD。有关评估数据集的准备和下载,请参阅 此链接

此管道由 maxin-cn 贡献。原始代码库可在 此处 找到。原始权重可在 hf.co/maxin-cn 找到。

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

推理

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

首先,加载管道

import torch
from diffusers import LattePipeline

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

然后将管道 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进行bitsandbytes推理。

import torch
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, LatteTransformer3DModel, LattePipeline
from diffusers.utils import export_to_gif
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, T5EncoderModel

quant_config = BitsAndBytesConfig(load_in_8bit=True)
text_encoder_8bit = T5EncoderModel.from_pretrained(
    "maxin-cn/Latte-1",
    subfolder="text_encoder",
    quantization_config=quant_config,
    torch_dtype=torch.float16,
)

quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = LatteTransformer3DModel.from_pretrained(
    "maxin-cn/Latte-1",
    subfolder="transformer",
    quantization_config=quant_config,
    torch_dtype=torch.float16,
)

pipeline = LattePipeline.from_pretrained(
    "maxin-cn/Latte-1",
    text_encoder=text_encoder_8bit,
    transformer=transformer_8bit,
    torch_dtype=torch.float16,
    device_map="balanced",
)

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

LattePipeline

class 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: int = 14 ) LattePipelineOutputtuple

参数

  • prompt (strList[str], 可选) — 用于引导视频生成的提示词或提示词列表。如果未定义,则必须传入 prompt_embeds
  • negative_prompt (strList[str], 可选) — 不用于引导视频生成的提示词或提示词列表。如果未定义,则必须传入 negative_prompt_embeds。如果未使用引导(即,如果 guidance_scale 小于 1),则忽略此参数。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步数。更多的去噪步数通常会导致更高质量的视频,但会牺牲推理速度。
  • timesteps (List[int], 可选) — 用于去噪过程的自定义时间步长。如果未定义,则使用等间距的 num_inference_steps 时间步长。必须按降序排列。
  • guidance_scale (float, 可选, 默认为 7.0) — Classifier-Free Diffusion Guidance 中定义的引导比例。guidance_scale 定义为 Imagen Paper 中公式 2 的 w。通过设置 guidance_scale > 1 启用引导比例。较高的引导比例鼓励生成与文本 prompt 紧密相关的视频,通常以牺牲视频质量为代价。
  • video_length (int, 可选, 默认为 16) — 生成视频的帧数。默认为 16 帧,每秒 8 帧。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示词生成的视频数量。
  • height (int, 可选, 默认为 self.unet.config.sample_size) — 生成视频的高度(像素)。
  • width (int, 可选, 默认为 self.unet.config.sample_size) — 生成视频的宽度(像素)。
  • eta (float, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η): https://huggingface.co/papers/2010.02502。仅适用于 schedulers.DDIMScheduler,对其他调度器将被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch 生成器,用于使生成确定。
  • latents (torch.FloatTensor, 可选) — 预生成的带噪潜在表示,从高斯分布中采样,用作视频生成的输入。可用于使用不同的提示词调整相同的生成。如果未提供,将使用提供的随机 generator 采样生成一个潜在张量。
  • prompt_embeds (torch.FloatTensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词加权。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负向文本嵌入。对于 Latte,此负向提示词应为 ""。如果未提供,负向提示词嵌入将从 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], 可选) — 应该传递给回调函数的张量输入列表。如果未定义,将传递所有张量输入。
  • clean_caption (bool, 可选, 默认为 True) — 是否在创建嵌入之前清理标题。需要安装 beautifulsoup4ftfy。如果未安装依赖项,则将从原始提示创建嵌入。
  • mask_feature (bool 默认为 True) — 如果设置为 True,文本嵌入将被遮罩。
  • enable_temporal_attentions (bool, 可选, 默认为 True) — 是否启用时间注意力。
  • decode_chunk_size (int, 可选) — 每次解码的帧数。较高的分块大小可以带来更好的时间一致性,但会消耗更多内存。默认情况下,解码器一次解码所有帧以实现最大质量。为了降低内存使用,请减小 decode_chunk_size

返回

LattePipelineOutputtuple

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

调用管道进行生成时调用的函数。

示例

>>> 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 (strList[str], 可选) — 待编码的提示。
  • negative_prompt (strList[str], 可选) — 不用于引导视频生成的提示。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时(即,如果 guidance_scale 小于 1 时),将被忽略。对于 Latte,这应该为空字符串 “”。
  • do_classifier_free_guidance (bool, 可选, 默认为 True) — 是否使用分类器自由引导。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示应生成的视频数量。
  • device — (torch.device, 可选):放置结果嵌入的 torch 设备。
  • prompt_embeds (torch.FloatTensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负面文本嵌入。对于 Latte,它应该是空字符串 “"” 的嵌入。
  • clean_caption (bool, 默认为 False) — 如果为 True,函数将在编码前预处理并清理提供的标题。
  • mask_feature — (bool, 默认为 True):如果为 True,函数将遮罩文本嵌入。

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

< > 在 GitHub 上更新