It seems you're interested in using Unsloth's dynamic quantization specifically within the Hugging Face transformers library. Unsloth is a library that builds on top of transformers and PyTorch to provide faster and more memory-efficient fine-tuning and inference for large language models, including dynamic quantization techniques[1][2].
Here's how you can generally use quantization, including dynamic quantization methods offered by libraries like Unsloth and bitsandbytes, with the transformers library:
Understanding Quantization in transformers
The transformers library supports various quantization methods, primarily through integration with libraries like bitsandbytes and torchao[3][4][5].
bitsandbytes: This is a popular library for 8-bit and 4-bit quantization, particularly for loading and running models with reduced memory. It offers techniques like LLM.int8() and QLoRA. transformers provides BitsAndBytesConfig to easily configure and load models with bitsandbytes quantization[4][5][6].
torchao: A PyTorch library for architecture optimization, including quantization. transformers has integration with torchao for various quantization methods like A16W8 (activations in FP16, weights in INT8) and A8W8 (activations and weights in INT8)[3].
- Other methods:
transformers also has support for other quantization algorithms like AWQ and GPTQ[4][8].
Unsloth's Dynamic Quantization
Unsloth has developed its own dynamic 4-bit quantization method[1]. This method selectively quantizes parameters, aiming to improve accuracy compared to standard quantization while still saving memory[1][11][12]. Unsloth models, including those with dynamic quantization, are often available on the Hugging Face Hub[11][2].
How to Use Quantization in transformers (General Approach)
The standard way to load a quantized model or apply quantization on the fly when loading a model using transformers is by using the from_pretrained method and providing a quantization_config[4][10].
Here's a general example using BitsAndBytesConfig for 8-bit quantization:
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
# Define the quantization configuration
quantization_config = BitsAndBytesConfig(
load_in_8bit=True
)
# Load the model with quantization
model = AutoModelForCausalLM.from_pretrained(
"your-model-name", # Replace with the model identifier
quantization_config=quantization_config,
device_map="auto" # Automatically distribute the model layers
)