Diffusers 文件

GGUF

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

GGUF

GGUF 檔案格式通常用於儲存模型以供 GGML 推理,並支援各種塊級量化選項。Diffusers 支援透過模型類的 from_single_file 載入預量化並以 GGUF 格式儲存的檢查點。目前不支援透過管道載入 GGUF 檢查點。

以下示例將使用 GGUF Q2_K 量化變體載入 FLUX.1 DEV 變換器模型。

開始前請在您的環境中安裝 gguf

pip install -U gguf

由於 GGUF 是單一檔案格式,請使用 ~FromSingleFileMixin.from_single_file 載入模型並傳入 GGUFQuantizationConfig

使用 GGUF 檢查點時,量化權重保持在低記憶體 dtype(通常為 torch.uint8),並在每次模組透過模型進行前向傳播時動態反量化並轉換為配置的 compute_dtypeGGUFQuantizationConfig 允許您設定 compute_dtype

用於動態反量化的函式基於 city96 所做的出色工作,他建立了原始 numpy 實現(由 compilade 完成)的 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
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.