Search

Search papers and lessons

QLoRA: Efficient Finetuning of Quantized LLMs

Tim Dettmers, Artidoro Pagnoni, Ari Holtzman et al. · 2023

QLoRA fine-tunes a 4-bit frozen model through small LoRA adapters, cutting 65B fine-tuning memory from over 780 GB to under 48 GB while matching 16-bit fine-tuning on the benchmarks tested.

On this page

  • Colour = topic mix
  • Linear algebra
  • Post-training
  • Calculus
  • Efficiency
  • Probability
  • Neural network basics
  • Sign in to see your progress

TL;DR

Full fine-tuning of a 65B-parameter model needs more than 780 GB of GPU memory, so almost nobody can do it. QLoRA freezes the base model at 4 bits and trains small low-rank adapters on top, backpropagating through the quantized weights. The paper reports that this brings 65B fine-tuning down to under 48 GB on one GPU while matching 16-bit fine-tuning on the benchmarks it tests.

Where you'll see it: in the paper's Guanaco chatbot family, whose 7B to 65B adapters the authors release, and in any setup that fine-tunes a large model on a single GPU.

The problem

Fine-tuning improves a language model and can add or remove behaviours. The paper's introduction notes that regular 16-bit fine-tuning of LLaMA 65B needs more than 780 GB of GPU memory. Quantization can shrink a model to 4 bits for inference, but the paper says such methods break down during training.

The gap is that the weights are not the only memory cost. The paper's background section gives a concrete breakdown for a 7B LLaMA with LoRA at batch size 1: the LoRA input gradients take 567 MB, the LoRA parameters take 26 MB, and the 4-bit base model takes 5,048 MB. Shrinking the adapters barely helps, so the base model and the activations are where the savings have to come from.

Key idea

QLoRA combines three ideas, each aimed at a different part of the memory budget.

  1. 4-bit NormalFloat (NF4). A 4-bit data type designed for weights that are roughly zero-centred and normally distributed, so each quantization bin holds about the same number of weights.
  2. Double quantization. The per-block quantization constants are themselves quantized, which trims about 0.37 bits per parameter.
  3. Paged optimizers. Optimizer state lives in NVIDIA unified memory, so it can move to CPU RAM when a long batch causes a memory spike, instead of crashing the run.

The base model is stored in 4 bits and frozen. The only trainable weights are LoRA adapters, kept in 16-bit precision. During the forward and backward pass, the 4-bit weights are dequantized to BFloat16 for the matrix multiplications, but gradients are computed only for the adapters.

The method

Block-wise quantization

Quantizing a tensor to kk bits means rescaling its values into the range of the target format and rounding. The paper's first formula rescales an FP32 tensor by its absolute maximum, so that the range [−127,127][-127, 127] of an 8-bit integer is used. A single outlier then wastes most of the bins. The standard fix is to split the tensor into blocks of size BB, quantize each block with its own constant cc, and store one constant per block.

NormalFloat

The paper assumes pretrained weights are roughly zero-centred normal with some standard deviation σ\sigma. Rescaling each weight tensor so that its standard deviation matches the target format lets one fixed data type serve every tensor. The paper builds that data type from the quantiles of N(0,1)N(0, 1), placed so that each bin holds the same expected number of weights. The construction is asymmetric, with 2k−12^{k-1} values for the negative side and 2k−1+12^{k-1} + 1 for the positive side, so that zero is represented exactly. Padding and other zero entries then quantize with no error. Appendix E of the paper gives the exact values.

The key claim is that equal-probability bins are information-theoretically optimal for normally distributed data. The paper then tests this empirically, which is covered in the results below.

Double quantization

With 32-bit constants and a block size of 64, the constants alone add 32/64=0.532 / 64 = 0.5 bits per parameter. Double quantization quantizes those constants again, using 8-bit floats with a block size of 256 for the second step, and it centres the constants by subtracting their mean first. The per-parameter cost falls to 8/64+32/(64⋅256)=0.1278/64 + 32/(64 \cdot 256) = 0.127 bits, a saving of 0.373 bits per parameter.

Paged optimizers

The paper uses NVIDIA unified memory to allocate optimizer state in paged memory. When the GPU runs out of memory, pages move to CPU RAM, and they come back when the optimizer update needs them. The paper says this matters most for 33B and 65B models on 24 GB and 48 GB GPUs, where long mini-batches cause spikes. It does not give hard measurements for the paging itself, because spikes are rare.

