In a recent development, large diffusion models like Flux (a flow-based text-to-image generation model) can create stunning images, but their size can be a hurdle, demanding significant memory and compute resources. Quantization offers a powerful solution, shrinking these models to make them more accessible without drastically compromising performance. But the big question always is: can you actually tell the difference in the final image?
Before we dive into the technical details of how various quantization backends in Hugging Face Diffusers work, why not test your own perception? (Related: Faster TensorFlow models in Hugging Face Transformers...)
In a recent development, we created a setup where you can provide a prompt, and we generate results using both the original, high-precision model (e.g., Flux-dev in BF16) and several quantized versions (BnB 4-bit, BnB 8-bit). The generated images are then presented to you and your challenge is to identify which ones came from the quantized models. (Related: How to train a newly introduced language model from scr...)
Industry observers note that often, especially with 8-bit quantization, the differences are subtle and may not be noticeable without close inspection. More aggressive quantization like 4-bit or lower might be more noticeable, but the results can still be good, especially considering the massive memory savings. NF4 often gives the best trade-off though.
Building on our previous post, "Memory-efficient Diffusion Transformers with Quanto and Diffusers", this post explores the diverse quantization backends integrated directly into Hugging Face Diffusers. We'll examine how bitsandbytes, GGUF, torchao, Quanto and native FP8 support make large and powerful models more accessible, demonstrating their use with Flux.
Before diving into the quantization backends, let's introduce the FluxPipeline (using the black-forest-labs/FLUX.1-dev checkpoint) and its components, which we'll be quantizing. Loading the full FLUX.1-dev model in BF16 precision requires approximately 31.447 GB of memory. The main components are:.
It has been revealed that bitsandbytes is a popular and user-friendly library for 8-bit and 4-bit quantization, widely used for LLMs and QLoRA fine-tuning. We can use it for transformer-based diffusion and flow models, too.
All benchmarks performed on 1x NVIDIA H100 80GB GPU.
According to recent reports, note: When using PipelineQuantizationConfig with bitsandbytes, you need to import DiffusersBitsAndBytesConfig from diffusers and TransformersBitsAndBytesConfig from transformers separately. This is because these components originate from different libraries. If you prefer a simpler setup without managing these distinct imports, you can use an alternative approach for pipeline-level quantization, an example of this method is in the Diffusers documentation on Pipeline-level quantization.
For more information check out the bitsandbytes docs.
torchao is a PyTorch-native library for architecture optimization, offering quantization, sparsity, and custom data types, designed for compatibility with torch.compile and FSDP. Diffusers supports a wide range of torchao's exotic data types, enabling fine-grained control over model optimization.
For more information check out the torchao docs.
Quanto is a quantization library integrated with the Hugging Face ecosystem via the optimum library.
Note: At the time of writing, for float8 support with Quanto, you'll need optimum-quanto<0.2.5 and use quanto directly. We will be working on fixing this.
For more information check out the Quanto docs.
GGUF is a file format popular in the llama.cpp community for storing quantized models.
For more information check out the GGUF docs.
FP8 Layerwise Casting is a memory optimization technique. It works by storing the model's weights in the compact FP8 (8-bit floating point) format, which uses roughly half the memory of standard FP16 or BF16 precision. Just before a layer performs its calculations, its weights are dynamically cast up to a higher compute precision (like FP16/BF16). Immediately afterward, the weights are cast back down to FP8 for efficient storage. This approach works because the core computations retain high precision, and layers particularly sensitive to quantization (like normalization) are typically skipped. This technique can also be combined with group offloading for further memory savings.
For more information check out the Layerwise casting docs.
Most of these quantization backends can be combined with the memory optimization techniques offered in Diffusers. Let's explore CPU offloading, group offloading, and torch.compile. You can learn more about these techniques in the Diffusers documentation.
Note: At the time of writing, bnb + torch.compile also works if bnb is installed from source and using pytorch nightly or with fullgraph=False.
Model CPU Offloading (enable_model_cpu_offload): This method moves entire model components (like the UNet, text encoders, or VAE) between the CPU and GPU during the inference pipeline. It offers substantial VRAM savings and is generally faster than more granular offloading because it involves fewer, larger data transfers.
bnb + enable_model_cpu_offload:.
Group offloading (enable_group_offload for diffusers components or apply_group_offloading for generic torch.nn.Modules): It moves groups of internal model layers (like torch.nn.ModuleList or torch.nn.Sequential instances) to the CPU. This approach is typically more memory-efficient than full model offloading and faster than sequential offloading.
FP8 layerwise casting + group offloading:.
Note: torch.compile can introduce subtle numerical differences, leading to changes in image output.
torch.compile: Another complementary approach is to accelerate the execution of your model with PyTorch 2.xβs torch.compile() feature. Compiling the model doesnβt directly lower memory, but it can significantly speed up inference. PyTorch 2.0βs compile (Torch Dynamo) works by tracing and optimizing the model graph ahead-of-time.
Explore some benchmarking results here:.
You can find bitsandbytes and torchao quantized models from this blog post in our Hugging Face collection: link to collection.
Here's a quick guide to choosing a quantization backend:.
Quantization significantly lowers the barrier to entry for using large diffusion models. Experiment with these backends to find the best balance of memory, speed, and quality for your needs.
Acknowledgements: Thanks to Chunte for providing the thumbnail for this post.