Diffusers 文档

Lumina-T2X

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Lumina-T2X

concepts

Lumina-Next:使用 Next-DiT 让 Lumina-T2X 更强更快,来自 Alpha-VLLM、OpenGVLab、上海人工智能实验室。

论文摘要如下:

Lumina-T2X 是一个基于流的大型扩散变换器(Flag-DiT)的新兴系列,它建立了一个统一的框架,用于将噪声转化为各种模态,例如图像和视频,并以文本指令为条件。尽管 Lumina-T2X 具有良好的前景,但它仍然面临着训练不稳定、推理缓慢和外推伪影等挑战。本文中,我们介绍了 Lumina-Next,它是 Lumina-T2X 的改进版本,展示了更强的生成性能,并提高了训练和推理效率。我们首先对 Flag-DiT 架构进行了全面的分析,并确定了几个次优组件,我们通过引入具有 3D RoPE 和三明治归一化的 Next-DiT 架构来解决这些问题。为了实现更好的分辨率外推,我们彻底比较了应用于文本到图像生成与 3D RoPE 的不同上下文外推方法,并提出了针对扩散变换器量身定制的频率和时间感知缩放 RoPE。此外,我们引入了 sigmoid 时间离散化调度以减少求解流 ODE 的采样步骤,以及上下文丢弃方法以合并冗余视觉标记以实现更快的网络评估,从而有效提高了整体采样速度。得益于这些改进,Lumina-Next 不仅提高了基本文本到图像生成的质量和效率,还展示了卓越的分辨率外推能力和使用基于解码器的 LLM 作为文本编码器的多语言生成能力,所有这些都以零样本方式实现。为了进一步验证 Lumina-Next 作为一个多功能生成框架,我们将其应用于各种任务,包括视觉识别、多视图、音频、音乐和点云生成,在这些领域都展示了强大的性能。通过在 https://github.com/Alpha-VLLM/Lumina-T2X 发布所有代码和模型权重,我们旨在推动能够进行通用建模的下一代生成式 AI 的发展。

亮点:Lumina-Next 是一款下一代扩散变换器,通过引入 Next-DiT 架构、3D RoPE 以及频率和时间感知的 RoPE 等改进,显著增强了文本到图像生成、多语言生成和多任务性能。

Lumina-Next 具有以下组件:

  • 它以更少、更快的步骤提高了采样效率。
  • 它使用 Next-DiT 作为变换器骨干,具有 Sandwichnorm 3D RoPE 和分组查询注意力。
  • 它使用频率和时间感知的缩放 RoPE。

Lumina-T2X:通过基于流的大型扩散变换器将文本转化为任何模态、分辨率和持续时间,来自 Alpha-VLLM、OpenGVLab、上海人工智能实验室。

论文摘要如下:

Sora 揭示了扩展扩散变换器在生成任意分辨率、宽高比和持续时间的光学真实图像和视频方面的潜力,但它仍然缺乏足够的实现细节。在本技术报告中,我们介绍了 Lumina-T2X 系列——一个配备零初始化注意力的基于流的大型扩散变换器 (Flag-DiT) 系列,作为一个统一框架,旨在将噪声转化为图像、视频、多视图 3D 对象和音频剪辑,并以文本指令为条件。通过对潜在时空进行标记化并结合可学习的占位符,例如 [nextline] 和 [nextframe] 标记,Lumina-T2X 无缝地统一了不同模态在各种时空分辨率上的表示。这种统一的方法使得可以在单个框架内训练不同模态,并允许在推理过程中灵活生成任何分辨率、宽高比和长度的多模态数据。RoPE、RMSNorm 和流匹配等先进技术增强了 Flag-DiT 的稳定性、灵活性和可伸缩性,使 Lumina-T2X 的模型能够扩展到 70 亿个参数,并将上下文窗口扩展到 128K 标记。这对于使用我们的 Lumina-T2I 模型创建超高清图像以及使用我们的 Lumina-T2V 模型创建长 720p 视频特别有益。值得注意的是,由 50 亿参数 Flag-DiT 提供支持的 Lumina-T2I 仅需要 6 亿参数朴素 DiT 35% 的训练计算成本。我们进一步的全面分析强调了 Lumina-T2X 在分辨率外推、高分辨率编辑、生成一致 3D 视图和合成具有无缝过渡的视频方面的初步能力。我们期望 Lumina-T2X 的开源将进一步促进生成式 AI 社区的创造力、透明度和多样性。

