Diffusers documentation

Lumina2

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

Lumina2

LoRA

Lumina Image 2.0: A Unified and Efficient Image Generative Model 是一个拥有 20 亿参数的基于流的扩散 Transformer,能够根据文本描述生成多样化的图像。

该论文的摘要如下:

我们推出了 Lumina-Image 2.0,这是一款先进的文本生成图像模型,它在多个基准测试中超越了此前的最先进方法,同时也揭示了其演变为通用视觉智能模型的潜力。Lumina-Image 2.0 具备三个关键特性:(1) 统一性——它采用统一架构,将文本和图像标记视为一个联合序列,从而实现自然的跨模态交互并促进任务扩展。此外,由于高质量的描述器能够提供语义对齐更好的文本-图像训练对,我们引入了一个统一的描述系统 UniCaptioner,为模型生成全面且精确的描述。这不仅加速了模型收敛,还通过提示模板增强了提示遵循能力、变长提示处理以及任务泛化能力。(2) 高效性——为了提高统一架构的效率,我们开发了一系列优化技术,在训练过程中改善语义学习和细粒度纹理生成,同时在不牺牲图像质量的前提下引入推理时加速策略。(3) 透明性——我们开源了所有训练细节、代码和模型,以确保完全可复现性,旨在弥合资源充足的闭源研究团队与独立开发者之间的差距。

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

使用单文件加载方式加载 Lumina Image 2.0

Lumina2Transformer2DModel 现已支持 Lumina Image 2.0 的单文件加载

import torch
from diffusers import Lumina2Transformer2DModel, Lumina2Pipeline

ckpt_path = "https://huggingface.co/Alpha-VLLM/Lumina-Image-2.0/blob/main/consolidated.00-of-01.pth"
transformer = Lumina2Transformer2DModel.from_single_file(
    ckpt_path, dtype=torch.bfloat16
)

pipe = Lumina2Pipeline.from_pretrained(
    "Alpha-VLLM/Lumina-Image-2.0", transformer=transformer, dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()
image = pipe(
    "a cat holding a sign that says hello",
    generator=torch.Generator("cpu").manual_seed(0),
).images[0]
image.save("lumina-single-file.png")

使用 GGUF 量化检查点与 Lumina Image 2.0

Lumina2Transformer2DModel 的 GGUF 量化检查点可以通过 from_single_file 配合 GGUFQuantizationConfig 进行加载。

from diffusers import Lumina2Transformer2DModel, Lumina2Pipeline, GGUFQuantizationConfig 

ckpt_path = "https://huggingface.co/calcuis/lumina-gguf/blob/main/lumina2-q4_0.gguf"
transformer = Lumina2Transformer2DModel.from_single_file(
    ckpt_path,
    quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
    dtype=torch.bfloat16,
)

pipe = Lumina2Pipeline.from_pretrained(
    "Alpha-VLLM/Lumina-Image-2.0", transformer=transformer, dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()
image = pipe(
    "a cat holding a sign that says hello",
    generator=torch.Generator("cpu").manual_seed(0),
).images[0]
image.save("lumina-gguf.png")

Lumina2Pipeline

类 diffusers.Lumina2Pipeline

< >

( transformer: Lumina2Transformer2DModelscheduler: FlowMatchEulerDiscreteSchedulervae: AutoencoderKLtext_encoder: Gemma2PreTrainedModeltokenizer: GemmaTokenizer )

参数

  • vae (AutoencoderKL) — 变分自编码器(VAE)模型,用于将图像编码为潜在表示,以及从潜在表示解码回图像。
  • text_encoder (Gemma2PreTrainedModel) — 冻结的 Gemma2 文本编码器。
  • 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 = Trueattention_kwargs: dict[str, typing.Any] | None = Nonecallback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents']system_prompt: str | None = Nonecfg_trunc_ratio: float = 1.0cfg_normalization: bool = Truemax_sequence_length: int = 256 ) → 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 而不是普通元组。
  • attention_kwargs — 一个 kwargs 字典,如果指定,将传递给 AttentionProcessor,该处理器在 diffusers.models.attention_processor 中定义,并位于 self.processor 下。
  • 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 属性中列出的变量。
  • system_prompt (str, 可选) — 用于图像生成的系统提示词。
  • cfg_trunc_ratio (float,可选,默认为 1.0) — 应用基于归一化的 guidance scale 的时间步区间比例。
  • cfg_normalization (bool,可选,默认为 True) — 是否应用基于归一化的引导尺度。
  • max_sequence_length (int,默认为 256) — 与 prompt 配合使用的最大序列长度。

返回值

ImagePipelineOutput 或 tuple

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

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

示例:

>>> import torch
>>> from diffusers import Lumina2Pipeline

>>> pipe = Lumina2Pipeline.from_pretrained("Alpha-VLLM/Lumina-Image-2.0", 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] = Nonesystem_prompt: str | None = Nonemax_sequence_length: int = 256 )

参数

  • 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,它应该是空字符串 "" 的嵌入。
  • max_sequence_length (int,默认为 256) — 用于提示词的最大序列长度。

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

Update on GitHub