There are many quantization methods available in Transformers for inference and fine-tuning. This guide helps you choose the most common and production-ready quantization techniques depending on your use case, and presents the advantages and disadvantages of each technique.
For a comprehensive overview of all supported methods and their features, refer back to the table in the Overview.
Consider the quantization methods below for inference.
| quantization method | use case |
|---|---|
| bitsandbytes | ease of use and QLoRA fine-tuning on NVIDIA GPUs |
| compressed-tensors | loading specific quantized formats (FP8, Sparse) |
| GPTQModel or AWQ | good 4-bit accuracy with upfront calibration |
| HQQ | fast on the fly quantization without calibration |
| torchao | flexibility and fast inference with torch.compile |
These methods are generally easier to use as they don’t need a separate calibration dataset or step.
| Pros | Cons |
|---|---|
| Very simple, no calibration dataset required for inference. | Primarily optimized for NVIDIA GPUs (CUDA). |
| Good community support and widely adopted. | Inference speedup isn’t guaranteed. |
See the bitsandbytes documentation for more details.
| Pros | Cons |
|---|---|
| Fast quantization process, no calibration data needed. | Accuracy can degrade significantly at bit depths <4-bit. |
| Multiple backends for fast inference. | Inference speed may not match others unless using torch.compile or backends. |
Compatible with torch.compile. | |
| Supports wide range of bit depths (8, 4, 3, 2, 1-bit). |
See the HQQ documentation for more details.
| Pros | Cons |
|---|---|
Strong integration with torch.compile for potential speedups. | Newer library, ecosystem still evolving. |
| Offers decent CPU quantization support. | Performance depends on torch.compile working well. |
| Flexibility in quantization schemes (int8, int4, fp8). | 4-bit quantization (int4wo) may not match GPTQ/AWQ in accuracy. |
See the torchao documentation for more details.
These methods require an upfront calibration step using a dataset to potentially achieve higher accuracy.
Calibration for 8B model takes ~20 minutes on one A100 gpu.
| Pros | Cons |
|---|---|
| Often achieves high accuracy. | Requires a calibration dataset and a separate calibration step. |
| Can lead to inference speedups. | Possible to overfit on calibration data. |
| Many pre-quantized GPTQ models on Hugging Face Hub. |
See the GPTQ documentation for more details.
Calibration for 8B model takes ~10 minutes on one A100 gpu.
| Pros | Cons |
|---|---|
| Often achieves high accuracy at 4-bit. (Sometimes surpasses GPTQ on specific tasks.) | Requires calibration if quantizing yourself. |
| Can lead to inference speedups. | |
| Shorter calibration time than GPTQ. | |
| Many pre-quantized AWQ models on Hugging Face Hub. |
See the AWQ documentation for more details.
| Pros | Cons |
|---|---|
| Supports flexible formats including FP8 and sparsity. | Primarily for loading pre-quantized models. |
| Doesn’t perform quantization within Transformers directly. |
See the compressed-tensors documentation for more details.
Consider the quantization method below during fine-tuning to save memory.
Other methods offer PEFT compatibility, though bitsandbytes is the most established and straightforward path for QLoRA.
See the bitsandbytes documentation and PEFT Docs for more details.
Methods like AQLM, SpQR, VPTQ, HIGGS, etc., push the boundaries of compression (< 2-bit) or explore novel techniques.
Always benchmark the performance (accuracy and speed) of the quantized model on your specific task and hardware to ensure it meets your requirements. Refer to the individual documentation pages linked above for detailed usage instructions.