欢迎 Stable Diffusion 3.5 Large 加入 Diffusers

news/2025/3/13 10:40:39/文章来源:https://www.cnblogs.com/huggingface/p/18532238

作为 Stable Diffusion 3 的改进版本,Stable Diffusion 3.5 如今已在 Hugging Face Hub 中可用,并可以直接使用 🧨 Diffusers 中的代码运行。

本次发布包含 两套模型参数:

  • 一个大型的模型 (large,8B)
  • 该模型经过时间步蒸馏的版本,仅需几步推理即可生成图片

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

目录

  • 模型结构改进
  • 在 Diffusers 中使用 SD3.5
  • 在推理过程中使用量化策略
  • 在 SD3.5-large 上使用量化策略训练 LoRA
  • 使用 single-file 方法加载 SD3.5 的 Transformer 模型
  • 重要链接

模型结构改进

对于 SD3.5-large 使用的 transformer 模型,其结构基本和 SD3-medium 里的相同,但有以下更改:

  • QK normalization: 对于训练大型的 Transformer 模型,使用 QK normalization 已经成为标准做法,所以 SD3.5-large 也不例外。
  • 双注意力层: 在 MMDiT 结构中,文本和图像两个模态都在使用同一个注意力层; 而 SD3.5-large 则使用了两个注意力层。

除此之外,文本编码器 (text encoder)、图像的变分自编码器 (VAE) 以及噪声调度器 (noise scheduler) 均和 SD3-medium 保持一致。如果对 SD3 感兴趣,可以参考 这篇论文。

在 Diffusers 中使用 SD3.5

首先你需要确保安装的 Diffusers 是最新版本:

pip install -U diffusers

由于模型存在访问限制,你还需要到 Hugging Face 上 Stable Diffusion 3.5 Large 的页面 填写表格并同意相关条款。完成后你还需要登陆账号,才能访问到模型。使用如下方法登陆 Hugging Face 账号:

huggingface-cli login

下列代码将下载 SD3.5 的 8B 模型。下载的模型使用 torch.bfloat16 精度,这是 Stability AI 的原版格式,也推荐使用该精度进行推理。

import torch
from diffusers import StableDiffusion3Pipelinepipe = 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

本次发布也包含了一个 “时间步蒸馏” 的模型,该模型推理时无需 classifier-free guidance,可在短短几步推理内生成图片 (通常是 4 到 8 步)。

import torch
from diffusers import StableDiffusion3Pipelinepipe = 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”精度 的模型:

from diffusers import BitsAndBytesConfig, SD3Transformer2DModel
import torchmodel_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 StableDiffusion3Pipelinepipeline = 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 配置的已量化模型也是可以的,这对 RAM 较低的机器来说非常实用,读者可以在 这里的 Colab Notebook 来获取完整示例。

在 SD3.5-large 上使用量化策略训练 LoRA

借助 bitsandbytespeft ,我们可以在消费级显卡 (24GB 显存) 上微调像 SD3.5 这样的大模型。我们提供的 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() 函数对模型进行准备操作。
  • 其它步骤和原代码保持一致即可 (感谢 peftbitsandbytes 的强力支持)。

读者可参考 这里 的完整示例。

使用 single-file 方法加载 SD3.5 的 Transformer 模型

Stable Diffusion 3.5 的 transformer 模型还可以使用 Stability AI 发布的原生参数文件来进行初始化 。 这里需要使用 from_single_file 方法:

import torch
from diffusers import SD3Transformer2DModel, StableDiffusion3Pipelinetransformer = 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")

重要链接

  • SD3.5-large 在 Hugging Face Hub 上的 模型集合
  • Diffusers 中 SD3.5 的 官方文档
  • 用来运行 SD3.5 量化推理的 Colab Notebook
  • LoRA 训练代码
  • Stable Diffusion 3 官方论文
  • Stable Diffusion 3 中文博客

声明: 感谢 Daniel Frank 为本博客提供了封面图,感谢 Pedro Cuenca 和 Tom Aarsen 对本文的审校。


英文原文: https://hf.co/blog/sd3-5

原文作者: YiYi Xu, Aryan V S, Dhruv Nair, Sayak Paul, Linoy Tsaban, Apolinário from multimodal AI art, Alvaro Somoza, Aritra Roy Gosthipaty

