Diffusers documentation

Lumina-T2X

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

Lumina-T2X

concepts

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

该论文的摘要如下:

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

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

Lumina-Next 包含以下组件:

  • 它通过减少步数并加快每步速度来提升采样效率。
  • 它使用 Next-DiT 作为 Transformer 骨干网络,并采用 Sandwichnorm 3D RoPE 和 Grouped-Query Attention。
  • 它使用了频率和时间感知的缩放 RoPE。

来自 Alpha-VLLM、OpenGVLab 和上海人工智能实验室的 Lumina-T2X:基于流的大型扩散 Transformer,将文本转换为任意模态、分辨率和时长。

该论文的摘要如下:

Sora 展示了 Diffusion Transformer 在生成任意分辨率、宽高比和时长的逼真图像和视频方面的潜力,但其实现细节仍不够充分。在本技术报告中,我们介绍了 Lumina-T2X 系列——一系列配备零初始化注意力的基于 Flow 的大型 Diffusion Transformer(Flag-DiT),作为一个统一的框架,旨在根据文本指令将噪声转化为图像、视频、多视角 3D 对象和音频片段。通过对潜在时空空间进行分词,并引入诸如 [nextline] 和 [nextframe] 等可学习占位符,Lumina-T2X 无缝统一了不同模态在各种时空分辨率下的表示。这种统一方法使得可以在单一框架内针对不同模态进行训练,并允许在推理时以任意分辨率、宽高比和长度灵活生成多模态数据。RoPE、RMSNorm 和 flow matching 等先进技术增强了 Flag-DiT 的稳定性、灵活性和可扩展性,使 Lumina-T2X 的模型能够扩展至 70 亿参数,并将上下文窗口扩展至 128K 个 token。这对于使用我们的 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 包含以下组件:

  • 它使用基于 Flow 的大型扩散 Transformer 作为骨干网络
  • 它支持使用一个骨干网络及相应的编码器、解码器来处理不同的任意模态。

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

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

推理(文本到图像)

使用 torch.compile 来降低推理延迟。

首先,加载 pipeline:

from diffusers import LuminaPipeline
import torch

pipeline = LuminaPipeline.from_pretrained(
	"Alpha-VLLM/Lumina-Next-SFT-diffusers", dtype=torch.bfloat16
).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, 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]

量化

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

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

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,
    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,
    dtype=torch.float16,
)

pipeline = LuminaPipeline.from_pretrained(
    "Alpha-VLLM/Lumina-Next-SFT-diffusers",
    text_encoder=text_encoder_8bit,
    transformer=transformer_8bit,
    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: LuminaNextDiT2DModelscheduler: FlowMatchEulerDiscreteSchedulervae: AutoencoderKLtext_encoder: GemmaPreTrainedModeltokenizer: GemmaTokenizer )

参数

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

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

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

__call__

< >

( prompt: str | list[str] = Nonewidth: int | None = Noneheight: int | None = Nonenum_inference_steps: int = 30guidance_scale: float = 4.0negative_prompt: str | list[str] = Nonesigmas: list = Nonenum_images_per_prompt: int | None = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = 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] = Noneoutput_type: str | None = 'pil'return_dict: bool = Trueclean_caption: bool = Truemax_sequence_length: int = 256scaling_watershed: float | None = 1.0proportional_attn: bool | None = 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'] ) → ImagePipelineOutput or tuple

参数

  • prompt (str 或 list[str],可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须改为传入 prompt_embeds。
  • negative_prompt (str 或 list[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) — 生成图像的宽度(以像素为单位)。
  • 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, 可选) — 预生成的负向文本嵌入。对于 Lumina-T2I,此负向提示应为 ""。如果未提供,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 而不是普通元组。
  • clean_caption (bool,可选,默认为 True) — 是否在创建嵌入之前清理字幕。需要安装 beautifulsoup4 和 ftfy。如果未安装这些依赖项,嵌入将直接从原始提示创建。
  • max_sequence_length (int,默认为 120) — 与 prompt 配合使用的最大序列长度。
  • scaling_watershed (float, 可选,默认值为 1.0) — Lumina 用于在标准注意力与扩展上下文注意力之间切换的分辨率缩放阈值。
  • proportional_attn (bool,可选,默认为 True) — 是否按比例缩放注意力机制,以用于高分辨率生成。
  • 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 属性中列出的变量。

返回值

ImagePipelineOutput 或 tuple

如果 return_dict 为 True,则返回 ImagePipelineOutput;否则返回一个 tuple,其中第一个元素是包含生成图像的列表

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

示例:

>>> 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: str | list[str]do_classifier_free_guidance: bool = Truenegative_prompt: str | list[str] = Nonenum_images_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 = False**kwargs )

参数

  • prompt (str 或 list[str],可选) — 要编码的提示
  • negative_prompt (str 或 list[str],可选) — 用于不引导图像生成的提示词。如果未定义,则必须改为传入 negative_prompt_embeds。在未使用引导时会被忽略(即当 guidance_scale 小于 1 时会被忽略)。对于 Lumina-T2I,此值应为 ""。
  • do_classifier_free_guidance (bool,可选,默认为 True) — 是否使用 classifier free guidance
  • 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,该函数将在编码之前对提供的 caption 进行预处理和清理。
  • max_sequence_length (int,默认为 256) — 用于提示词的最大序列长度。

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

Update on GitHub