The QLoRA layer

For one linear layer, with X\mathbf{X} the input, the paper defines

YBF16=XBF16 doubleDequant(c1FP32,c2k-bit,WNF4)+XBF16L1BF16L2BF16\mathbf{Y}^{\text{BF16}} = \mathbf{X}^{\text{BF16}} \, \text{doubleDequant}(c_1^{\text{FP32}}, c_2^{\text{k-bit}}, \mathbf{W}^{\text{NF4}}) + \mathbf{X}^{\text{BF16}} \mathbf{L}_1^{\text{BF16}} \mathbf{L}_2^{\text{BF16}}

Here W\mathbf{W} is the frozen 4-bit weight, L1\mathbf{L}_1 and L2\mathbf{L}_2 are the low-rank adapter matrices, and doubleDequant undoes both levels of quantization. The paper uses NF4 for the weights, FP8 for the second-level constants, a block size of 64 for the weights and 256 for the constants. Gradients are needed only with respect to the adapters, but computing them still requires passing through the dequantized weights, which is why the forward pass is in BFloat16.

Hyperparameters

The paper reports the settings it used for all QLoRA runs: NF4 with double quantization, a BFloat16 compute type, LoRA rank r=64r = 64 with α=16\alpha = 16, and LoRA modules on all linear layers of the base model. It uses Adam with β2=0.999\beta_2 = 0.999, gradient norm clipping at 0.3, and LoRA dropout of 0.1 for models up to 13B and 0.05 for 33B and 65B. It uses a constant learning rate and groups examples of similar length into the same batch. Per-model learning rates and batch sizes are in its Table 9, and the authors use the MMLU 5-shot dev set for tuning.

The paper also checks that its baselines are fair. For full fine-tuning, the default hyperparameters were undertuned, so the authors searched learning rates from 10−610^{-6} to 5×10−55 \times 10^{-5} and batch sizes from 8 to 128.

Results

Matching 16-bit fine-tuning

On the paper's academic benchmarks, QLoRA with NF4 and double quantization matches 16-bit fine-tuning. Table 3 compares fine-tuning on GLUE with RoBERTa-large and on Super-NaturalInstructions with T5 models. For T5-11B, BF16 full fine-tuning scores 62.0 RougeL, LoRA in BF16 scores 60.7, and QLoRA with NF4 and double quantization scores 60.9. The paper's summary says 4-bit QLoRA with NF4 "matches 16-bit full finetuning and 16-bit LoRA finetuning performance on academic benchmarks with well-established evaluation setups".

For LLaMA 7B to 65B fine-tuned on Alpaca and FLAN v2 and evaluated by 5-shot MMLU (Table 4), the caption says NF4 with double quantization matches BFloat16 performance, while FP4 is consistently about one percentage point behind.

NF4 beats FP4 and Int4

The paper measures post-quantization perplexity on the Pile Common Crawl, averaged over 125M to 13B OPT, BLOOM, LLaMA and Pythia models (Table 2). The results are:

Data typeMean perplexity
Int434.34
Float4 (E2M1)31.07
Float4 (E3M0)29.48
NFloat4 with double quantization27.41

Lower is better, so NF4 with double quantization has the lowest perplexity. The gap to Float4 (E3M0) is 29.48−27.41=2.0729.48 - 27.41 = 2.07 points.

Memory

The paper reports that QLoRA reduces the average memory needed to fine-tune a 65B model from more than 780 GB to under 48 GB, without degrading runtime or predictive performance relative to a 16-bit baseline. It also reports that a 7B model deployed with QLoRA needs about 5 GB, and that the paper's Figure 6 shows a 33B model does not fit into a 24 GB GPU without paged optimizers.

Guanaco and chatbot evaluation

The Guanaco family is the paper's showcase. On the Vicuna benchmark, the abstract says the best model reaches 99.3% of ChatGPT's performance, trained for 24 hours on one GPU, and the second-best model reaches 97.8% in less than 12 hours on a consumer GPU. In the Elo tournament of Table 1, judged by GPT-4, Guanaco 65B scores 1022 against 966 for ChatGPT and 1348 for GPT-4.

