AI & prompting

LoRA and QLoRA Explained: Differences and When to Use Each

LoRA and QLoRA train the same thing, a pair of small adapter matrices next to each frozen weight matrix. The difference is how the frozen base model is stored during training: LoRA typically keeps it in 16-bit precision, while QLoRA quantizes it to 4-bit, so QLoRA needs far less GPU memory at the cost of some speed.

LoRA and QLoRA in one table

LoRAQLoRA
Frozen base modelFull precision, usually 16-bit4-bit (NormalFloat, NF4)
Trained parametersSmall low-rank matrices A and BThe same small low-rank matrices A and B
Adapter fileMegabytesMegabytes, same format
Memory during trainingBase weights at 16 bits, plus adapter, gradients and optimizer stateBase weights at about 4 bits, plus the same adapter overhead
SpeedFaster per stepSlower per step, because 4-bit weights are converted for computation
Introduced inHu et al., 2021Dettmers et al., 2023

QLoRA is not a different adapter design. It is LoRA applied on top of a quantized model. If you already understand LoRA, QLoRA is one extra decision about how the frozen weights are held in memory.

What LoRA does

LoRA (Low-Rank Adaptation) freezes the pretrained weights and inserts trainable low-rank matrices into each Transformer layer. For a weight matrix W₀ of shape d × k, it learns two thin matrices B (d × r) and A (r × k) and computes the output as W₀x + BAx. The rank r is much smaller than d and k, so the trainable parameter count is r × (d + k) instead of d × k.

The LoRA paper reports that, compared with fully fine-tuning GPT-3 175B with Adam, LoRA reduces the number of trainable parameters by about 10,000 times and the GPU memory requirement by about 3 times, with no added inference latency once the adapter is merged. Most of the remaining memory goes to the frozen base weights, because LoRA does not shrink them. That is the gap QLoRA targets. For the mechanics of rank, alpha and merging, see the PEFT documentation; this article stays on the comparison.

What QLoRA adds

The QLoRA paper (Dettmers et al., 2023) backpropagates gradients through a frozen 4-bit quantized model into LoRA adapters. Its abstract names three techniques that make this work:

  • 4-bit NormalFloat (NF4): a data type the authors describe as information theoretically optimal for normally distributed weights.
  • Double quantization: reduces the average memory footprint by quantizing the quantization constants themselves.
  • Paged optimizers: manage memory spikes during training.

The headline result from the same abstract: QLoRA can fine-tune a 65-billion-parameter model on a single 48 GB GPU. The authors also report that their Guanaco model family, trained with QLoRA, reached 99.3% of ChatGPT's performance level on the Vicuna benchmark, with 24 hours of fine-tuning on a single GPU. Those are the authors' own results on their benchmark, not a guarantee for your task.

The PEFT documentation explains why this split is possible. A quantized model isn't typically trained directly, because training can be unstable at low precision. But PEFT methods only add extra trainable parameters, so you can keep the quantized weights frozen and train the adapter, which stays in higher precision, on top.

The memory math: how much does 4-bit save?

Weights alone are easy to estimate. Multiply the parameter count by the bytes per parameter. This is arithmetic you can check, and it covers weights only. Activations, the adapter's gradients and optimizer state, and the quantization constants all come on top.

Model size16-bit weights (2 bytes each)4-bit weights (0.5 bytes each)
7 billion14 GB3.5 GB
13 billion26 GB6.5 GB
65 billion130 GB32.5 GB

The 65-billion row shows why the paper's claim is plausible: 32.5 GB of 4-bit weights fits in a 48 GB card with room left for everything else, while 130 GB of 16-bit weights does not fit in any single GPU. The base weights shrink by a factor of four. The total training memory shrinks by less than that, because the adapter, gradients, optimizer state and activations are not quantized away. For more on how precision maps to memory when you run models, see how much VRAM you need to run an LLM.

How to set up QLoRA with PEFT

The PEFT quantization guide shows the flow: configure bitsandbytes for 4-bit loading, load the model, prepare it for training, then add a LoRA config. The first block below is taken from that guide. It loads the base model in NF4 with double quantization and bfloat16 compute:

import torch
from 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,
)

Pass that config to from_pretrained, then prepare the model and attach the adapter. Replace the model name with your own base model:

from transformers import AutoModelForCausalLM
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training

model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", quantization_config=config)
model = prepare_model_for_kbit_training(model)

lora_config = LoraConfig(
    r=16,
    lora_alpha=8,
    target_modules="all-linear",
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
)
model = get_peft_model(model, lora_config)

The guide's own example lists ["q_proj", "k_proj", "v_proj", "o_proj"] as target modules. The "all-linear" value here follows the guide's separate "QLoRA-style training" section, which says QLoRA adds trainable weights to all linear layers. The values for r, lora_alpha and dropout are the guide's example values, not tuned recommendations.

To use plain LoRA instead, skip BitsAndBytesConfig and the quantization_config argument, and load the model in 16-bit. Everything from LoraConfig onward stays the same.

Which one should you use?

  • Use LoRA when the 16-bit model fits. If the base weights plus training overhead fit in your GPU memory, LoRA avoids the quantization step and trains faster per step. There is also no question about how the base model's precision affects the result.
  • Use QLoRA when it doesn't fit. If the 16-bit weights alone exceed your memory, as in the 13-billion and 65-billion rows above on a single consumer or workstation GPU, QLoRA is the route the PEFT docs point to for training large models on one GPU.
  • Test before committing. The QLoRA paper reports preserving full 16-bit fine-tuning performance on its benchmarks. Your task may differ, so evaluate the adapter on held-out examples against the base model with a good prompt, whichever method you pick.

If the goal is to teach the model facts that change or need citations, neither method is the right tool; see fine-tuning vs RAG.

Merging a QLoRA adapter

With plain LoRA you can merge the adapter into the base weights and get an ordinary model. With QLoRA the base is quantized, and the PEFT docs describe what merging then does: merge_adapter() dequantizes the base weight, adds the adapter weights and quantizes the result again. They add that unmerge_adapter() does not restore the original quantized weights, and that repeated merge and unmerge cycles can accumulate rounding error. To get the original weights back, reload the base model. To switch between base and adapter without merging, use the disable_adapter() context manager.

If you want to run the final model with llama.cpp or Ollama, you will end up quantizing again anyway; see what is LLM quantization and GGUF for that step.

Frequently asked questions

Is QLoRA slower than LoRA?
Usually per step, yes, because the 4-bit weights must be converted to a higher-precision compute type (such as bfloat16) on the fly during the forward and backward passes. You trade some training speed for a much smaller memory footprint. Measure on your own hardware, since the gap depends on the model, GPU and batch size.
Can I merge a QLoRA adapter into the base model?
Yes, for bitsandbytes 4-bit models, but the PEFT docs note that merging dequantizes the base weight, adds the adapter and quantizes the result again, so rounding error can build up over repeated merge and unmerge cycles. Many people reload the base model in 16-bit and merge the adapter into that instead.
Does QLoRA change the adapter file?
No. The adapter is the same kind of small LoRA matrix pair in both methods. The difference is in how the frozen base model is stored while you train, not in what you save afterwards.
Which layers does QLoRA train?
The PEFT docs say QLoRA adds trainable weights to all the linear layers in the transformer architecture. In PEFT you can set target_modules to "all-linear" to do this without listing attribute names for each architecture.

Sources