Buckets:
| # はじめる | |
| Quantizationは、元のデータの精度を維持しつつ、より少ないビット数でデータを表現することに焦点を当てています。これは通常、同じ情報をより少ないビット数で表現するためにデータ型を変換することを意味します。例えば、モデルの重みが32ビット浮動小数点数として保存されている場合、これを16ビット浮動小数点数にQuantizationすると、モデルサイズが半分になり、保存が容易になり、メモリ使用量が削減されます。また、精度が低くなると、少ないビット数での計算により時間が短縮されるため、推論の速度も向上します。 | |
| Diffusers は、[Flux](../api/pipelines/flux) などの大規模な拡散モデルをより利用しやすくするために、複数の量子化バックエンドをサポートしています。このガイドでは、事前学習済みまたは非量子化のチェックポイントからパイプラインを初期化する際に、[PipelineQuantizationConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.PipelineQuantizationConfig) クラスを使用してパイプラインを量子化する方法を示します。 | |
| ## パイプラインレベルの量子化 | |
| 量子化設定にどの程度のカスタマイズを適用したいかによって、[PipelineQuantizationConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.PipelineQuantizationConfig)の使用方法は2つあります。 | |
| - 基本的なユースケースでは、`quant_backend`、`quant_kwargs`、`components_to_quantize` の引数を定義します。 | |
| - 詳細な量子化制御を行うには、`quant_mapping`を定義して、個々のモデルコンポーネントに対する量子化設定を指定します。 | |
| ### 基本的な量子化 | |
| [PipelineQuantizationConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.PipelineQuantizationConfig) を以下のパラメータで初期化します。 | |
| - `quant_backend` は使用する量子化バックエンドを指定します。現在サポートされているバックエンドには、`bitsandbytes_4bit`、`bitsandbytes_8bit`、`gguf`、`quanto`、`torchao` が含まれます。 | |
| - `quant_kwargs` は、使用する量子化引数を指定します。 | |
| > [!TIP] | |
| > これらの `quant_kwargs` 引数は、バックエンドごとに異なります。各バックエンドの引数については、[Quantization API](../api/quantization) のドキュメントを参照してください。 | |
| - `components_to_quantize` は、パイプラインのどのコンポーネントを量子化するかを指定します。通常、transformer などの計算負荷が最も大きいコンポーネントを量子化すべきです。[FluxPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/flux#diffusers.FluxPipeline) のように複数のテキストエンコーダーを持つパイプラインでは、テキストエンコーダーも量子化の対象として検討すべきです。以下の例では、[FluxPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/flux#diffusers.FluxPipeline) の T5 テキストエンコーダーを量子化し、CLIP モデルはそのままにしています。 | |
| `components_to_quantize` は、複数のモデルを指定する場合はリストを、単一のモデルを指定する場合は文字列を受け取ります。 | |
| 以下の例では、[BitsAndBytesConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.BitsAndBytesConfig)から `load_in_4bit`、`bnb_4bit_quant_type`、`bnb_4bit_compute_dtype` という引数を指定して、bitsandbytes バックエンドを読み込みます。 | |
| ```py | |
| import torch | |
| from diffusers import DiffusionPipeline | |
| from diffusers.quantizers import PipelineQuantizationConfig | |
| pipeline_quant_config = PipelineQuantizationConfig( | |
| quant_backend="bitsandbytes_4bit", | |
| quant_kwargs={"load_in_4bit": True, "bnb_4bit_quant_type": "nf4", "bnb_4bit_compute_dtype": torch.bfloat16}, | |
| components_to_quantize=["transformer", "text_encoder_2"], | |
| ) | |
| ``` | |
| パイプラインを量子化するには、`pipeline_quant_config` を [from_pretrained()](/docs/diffusers/v0.43.0/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) に渡します。 | |
| ```py | |
| pipe = DiffusionPipeline.from_pretrained( | |
| "black-forest-labs/FLUX.1-dev", | |
| quantization_config=pipeline_quant_config, | |
| dtype=torch.bfloat16, | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| image = pipe("photo of a cute dog").images[0] | |
| ``` | |
| ### 高度な量子化 | |
| `quant_mapping` 引数により、パイプライン内の各コンポーネントの量子化方法をより詳細に指定できます。異なる量子化バックエンドを組み合わせることも可能です。 | |
| [PipelineQuantizationConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.PipelineQuantizationConfig) を初期化し、`quant_mapping` を渡します。`quant_mapping` を使用すると、transformer や text encoder など、パイプライン内の各コンポーネントに対する量子化オプションを指定できます。 | |
| 以下の例では、transformerとテキストエンコーダーにそれぞれ2つの量子化バックエンド、[QuantoConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.QuantoConfig)と[transformers.BitsAndBytesConfig](https://huggingface.co/docs/transformers/v5.22.2/en/main_classes/quantization#transformers.BitsAndBytesConfig)を使用しています。 | |
| ```py | |
| import torch | |
| from diffusers import DiffusionPipeline | |
| from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig | |
| from diffusers.quantizers.quantization_config import QuantoConfig | |
| from diffusers.quantizers import PipelineQuantizationConfig | |
| from transformers import BitsAndBytesConfig as TransformersBitsAndBytesConfig | |
| pipeline_quant_config = PipelineQuantizationConfig( | |
| quant_mapping={ | |
| "transformer": QuantoConfig(weights_dtype="int8"), | |
| "text_encoder_2": TransformersBitsAndBytesConfig( | |
| load_in_4bit=True, compute_dtype=torch.bfloat16 | |
| ), | |
| } | |
| ) | |
| ``` | |
| [Transformers](https://huggingface.co/docs/transformers/main_classes/quantization#transformers.BitsAndBytesConfig)には別のbitsandbytesバックエンドがあります。Transformers由来のコンポーネントには、[transformers.BitsAndBytesConfig](https://huggingface.co/docs/transformers/v5.22.2/en/main_classes/quantization#transformers.BitsAndBytesConfig)をインポートして使用する必要があります。例えば、[FluxPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/flux#diffusers.FluxPipeline)内の`text_encoder_2`はTransformersの[T5EncoderModel](https://huggingface.co/docs/transformers/v5.22.2/en/model_doc/t5#transformers.T5EncoderModel)であるため、[diffusers.BitsAndBytesConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.BitsAndBytesConfig)ではなく[transformers.BitsAndBytesConfig](https://huggingface.co/docs/transformers/v5.22.2/en/main_classes/quantization#transformers.BitsAndBytesConfig)を使用する必要があります。 | |
| > [!TIP] | |
| > これらの個別のインポートを管理したくない場合、または各パイプラインコンポーネントの由来が不明な場合は、上記の[基本的な量子化](#basic-quantization)メソッドを使用してください。 | |
| ```py | |
| import torch | |
| from diffusers import DiffusionPipeline | |
| from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig | |
| from diffusers.quantizers import PipelineQuantizationConfig | |
| from transformers import BitsAndBytesConfig as TransformersBitsAndBytesConfig | |
| pipeline_quant_config = PipelineQuantizationConfig( | |
| quant_mapping={ | |
| "transformer": DiffusersBitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16), | |
| "text_encoder_2": TransformersBitsAndBytesConfig( | |
| load_in_4bit=True, compute_dtype=torch.bfloat16 | |
| ), | |
| } | |
| ) | |
| ``` | |
| パイプラインを量子化するには、`pipeline_quant_config` を [from_pretrained()](/docs/diffusers/v0.43.0/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) に渡します。 | |
| ```py | |
| pipe = DiffusionPipeline.from_pretrained( | |
| "black-forest-labs/FLUX.1-dev", | |
| quantization_config=pipeline_quant_config, | |
| dtype=torch.bfloat16, | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| image = pipe("photo of a cute dog").images[0] | |
| ``` | |
| ## 事前量子化済みチェックポイントの読み込み | |
| 一部の量子化バックエンドは、すでに量子化されて Hub にアップロードされたチェックポイントの読み込みをサポートしています。この場合、量子化設定はモデルの `config.json` に保存され、自動的に読み込まれます。そのため、[PipelineQuantizationConfig](/docs/diffusers/v0.43.0/ja/api/quantization#diffusers.PipelineQuantizationConfig) を作成したり、`quantization_config` 引数を渡したりする必要はありません。 | |
| ```py | |
| import torch | |
| from diffusers import DiffusionPipeline | |
| pipe = DiffusionPipeline.from_pretrained( | |
| "rootonchair/ERNIE-Image-Turbo-nunchaku-lite-nvfp4", | |
| dtype=torch.bfloat16, | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| ``` | |
| 以下のバックエンドは、プリクオンタイズ済みチェックポイントの読み込みをそのままサポートしています。 | |
| | バックエンド | 注記 | | |
| |---|---| | |
| | [bitsandbytes](./bitsandbytes) | Config は `config.json` に保存されます。追加の引数は不要です。オンザフライでの量子化と、事前量子化済みチェックポイントの読み込みの両方をサポートしています。 | | |
| | [torchao](./torchao) | 上記と同様です。 | | |
| | [GGUF](./gguf) | Model クラスで `from_single_file` を使用します。パイプラインレベルでの読み込みはサポートされていません。 | | |
| | [BFL](./bfl) | Black Forest Labs の `fp8r` および `nvfp4` の単一ファイル形式のチェックポイントを `from_single_file` で読み込みます。パイプラインレベルでの読み込みはサポートされていません。 | | |
| | [AutoRound](./autoround) | 読み込みのみがサポートされています。まず、AutoRound CLIまたはPython APIで量子化してください。 | | |
| | [Nunchaku Lite](./nunchaku) | Config は `config.json` に保存されます。`kernels` パッケージが必要です。読み込みのみサポートされています。 | | |
| | [ModelOpt](./modelopt) | 事前量子化済みモデルの読み込みのみがサポートされています。 | | |
| | [SDNQ](./sdnq) | `sdnq`パッケージが必要です。オンザフライでの量子化と、事前量子化済みモデルの読み込みの両方をサポートします。 | | |
| ## リソース | |
| 量子化について詳しく学ぶには、以下のリソースをご覧ください。 | |
| - 量子化が初めての方は、DeepLearning.AI との協力により提供されている、初心者向けの以下のコースを参照することをお勧めします。 | |
| - [Hugging Faceによる量子化の基礎](https://www.deeplearning.ai/short-courses/quantization-fundamentals-with-hugging-face/) | |
| - [量子化の深掘り](https://www.deeplearning.ai/short-courses/quantization-in-depth/) | |
| - 新しい量子化手法の追加にご興味がある場合は、[新しい量子化手法の貢献ガイド](https://huggingface.co/docs/transformers/main/en/quantization/contribute)を参照してください。 | |
| - Transformers の量子化 [概要](https://huggingface.co/docs/transformers/quantization/overview#when-to-use-what) では、異なる量子化バックエンドの長所と短所について概説しています。 | |
| - [Diffusersにおける量子化バックエンドの探索](https://huggingface.co/blog/diffusers-quantization)のブログ記事では、各量子化バックエンドの簡単な紹介、バックエンドの選択方法、および量子化と他のメモリ最適化の組み合わせについて解説しています。 | |
Xet Storage Details
- Size:
- 12.1 kB
- Xet hash:
- 14193900ce1a42f8fa127b2b56fc46e60f00d9e373a20d87b7bf052baaf05f96
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.