Image-to-Image
Diffusers
Safetensors
Flux2KleinPipeline
FLUX
FLUX.2
Klein
quantization
svdquant
nunchaku
fp4
int4
Run this model with HF Jobs
Run tonera/FLUX.2-klein-9B-Nunchaku on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/tonera/FLUX.2-klein-9B-Nunchaku. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/tonera/FLUX.2-klein-9B-Nunchaku on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use tonera/FLUX.2-klein-9B-Nunchaku with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use tonera/FLUX.2-klein-9B-Nunchaku with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("tonera/FLUX.2-klein-9B-Nunchaku", dtype=torch.bfloat16, device_map="cuda") prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] - Notebooks
- Google Colab
- Kaggle
File size: 10,678 Bytes
0e92a71 677e0df 0e92a71 e035418 d76d4d7 f4d92cc 0e92a71 f4d92cc 0e92a71 f4d92cc 0e92a71 f4d92cc 0e92a71 f4d92cc 0e92a71 f4d92cc 0e92a71 f4d92cc 0e92a71 f4d92cc 0e92a71 d76d4d7 0e92a71 f4d92cc 0e92a71 f4d92cc 677e0df 0e92a71 677e0df 0e92a71 582aa68 0e92a71 f4d92cc fe4fdad f4d92cc 446e4d8 f4d92cc 446e4d8 d76d4d7 0e92a71 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | ---
pipeline_tag: image-to-image
library_name: diffusers
tags:
- FLUX
- FLUX.2
- Klein
- quantization
- svdquant
- nunchaku
- fp4
- int4
base_model: black-forest-labs/FLUX.2-klein-9B
base_model_relation: quantized
license: other
---
# 模型说明(SVDQuant / Nunchaku)
> **文档语言**:中文|[English](README.md)
## 模型与上游
- **量化权重仓库**:[`tonera/FLUX.2-klein-9B-Nunchaku`](https://huggingface.co/tonera/FLUX.2-klein-9B-Nunchaku)
- **官方全精度源模型**:[`black-forest-labs/FLUX.2-klein-9B`](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B)
- **本仓库中的量化 Transformer**:`svdq-<precision>_r32-FLUX.2-klein-9B-Nunchaku.safetensors`;`<precision>` 请按环境与 Nunchaku 构建使用 `nunchaku.utils.get_precision()`(常见为 `fp4` 或 `int4`)拼入文件名
- **Diffusers 侧配套文件**(VAE、text encoder 等):与上述 Hugging Face 仓库根目录一致,加载 pipeline 时使用同一 `from_pretrained` 路径即可
FLUX.2 klein 9B 为 BFL 的蒸馏流模型,支持文生图与多参考编辑;硬件与许可说明见 [官方模型卡](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B)。
## 量化质量(本仓库评测摘录)
| 指标 | 均值 | 中位数 p50 | p90 |
|------|------|------------|-----|
| PSNR | 17.56 | 17.52 | 20.62 |
| SSIM | 0.735 | 0.741 | 0.837 |
| LPIPS | 0.212 | 0.194 | 0.300 |
另有:Mean rel_l2 ≈ 0.0717,Mean cosine ≈ 1.0006(`data.json` 中为 1.000562,可能含浮点误差)。更完整的说明与更新以 Hugging Face 模型页为准。
## 性能测试(RTX 5090 32GB,8 steps,guidance scale = 1.0)
- `Base` = `black-forest-labs/FLUX.2-klein-9B`
- `TE` = `svdq-int4-Qwen3-text-Nunchaku.safetensors`
- `TR` = `svdq-{precision}_r32-FLUX.2-klein-9B-Nunchaku.safetensors`
- `MCO` = `enable-model-cpu-offload`
- `SCO` = `enable-sequential-cpu-offload`
### 图片编辑(1024x1024)
| 方案 | 运行方式 | 峰值显存 | 吞吐 | 出图时间 | 吞吐变化(相对原生) | 显存变化(相对原生) |
|---|---|---:|---:|---:|---:|---:|
| `Base` | `CUDA` | OOM | - | - | 基线不可用 | 基线不可用 |
| `Base` | `MCO` | 20.18 GB | 0.62 it/s | 12 s | 基线 | 基线 |
| `Base` | `SCO` | 2.55 GB | 0.48 it/s | 16 s | 基线 | 基线 |
| `Base + TE` | `CUDA` | 25.60 GB | 1.28 it/s | 6 s | N/A(原生 OOM) | N/A |
| `Base + TE` | `MCO` | 20.16 GB | 0.83 it/s | 9 s | +33.9% | -0.1% |
| `Base + TE` | `SCO` | 6.08 GB | 0.48 it/s | 16 s | -0.5% | +138.4% |
| `Base + TR` | `CUDA` | 24.51 GB | 3.79 it/s | 2 s | N/A(原生 OOM) | N/A |
| `Base + TR` | `MCO` | 17.39 GB | 1.08 it/s | 7 s | +75.0% | -13.8% |
| `Base + TR` | `SCO` | 4.35 GB | 2.70 it/s | 2 s | +461.6% | +70.6% |
| `Base + TR + TE` | `CUDA` | 14.00 GB | 3.81 it/s | 2 s | N/A(原生 OOM) | N/A |
| `Base + TR + TE` | `MCO` | 7.52 GB | 1.88 it/s | 4 s | +204.6% | -62.7% |
| `Base + TR + TE` | `SCO` | 7.69 GB | 2.68 it/s | 2 s | +457.4% | +201.6% |
### 文生图(1024x1024)
| 方案 | 运行方式 | 峰值显存 | 吞吐 | 出图时间 | 吞吐变化(相对原生) | 显存变化(相对原生) |
|---|---|---:|---:|---:|---:|---:|
| `Base` | `CUDA` | OOM | - | - | 基线不可用 | 基线不可用 |
| `Base` | `MCO` | 18.53 GB | 0.83 it/s | 9 s | 基线 | 基线 |
| `Base` | `SCO` | 2.55 GB | 0.62 it/s | 12 s | 基线 | 基线 |
| `Base + TR + TE` | `CUDA` | 15.21 GB | 8.91 it/s | <1 s | N/A(原生 OOM) | N/A |
| `Base + TR + TE` | `MCO` | 6.42 GB | 2.60 it/s | 3 s | +214.5% | -65.4% |
| `Base + TR + TE` | `SCO` | 7.72 GB | 3.00 it/s | 2 s | +383.0% | +202.7% |
结论上,若目标是在 `enable-model-cpu-offload` 下兼顾速度与显存,双量化(Transformer + text encoder)收益最明显:图片编辑时显存从 20.18 GB 降到 7.52 GB,同时吞吐提升到原生的约 3.05 倍;文生图时显存从 18.53 GB 降到 6.42 GB,吞吐提升到原生的约 3.15 倍。若使用 `enable-sequential-cpu-offload`,量化方案通常能显著提速,但由于原生基线本身已极度压缩显存,量化后的峰值显存不一定继续下降。
## 在 ComfyUI 中使用
安装插件 [Vitoom Nunchaku](https://github.com/tonera/vitoom-nunchaku-comfy)。需要 ComfyUI 0.44 或更新版本,启动时加上 `--enable-manager`。安装插件时会装上匹配的 nunchaku wheel。
1. 打开 ComfyUI,点右上角 **管理扩展功能**。
2. 搜索 `Vitoom Nunchaku`,点 **安装**。
3. 重启 ComfyUI。
把本仓库的 transformer 放到 `ComfyUI/models/diffusion_models`:50 系用 `svdq-fp4_r32-FLUX.2-klein-9B-Nunchaku.safetensors`,30 系和 40 系用 `svdq-int4_r32-FLUX.2-klein-9B-Nunchaku.safetensors`。文本编码器 [`svdq-int4-Qwen3-text-Nunchaku.safetensors`](https://huggingface.co/tonera/Qwen3-text-Nunchaku) 放到 `~/weights` 或 `ComfyUI/models/text_encoders`。VAE `flux2-vae.safetensors` 放到 `ComfyUI/models/vae`。
把插件 `example_workflows/flux2_klein_lora.json` 拖进画布,或从工作流模板打开。这个工作流已经选好 9B 权重和对应的文本编码器。**Vitoom Nunchaku DiT Loader** 接到 LoRA 节点,再接到 `KSampler`;**Vitoom Nunchaku Text Encoder** 接到编码节点。不用 LoRA 时,对 **Vitoom Nunchaku LoRA Loader** 选 Bypass。文生图时断开参考图;编辑时把参考图经 `ReferenceLatent` 接上。
## 用法一(适合有编程经验用户)
- **引擎**:[vitoom-nunchaku](https://huggingface.co/tonera/vitoom-nunchaku) — 社区维护的 Nunchaku 扩展版,已内置 **FLUX.2 Klein** 与 **Qwen3 文本编码器** 支持
- **框架**:需支持 `Flux2KleinPipeline` 的 Diffusers(官方示例为从源码安装):
```bash
pip install "git+https://github.com/huggingface/diffusers.git"
```
### 安装 vitoom-nunchaku(推荐)
上游 [Nunchaku](https://github.com/nunchaku-ai/nunchaku) 长期未合并 FLUX.2 Klein 相关改动([PR #926](https://github.com/nunchaku-ai/nunchaku/pull/926) 仍在等待)。**无需再手动复制补丁文件。** 请从 **[tonera/vitoom-nunchaku](https://huggingface.co/tonera/vitoom-nunchaku)** 安装与平台、Python、CUDA 匹配的预编译 wheel。
示例(x86_64、Python 3.11、CUDA 13.0):
```bash
pip install torch==2.11.* torchvision==0.26.* torchaudio==2.11.* \
--index-url https://download.pytorch.org/whl/cu130
hf download tonera/vitoom-nunchaku \
nunchaku-1.3.0.dev20260622+cu13.0torch2.11-cp311-cp311-linux_x86_64.whl \
--local-dir ./wheels
pip install ./wheels/nunchaku-1.3.0.dev20260622+cu13.0torch2.11-cp311-cp311-linux_x86_64.whl
```
cu128、cp310、ARM64 aarch64 等 wheel 见 [vitoom-nunchaku 说明](https://huggingface.co/tonera/vitoom-nunchaku)。
可选量化文本编码器:[`tonera/Qwen3-text-Nunchaku`](https://huggingface.co/tonera/Qwen3-text-Nunchaku)(`svdq-int4-Qwen3-text-Nunchaku.safetensors`)。
验证安装:
```bash
python -c "import nunchaku; from nunchaku import NunchakuFlux2Transformer2DModel; print(nunchaku.__version__)"
```
### 使用示例(图生图 + 量化 Transformer)
以下示例假设已安装 **vitoom-nunchaku**,且权重在本地或通过 `tonera/FLUX.2-klein-9B-Nunchaku` 可访问;请将 `REPO` 换成你的本地目录或 Hugging Face ID。
```python
import torch
from diffusers import Flux2KleinPipeline
from diffusers.utils import load_image
from nunchaku import NunchakuFlux2Transformer2DModel
from nunchaku.utils import get_precision
REPO = "tonera/FLUX.2-klein-9B-Nunchaku" # 或本地绝对路径
NAME = "FLUX.2-klein-9B-Nunchaku"
transformer = NunchakuFlux2Transformer2DModel.from_pretrained(
f"{REPO}/svdq-{get_precision()}_r32-{NAME}.safetensors",
torch_dtype=torch.bfloat16,
)
pipe = Flux2KleinPipeline.from_pretrained(
REPO, torch_dtype=torch.bfloat16, transformer=transformer
)
pipe.to("cuda")
# transformer.set_offload(
# True, use_pin_memory=False, num_blocks_on_gpu=1
# )
# pipeline._exclude_from_cpu_offload.append("transformer")
# pipeline.enable_sequential_cpu_offload()
ref = load_image("https://example.com/your_ref.png").convert("RGB")
image = pipe(
prompt="按你的编辑意图写英文提示…",
image=ref,
guidance_scale=1.0, # 与官方 Klein 示例一致,可按需调整
num_inference_steps=4, # 蒸馏模型常用 4 步;非蒸馏设定见 Diffusers 文档
generator=torch.Generator("cpu").manual_seed(1),
).images[0]
image.save("flux2_klein_nunchaku.png")
```
纯文生图时可省略 `image` 参数(行为以当前 Diffusers `Flux2KleinPipeline` 文档为准)。显存紧张时可按需使用 `pipe.enable_model_cpu_offload()` 等策略。
### 如果以上操作对你来说难度过高,建议直接安装 vitoom 平台(见下方)。
## 用法二(推荐:vitoom 平台)
推荐通过 [vitoom](https://github.com/tonera/vitoom) 部署:内置完整 **vitoom-nunchaku** runtime、Web UI、多图编辑与 LoRA 管理,无需手动安装 wheel 或拷贝补丁。详见 [docker-usage-cn.md](https://github.com/tonera/vitoom/blob/main/docker-usage-cn.md)。
```bash
git clone https://github.com/tonera/vitoom.git
cd vitoom
python scripts/setup_vitoom.py
python scripts/load_vitoom_images.py --components backend,visual
docker compose up -d backend
docker compose -f docker-compose.inference.release.yml --profile visual up -d
```
Web UI:**Models** → 下载并激活 **`tonera/FLUX.2-klein-9B-Nunchaku`** → 在 **Image** 工作区推理。
## 关于 LoRA 使用
FLUX.2 Klein 9B 已支持在量化 Transformer 上叠加 LoRA。使用 **vitoom-nunchaku**(wheel 或 vitoom Visual 镜像)即可,**无需手动复制任何文件**:
```python
lora_path = "/path/to/your_lora.safetensors"
transformer.update_lora_params(lora_path)
transformer.set_lora_strength(0.8)
```
## 致谢
- [nunchaku](https://github.com/nunchux-ai/nunchaku):高性能推理引擎。
- [deepcompressor](https://github.com/nunchux-ai/deepcompressor):模型压缩工具箱。
- [fluxsd.com](https://fluxsd.com/):图片、视频、海报、漫画和绘本生成。
- [createpptx.com](https://createpptx.com/):高质量可编辑 PPT 生成。
## 许可与合规
量化权重衍生于 **FLUX.2-klein-9B**,使用须遵守 [FLUX Non-Commercial License](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B) 及 Black Forest Labs 的可接受使用政策;商用请单独确认授权。
模型卡 YAML 中的 `license: other` 仅因 Hugging Face 元数据许可证字段为固定枚举,未单独列出 FLUX 非商用许可;**具有约束力的是上文链接的协议条款**,而非 `other` 这一占位标签本身。
|