Diffusers documentation

Allegro

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Allegro

来自 RhymesAI 的 Allegro:打开商业级视频生成模型的黑盒,作者为 Yuan Zhou、Qiuyue Wang、Yuxuan Cai、Huan Yang。

该论文的摘要如下:

视频生成领域已取得显著进展,开源社区贡献了大量用于训练高质量模型的研究论文和工具。然而,尽管付出了这些努力,现有的信息和资源仍不足以实现商业级性能。在本报告中,我们揭开黑盒,介绍了 Allegro——一个在质量和时间一致性方面均表现卓越的先进视频生成模型。我们还指出了该领域当前的局限性,并提出了训练高性能、商业级视频生成模型的综合方法论,涵盖了数据、模型架构、训练流程和评估等关键方面。我们的用户研究表明,Allegro 超越了现有的开源模型以及大多数商业模型,排名仅次于 Hailuo 和 Kling。代码:https://github.com/rhymes-ai/Allegro ,模型:https://huggingface.co/rhymes-ai/Allegro ,展示:https://rhymes.ai/allegro_gallery 。

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

量化

量化通过以较低精度的数据类型存储模型权重,有助于降低超大模型的内存需求。不过,量化对视频质量的影响会因视频模型的不同而有所差异。

请参阅 量化 概述,以了解支持的量化后端以及如何选择支持您使用场景的量化后端。下面的示例演示了如何使用 bitsandbytes 加载用于推理的量化 AllegroPipeline。

import torch
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, AllegroTransformer3DModel, AllegroPipeline
from diffusers.utils import export_to_video
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, T5EncoderModel

quant_config = BitsAndBytesConfig(load_in_8bit=True)
text_encoder_8bit = T5EncoderModel.from_pretrained(
    "rhymes-ai/Allegro",
    subfolder="text_encoder",
    quantization_config=quant_config,
    dtype=torch.float16,
)

quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = AllegroTransformer3DModel.from_pretrained(
    "rhymes-ai/Allegro",
    subfolder="transformer",
    quantization_config=quant_config,
    dtype=torch.float16,
)

pipeline = AllegroPipeline.from_pretrained(
    "rhymes-ai/Allegro",
    text_encoder=text_encoder_8bit,
    transformer=transformer_8bit,
    dtype=torch.float16,
    device_map="balanced",
)

prompt = (
    "A seaside harbor with bright sunlight and sparkling seawater, with many boats in the water. From an aerial view, "
    "the boats vary in size and color, some moving and some stationary. Fishing boats in the water suggest that this "
    "location might be a popular spot for docking fishing boats."
)
video = pipeline(prompt, guidance_scale=7.5, max_sequence_length=512).frames[0]
export_to_video(video, "harbor.mp4", fps=15)

AllegroPipeline

类 diffusers.AllegroPipeline

< >

( tokenizer: T5Tokenizertext_encoder: T5EncoderModelvae: AutoencoderKLAllegrotransformer: AllegroTransformer3DModelscheduler: KarrasDiffusionSchedulers )

参数

  • vae (AllegroAutoEncoderKL3D) — 变分自编码器(VAE)模型,用于将视频编码为潜在表示,以及从潜在表示解码回视频。
  • text_encoder (T5EncoderModel) — 冻结的文本编码器。PixArt-Alpha 使用 T5,具体为 t5-v1_1-xxl 变体。
  • tokenizer (T5Tokenizer) — T5Tokenizer 类的分词器。
  • transformer (AllegroTransformer3DModel) — 一个以文本为条件的 AllegroTransformer3DModel,用于对编码后的视频潜变量进行去噪。
  • scheduler (SchedulerMixin) — 用于与 transformer 配合,对编码后的视频潜变量进行去噪的调度器。

使用 Allegro 进行文本到视频生成的 Pipeline。

该模型继承自 DiffusionPipeline。有关该库为所有流水线实现的通用方法(例如下载或保存、在特定设备上运行等),请参阅父类文档。

__call__

< >