The paper is careful about these numbers. It notes that Table 6 has very wide confidence intervals, that many models overlap, and that GPT-4 and human annotators show only moderate agreement (Fleiss kappa 0.25 at the example level), and that GPT-4 rates its own outputs higher than humans do. It recommends Elo rankings over the scaled scores, and it says its human-evaluation results point to limitations in current benchmarks and evaluation protocols.

Data matters more than size

The paper finds that data quality matters more than dataset size. A 9k-sample dataset (OASST1) outperformed a subsampled 450k-sample FLAN v2 dataset on chatbot performance. Strong MMLU performance also did not imply strong Vicuna performance, so the authors argue that dataset suitability matters more than size for a given task.

Why it mattered

The paper's own framing is accessibility. It says QLoRA makes the largest publicly available models fine-tunable on a single GPU, and it releases adapters for 7B, 13B, 33B and 65B models trained on eight instruction datasets, 32 models in total, along with the code, CUDA kernels for 4-bit training, and a Hugging Face integration.

It also states its limits. The authors did not establish that QLoRA matches full 16-bit fine-tuning at 33B and 65B scales, because the cost of those baselines was too high. They leave open where the performance-precision tradeoff lies for QLoRA, and they say that with a fixed budget it can be better to use a larger base model at lower precision.

Reading guide

Read the paper in this order. Each step names the prerequisites it needs.

  1. Section 1, Introduction. Needs Fine-Tuning for what is being optimised, and LoRA as the adapter method that QLoRA builds on.
  2. Section 2, Background. Needs Model Compression for quantization in general, Gaussian (Normal) Distribution for the normal weight assumption behind NF4, and LoRA for the adapter formula.
  3. Section 3, QLoRA finetuning. Needs QLoRA for the full method and Adam Optimizer for the optimizer whose state is paged. Work through the NF4 and double quantization paragraphs with the Gaussian lesson open.
  4. Section 4, QLoRA vs. standard finetuning. Needs Fine-Tuning for the baselines and Model Compression for the perplexity comparison.
  5. Section 5, Guanaco and chatbot evaluation. Needs Fine-Tuning for instruction tuning. The Elo method is self-contained.
  6. Appendices. The hyperparameter table (Table 9) and the NF4 values (Appendix E) are reference material for reproducing the runs.

Check yourself

QuestionWhy does the paper quantize the quantization constants, and what does the double-quantization formula save per parameter?Show answer

With 32-bit constants and a block size of 64, the constants add 32/64=0.532 / 64 = 0.5 bits per parameter. Double quantization stores them in 8-bit floats with a block size of 256, which brings the cost to 8/64+32/(64⋅256)=0.1278/64 + 32/(64 \cdot 256) = 0.127 bits. The saving is 0.3730.373 bits per parameter.

QuestionIn the paper's memory breakdown for a 7B LLaMA with LoRA at batch size 1, the 4-bit base model takes 5,048 MB and the LoRA parameters take 26 MB. Why does the paper say that shrinking the adapters gives only minor memory benefits?Show answer

The adapter parameters are tiny compared with the base model and the activation gradients. Reducing them further saves only a few megabytes. The large costs are the base model's weights and the gradients flowing through the activations, which is why QLoRA targets the base model with 4-bit storage and paged optimizers.

QuestionUsing Table 2, what is the perplexity gap between NF4 with double quantization and Float4 (E3M0), and which direction is better?Show answer

The gap is 29.48−27.41=2.0729.48 - 27.41 = 2.07 perplexity points. Lower perplexity is better, so NF4 with double quantization is better by that margin on average over the models tested.

QuestionWhy does QLoRA backpropagate through a 4-bit model without computing gradients for the 4-bit weights?Show answer

Only the LoRA adapters are trained, so only their gradients are needed. Computing the adapter gradients still requires the derivative of the layer's output with respect to its input, which passes through the frozen weights. The weights are dequantized to BFloat16 for that step, but no gradient is computed with respect to them, so they can stay in 4 bits.

QuestionThe paper says it did not establish that QLoRA matches full 16-bit fine-tuning at 33B and 65B scales. Why not, and what does that leave open?Show answer

The authors say the resource cost of the full 16-bit baselines at those scales was immense, so they could not run the comparison. The result that remains open is where the performance-precision tradeoff lies for QLoRA, which the authors leave to future work.

Sources

Read the full paper

Tim Dettmers, Artidoro Pagnoni, Ari Holtzman et al., 2023. arXiv:2305.14314