Black Forest Labs 的 FLUX.3 Image Klein 来了,而且第一天就能在 Diffusers 中运行。同一个模型既能做文生图,也能做多参考图的图生图,还新增了一项能力:布局控制。你可以把边界框(bounding box)直接写进提示词,指定图像各部分应该出现的位置,或者参考图中的哪些部分需要移动。
两个开放检查点都在 FLUX.3 Image Klein 合集中:
| 检查点 | 说明 | 默认设置 |
|---|---|---|
black-forest-labs/flux-3-image-klein |
时间步蒸馏版本,推理速度快 | 16 步,无 CFG |
black-forest-labs/flux-3-image-klein-base |
基础模型 | 50 步,使用 CFG |

你现在就可以前往 Hugging Face Space 生成一些图片!
太长不看
- 一个模型同时支持文生图、多参考图的图生图,以及边界框布局控制。
- 蒸馏版检查点在一块 H100 上生成一张 1024 × 1024 的图像只需约 2 秒。
- NVFP4 和 FP8 权重可通过
from_single_file加载,适合显存较小的 GPU。 - 提供文生图和图像编辑两种任务的 LoRA 训练脚本。
- 以 Modular Diffusers 流水线的形式发布,你可以随意拆下其中的模块,或加入自己的模块。
目录
快速上手
安装最新版的 diffusers 和 transformers(用于 Qwen3-VL 文本编码器):
pip install -U diffusers transformers
加载蒸馏版检查点并生成一张图像:
import torch
from diffusers import ModularPipeline
pipe = ModularPipeline.from_pretrained("black-forest-labs/flux-3-image-klein")
pipe.load_components(dtype=torch.bfloat16)
pipe.to("cuda")
pipe(prompt="a red fox", output="images")[0].save("fox.png")
FLUX.3 能做什么
文生图
FLUX.3 对完整的场景描述响应最好,描述中应包含主体、环境、光线、构图和风格。Black Forest Labs 的官方提示词指南里有更多技巧。蒸馏版检查点默认以 16 步生成 1024 × 1024 的图像,可以通过传入 height、width 或 num_inference_steps 来修改。
prompt = (
"A wide-angle wildlife photograph of a red fox standing alert in a snow-covered forest clearing at golden hour. "
"The fox is centered in the frame, its thick orange winter coat contrasting with the fresh white snow, breath "
"visible in the cold air. Snow-laden pine trees fill the softly blurred background, and low warm sunlight rakes "
"across the scene from the left, casting long blue shadows."
)
fox = pipe(
prompt=prompt,
generator=torch.Generator().manual_seed(0),
output="images"
)[0]
fox.save("fox.png")

文生图
图生图
通过 image 参数传入一张(或多张)参考图。提示词应写成你想要的那张图的描述,并把想要的改动包含进去。
prompt = (
"The same red fox in the same snow-covered forest clearing, now at night under a full moon: cool blue "
"moonlight, deep shadows, stars visible through the pine branches, the fox's eyes catching the light."
)
night = pipe(
prompt=prompt,
image=fox, # from the previous text to image example
generator=torch.Generator().manual_seed(0),
output="images",
)[0]
night.save("night.png")

图生图
有多张参考图时,在提示词中按它们传给 image 的顺序,依次用 <ref_image_0>、<ref_image_1>……来指代:
red_fox = pipe(prompt="a red fox", generator=torch.Generator().manual_seed(0), output="images")[0]
bench = pipe(prompt="a wooden bench", generator=torch.Generator().manual_seed(0), output="images")[0]
combined = pipe(
prompt="The fox from <ref_image_0> sitting on the wooden bench from <ref_image_1> in a snowy setting.",
image=[red_fox, bench],
output="images",
)[0]
combined.save("combined.png")



