Bookmarks

QLoRA — QLoRA method

Quantized Low-Rank Adaptation

A method of fine-tuning language models in which the frozen base model is stored in 4 bits and LoRA adapters are trained in 16-bit format.

Also: QLoRA, quantised LoRA, 4-bit fine-tuning

checked 18 Nov

All terms

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

TechniqueWhat 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 Quantizationquantises the quantisation constants themselves; saves about 0.37 bits per parameter on average, or roughly 3 GB for a 65-billion-parameter model
Paged Optimizerswhen 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

python4-bit loading settings via bitsandbytes; the model is then prepared with `prepare_model_for_kbit_training()` and LoRA adapters are added
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 pages

Verified 18 Nov

Not to be confused with

what it is mixed up with and how it differs

  • LoRALoRA method

    LoRA trains adapters on top of a 16-bit model, whereas QLoRA stores the frozen base in 4 bits and so reduces memory.

  • quantisation

    Ordinary quantisation compresses a finished model so it can be run, whereas QLoRA uses compression to make fine-tuning possible.

In depth in the references

where the topic is covered in detail