QLoRA is LoRA on top of a quantised model: the base weights are compressed to 4 bits and stay unchanged, and only small adapters are trained. This makes it possible to fine-tune a large model on a single GPU. The method was proposed by Dettmers and co-authors in 2023.
How it works
Gradients flow through the frozen 4-bit model and reach only the LoRA adapters. One data type is used to store the weights and another for computation, usually BF16: when a layer needs its weights, they are dequantised to BF16 and multiplied in 16 bits. According to the authors, quality stays on a par with full 16-bit fine-tuning.
Three tricks that save memory
| Technique | What it does |
|---|---|
| NF4 (4-bit NormalFloat) | a 4-bit data type for weights that are close to a normal distribution: each quantisation bin holds an equal number of weights |
| Double Quantization | quantises the quantisation constants themselves; saves about 0.37 bits per parameter on average, or roughly 3 GB for a 65-billion-parameter model |
| Paged Optimizers | when GPU memory runs short, optimiser states are automatically moved to CPU RAM and brought back when needed |
For comparison: ordinary 16-bit fine-tuning of a 65-billion-parameter LLaMA model requires more than 780 GB of GPU memory, whereas with QLoRA the authors fitted it into 48 GB.
How to enable it in Transformers and PEFT
import torchfrom transformers import BitsAndBytesConfig config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True, bnb_4bit_compute_dtype=torch.bfloat16,)Sources
the term is written from these pagesVerified 18 Nov