Diffusers 文档

Lumina2

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Lumina2

LoRA

Lumina Image 2.0: 一个统一高效的图像生成模型是一个20亿参数的基于流的扩散Transformer,能够从文本描述中生成多样化的图像。

论文摘要如下:

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

请务必查看调度器指南,了解如何权衡调度器速度和质量,并参阅跨管道复用组件部分,了解如何高效地将相同组件加载到多个管道中。

Lumina Image 2.0 的单文件加载

Lumina Image 2.0 的单文件加载适用于 Lumina2Transformer2DModel

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, torch_dtype=torch.bfloat16
)

pipe = Lumina2Pipeline.from_pretrained(
    "Alpha-VLLM/Lumina-Image-2.0", transformer=transformer, torch_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")

Lumina Image 2.0 使用 GGUF 量化检查点

Lumina2Transformer2DModel 的 GGUF 量化检查点可以通过 from_single_fileGGUFQuantizationConfig 加载

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),
    torch_dtype=torch.bfloat16,
)

pipe = Lumina2Pipeline.from_pretrained(
    "Alpha-VLLM/Lumina-Image-2.0", transformer=transformer, torch_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

class diffusers.Lumina2Pipeline

< >

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

参数

  • vae (AutoencoderKL) — 用于将图像编码和解码为潜在表示的变分自编码器 (VAE) 模型。
  • text_encoder (Gemma2PreTrainedModel) — 冻结的 Gemma2 文本编码器。
  • 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 attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] system_prompt: typing.Optional[str] = None cfg_trunc_ratio: float = 1.0 cfg_normalization: bool = True max_sequence_length: int = 256 ) 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) — 如无分类器扩散引导中所定义的引导比例。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_embeds。
  • negative_prompt_attention_mask (torch.Tensor, 可选) — 负面文本嵌入的预生成注意力掩码。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。选择 PIL: PIL.Image.Imagenp.array
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ~pipelines.stable_diffusion.IFPipelineOutput 而不是普通元组。
  • attention_kwargs — 一个 kwargs 字典,如果指定,将作为参数传递给 diffusers.models.attention_processor 中定义的 self.processor 下的 AttentionProcessor
  • 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) — 应用基于归一化的指导尺度的步长间隔比率。
  • cfg_normalization (bool, 可选, 默认为 True) — 是否应用基于归一化的指导尺度。
  • max_sequence_length (int, 默认为 256) — 与 prompt 一起使用的最大序列长度。

返回

ImagePipelineOutputtuple

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

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

示例

>>> 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]

disable_vae_slicing

< >

( )

禁用切片 VAE 解码。如果之前启用了 enable_vae_slicing,此方法将返回一步计算解码。

disable_vae_tiling

< >

( )

禁用平铺 VAE 解码。如果之前启用了 enable_vae_tiling,此方法将恢复一步计算解码。

enable_vae_slicing

< >

( )

启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。

enable_vae_tiling

< >

( )

启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。

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 system_prompt: typing.Optional[str] = None max_sequence_length: int = 256 )

参数

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

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

< > 在 GitHub 上更新