( prompt: str | list[str] = Nonenegative_prompt: str = ''num_inference_steps: int = 100timesteps: list = Noneguidance_scale: float = 7.5num_frames: int | None = Noneheight: int | None = Nonewidth: int | None = Nonenum_videos_per_prompt: int = 1eta: float = 0.0generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneprompt_embeds: typing.Optional[torch.Tensor] = Noneprompt_attention_mask: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_attention_mask: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = '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 = Truemax_sequence_length: int = 512 ) → AllegroPipelineOutput 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.5) — Classifier-Free Diffusion Guidance 中定义的引导尺度。guidance_scale 定义为 Imagen Paper 中公式 2 的 w。通过设置 guidance_scale > 1 来启用引导尺度。较高的引导尺度会促使生成与文本 prompt 紧密关联的视频,但通常以牺牲视频质量为代价。
  • num_videos_per_prompt (int,可选,默认为 1) — 每个提示词生成的视频数量。
  • num_frames — (int,可选,默认值为 88): 该参数控制生成视频中的帧数。
  • 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.Tensor, 可选) — 预生成的噪声潜变量,从高斯分布中采样,用作视频生成的输入。可用于使用不同的提示词微调同一次生成。如果未提供,则使用提供的随机generator进行采样,生成一个潜变量张量。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • prompt_attention_mask (torch.Tensor,可选) — 为文本嵌入预生成的注意力掩码。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负向文本嵌入。对于 PixArt-Sigma,此负向提示应为 ""。如果未提供,negative_prompt_embeds 将根据 negative_prompt 输入参数生成。
  • negative_prompt_attention_mask (torch.Tensor, 可选) — 为负向文本嵌入预生成的注意力掩码。
  • output_type (str,可选,默认为 "pil") — 生成视频的输出格式。可选值: PIL:PIL.Image.Image 或 np.array。
  • return_dict (bool,可选,默认为 True) — 是否返回 ~pipelines.stable_diffusion.IFPipelineOutput 而不是普通元组。
  • callback_on_step_end (Callable, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数。该函数以以下参数被调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)。callback_kwargs 将包含由 callback_on_step_end_tensor_inputs 指定的所有张量列表。
  • callback_on_step_end_tensor_inputs (List,可选) — callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传入。您只能包含流水线类的 ._callback_tensor_inputs 属性中列出的变量。
  • clean_caption (bool,可选,默认为 True) — 是否在创建嵌入之前清理字幕。需要安装 beautifulsoup4 和 ftfy。如果未安装这些依赖项,嵌入将直接从原始提示创建。
  • max_sequence_length(int,默认为 512)— 与 prompt 一起使用的最大序列长度。

返回值

AllegroPipelineOutput 或 tuple

如果 return_dict 为 True,则返回 AllegroPipelineOutput,否则返回一个 tuple,其中第一个元素是包含生成视频的列表。

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

示例:

>>> import torch
>>> from diffusers import AutoencoderKLAllegro, AllegroPipeline
>>> from diffusers.utils import export_to_video

>>> vae = AutoencoderKLAllegro.from_pretrained("rhymes-ai/Allegro", subfolder="vae", torch_dtype=torch.float32)
>>> pipe = AllegroPipeline.from_pretrained("rhymes-ai/Allegro", vae=vae, torch_dtype=torch.bfloat16).to("cuda")
>>> pipe.vae.enable_tiling()

>>> prompt = (
...     "A seaside harbor with bright sunlight and sparkling seawater, with many boats in the water. From an aerial view, "
...     "the boats vary in size and color, some moving and some stationary. Fishing boats in the water suggest that this "
...     "location might be a popular spot for docking fishing boats."
... )
>>> video = pipe(prompt, guidance_scale=7.5, max_sequence_length=512).frames[0]
>>> export_to_video(video, "output.mp4", fps=15)

encode_prompt

< >

( prompt: str | list[str]do_classifier_free_guidance: bool = Truenegative_prompt: str = ''num_videos_per_prompt: int = 1device: typing.Optional[torch.device] = Noneprompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Noneprompt_attention_mask: typing.Optional[torch.Tensor] = Nonenegative_prompt_attention_mask: typing.Optional[torch.Tensor] = Noneclean_caption: bool = Falsemax_sequence_length: int = 512**kwargs )

参数

  • prompt (str 或 list[str],可选) — 要编码的提示
  • negative_prompt (str 或 list[str],可选) — 用于不引导图像生成的提示词。如果未定义,则必须改为传入 negative_prompt_embeds。在未使用引导时会被忽略(即当 guidance_scale 小于 1 时会被忽略)。对于 PixArt-Alpha,此值应为 ""。
  • do_classifier_free_guidance (bool,可选,默认为 True) — 是否使用 classifier free guidance
  • num_videos_per_prompt (int,可选,默认为 1) — 每个提示词应生成的图像数量
  • device — (torch.device,可选): 用于放置生成的嵌入向量的 torch 设备
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负向文本嵌入。对于 PixArt-Alpha,它应该是空字符串 "" 的嵌入。
  • clean_caption (bool,默认为 False) — 如果为 True,该函数将在编码之前对提供的 caption 进行预处理和清理。
  • max_sequence_length (int,默认为 512) — 用于提示词的最大序列长度。

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

AllegroPipelineOutput

class diffusers.pipelines.allegro.pipeline_output.AllegroPipelineOutput

< >

( frames: typing.Union[torch.Tensor, numpy.ndarray, list[list[PIL.Image.Image]]] )

参数

  • frames (torch.Tensor、np.ndarray 或 list[list[PIL.Image.Image]]) — 视频输出列表 - 它可以是一个长度为 batch_size, 的嵌套列表,其中每个子列表包含长度为 num_frames. 的去噪 PIL 图像序列。它也可以是一个形状为 (batch_size, num_frames, channels, height, width) 的 NumPy 数组或 Torch 张量。

Allegro 管道的输出类。

Update on GitHub