Diffusers 文档

Lumina-T2X

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Lumina-T2X

concepts

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

论文摘要如下

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

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

Lumina-Next 具有以下组件

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

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

论文摘要如下

Sora 揭示了扩展扩散 Transformer 在任意分辨率、宽高比和持续时间生成照片级真实图像和视频的潜力,但它仍然缺乏足够的实现细节。在本技术报告中,我们介绍了 Lumina-T2X 系列 - 一系列配备零初始化注意力的基于流的大型扩散 Transformer (Flag-DiT),作为一个统一的框架,旨在将噪声转换为图像、视频、多视图 3D 对象和音频片段,并以文本指令为条件。通过 token 化潜在的空间-时间空间并结合可学习的占位符(例如 [nextline] 和 [nextframe] token),Lumina-T2X 无缝地统一了跨各种空间-时间分辨率的不同模态的表示。这种统一的方法使得可以在单个框架内针对不同的模态进行训练,并允许在推理期间灵活生成任何分辨率、宽高比和长度的多模态数据。RoPE、RMSNorm 和流匹配等先进技术增强了 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 具有以下组件

  • 它使用基于流的大型扩散 Transformer 作为主干
  • 它支持使用一个主干和相应的编码器、解码器来处理不同的任何模态。

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

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

推理(文本到图像)

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

首先,加载 pipeline

from diffusers import LuminaText2ImgPipeline
import torch

pipeline = LuminaText2ImgPipeline.from_pretrained(
	"Alpha-VLLM/Lumina-Next-SFT-diffusers", torch_dtype=torch.bfloat16
).to("cuda")

然后将 pipelines 的 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]

LuminaText2ImgPipeline

class diffusers.LuminaText2ImgPipeline

< >

( transformer: LuminaNextDiT2DModel scheduler: FlowMatchEulerDiscreteScheduler vae: AutoencoderKL text_encoder: AutoModel tokenizer: AutoTokenizer )

Parameters

  • vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将图像编码和解码为潜在表示。
  • text_encoder (AutoModel) — 冻结的文本编码器。Lumina-T2I 使用 T5,特别是 t5-v1_1-xxl 变体。
  • tokenizer (AutoModel) — AutoModel 类的分词器。
  • transformer (Transformer2DModel) — 一个文本条件 Transformer2DModel,用于对编码的图像潜在空间进行去噪。
  • scheduler (SchedulerMixin) — 一个调度器,与 transformer 结合使用,以对编码的图像潜在空间进行去噪。

Pipeline for text-to-image generation using Lumina-T2I.

This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)

__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 ) ImagePipelineOutput or tuple

Parameters

  • prompt (strList[str], 可选) — 用于引导图像生成的提示或提示列表。如果未定义,则必须传递 prompt_embeds 代替。
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传递 negative_prompt_embeds 代替。当不使用 guidance 时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • num_inference_steps (int, 可选, 默认为 30) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价,产生更高质量的图像。
  • sigmas (List[float], 可选) — 用于去噪过程的自定义 sigmas,适用于在其 set_timesteps 方法中支持 sigmas 参数的调度器。如果未定义,则将使用传递 num_inference_steps 时的默认行为。
  • guidance_scale (float, 可选, 默认为 4.0) — Guidance scale,如 Classifier-Free Diffusion Guidance 中定义。 guidance_scale 定义为 Imagen Paper 的等式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,但通常以较低的图像质量为代价。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个 prompt 生成的图像数量。
  • height (int, 可选, 默认为 self.unet.config.sample_size) — 生成图像的高度,以像素为单位。
  • width (int, 可选, 默认为 self.unet.config.sample_size) — 生成图像的宽度,以像素为单位。
  • eta (float, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η):https://arxiv.org/abs/2010.02502。仅适用于 schedulers.DDIMScheduler,对于其他调度器将被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成具有确定性的一个或一组 torch 生成器。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜在空间,从高斯分布中采样,用作图像生成的输入。可用于使用不同的 prompt 微调相同的生成。如果未提供,将通过使用提供的随机 generator 进行采样来生成潜在空间张量。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 prompt 加权。如果未提供,将从 prompt 输入参数生成文本嵌入。
  • prompt_attention_mask (torch.Tensor, 可选) — 文本嵌入的预生成注意力掩码。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负文本嵌入。对于 Lumina-T2I,此负面提示应为 ""。如果未提供,将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • 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。如果未安装依赖项,则将从原始 prompt 创建嵌入。
  • max_sequence_length (int 默认为 120) — 与 prompt 一起使用的最大序列长度。
  • callback_on_step_end (Callable, optional) — 一个函数,它在推理过程的每个去噪步骤结束时被调用。该函数被调用时会传入以下参数: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, optional) — callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。您将只能包含管道类的 ._callback_tensor_inputs 属性中列出的变量。

返回

ImagePipelineOutputtuple

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

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

示例

>>> import torch
>>> from diffusers import LuminaText2ImgPipeline

>>> pipe = LuminaText2ImgPipeline.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 )

Parameters

  • 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 上更新