跳到正文
Hugging Face Blog·· 2024-10-22精选AI 评分62

Diffusers 支持 Stable Diffusion 3.5 Large,含 8B 与 timestep-distilled 两个 checkpoint

Diffusers welcomes Stable Diffusion 3.5 Large

AI 导读

Stable Diffusion 3.5 已在 Hugging Face Hub 上线并可通过 Diffusers 使用,发布包含 8B 的 Large 模型和 8B 的 timestep-distilled 模型,后者支持 4-8 步少步推理。

推荐理由

Stable Diffusion 3.5 Large 已在 Hub 上线并接入 Diffusers,文中给出量化推理与 LoRA 训练的完整代码路径。

正文 · AI 翻译

Stable Diffusion 3.5 是其前身 Stable Diffusion 3 的改进版本。 截至目前,这些模型已在 Hugging Face Hub 上提供,并可与 🧨 Diffusers 一起使用。

此次发布包含 两个检查点:

  • 大型(8B)模型
  • 大型(8B)时间步蒸馏模型,支持少步推理

在本文中,我们将重点介绍如何将 Stable Diffusion 3.5(SD3.5)与 Diffusers 结合使用,涵盖推理和训练两方面。

目录

架构变更

SD3.5(大型)的 Transformer 架构与 SD3(中型)非常相似,但有以下变化:

  • QK 归一化:对于训练大型 Transformer 模型,QK 归一化 现已成为标准,SD3.5 Large 也不例外。
  • 双重注意力层:SD3.5 不再为 MMDiT 块中的每种模态流使用单一注意力层,而是使用双重注意力层。

文本编码器、VAE 和噪声调度器方面的其余细节与 SD3 Medium 完全相同。有关 SD3 的更多信息,我们建议查看 原始论文。

使用 Diffusers 运行 SD3.5

请确保安装最新版本的 diffusers:

pip install -U diffusers

由于该模型受门控限制,在使用 diffusers 之前,您首先需要访问 Stable Diffusion 3.5 Large Hugging Face 页面,填写表格并接受门控。 进入后,您需要登录,以便系统知道您已接受门控。使用以下命令登录:

huggingface-cli login

以下代码片段将下载 torch.bfloat16 精度的 8B 参数版本 SD3.5。 这是 Stability AI 发布的原始检查点所使用的格式,也是推荐的推理运行方式。

import torch
from diffusers import StableDiffusion3Pipeline

pipe = StableDiffusion3Pipeline.from_pretrained(
    "stabilityai/stable-diffusion-3.5-large", torch_dtype=torch.bfloat16
).to("cuda")

image = pipe(
    prompt="a photo of a cat holding a sign that says hello world",
    negative_prompt="",
    num_inference_steps=40,
    height=1024,
    width=1024,
    guidance_scale=4.5,
).images[0]

image.save("sd3_hello_world.png")

hello_world_cat

此次发布还包含一个“时间步蒸馏”模型,它消除了无分类器引导,让我们能在更少的步骤内生成图像(通常为 4-8 步)。

import torch
from diffusers import StableDiffusion3Pipeline

pipe = StableDiffusion3Pipeline.from_pretrained(
    "stabilityai/stable-diffusion-3.5-large-turbo", torch_dtype=torch.bfloat16
).to("cuda")

image = pipe(
    prompt="a photo of a cat holding a sign that says hello world",
    num_inference_steps=4,
    height=1024,
    width=1024,
    guidance_scale=1.0,
).images[0]

image.save("sd3_hello_world.png")

hello_world_cat_2

我们在 SD3 博客文章 和 官方 Diffusers 文档 中展示的所有示例应该已经可以在 SD3.5 上运行。 特别是,这两份资源都深入探讨了优化运行推理所需内存的方法。 由于 SD3.5 Large 比 SD3 Medium 大得多,内存优化对于在消费级设备上运行推理变得至关重要。

使用量化运行推理

Diffusers 原生支持 bitsandbytes 量化,这可以进一步优化内存。

首先,确保安装所有必要的库:

pip install -Uq git+https://github.com/huggingface/transformers@main
pip install -Uq bitsandbytes

然后以 “NF4”精度 加载 Transformer:

from diffusers import BitsAndBytesConfig, SD3Transformer2DModel
import torch

