Topic 2 of 5 · 4 min read
QLoRA and fitting tuning in memory
Goal: Estimate whether a stated model size fits LoRA or QLoRA tuning on a given GPU and pick the setup that fits.
QLoRA keeps the frozen base model in four-bit precision and trains 16-bit LoRA adapters on top, which cuts the memory for the weights to about a quarter and lets large models tune on one GPU.
Check yourself · 1 of 3
What does QLoRA keep in four-bit precision?
Why it matters
Before you pick a method, you need to know whether it fits. Fine-tuning fails with an out-of-memory error after you have queued the job, or worse, after hours of training. A rough estimate takes a minute and tells you which setup to choose.
What takes memory
- Base weights. Parameters times bytes per parameter. At 16-bit (2 bytes) a 7B model needs about 14 GB. At 4-bit (0.5 byte) it needs about 3.5 GB, plus a little for quantization constants.
- Adapter weights, gradients and optimizer state. Small, because only the adapter trains. A few million to a few hundred million parameters.
- Activations. They grow with batch size and sequence length. This is often what runs you out of memory. Gradient checkpointing trades extra compute for much less activation memory.
- Overhead. The CUDA context, fragmentation and logits for a large vocabulary. Leave a margin of a few GB.
What QLoRA adds
The QLoRA paper (Dettmers et al., 2023) combines three ideas: the 4-bit NF4 data type, double quantization of the quantization constants, and paged optimizers that spill optimizer state to CPU memory during spikes. The authors report tuning a 65B model on a single 48 GB GPU. During the forward pass the weights are dequantized layer by layer to 16-bit for the math, so QLoRA runs slower than plain LoRA and can lose a little quality against a 16-bit base.
Before and after
Weak plan: "We have one 24 GB GPU, so we will LoRA-tune a 13B model in 16-bit." The weights alone are about 26 GB. It cannot load.
Better plan: QLoRA on the same 13B model. Weights are about 6.5 GB at 4-bit, leaving room for adapters, activations and a batch of a few thousand tokens with gradient checkpointing on. If quality matters more than fitting, drop to a 7B model in 16-bit LoRA (about 14 GB of weights) instead.
A quick decision rule: if the 16-bit weights fit with at least 30 to 40 percent of the GPU free, use plain LoRA. If not, use QLoRA. If even 4-bit weights do not fit, pick a smaller model or more GPUs.
What goes wrong
- Counting only the weights. Activations at long sequence length can exceed the adapter and weights together.
- Assuming QLoRA is free. It is slower per step, and a merged result still needs care when you export it.
- Quantizing the evaluation differently. If you train on a 4-bit base and serve a 16-bit one, test the exact setup you will ship.
Examples
Choosing a setup for a 24 GB GPU
| Model | 16-bit weights | 4-bit weights | Verdict on 24 GB |
|---|---|---|---|
| 7B | about 14 GB | about 3.5 GB | LoRA fits with modest batch size |
| 13B | about 26 GB | about 6.5 GB | Only QLoRA fits |
| 34B | about 68 GB | about 17 GB | QLoRA is tight; short sequences needed |
The numbers are weights only. Reserve several GB for activations and overhead.
Out of memory at step 1
A QLoRA run on a 7B model fails at the first step with a CUDA out-of-memory error, though the weights take 3.5 GB on a 24 GB card. The cause is max_seq_length = 8192 with batch size 8. The fix is not a smaller model. Drop to batch size 1 with gradient accumulation of 8 and turn on gradient checkpointing.
Key points
- Weights cost parameters times bytes: about 2 bytes each at 16-bit and 0.5 byte at 4-bit.
- QLoRA combines a 4-bit NF4 frozen base, double quantization and paged optimizers with 16-bit LoRA adapters.
- Activations grow with batch size and sequence length and are the usual cause of out-of-memory errors.
- QLoRA trades some speed, and possibly a little quality, for the ability to fit a larger model.
- Use plain LoRA when the 16-bit weights leave comfortable headroom, and QLoRA when they do not.
Common mistakes
- Budgeting memory for weights alone and forgetting activations, which scale with sequence length and batch size.
- Choosing QLoRA by reflex when plain LoRA fits easily and would train faster.
- Assuming a 4-bit base gives the same results as a 16-bit one without running the held-out eval on the setup you will ship.
- Fixing out-of-memory errors by shrinking the model before trying batch size, sequence length and gradient checkpointing.
Check yourself · 2 of 3
Roughly how much memory do the weights of a 13B model need at 16-bit precision?
Check yourself · 3 of 3
You have one 24 GB GPU and want to tune a 13B model. Which setup is the realistic choice?
Practice items, not counted towards your mastery.
Start here
- LoRA: Low-Rank Adaptation of Large Language Models (opens in a new tab)PaperFree
The original method: frozen weights, low-rank update matrices and why no inference latency is added.
- QLoRA: Efficient Finetuning of Quantized LLMs (Dettmers et al.) (opens in a new tab)PaperFree
Four-bit NF4 base weights, double quantization and paged optimizers, with the single-GPU memory results.
- PEFT documentation (Hugging Face) (opens in a new tab)DocsFree
LoraConfig fields, target modules, DoRA, merging and saving adapters.
More for this topic
- Unsloth documentation (opens in a new tab)DocsFree
Faster, lower-memory LoRA and QLoRA tuning with notebooks for common open models.