Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
feat: pipeline-level quantization config#11130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
316ff46eec5b98c94d85a4d3dededc79f32df749e4d0ad15ef8b514b925094113d5589f6784375a85871557136d0d9814ff8d1bd1c7e07746861da182bcce078f134bf2b39e03b76e0a695061b969325173f1ad1dc90b06872c91efbdf4c6da6df869a418a95b6ee10478a353f96bcc70ae2a9ad6b48eaca2e116ffb974f86ee773037a68b7b8a73dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -39,3 +39,90 @@ Diffusers currently supports the following quantization methods. | ||
| - [Quanto](./quanto.md) | ||
| [This resource](https://huggingface.co/docs/transformers/main/en/quantization/overview#when-to-use-what) provides a good overview of the pros and cons of different quantization techniques. | ||
| ## Pipeline-level quantization | ||
| Diffusers allows users to directly initialize pipelines from checkpoints that may contain quantized models ([example](https://huggingface.co/hf-internal-testing/flux.1-dev-nf4-pkg)). However, users may want to apply | ||
| quantization on-the-fly when initializing a pipeline from a pre-trained and non-quantized checkpoint. You can | ||
| do this with [`~quantizers.PipelineQuantizationConfig`]. | ||
| Start by defining a `PipelineQuantizationConfig`: | ||
| ```py | ||
| import torch | ||
| from diffusers import DiffusionPipeline | ||
| from diffusers.quantizers.quantization_config import QuantoConfig | ||
| from diffusers.quantizers import PipelineQuantizationConfig | ||
| from transformers import BitsAndBytesConfig | ||
| pipeline_quant_config = PipelineQuantizationConfig( | ||
sayakpaul marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| quant_mapping={ | ||
| "transformer": QuantoConfig(weights_dtype="int8"), | ||
| "text_encoder_2": BitsAndBytesConfig( | ||
sayakpaul marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| load_in_4bit=True, compute_dtype=torch.bfloat16 | ||
| ), | ||
| } | ||
| ) | ||
| ``` | ||
| Then pass it to [`~DiffusionPipeline.from_pretrained`] and run inference: | ||
| ```py | ||
| pipe = DiffusionPipeline.from_pretrained( | ||
| "black-forest-labs/FLUX.1-dev", | ||
| quantization_config=pipeline_quant_config, | ||
| torch_dtype=torch.bfloat16, | ||
| ).to("cuda") | ||
| image = pipe("photo of a cute dog").images[0] | ||
| ``` | ||
| This method allows for more granular control over the quantization specifications of individual | ||
| model-level components of a pipeline. It also allows for different quantization backends for | ||
| different components. In the above example, you used a combination of Quanto and BitsandBytes. However, | ||
| one caveat of this method is that users need to know which components come from `transformers` to be able | ||
| to import the right quantization config class. | ||
| The other method is simpler in terms of experience but is | ||
| less-flexible. Start by defining a `PipelineQuantizationConfig` but in a different way: | ||
| ```py | ||
| pipeline_quant_config = PipelineQuantizationConfig( | ||
| quant_backend="bitsandbytes_4bit", | ||
sayakpaul marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| quant_kwargs={"load_in_4bit": True, "bnb_4bit_quant_type": "nf4", "bnb_4bit_compute_dtype": torch.bfloat16}, | ||
| components_to_quantize=["transformer", "text_encoder_2"], | ||
| ) | ||
| ``` | ||
| This `pipeline_quant_config` can now be passed to [`~DiffusionPipeline.from_pretrained`] similar to the above example. | ||
| In this case, `quant_kwargs` will be used to initialize the quantization specifications | ||
| of the respective quantization configuration class of `quant_backend`. `components_to_quantize` | ||
| is used to denote the components that will be quantized. For most pipelines, you would want to | ||
| keep `transformer` in the list as that is often the most compute and memory intensive. | ||
| The config below will work for most diffusion pipelines that have a `transformer` component present. | ||
| In most case, you will want to quantize the `transformer` component as that is often the most compute- | ||
| intensive part of a diffusion pipeline. | ||
| ```py | ||
| 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"], | ||
| ) | ||
| ``` | ||
| Below is a list of the supported quantization backends available in both `diffusers` and `transformers`: | ||
| * `bitsandbytes_4bit` | ||
| * `bitsandbytes_8bit` | ||
| * `gguf` | ||
| * `quanto` | ||
| * `torchao` | ||
| Diffusion pipelines can have multiple text encoders. [`FluxPipeline`] has two, for example. It's | ||
| recommended to quantize the text encoders that are memory-intensive. Some examples include T5, | ||
| Llama, Gemma, etc. In the above example, you quantized the T5 model of [`FluxPipeline`] through | ||
| `text_encoder_2` while keeping the CLIP model intact (accessible through `text_encoder`). | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.