您可以在 Alpha-VLLM 找到原始代码库,在 Alpha-VLLM Lumina Family 找到所有可用的检查点。

亮点:Lumina-T2X 支持任何模态、分辨率和持续时间。

Lumina-T2X 具有以下组件:

  • 它使用基于流的大型扩散变换器作为骨干
  • 它支持具有一个骨干和相应编码器、解码器的不同模态。

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

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

推理(文本到图像)

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

首先,加载管道

from diffusers import LuminaPipeline
import torch

pipeline = LuminaPipeline.from_pretrained(
	"Alpha-VLLM/Lumina-Next-SFT-diffusers", torch_dtype=torch.bfloat16
).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, mode="max-autotune", fullgraph=True)
pipeline.vae.decode = torch.compile(pipeline.vae.decode, mode="max-autotune", fullgraph=True)

image = pipeline(prompt="Upper body of a young woman in a Victorian-era outfit with brass goggles and leather straps. Background shows an industrial revolution cityscape with smoky skies and tall, metal structures").images[0]

量化

量化有助于通过以较低精度数据类型存储模型权重来减少大型模型的内存需求。但是,量化对视频质量的影响可能因视频模型而异。

请参阅量化概述,了解更多支持的量化后端以及如何选择适合您用例的量化后端。以下示例演示如何加载量化的 LuminaPipeline 以使用 bitsandbytes 进行推理。

import torch
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, Transformer2DModel, LuminaPipeline
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, T5EncoderModel

quant_config = BitsAndBytesConfig(load_in_8bit=True)
text_encoder_8bit = T5EncoderModel.from_pretrained(
    "Alpha-VLLM/Lumina-Next-SFT-diffusers",
    subfolder="text_encoder",
    quantization_config=quant_config,
    torch_dtype=torch.float16,
)

quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = Transformer2DModel.from_pretrained(
    "Alpha-VLLM/Lumina-Next-SFT-diffusers",
    subfolder="transformer",
    quantization_config=quant_config,
    torch_dtype=torch.float16,
)

pipeline = LuminaPipeline.from_pretrained(
    "Alpha-VLLM/Lumina-Next-SFT-diffusers",
    text_encoder=text_encoder_8bit,
    transformer=transformer_8bit,
    torch_dtype=torch.float16,
    device_map="balanced",
)

prompt = "a tiny astronaut hatching from an egg on the moon"
image = pipeline(prompt).images[0]
image.save("lumina.png")

LuminaPipeline

class diffusers.LuminaPipeline

< >

( transformer: LuminaNextDiT2DModel scheduler: FlowMatchEulerDiscreteScheduler vae: AutoencoderKL text_encoder: GemmaPreTrainedModel tokenizer: typing.Union[transformers.models.gemma.tokenization_gemma.GemmaTokenizer, transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast] )

参数

  • vae (AutoencoderKL) — 用于将图像编码和解码为潜在表示的变分自编码器 (VAE) 模型。
  • text_encoder (GemmaPreTrainedModel) — 冻结的 Gemma 文本编码器。
  • tokenizer (GemmaTokenizerGemmaTokenizerFast) — Gemma 分词器。
  • transformer (Transformer2DModel) — 用于对编码图像潜变量进行去噪的文本条件 Transformer2DModel
  • scheduler (SchedulerMixin) — 用于与 transformer 结合以对编码图像潜变量进行去噪的调度器。