model_id = "stabilityai/stable-diffusion-3.5-large"
nf4_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)
model_nf4 = SD3Transformer2DModel.from_pretrained(
    model_id,
    subfolder="transformer",
    quantization_config=nf4_config,
    torch_dtype=torch.bfloat16
)

现在,我们准备运行推理:

from diffusers import StableDiffusion3Pipeline

pipeline = StableDiffusion3Pipeline.from_pretrained(
    model_id, 
    transformer=model_nf4,
    torch_dtype=torch.bfloat16
)
pipeline.enable_model_cpu_offload()

prompt = "A whimsical and creative image depicting a hybrid creature that is a mix of a waffle and a hippopotamus, basking in a river of melted butter amidst a breakfast-themed landscape. It features the distinctive, bulky body shape of a hippo. However, instead of the usual grey skin, the creature's body resembles a golden-brown, crispy waffle fresh off the griddle. The skin is textured with the familiar grid pattern of a waffle, each square filled with a glistening sheen of syrup. The environment combines the natural habitat of a hippo with elements of a breakfast table setting, a river of warm, melted butter, with oversized utensils or plates peeking out from the lush, pancake-like foliage in the background, a towering pepper mill standing in for a tree.  As the sun rises in this fantastical world, it casts a warm, buttery glow over the scene. The creature, content in its butter river, lets out a yawn. Nearby, a flock of birds take flight"
image = pipeline(
    prompt=prompt,
    negative_prompt="",
    num_inference_steps=28,
    guidance_scale=4.5,
    max_sequence_length=512,
).images[0]
image.save("whimsical.png")

happy_hippo

您可以在 BitsAndBytesConfig 中控制其他参数。有关详细信息,请参阅 文档。

也可以直接加载使用与上述相同 nf4_config 量化的模型。 这对于内存较低的机器特别有用。有关端到端示例,请参阅 此 Colab Notebook。

使用量化训练 SD3.5 Large 的 LoRA

得益于 bitsandbytes 和 peft 等库,可以在拥有 24GB 显存的消费级 GPU 显卡上微调 SD3.5 Large 这样的大型模型。我们已经可以利用现有的 SD3 训练脚本来训练 LoRA。 下面的训练命令已经可以正常工作:

accelerate launch train_dreambooth_lora_sd3.py \
  --pretrained_model_name_or_path="stabilityai/stable-diffusion-3.5-large"  \
  --dataset_name="Norod78/Yarn-art-style" \
  --output_dir="yart_art_sd3-5_lora" \
  --mixed_precision="bf16" \
  --instance_prompt="Frog, yarn art style" \
  --caption_column="text"\
  --resolution=768 \
  --train_batch_size=1 \
  --gradient_accumulation_steps=1 \
  --learning_rate=4e-4 \
  --report_to="wandb" \
  --lr_scheduler="constant" \
  --lr_warmup_steps=0 \
  --max_train_steps=700 \
  --rank=16 \
  --seed="0" \
  --push_to_hub

然而,要使其与量化配合工作,我们需要调整几个参数。下面,我们提供如何操作的指引:

  • 我们使用量化配置来初始化 transformer,或者直接加载一个量化检查点。
  • 然后,我们使用 peft 中的 prepare_model_for_kbit_training() 来准备它。
  • 其余流程保持不变,这得益于 peft 对 bitsandbytes 的强大支持!

请参阅此示例脚本以获取更完整的示例。

使用单文件加载 Stable Diffusion 3.5 Transformer

你可以使用 Stability AI 发布的原始检查点文件,通过 from_single_file 方法加载 Stable Diffusion 3.5 Transformer 模型:

import torch
from diffusers import SD3Transformer2DModel, StableDiffusion3Pipeline

transformer = SD3Transformer2DModel.from_single_file(
    "https://huggingface.co/stabilityai/stable-diffusion-3.5-large-turbo/blob/main/sd3.5_large.safetensors",
    torch_dtype=torch.bfloat16,
)
pipe = StableDiffusion3Pipeline.from_pretrained(
    "stabilityai/stable-diffusion-3.5-large",
    transformer=transformer,
    torch_dtype=torch.bfloat16,
)
pipe.enable_model_cpu_offload()
image = pipe("a cat holding a sign that says hello world").images[0]
image.save("sd35.png") 

重要链接

致谢:Daniel Frank 提供了本博客文章缩略图中使用的背景照片。感谢 Pedro Cuenca 和 Tom Aarsen 对文章草稿的审阅。

来源:Hugging Face Blog · huggingface.co