Diffusers documentation

AutoRound

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

AutoRound

AutoRound は高度な量子化ツールキットです。符号勾配降下法を活用し、チューニングを最小限に抑えながら、超低ビット幅(2〜4ビット)で高い精度を実現するとともに、幅広いハードウェアとの互換性を備えています。詳細については、論文 SignRoundV1 および SignRoundV2 をご覧ください。

auto-round(バージョン ≥ 0.13.0)をインストールします:

pip install "auto-round>=0.13.0"

より高速な CUDA 推論のために Marlin カーネルを使用するには、gptqmodel をインストールしてください。

pip install "gptqmodel>=5.8.0"

量子化モデルの読み込み

AutoRoundConfigをfrom_pretrained()に渡すことで、事前量子化済みのAutoRoundモデルを読み込みます。このメソッドは、Accelerate経由で読み込めるモデルであれば、torch.nn.Linearレイヤーを持つ限りすべてで動作します。

PipelineQuantizationConfig を使用して、パイプラインの特定のコンポーネントを量子化できます:

import torch
from diffusers import DiffusionPipeline, PipelineQuantizationConfig, AutoRoundConfig

pipeline_quant_config = PipelineQuantizationConfig(
    quant_mapping={"transformer": AutoRoundConfig(backend="auto")}
)
pipe = DiffusionPipeline.from_pretrained(
    "INCModel/Z-Image-W4A16-AutoRound",
    quantization_config=pipeline_quant_config,
    dtype=torch.bfloat16,
    device_map="cuda",  # or "mps", "xpu", "cpu"
)

image = pipe("a cat holding a sign that says hello").images[0]
image.save("output.png")

または、量子化されたモデルコンポーネントを直接読み込みます:

import torch
from diffusers import ZImageTransformer2DModel, ZImagePipeline, AutoRoundConfig

model_id = "INCModel/Z-Image-W4A16-AutoRound"

quantization_config = AutoRoundConfig(backend="auto")
transformer = ZImageTransformer2DModel.from_pretrained(
    model_id,
    subfolder="transformer",
    quantization_config=quantization_config,
    dtype=torch.bfloat16,
    device_map="cuda",  # or "mps", "xpu", "cpu"
)

pipe = ZImagePipeline.from_pretrained(
    model_id,
    transformer=transformer,
    dtype=torch.bfloat16,
    device_map="cuda",
)

image = pipe("a cat holding a sign that says hello").images[0]
image.save("output.png")

Diffusers における AutoRound は、事前量子化済みモデルの読み込みのみをサポートしています。モデルをゼロから量子化するには、AutoRound CLI または Python APIを直接使用し、その結果を Diffusers で読み込んでください。

torch.compile

AutoRound は、より高速な推論のために torch.compile と互換性があります。パフォーマンスを向上させるために、量子化された Transformer(DiT)をコンパイルできます。

import torch
from diffusers import DiffusionPipeline, PipelineQuantizationConfig, AutoRoundConfig

pipeline_quant_config = PipelineQuantizationConfig(
    quant_mapping={"transformer": AutoRoundConfig(backend="auto")}
)
pipe = DiffusionPipeline.from_pretrained(
    "INCModel/Z-Image-W4A16-AutoRound",
    quantization_config=pipeline_quant_config,
    dtype=torch.bfloat16,
    device_map="cuda",  # or "mps", "xpu", "cpu"
)

pipe.transformer = torch.compile(pipe.transformer, mode="default", fullgraph=False)

バックエンド

AutoRound は、Weight-only 量子化モデルに対して複数の推論バックエンドをサポートしています。バックエンドは、フォワードパス中のデクォアンティゼーションをどのカーネルが処理するかを制御します。AutoRoundConfig の backend パラメータを設定して、バックエンドを選択します。

バックエンド値デバイス要件注記
Auto"auto"すべて—デフォルト。利用可能な最適なバックエンドを自動的に選択します。
PyTorch"torch"CPU / CUDA—純粋なPyTorch実装。最も広い互換性。
Triton"tritonv2"CUDAtritonGPU推論用のTritonベースのカーネル。
ExllamaV2"exllamav2"CUDAgptqmodel>=5.8.0ExllamaV2カーネルによる優れたCUDAパフォーマンス。
Marlin"marlin"CUDAgptqmodel>=5.8.0Marlinカーネルによる最高のCUDAパフォーマンス。
from diffusers import AutoRoundConfig

# Auto-select (default)
config = AutoRoundConfig()

# Explicit Triton backend for CUDA
config = AutoRoundConfig(backend="tritonv2")

# Marlin backend for best CUDA performance (requires gptqmodel>=5.8.0)
config = AutoRoundConfig(backend="marlin")

# ExllamaV2 backend for good CUDA performance (requires gptqmodel>=5.8.0)
config = AutoRoundConfig(backend="exllamav2")

# PyTorch backend for CPU/CUDA inference
config = AutoRoundConfig(backend="torch")

保存と読み込み

保存
load

AutoRoundでは、モデルの量子化にデータキャリブレーションが必要です。これはDiffusersの外側で、AutoRoundライブラリを直接使用して行います。

from auto_round import AutoRound

autoround = AutoRound(
    "Tongyi-MAI/Z-Image",
    scheme="W4A16",  # W4G128 symmetric
    enable_torch_compile=True,
    num_inference_steps=3,
    guidance_scale=7.5,
    dataset="coco2014",
)
autoround.quantize_and_save("Z-Image-W4A16-AutoRound")

キャリブレーションのオプションの詳細については、AutoRoundのドキュメントを参照してください。

サポートされている量子化スキーム

AutoRoundは複数のSchemesをサポートしています:

  • W4A16(bits:4,group_size:128,sym:True,act_bits:16)
  • W8A16(bits:8,group_size:128,sym:True,act_bits:16)
  • W3A16(bits:3,group_size:128,sym:True,act_bits:16)
  • W2A16(bits:2,group_size:128,sym:True,act_bits:16)
  • GGUF:Q4_K_M(llamacpp が提供するすべての Q_K,Q_0,Q*_1 はサポートされています)
  • NVFP4(実験的な機能です。llm_compressor形式へのエクスポートを推奨します。data_type nvfp4,act_data_type nvfp4,static_global_scale,group_size 16)
  • MXFP4(研究機能、実用カーネルなし、標準 MXFP4、data_type mxfp、act_data_type mxfp、bits 4、act_bits 4、group_size 32)
  • MXINT4(研究機能、実用カーネルなし、Standard MXINT4、data_type mxint、act_data_type mxint、bits 4、act_bits 4、group_size 32)
  • MXFP4_RCEIL(研究機能、実装されたカーネルなし、NVIDIA のバリアント、data_type mxfp、act_data_type mxfp_rceil、bits 4、act_bits 4、group_size 32)
  • MXFP8(研究機能、実装済みカーネルなし、data_type mxfp、act_data_type mxfp_rceil、group_size 32)
  • FPW8A16(研究機能、実装済みカーネルなし、data_type fp8、group_size 0->テンソルごと)
  • FP8_STATIC(研究段階の機能、実装済みカーネルなし、data_type:fp8、act_data_type:fp8、group_size -1 ->チャンネルごと、act_group_size=0->テンソルごと)

また、group_size、bits、sym など、必要に応じて多くの設定を変更できますが、実際には対応するカーネルが存在しない可能性があります。

リソース

Update on GitHub