<ref_image_0>:一只红狐
<ref_image_1>:一条木长椅
长椅上的狐狸
布局控制
在描述文字之后添加一个区域的 JSON 列表。每个区域包含 id、bbox 和 desc,描述文字中用 <id> 来提及它。
边界框写作
[y_min, x_min, y_max, x_max],y 在前,取值范围为 0–1000。由于坐标经过归一化,同一个框可以用于任意尺寸的图像。
在新图像中放置区域
layout_prompt = (
"Minimalist graphic illustration featuring a black silhouette of a person <silhouette_1> centered against a "
"solid, vibrant chartreuse background <background_1>. The figure is captured in a dynamic, mid-stride running "
"pose, facing toward the left side of the frame. "
'[{"id": "background_1", "bbox": [0, 0, 1000, 1000], "desc": "A flat field of neon yellow-green with a subtle '
'paper texture and fine, organic grain."}, {"id": "silhouette_1", "bbox": [150, 150, 850, 850], "desc": "A '
"black silhouette of a person in motion, made of dense stippled ink. The leading edges are solid; the trailing "
'arm and leg dissolve into coarse square pixels and scattered dots."}]'
)
runner = pipe(prompt=layout_prompt, generator=torch.Generator().manual_seed(0), output="images")[0]

每个区域的 desc 都绘制在它自己的 bbox 之内
在参考图中移动区域
对于编辑任务,每个区域都要说明其内容来自哪里(src_bbox,在参考图中),以及要去往哪里(tgt_bbox,在输出图中)。两个框相同的区域会保持原位。
这个例子把针织骑士向左上方移动,并固定了背景、悬崖、织针、龙和火焰:
from diffusers.utils import load_image
knight_image = load_image("knight_orig.png")
edit_prompt = (
"In <ref_image_0>, move the miniature grey amigurumi knight figure <knight_1> upwards and to the left along the "
"yarn cliff <cliff_1>. Keep the rest of the image completely unchanged, preserving the blurred yarn backdrop "
"<backdrop_1>, the large knitted dragon <dragon_1>, and the fiery orange thread <fire_1>. "
'[{"id": "knight_1", "from": "ref_image_0", "src_bbox": [500, 150, 850, 350], "tgt_bbox": [194, 55, 544, 255], '
'"desc": "A miniature amigurumi knight crocheted from thick grey woolen yarn."}, '
'{"id": "backdrop_1", "from": "ref_image_0", "src_bbox": [0, 0, 1000, 1000], "tgt_bbox": [0, 0, 1000, 1000], '
'"desc": "A soft, out-of-focus backdrop of cream and indigo knitted yarn."}, '
'{"id": "cliff_1", "from": "ref_image_0", "src_bbox": [300, 0, 1000, 450], "tgt_bbox": [300, 0, 1000, 450], '
'"desc": "A steep cliff of stacked yarn skeins in mustard, terracotta, and indigo."}, '
'{"id": "needle_1", "from": "ref_image_0", "src_bbox": [420, 250, 550, 450], "tgt_bbox": [420, 250, 550, 450], '
'"desc": "An oversized polished steel tapestry needle."}, '
'{"id": "dragon_1", "from": "ref_image_0", "src_bbox": [100, 600, 950, 1000], "tgt_bbox": [100, 600, 950, 1000], '
'"desc": "A large, fluffy knitted dragon in terracotta and mustard yarn."}, '
'{"id": "fire_1", "from": "ref_image_0", "src_bbox": [350, 400, 650, 650], "tgt_bbox": [350, 400, 650, 650], '
'"desc": "Curled puffs of fiery orange angora thread suspended in mid-air."}]'
)
moved = pipe(prompt=edit_prompt, image=knight_image, generator=torch.Generator().manual_seed(0), output="images")[0]

