Diffusers 文档
Allegro
并获得增强的文档体验
开始使用
Allegro
Allegro:打开商业级视频生成模型的黑匣子,作者:RhymesAI,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,
torch_dtype=torch.float16,
)
quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = AllegroTransformer3DModel.from_pretrained(
"rhymes-ai/Allegro",
subfolder="transformer",
quantization_config=quant_config,
torch_dtype=torch.float16,
)
pipeline = AllegroPipeline.from_pretrained(
"rhymes-ai/Allegro",
text_encoder=text_encoder_8bit,
transformer=transformer_8bit,
torch_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
class diffusers.AllegroPipeline
< 来源 >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLAllegro transformer: AllegroTransformer3DModel scheduler: KarrasDiffusionSchedulers )
参数
- vae (
AllegroAutoEncoderKL3D
) — 用于编码和解码视频到潜在表示以及从潜在表示解码的变分自动编码器 (VAE) 模型。 - text_encoder (
T5EncoderModel
) — 冻结文本编码器。PixArt-Alpha 使用 T5,特别是 t5-v1_1-xxl 变体。 - tokenizer (
T5Tokenizer
) — T5Tokenizer 类的分词器。 - transformer (AllegroTransformer3DModel) — 一个文本条件 AllegroTransformer3DModel,用于对编码后的视频潜在表示进行去噪。
- scheduler (SchedulerMixin) — 一个调度器,与 transformer 结合使用,对编码后的视频潜在表示进行去噪。
用于文本到视频生成的 Allegro 管道。
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等)
__call__
< 来源 >( prompt: typing.Union[str, typing.List[str]] = None negative_prompt: str = '' num_inference_steps: int = 100 timesteps: typing.List[int] = None guidance_scale: float = 7.5 num_frames: typing.Optional[int] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_videos_per_prompt: int = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None output_type: typing.Optional[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 max_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 (
Callable
, 可选) — 在推理过程中每callback_steps
步都会调用的函数。该函数将使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则每一步都会调用回调。 - clean_caption (
bool
, 可选, 默认为True
) — 在创建嵌入之前是否清理标题。需要安装beautifulsoup4
和ftfy
。如果未安装依赖项,将从原始提示创建嵌入。 - max_sequence_length (
int
默认为512
) — 与prompt
一起使用的最大序列长度。
返回
AllegroPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 AllegroPipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的视频列表。
调用管道进行生成时调用的函数。
示例
>>> 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.enable_vae_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)
禁用切片 VAE 解码。如果之前启用了 enable_vae_slicing
,此方法将返回一步计算解码。
禁用平铺 VAE 解码。如果之前启用了 enable_vae_tiling
,此方法将恢复一步计算解码。
启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。
启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。
encode_prompt
< source >( prompt: typing.Union[str, typing.List[str]] do_classifier_free_guidance: bool = True negative_prompt: str = '' num_videos_per_prompt: int = 1 device: typing.Optional[torch.device] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None clean_caption: bool = False max_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
) — 是否使用无分类器引导。 - 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
,函数将在编码前预处理并清理提供的标题。 - max_sequence_length (
int
, 默认为 512) — 用于提示的最大序列长度。
将提示编码为文本编码器隐藏状态。
AllegroPipelineOutput
class diffusers.pipelines.allegro.pipeline_output.AllegroPipelineOutput
< source >( frames: typing.Union[torch.Tensor, numpy.ndarray, typing.List[typing.List[PIL.Image.Image]]] )
Allegro 管道的输出类。