Quant Memo
Advanced

QLoRA and Quantized Fine-Tuning

QLoRA fine-tunes huge language models on a single ordinary GPU by freezing and compressing the base model down to 4-bit precision, then training only a tiny set of added low-rank adapter weights in full precision on top of it.

Prerequisites: Instruction Tuning and Supervised Fine-Tuning, Transfer Learning and Fine-Tuning

Fine-tuning a large language model the ordinary way means updating every parameter, and standard training keeps not just weights but gradients and optimiser state (for Adam, two extra numbers per weight) in memory at full precision — for a 65-billion-parameter model, that's well over 780 GB, far beyond any single consumer or most enterprise GPUs. QLoRA makes fine-tuning this large model possible on a single 48 GB GPU by combining two ideas that together cut memory by roughly two orders of magnitude: quantization (storing the frozen base model at much lower numerical precision) and LoRA (training only a tiny number of newly added parameters, never touching the original weights).

The analogy: annotating a reference book without rewriting it

Imagine adapting a 1,000-page reference book for a new audience. One approach is rewriting the entire book — expensive, and you need the full, high-resolution original open the whole time. QLoRA's approach is to photocopy the book at reduced resolution (still readable, just compressed — quantization, applied to the frozen base weights) and write a small set of margin notes and sticky inserts (a handful of new, small trainable matrices — LoRA) that redirect the content for the new audience, without reprinting the original pages. The reduced-resolution copy stays fixed; only the sticky notes get rewritten during training, tiny compared to the book itself.

The two mechanisms combined

4-bit quantization of the frozen base model. Every weight in the pretrained model is compressed from 16-bit floating point down to a specialised 4-bit format (QLoRA introduces "NF4," a format tuned for the roughly-normal distribution of trained neural network weights), cutting the base model's memory footprint by about 4×. These weights are frozen — never updated — so precision loss here only affects the starting point, not the ability to learn.

LoRA adapters, trained in higher precision. For selected weight matrices WW (typically the attention projections), LoRA adds a small trainable update ΔW=BA\Delta W = BA, where BB is d×rd \times r and AA is r×dr \times d, with rank rr chosen far smaller than dd (commonly r=8r=8 to 6464):

W=W4-bit, frozen+BAW' = W_{\text{4-bit, frozen}} + BA

In words: the model's effective weight WW' is the frozen, compressed original plus a small correction built from two skinny matrices BB and AA — only BB and AA ever receive gradient updates, and because rdr \ll d, they contain a tiny fraction of the parameters WW itself has.

Worked example 1: counting trainable parameters

For WW of size 4096×40964096\times4096 (about 16.8 million parameters) and rank r=16r=16: BB is 4096×164096\times16 and AA is 16×409616\times4096, together 2×4096×16=131,0722\times4096\times16 = 131{,}072 parameters — about 0.78% of the original matrix. Applied across a 65-billion-parameter model's attention projections, LoRA typically trains well under 1% of total parameters, while the frozen 4-bit base still needs forward and backward passes through it, just at drastically reduced memory.

Worked example 2: memory budget comparison

For a 65B-parameter model: full 16-bit fine-tuning needs roughly 65B×2 bytes=13065\text{B} \times 2\text{ bytes} = 130 GB for weights, plus gradients (another 130 GB) plus Adam state (~260 GB) — over 500 GB total, requiring multiple high-end GPUs.

QLoRA: base weights at 4-bit 65B×0.5 byte=32.5\approx 65\text{B} \times 0.5\text{ byte} = 32.5 GB, frozen (no gradient or optimiser state needed), plus LoRA parameters at 16-bit — under 1% of 65B, well under 1 GB combined. Total: roughly 33–36 GB, comfortably inside a single 48 GB GPU — the paper's actual reported result, where standard fine-tuning would need over a dozen GPUs.

ApproachApprox. memory
Full 16-bit fine-tuning, 65B model~520 GB
QLoRA (4-bit frozen base + LoRA)~35 GB
Frozen 4-bit base W (large, fixed) 4096×4096
frozen, 4-bit + Trainable low-rank update (small) B × A rank r ≪ 4096
The large base weight matrix stays frozen and compressed; only the two thin low-rank matrices B and A are trained, holding a tiny fraction of the parameter count.

Function explorer
-224.4
x = 1.00f(x) = 1.000

Trainable parameter count as a fraction of the full matrix shrinks steeply as rank rr falls relative to dimension dd — a curve of this shape — which is why even a modest rank like 16 or 32 keeps the adapter tiny relative to a multi-thousand-dimension weight matrix.

QLoRA fine-tunes a large model by freezing and compressing the entire base model to 4-bit precision (so it never needs gradients or optimiser state) and training only small, added low-rank adapter matrices in full precision — combining quantization's memory savings on the frozen part with LoRA's parameter savings on the trained part.

What this means in practice

QLoRA (and LoRA more broadly) made fine-tuning frontier-scale open models accessible outside large labs, underlying most of the "custom fine-tuned model" ecosystem — a quant team can fine-tune a large open model on a single workstation GPU, storing only small adapter weights per task, and swap adapters onto the same frozen base for different uses without duplicating the multi-gigabyte base model.

Quantizing the frozen base model to 4-bit is safe because those weights are never updated by gradient descent — but naively trying to fine-tune weights directly at 4-bit precision (updating the low-precision weights themselves) degrades badly, because gradient updates need much finer numerical resolution than 4 bits can represent. QLoRA's key design choice is keeping the trainable path (the LoRA adapters) at full precision throughout, and only the untouched, frozen weights are ever compressed.

Practice

  1. For a 2048×20482048 \times 2048 weight matrix and LoRA rank r=8r=8, how many trainable parameters does the adapter add, and what fraction of the original matrix's parameter count is that?
  2. Why does freezing the base model's weights make it safe to store them at much lower numerical precision than the parameters that are actually being trained?

Related concepts

Practice in interviews

Further reading

  • Dettmers, Pagnoni, Holtzman & Zettlemoyer, QLoRA: Efficient Finetuning of Quantized LLMs (2023)
  • Hu et al., LoRA: Low-Rank Adaptation of Large Language Models (2021)
ShareTwitterLinkedIn