Diffusers documentation
Latte
Latte

来自莫纳什大学、上海人工智能实验室、南京大学和南洋理工大学的Latte: Latent Diffusion Transformer for Video Generation。
该论文的摘要如下:
我们提出了一种新颖的潜在扩散 Transformer,即 Latte,用于视频生成。Latte 首先从输入视频中提取时空 token,然后采用一系列 Transformer 块在潜在空间中建模视频分布。为了对从视频中提取的大量 token 进行建模,我们从分解输入视频的空间和时间维度的角度,引入了四种高效变体。为了提高生成视频的质量,我们通过严格的实验分析确定了 Latte 的最佳实践,包括视频片段 patch 嵌入、模型变体、时间步-类别信息注入、时间位置嵌入以及学习策略。我们的全面评估表明,Latte 在四个标准视频生成数据集(即 FaceForensics、SkyTimelapse、UCF101 和 Taichi-HD)上均达到了最先进的性能。此外,我们将 Latte 扩展到文本到视频生成(T2V)任务,其中 Latte 取得了与近期 T2V 模型相当的结果。我们坚信,Latte 为未来将 Transformer 融入扩散模型以进行视频生成的研究提供了宝贵的见解。
亮点:Latte 是一种潜在扩散 Transformer,被提出作为建模不同模态的骨干网络(此处用于训练文本到视频生成)。它在四个标准视频基准测试——FaceForensics、SkyTimelapse、UCF101 和 Taichi-HD——上均达到了最先进的性能。如需准备并下载用于评估的数据集,请参阅 此链接。
此流水线由 maxin-cn 贡献。原始代码库可在此处找到。原始权重可在 hf.co/maxin-cn 下找到。
请务必参阅 调度器指南,了解如何探索调度器速度与质量之间的权衡,并查看 跨流水线复用组件 部分,了解如何高效地将相同组件加载到多个流水线中。
推理
使用 torch.compile 来降低推理延迟。
首先,加载 pipeline:
import torch
from diffusers import LattePipeline
pipeline = LattePipeline.from_pretrained(
"maxin-cn/Latte-1", dtype=torch.float16
).to("cuda") # or "mps", "xpu", "cpu"然后,将流水线中 transformer 和 vae 组件的内存布局更改为 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 机器上的 benchmark 结果如下:
Without torch.compile(): Average inference time: 16.246 seconds.
With torch.compile(): Average inference time: 14.573 seconds.量化
量化通过以较低精度的数据类型存储模型权重,有助于降低超大模型的内存需求。不过,量化对视频质量的影响会因视频模型的不同而有所差异。
请参阅 量化 概述,以了解支持的量化后端以及如何选择支持您使用场景的量化后端。以下示例演示了如何使用 bitsandbytes 加载用于推理的量化 LattePipeline。
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,
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,
dtype=torch.float16,
)
pipeline = LattePipeline.from_pretrained(
"maxin-cn/Latte-1",
text_encoder=text_encoder_8bit,
transformer=transformer_8bit,
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
类 diffusers.LattePipeline
< source >( tokenizer: T5Tokenizertext_encoder: T5EncoderModelvae: AutoencoderKLtransformer: LatteTransformer3DModelscheduler: KarrasDiffusionSchedulers )
参数
- vae (AutoencoderKL) — 变分自编码器(VAE)模型,用于将视频编码和解码为潜在表示。
- text_encoder (
T5EncoderModel) — 冻结的文本编码器。Latte 使用的是 T5,具体为 t5-v1_1-xxl 变体。 - tokenizer (
T5Tokenizer) — T5Tokenizer 类的分词器。 - transformer (LatteTransformer3DModel) —
一个以文本为条件的
LatteTransformer3DModel,用于对编码后的视频潜变量进行去噪。 - scheduler (SchedulerMixin) —
用于与
transformer配合,对编码后的视频潜变量进行去噪的调度器。
使用 Latte 进行文本到视频生成的 Pipeline。
该模型继承自 DiffusionPipeline。有关该库为所有流水线实现的通用方法(例如下载或保存、在特定设备上运行等),请参阅父类文档。
__call__
< source >( prompt: str | list[str] = Nonenegative_prompt: str = ''num_inference_steps: int = 50timesteps: list[int] | None = Noneguidance_scale: float = 7.5num_images_per_prompt: int = 1video_length: int = 16height: int = 512width: int = 512eta: float = 0.0generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneprompt_embeds: typing.Optional[torch.FloatTensor] = Nonenegative_prompt_embeds: typing.Optional[torch.FloatTensor] = Noneoutput_type: str = 'pil'return_dict: bool = Truecallback_on_step_end: typing.Union[typing.Callable[[int, int], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = Nonecallback_on_step_end_tensor_inputs: list = ['latents']clean_caption: bool = Truemask_feature: bool = Trueenable_temporal_attentions: bool = Truedecode_chunk_size: int = 14 ) → LattePipelineOutput or tuple
参数
- prompt (
str或list[str],可选) — 用于引导视频生成的提示词或提示词列表。如果未定义,则必须改为传入prompt_embeds。 - negative_prompt (
str或list[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.Generator或list[torch.Generator],可选) — 一个或一组 torch 生成器,用于使生成过程具有确定性。 - latents (
torch.FloatTensor, 可选) — 预先生成的噪声潜变量,从高斯分布中采样,用作视频生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供,将通过使用提供的随机generator进行采样来生成一个潜变量张量。 - prompt_embeds (
torch.FloatTensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,文本嵌入将从prompt输入参数生成。 - negative_prompt_embeds (
torch.FloatTensor, 可选) — 预生成的负向文本嵌入。对于 Latte,此负向提示应为 ""。如果未提供, negative_prompt_embeds 将根据negative_prompt输入参数生成。 - output_type (
str,可选,默认为"pil") — 生成视频的输出格式。可选值: PIL:PIL.Image.Image或np.array。 - return_dict (
bool,可选,默认为True) — 是否返回~pipelines.stable_diffusion.IFPipelineOutput而不是普通元组。 - callback_on_step_end (
Callable[[int, int], None],PipelineCallback,MultiPipelineCallbacks, 可选) — 在每个去噪步骤结束时调用的回调函数或回调函数列表。 - callback_on_step_end_tensor_inputs (
list[str],可选) — 应传递给回调函数的张量输入列表。如果未定义,则所有张量输入都会被传递。 - clean_caption (
bool,可选,默认为True) — 是否在创建嵌入之前清理字幕。需要安装beautifulsoup4和ftfy。如果未安装这些依赖项,嵌入将直接从原始提示创建。 - mask_feature(
bool,默认为True)— 如果设置为True,文本嵌入将被屏蔽。 - enable_temporal_attentions (
bool,可选,默认为True) — 是否启用时间注意力 - decode_chunk_size (
int, 可选) — 每次解码的帧数。较大的块大小可以提高时间一致性,但会增加内存占用。默认情况下,解码器会一次性解码所有帧以获得最佳质量。若要降低内存占用,请减小decode_chunk_size。
返回值
LattePipelineOutput 或 tuple
如果 return_dict 为 True,则返回 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
< source >( prompt: str | list[str]do_classifier_free_guidance: bool = Truenegative_prompt: str = ''num_images_per_prompt: int = 1device: typing.Optional[torch.device] = Noneprompt_embeds: typing.Optional[torch.FloatTensor] = Nonenegative_prompt_embeds: typing.Optional[torch.FloatTensor] = Noneclean_caption: bool = Falsemask_feature: bool = Truedtype = None )
参数
- prompt (
str或list[str],可选) — 要编码的提示 - negative_prompt (
str或list[str],可选) — 用于不引导视频生成的提示词。如果未定义,则必须改为传入negative_prompt_embeds。在未使用引导时会被忽略(即当guidance_scale小于1时会被忽略)。对于 Latte,此值应为 ""。 - do_classifier_free_guidance (
bool,可选,默认为True) — 是否使用 classifier free guidance - 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,该函数将在编码之前对提供的 caption 进行预处理和清理。 - mask_feature — (bool,默认为
True): 如果True,该函数将屏蔽文本嵌入。
将提示词编码为文本编码器的隐藏状态。