骑士从 src_bbox 移动到 tgt_bbox,场景的其余部分保持不变
基础模型与量化版本
基础模型
上面所有示例用的都是蒸馏版检查点。基础检查点使用 50 步和无分类器引导(classifier-free guidance),也是适合用来微调的那个(见 LoRA 微调)。
pipe = ModularPipeline.from_pretrained("black-forest-labs/flux-3-image-klein-base")
pipe.load_components(dtype={"vae": torch.float32, "default": torch.bfloat16})
pipe.to("cuda")
pipe.guider = pipe.guider.new(guidance_scale=5.0) # optional
image = pipe(prompt=prompt, output="images")[0]
单文件格式检查点
Black Forest Labs 还把 transformer 以单文件形式发布,提供 bf16 以及两种量化格式:fp8r(逐行 FP8)和 nvfp4。可以用 from_single_file 加载其中任何一个,再按名称加载其他组件,这样就不会连 bf16 的 transformer 一起下载。
from diffusers import BFLQuantizationConfig, Flux3Transformer2DModel, ModularPipeline
model_id = "black-forest-labs/flux-3-image-klein-base"
variant = "fp8r" # or "nvfp4"
transformer = Flux3Transformer2DModel.from_single_file(
f"https://huggingface.co/{model_id}/blob/main/variants/{variant}/model.safetensors",
config=model_id,
subfolder="transformer",
quantization_config=BFLQuantizationConfig(compute_dtype=torch.bfloat16),
dtype=torch.bfloat16,
)
pipe = ModularPipeline.from_pretrained(model_id)
pipe.load_components(dtype=torch.bfloat16)
pipe.update_components(transformer=transformer)
pipe.to("cuda")
对于未量化的单文件,请使用 https://huggingface.co/{model_id}/blob/main/model.safetensors 并去掉 quantization_config。
| 版本 | 快速内核适用于 | 其他 GPU |
|---|---|---|
fp8r |
计算能力 8.9 及以上(Ada、Hopper、Blackwell) | 权重在每一步都要反量化:显存占用更低,但没有加速 |
nvfp4 |
Blackwell,且 flashinfer-python 与 flashinfer-cubin 版本一致 |
同上 |
LoRA 微调
FLUX.3 既能做文生图,也能做图生图,因此很适合作为自定义 LoRA 的基座。diffusers 提供了针对这两类任务的 DreamBooth LoRA 训练脚本(你也可以使用 Ostris 的 AI Toolkit 来训练)。
下面是一个文生图示例,它从 multimodalart/1920-raider-waite-tarot-public-domain 数据集中学习塔罗牌风格,并用 trtcrd 触发:
accelerate launch train_dreambooth_lora_flux3_klein.py \
--pretrained_model_name_or_path="black-forest-labs/flux-3-image-klein-base" \
--dataset_name="multimodalart/1920-raider-waite-tarot-public-domain" \
--caption_column="caption" \
--instance_prompt="trtcrd" \
--output_dir="trained-flux3-tarot-lora" \
--mixed_precision="bf16" \
--resolution=1024 \
--use_aspect_ratio_buckets \
--train_batch_size=1 \
--rank=16 \
--lora_alpha=16 \
--learning_rate=1e-4 \
--lr_scheduler="constant" \
--max_train_steps=1500 \
--validation_prompt="a trtcrd of a cat, tarot style" \
--validation_epochs=25 \
--seed=0 \
--push_to_hub
要降低显存占用,可以添加几个优化参数:
--gradient_checkpointing \ --cache_latents \ --use_8bit_adam
--gradient_checkpointing在反向传播时重新计算激活值(显存更低,速度略慢),--cache_latents预先编码图像并释放 VAE,--use_8bit_adam则以 8 位保存优化器状态。
有几个参数值得多试试。下面的网格用不同的 LoRA 秩和优化器训练了同一种塔罗牌风格;这些适配器都在 Hub 上,地址是 diffusers-modular/flux3-tarot-lora。更多训练功能和配置记录在 DreamBooth FLUX.3 README 中。

