QLoRA: Fine-Tuning Big Models on One GPU

4-bit NormalFloat, double quantisation and paged optimizers: how QLoRA fits large-model fine-tuning onto a single GPU.

Fine-TuningBy Fahim Zafri6 min readUpdated Oct 21, 2026

Cartoon robot sitting on an overstuffed suitcase labelled 4-bit to force it shut, while a person holding a small GPU card asks if it will fit.
On this page

LoRA shrinks what you train, but the frozen base model still has to sit in GPU memory at 16 bits per weight. For a 70-billion-parameter model that's around 140 GB before you've trained anything. QLoRA removes that wall by storing the frozen base model in 4 bits and training LoRA adapters on top of it. Models that once needed a multi-GPU server can be fine-tuned on a single workstation card, with quality that holds up remarkably well.

The memory wall

Where does fine-tuning memory go? For each parameter you may need to hold the weight itself, its gradient, and the optimizer's running statistics. Adam keeps two extra numbers per parameter, usually in 32-bit precision, plus a 32-bit master copy of the weights. Compare the three approaches:

InteractiveGPU memory to fine-tune, by method
Full fine-tune
208 GB
LoRA (16-bit base)
27 GB
QLoRA (4-bit base)
7.7 GB
24 GB48 GB80 GB
  • Model weights
  • Gradients / adapter training state
  • Optimizer state
Rough estimates for weights, gradients and optimizer state only; activations, CUDA context and fragmentation come on top. The dashed lines are common single-GPU memory sizes.

Full fine-tuning is out of reach for almost everyone above 7B parameters. LoRA removes the gradient and optimizer cost for the frozen weights, but the 16-bit weights themselves remain the bottleneck. QLoRA cuts that bottleneck by roughly 4×.

These figures exclude activations, the intermediate values saved during the forward pass for use in the backward pass. They grow with batch size and sequence length. Gradient checkpointing, which recomputes activations instead of storing them, is standard with QLoRA for that reason.

Three ideas that make it work

QLoRA combines three techniques:

  1. 4-bit NormalFloat (NF4). A 4-bit data type whose 16 levels are placed to match the bell-shaped distribution of trained weights, rather than spaced evenly.
  2. Double quantisation. Quantising a block of weights needs a scaling constant per block. QLoRA quantises those constants too, saving roughly another 0.4 bits per parameter.
  3. Paged optimizers. Optimizer state can spill to CPU memory automatically during memory spikes (such as an unusually long batch), instead of crashing the run with an out-of-memory error.

During training, each 4-bit weight is dequantised on the fly to 16-bit (usually bfloat16) for the computation, used, and discarded. Gradients flow through the frozen quantised weights into the LoRA adapters, which stay in full 16-bit precision. Only the adapters are updated.

Quantisation, visually

Quantisation means rounding each weight to the nearest of a small set of allowed values. With 4 bits you get 16 values. The question is where to put them. Compare evenly spaced levels with normal-quantile levels (the idea behind NF4) on a set of normally distributed weights:

InteractiveSqueezing weights into 4 bits
original (16-bit)quantised
Mean absolute error0.0429
Other spacing would give0.0566
Memory vs 16-bit25%
Top row: original weights. Bottom row: where each one lands after rounding to the nearest allowed level (vertical ticks). Normal-quantile levels sit closer together near zero, where most weights live. This is the idea behind the NF4 data type, simplified.

Because most weights cluster near zero, placing more levels there reduces the average rounding error for the same number of bits. The real NF4 type is slightly more refined (it guarantees an exact zero, for instance), but the principle is the same.

QLoRA in code

With Hugging Face transformers, bitsandbytes and peft, the change from plain LoRA is mostly in how the model is loaded:

Python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TrainingArguments
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training

base_id = "meta-llama/Llama-3.1-8B-Instruct"

bnb = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",              # NormalFloat 4-bit
    bnb_4bit_use_double_quant=True,         # quantise the quantisation constants too
    bnb_4bit_compute_dtype=torch.bfloat16,  # dequantise to bf16 for the actual matmuls
)

tokenizer = AutoTokenizer.from_pretrained(base_id)
model = AutoModelForCausalLM.from_pretrained(base_id, quantization_config=bnb, device_map="auto")
model = prepare_model_for_kbit_training(model)  # enables gradient checkpointing, stabilises norms

model = get_peft_model(model, LoraConfig(
    r=16,
    lora_alpha=32,
    lora_dropout=0.05,
    target_modules="all-linear",
    task_type="CAUSAL_LM",
))

args = TrainingArguments(
    output_dir="runs/claims-qlora",
    per_device_train_batch_size=4,
    gradient_accumulation_steps=4,          # effective batch of 16 without the memory cost
    learning_rate=2e-4,
    num_train_epochs=2,
    bf16=True,
    optim="paged_adamw_8bit",               # paged optimizer: survives memory spikes
    logging_steps=10,
    eval_strategy="steps",
    eval_steps=100,
    save_strategy="steps",
    save_steps=100,
)

Pass model, args and your train and eval datasets to a trainer (for example TRL's SFTTrainer) and train as usual. The saved output is a LoRA adapter, the same size as one trained without quantisation.

Trade-offs to know about

  • Quality. The original QLoRA research showed 4-bit fine-tuning matching 16-bit LoRA on standard benchmarks. In our experience that holds for most narrow business tasks. Verify it on your own eval set anyway.
  • Speed. Dequantising weights on every forward and backward pass costs time. Expect each training step to be noticeably slower than 16-bit LoRA on the same hardware. You're trading speed for the ability to run at all.
  • Hardware support. bitsandbytes 4-bit kernels are best supported on NVIDIA GPUs. Check support before planning a run on other accelerators.
  • Long sequences. Activations still scale with sequence length. A 70B model on a 48 GB card works for typical chat-length examples, not for 32k-token documents.

Deploying a QLoRA adapter

The adapter was trained against 4-bit weights, but it doesn't have to be served that way. Common options:

  • Merge into the 16-bit base for the highest quality and fastest inference on hardware with enough memory. Load the base model in bfloat16, not 4-bit, before merging. Merging into quantised weights loses precision.
  • Serve 4-bit base plus adapter when memory is tight, accepting slightly slower generation.
  • Quantise the merged model with an inference-oriented format afterwards, then re-run your eval set, since a second round of quantisation can shift behaviour.

Checklist

  • Load the base model in NF4 with double quantisation and a bfloat16 compute dtype.
  • Call prepare_model_for_kbit_training and keep gradient checkpointing on.
  • Target all linear layers with LoRA; start at r = 16, α = 32.
  • Use a paged optimizer and gradient accumulation to fit the batch size you want.
  • Hold out an eval set; compare against the base model with your best prompt.
  • Merge into a 16-bit base for deployment where memory allows, and evaluate the final artifact.
Get Consultancy