Diffusers 文档
GGUF
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
GGUF
GGUF 文件格式通常用于存储模型,以便使用 GGML 进行推理,并支持各种块状量化选项。Diffusers 支持使用模型类通过 from_single_file
加载来加载预量化并以 GGUF 格式保存的检查点。目前不支持通过 Pipelines 加载 GGUF 检查点。
以下示例将使用 GGUF Q2_K 量化变体加载 FLUX.1 DEV transformer 模型。
在开始之前,请在您的环境中安装 gguf
pip install -U gguf
由于 GGUF 是单文件格式,请使用 ~FromSingleFileMixin.from_single_file
加载模型并传入 GGUFQuantizationConfig。
当使用 GGUF 检查点时,量化后的权重会保留在低内存 dtype
(通常为 torch.uint8
)中,并在每个模块通过模型的前向传播过程中动态反量化并转换为配置的 compute_dtype
。GGUFQuantizationConfig
允许您设置 compute_dtype
。
用于动态反量化的函数基于 city96 的出色工作,他创建了 compilade 的原始 numpy
实现的 Pytorch 端口。
import torch
from diffusers import FluxPipeline, FluxTransformer2DModel, GGUFQuantizationConfig
ckpt_path = (
"https://huggingface.co/city96/FLUX.1-dev-gguf/blob/main/flux1-dev-Q2_K.gguf"
)
transformer = FluxTransformer2DModel.from_single_file(
ckpt_path,
quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
torch_dtype=torch.bfloat16,
)
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev",
transformer=transformer,
torch_dtype=torch.bfloat16,
)
pipe.enable_model_cpu_offload()
prompt = "A cat holding a sign that says hello world"
image = pipe(prompt, generator=torch.manual_seed(0)).images[0]
image.save("flux-gguf.png")
支持的量化类型
- BF16
- Q4_0
- Q4_1
- Q5_0
- Q5_1
- Q8_0
- Q2_K
- Q3_K
- Q4_K
- Q5_K
- Q6_K