对于基于指令的编辑,图生图脚本(train_dreambooth_lora_flux3_klein_img2img.py)使用(参考图、目标图、指令)三元组进行训练,其中参考图在提示词中以 <ref_image_0> 来指代。
全部选项和完整的图生图示例请参阅 DreamBooth FLUX.3 README。
运行成本
本节将介绍这些模型的显存占用和生成速度。
显存
| 名称 | 峰值分配量(加载后),GiB | 峰值分配量(推理后),GiB |
|---|---|---|
| nvfp4(RTX6000) | 13.4 | 15.3 |
| fp8r(H100) | 14.35 | 16.27 |
| bf16(H100) | 18.80 | 20.72 |
如果显存不足:
- 加载量化后的 transformer。
- 单独运行文本编码器并在用完后释放。它在每张图像的去噪之前只运行一次(见编码一次,多次生成)。
- 开启自动 CPU 卸载,让每个组件仅在运行时才被移到 GPU 上(我们没有对此做基准测试)
from diffusers import ComponentsManager, ModularPipeline
manager = ComponentsManager()
manager.enable_auto_cpu_offload(device="cuda")
pipe = ModularPipeline.from_pretrained("black-forest-labs/flux-3-image-klein", components_manager=manager)
pipe.load_components(dtype={"vae": torch.float32, "default": torch.bfloat16})
# no pipe.to("cuda"): the manager handles device placement
速度
蒸馏版检查点(16 步)的端到端耗时,预热后取三次运行的平均值:
| 工作流 | 输出尺寸 | 耗时 | 峰值显存 |
|---|---|---|---|
| 文生图 | 1024 × 1024 | 1.93 秒 | 23.8 GiB |
| 图生图,一张 1024 × 1024 的参考图 | 1024 × 1024 | 3.87 秒 | 23.8 GiB |
| 布局编辑(骑士),一张 1360 × 768 的参考图 | 1360 × 768 | 4.10 秒 | 23.8 GiB |
单块 NVIDIA H100 80GB,无卸载,默认注意力后端。
加入一张参考图,耗时大约翻倍。参考图的词元与文本和正在生成的图像处于同一个序列中,因此一张 1024 × 1024 的参考图会让每次 transformer 调用增加 4,096 个词元