译者: hugging-hoi2022

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/828304.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IDEA远程运行使用rsync异常:rsync: connection unexpectedly closed (0 bytes received so far) [sender]

前提:已经使用了Cygwin64且IDEA版本2023、2024均有这个问题发生这个问题可能是使用的是windows自带的C:\Windows\System32\OpenSSH\ssh.exe 安装Cygwin64时,需要同时选择安装rsync和ssh。第一次安装没选也没关系,可以再次运行安装程序安装,不用删除上一次的安装。或者使用C…

代码随想录一刷day7 哈希表day1

遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法。 但是哈希法也是牺牲了空间换取了时间,因为我们要使用额外的数组,set或者是map来存放数据,才能实现快速的查找。 常见三种实现哈希表的数据结构:数组 set集合 map映射 下面是set map的红黑树是一种平衡二叉…

SpringCloud入门到高级

服务注册与发现 服务调用和负载均衡(LoadBalancer/OpenFeign) LoadBalancer 案例:80通过轮询负载访问8001/8002/8003LoadBalancer 在工作时分成两步: 第一步,先选择ConsulServer从服务端查询并拉取服务列表,知道了它有多个服务(上图3个服务),这3个实现是完全一样的, 默认…

探索高效项目管理新境界:项目管理应用深度解析

在当今这个快节奏、高效率的时代,项目管理已成为企业成功的关键要素之一。无论是初创公司还是大型企业,都需要借助高效的项目管理工具来确保项目按时、按质、按量完成。今天,我们将一起探索几款备受推崇的项目管理应用,它们各自拥有独特的优势和功能,旨在帮助团队提升协作…

Wgpu图文详解(02)渲染管线与着色器

在本系列的第一篇文章中(《Wgpu图文详解(01)窗口与基本渲染》),我们介绍了如何基于0.30+版本的winit搭建Wgpu的桌面环境,同时也讲解了关于Wgpu一些基本的概念、模块以及架构思路,并基于wgpu库实现了一个能展示有颜色背景的窗体。而在本篇文章中,我们将开始介绍Wgpu中的…

ES IK分词器配置本地词典 远程词典

修改IK分词器配置文件路径一般在:xxx/plugins/xxx/config/IKAnalyzer.cfg.xml 配置本地词典本地词典,放到同级目录下,重启ES服务即可;配置远程词典新建springboot工程,将文件放到statis目录下;远程词典,放到其他共享地址也可以哈~,没必要非得springboot工程中修改配置配…

(触摸屏cMT2079x + 路由器DAYUA-BE30) 实现PC,手机,平板端的设备监控

1, 华为路由器设置 2, 威纶通触摸屏cMT2079x设置 (1)网络连接, 可以连接LAN1, 也可以连接LAN2, 同网段即可;

移远EC200U-EU欧洲版 CAT1模组使用

1. 外观如下,CAT1模组,主要是用来做一个欧洲的电压检测设备

UE5 GameFeature示例项目《古代山谷(Valley of the Ancient)》中Action_SpawnActor无法动态拔插的问题

前因 最近在研究GameFeature这个功能,UE官方推荐的是《古代山谷》这个项目,因为在其中用到了很多的新功能,GameFeature也在其中。观察到原来的GameFeature默认提供的action中其实功能比较有限,于是就从《古代山谷》拷贝了一些Action来用,结果Action_SpawnActor并无法在我自…

Java并发编程 --- 线程安全

为什么会有线程安全问题? 为什么会存在线程安全问题呢?那我们先来探究一下JMM(Java内存模型)线程与JMM 每个线程都有自己的工作内存,它会存储主内存中变量的Copy值,再对变量进行操作的时候,也是操作工作内存中变量的Copy值。 当线程Dead(生命周期结束)时,才会将自己工作内…

WEB_方案查询F7的类型设置为F7某个字段的查询

如下图,在方案查询条件中,【票据号码】与【软通票据】在单据上其实都是F7字段,但是票据号码在这里是字符串查询,而软通票据是F7的样式,这是怎么样将F7的字段查询弄成文本框查询的呢,实际上是通过修改单据列表的query里的属性来实现的,具体修改如下:如果选择的使用F7,则…