使用 Lumina-T2I 进行文本到图像生成的管道。

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

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None width: typing.Optional[int] = None height: typing.Optional[int] = None num_inference_steps: int = 30 guidance_scale: float = 4.0 negative_prompt: typing.Union[str, typing.List[str]] = None sigmas: typing.List[float] = None num_images_per_prompt: typing.Optional[int] = 1 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 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 output_type: typing.Optional[str] = 'pil' return_dict: bool = True clean_caption: bool = True max_sequence_length: int = 256 scaling_watershed: typing.Optional[float] = 1.0 proportional_attn: typing.Optional[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'] ) ImagePipelineOutputtuple

参数

  • prompt (strList[str], 可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须传递 prompt_embeds
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示词或提示词列表。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时(即,如果 guidance_scale 小于 1 则忽略),此参数将被忽略。
  • num_inference_steps (int, 可选, 默认为 30) — 去噪步骤的数量。更多的去噪步骤通常会生成更高质量的图像,但推理速度会变慢。
  • sigmas (List[float], 可选) — 用于去噪过程的自定义 sigmas,适用于其 set_timesteps 方法支持 sigmas 参数的调度器。如果未定义,将使用传递 num_inference_steps 时的默认行为。
  • guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导比例。guidance_scale 定义为 Imagen Paper 方程 2 中的 w。通过设置 guidance_scale > 1 启用引导比例。较高的引导比例鼓励生成与文本 prompt 紧密相关的图像,通常以牺牲较低图像质量为代价。
  • 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.Tensor, 可选) — 预先生成的噪声潜在变量,从高斯分布中采样,用作图像生成的输入。可用于通过不同的提示调整相同的生成。如果未提供,将使用提供的随机 generator 采样生成潜在变量张量。
  • prompt_embeds (torch.Tensor, 可选) — 预先生成的文本嵌入。可用于轻松调整文本输入,例如提示加权。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • prompt_attention_mask (torch.Tensor, 可选) — 文本嵌入的预生成注意力掩码。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预先生成的负面文本嵌入。对于 Lumina-T2I,此负面提示应为""。如果未提供,负面提示嵌入将从 negative_prompt 输入参数生成。
  • negative_prompt_attention_mask (torch.Tensor, 可选) — 负面文本嵌入的预生成注意力掩码。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。在 PIL: PIL.Image.Imagenp.array 之间选择。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ~pipelines.stable_diffusion.IFPipelineOutput 而不是普通元组。
  • clean_caption (bool, 可选, 默认为 True) — 在创建嵌入之前是否清理标题。需要安装 beautifulsoup4ftfy。如果未安装依赖项,嵌入将从原始提示创建。
  • max_sequence_length (int 默认为 120) — 用于 prompt 的最大序列长度。
  • 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 属性中列出的变量。

返回

ImagePipelineOutputtuple

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

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

示例

>>> import torch
>>> from diffusers import LuminaPipeline

>>> pipe = LuminaPipeline.from_pretrained("Alpha-VLLM/Lumina-Next-SFT-diffusers", torch_dtype=torch.bfloat16)
>>> # Enable memory optimizations.
>>> pipe.enable_model_cpu_offload()

>>> prompt = "Upper body of a young woman in a Victorian-era outfit with brass goggles and leather straps. Background shows an industrial revolution cityscape with smoky skies and tall, metal structures"
>>> image = pipe(prompt).images[0]

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] do_classifier_free_guidance: bool = True negative_prompt: typing.Union[str, typing.List[str]] = None num_images_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 **kwargs )

参数

  • prompt (strList[str], 可选) — 要编码的提示。
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时(即,如果 guidance_scale 小于 1 时),将被忽略。对于 Lumina-T2I,这应为""。
  • do_classifier_free_guidance (bool, 可选, 默认为 True) — 是否使用无分类器引导。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示应生成的图像数量。
  • device — (torch.device, 可选): 将生成的嵌入放置在哪个 torch 设备上。
  • prompt_embeds (torch.Tensor, 可选) — 预先生成的文本嵌入。可用于轻松调整文本输入,例如提示加权。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预先生成的负面文本嵌入。对于 Lumina-T2I,它应该是""字符串的嵌入。
  • clean_caption (bool, 默认为 False) — 如果为 True,函数将在编码前预处理和清理提供的标题。
  • max_sequence_length (int, 默认为 256) — 用于提示的最大序列长度。

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

< > 在 GitHub 上更新