每次 transformer 调用的词元数堆叠柱状图:文生图 4,256 个词元;图生图 8,272;布局文生图 4,336;布局图生图 8,800
模型架构
下面我们一个部分一个部分地把模型拆开来看。模型处理三类词元:
- 文本
- 正在生成的带噪图像
- 以及可选的参考图像
从像素到词元
FLUX.3 是一个用于图像生成的潜空间流模型(latent-space flow model)。也就是说,去噪器以潜变量为输入,输出的也是潜变量。我们用 VAE 把潜变量解码回像素。FLUX.3 使用与 Flux.2 相同的 VAE 架构。
FLUX.2 的 VAE 在每个方向上把图像压缩 8 倍,得到 32 个通道,然后潜变量中每个 2 × 2 的块被打包成一个 128 通道的词元。
因此,一个词元对应 16 × 16 像素的图像块,一张 1024 × 1024 的图像会变成 64 × 64 的网格,共 4,096 个词元。带噪图像一开始就是恰好这个形状的随机噪声。参考图像会经过同样的 VAE 编码器和打包过程,并保持自己的尺寸。
从提示词到词元
文本编码器是视觉语言模型 Qwen3-VL。FLUX.3 并不只取它的最后一层,而是读取其 36 层中 8 层(第 4、8、……、32 层)的隐藏状态并把它们堆叠起来,因此每个文本词元会变成一个 8 × 2560 = 20,480 维的向量,再经过一个线性层投影到 transformer 的宽度 3072。
这样 transformer 拿到的是来自语言模型多个深度的特征,而不仅仅是最后一层。提示词会被填充到 80 个词元的整数倍,transformer 也会关注填充部分,这就是流水线每次调用只接受一条提示词的原因。
两个阶段
transformer 分两个阶段处理这些词元:
- 第一阶段:分模态模块。每个数据流经过 5 个模块,各自使用独立的权重。文本有自己的模块,带噪图像有自己的,参考图像也有自己的。
- 第二阶段:联合模块。各数据流被拼接成一个序列
[text | image | reference],经过 28 个共享同一组权重的模块。
这两个阶段的区别在于谁可以关注谁。
在第一阶段,每个数据流只关注自身,因此每类词元都先按各自的方式得到精炼。在第二阶段,所有词元彼此互相关注,图像从文本和参考图中学习。作为对比,FLUX.2 的双流模块同样让文本和图像使用各自独立的权重,但从一开始就让两者互相关注。FLUX.3 则先让各数据流完全分开,再进行合并。
最后,文本和参考图的词元会被直接丢弃。只有图像词元会经过输出层,输出层预测如何把它们朝着干净图像的方向推进一步。
单个模块的内部
无论在哪个阶段,每个模块的结构都一样。注意力和 MLP 对同一份归一化后的输入并行运行,两者之和再加回残差流。
每个模块都以一个没有自身可学习缩放和偏移的 LayerNorm 开头。缩放、偏移和门控则来自时间步嵌入(这称为调制,modulation)。在注意力内部,查询和键会做 RMS 归一化,并加上旋转位置编码(RoPE),下文会详细介绍。
在联合模块中,所有词元共享同一组权重,但文本、图像和参考图词元各有自己的缩放、偏移和门控。
def block(x, shift, scale, gate): # shift, scale, gate come from the token's timestep embedding
h = layer_norm(x) # no learned scale or shift
h = (1 + scale) * h + shift
q, k, v = to_q(h), to_k(h), to_v(h)
q, k = rope(rms_norm(q)), rope(rms_norm(k))
out = attention(q, k, v) + mlp(h) # parallel, MLP is SwiGLU
return x + gate * out
每个词元都有自己的时间步
在 FLUX.1 和 FLUX.2 中,同一个时间步嵌入会调制所有词元。在 FLUX.3 中,调制方式取决于词元的类型。
带噪图像词元由当前时间步 t 调制。文本和参考图词元则始终由时间步 0 调制,意思是“已经干净,无需去噪”。因此,即使某张参考图看起来与输出几乎一样,它也会被标记为给定的上下文,而不是需要生成的内容。
蒸馏版检查点是一个流映射(flow map)。它还会被告知每一步要跳到哪个时间步。这第二个时间步有自己的嵌入,并被加到每个词元的调制中,所以它的 16 步中每一步都可以跳得更远。
Positions in four dimensions
注意力本身并不知道词元在什么位置(这种现象称为置换等变性)。RoPE 通过按词元位置对每个查询和键旋转相应的角度来解决这个问题。FLUX.3 使用四个位置轴 (t, h, w, l)(时间步、高度、宽度、序列长度),并为每个轴分配每个 128 维注意力头的四分之一(32 维):
| 词元 | t:时间步 | h:高度 | w:宽度 | l:序列长度 |
|---|---|---|---|---|
| 文本词元 i | 0 | 0 | 0 | i |
| 位于(行,列)的图像词元 | 0 | 行 / 2 | 列 / 2 | 0 |
| 位于(行,列)的参考图 k | 0 | 行 / 2 | 列 / 2 | −(k + 1) |
文本词元沿 l 排列,就像句子里的词。图像和参考图则是 (h, w) 中的二维网格,彼此沿 l 分开。由于参考图使用与输出相同的 (h, w) 坐标,“参考图中的同一位置”对模型来说就有了精确的含义,而 src_bbox / tgt_bbox 编辑正是依赖这一点。
去噪循环
FLUX.3 是一个流匹配(flow-matching)模型。生成从纯噪声开始,在 50 步中的每一步,transformer 都会预测一个速度,即从噪声指向干净图像的方向。调度器沿这个方向走一步。
在每一步,你还可以读出模型当前对最终图像的猜测。在若干步上解码这个猜测,就能看到图像是如何逐渐成形的:






第 1 步
第 5 步
第 10 步
第 15 步
第 25 步
第 50 步
到第 10 步时,狐狸、它的姿态和光线就已经就位,而此时噪声水平仍有 0.97。挂满积雪的树枝和毛发的质感直到最后几步才出现。FLUX.3 的调度器使用了很大的时间偏移(6.93),使噪声水平在大部分过程中保持在高位:50 步中有 44 步发生在 σ = 0.5 以上。这张图是我们给流水线加了一个小模块得到的,这也引出了 Modular Diffusers。
Modular Diffusers
FLUX.3 以 Modular Diffusers 流水线的形式发布。
查看工作流
你可以在不加载任何权重的情况下,列出各个工作流并打印每个工作流的具体模块。
from diffusers import Flux3AutoBlocks
blocks = Flux3AutoBlocks()
print(blocks.available_workflows)
# ['text2image', 'image_conditioned']
print(list(blocks.get_workflow("text2image").sub_blocks))
# ['text_encoder', 'denoise.input', 'denoise.prepare_latents', 'denoise.set_timesteps',
# 'denoise.prepare_rope_inputs', 'denoise.denoise', 'denoise.after_denoise', 'decode']
get_workflow() 会解析自动分支,并给出将要运行的纯步骤序列。print(blocks.get_workflow("image_conditioned").doc) 会打印该工作流的组件、输入、默认值和输出。
编码一次,多次生成
把文本编码器拿出来,对提示词只编码一次,释放它,然后用缓存的嵌入进行生成。
import torch
from diffusers import ModularPipeline
model_id = "black-forest-labs/flux-3-image-klein-base"
blocks = ModularPipeline.from_pretrained(model_id).blocks
text_blocks = blocks.sub_blocks.pop("text_encoder") # `blocks` now starts at the VAE encoder
text_pipe = text_blocks.init_pipeline(model_id)
text_pipe.load_components(dtype=torch.bfloat16)
text_pipe.to("cuda")
state = text_pipe(prompt=prompt, negative_prompt="") # no guider here, so ask for the negative embeddings
prompt_embeds = state.get("prompt_embeds")
negative_prompt_embeds = state.get("negative_prompt_embeds")
del text_pipe
torch.cuda.empty_cache()
image_pipe = blocks.init_pipeline(model_id) # no text encoder to load
image_pipe.load_components(dtype={"vae": torch.float32, "default": torch.bfloat16})
image_pipe.to("cuda")
for seed in range(3):
image = image_pipe(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_prompt_embeds,
generator=torch.Generator().manual_seed(seed),
output="images",
)[0]
image.save(f"{seed}.png")
在循环内部添加一个步骤
上面的去噪图来自一个额外的模块,它会在每一步记录模型对干净图像的猜测:
from diffusers.modular_pipelines import Flux3AutoBlocks, InputParam, ModularPipelineBlocks, OutputParam
class Flux3PreviewStep(ModularPipelineBlocks):
model_name = "flux3"
@property
def inputs(self):
return [InputParam("latents"), InputParam("noise_pred")]
@property
def intermediate_outputs(self):
return [OutputParam("x0_previews", type_hint=list)]
@torch.no_grad()
def __call__(self, components, block_state, i, t):
if i == 0:
block_state.x0_previews = []
sigma = t / 1000 # timesteps are sigmas scaled to [0, 1000]
block_state.x0_previews.append(block_state.latents - sigma * block_state.noise_pred) # x0 = x_t - sigma * v
return components, block_state
blocks = Flux3AutoBlocks()
denoise_loop = blocks.sub_blocks["denoise"].sub_blocks["text2image"].sub_blocks["denoise"]
denoise_loop.sub_blocks.insert("preview", Flux3PreviewStep(), 1) # between "denoiser" and "after_denoiser"
pipe = blocks.init_pipeline(model_id)
pipe.load_components(dtype={"vae": torch.float32, "default": torch.bfloat16})
pipe.to("cuda")
out = pipe(prompt=prompt, generator=torch.Generator().manual_seed(0), output=["x0_previews", "images"])
动手试试
用 FLUX.3 做出了什么好东西吗?欢迎把你的 LoRA 和流水线分享到 Hub 上,我们很期待看到!😎
