AI & prompting

What Is LoRA Fine-Tuning? Low-Rank Adapters Explained

LoRA (Low-Rank Adaptation) fine-tunes a large model by freezing all of its original weights and training two small extra matrices alongside selected weight matrices. Because only those small matrices are trained, fine-tuning needs a fraction of the memory and produces an adapter file of megabytes instead of a full copy of the model.

The problem LoRA solves

Full fine-tuning updates every weight in a model. For a model with billions of parameters, that means storing gradients and optimizer state for all of them during training, and saving a complete new copy of the model for every task. The LoRA paper (Hu et al., 2021, from Microsoft) uses GPT-3 175B as the extreme case and reports that, compared with full fine-tuning using the Adam optimizer, LoRA:

  • reduces the number of trainable parameters by 10,000 times,
  • reduces GPU memory needed for training by 3 times (from about 1.2 TB to 350 GB of VRAM),
  • shrinks the saved checkpoint from about 350 GB to about 35 MB (with rank 4 on the query and value matrices),
  • and, unlike adapter layers, adds no inference latency once merged.

How LoRA works

Take one weight matrix W₀ inside the model, with shape d × k. Fine-tuning would learn an update ΔW of the same shape. LoRA's bet is that this update doesn't need full freedom: it can be written as the product of two thin matrices:

h = W₀x + ΔWx = W₀x + BAx

W₀ : d × k   (frozen, not trained)
B  : d × r   (trained)
A  : r × k   (trained)
r  is much smaller than d and k

The product BA has the same shape as W₀ but rank at most r, which is what "low-rank" means. A few details from the paper matter in practice:

  • Initialisation: A starts with random values and B starts at zero, so BA is zero at the start. The model begins training behaving exactly like the original.
  • Scaling: the update is multiplied by α/r, where α (alpha) is a constant you choose. This keeps the effective size of the update stable when you change r.
  • Where it goes: the paper applied LoRA mainly to the attention layers' query and value projection matrices and left the MLP layers frozen. Libraries let you choose the target modules.

We checked those properties numerically with NumPy on a toy 6 × 5 matrix with r = 2. With B at zero, the output was identical to the frozen model's. After filling B with random values, the product BA had rank 2, and the merged matrix W₀ + (α/r)BA gave exactly the same output as computing the frozen and adapter paths separately:

import numpy as np
rng = np.random.default_rng(0)
d, k, r, alpha = 6, 5, 2, 4
W0 = rng.normal(size=(d, k)); A = rng.normal(size=(r, k)); B = rng.normal(size=(d, r))
x = rng.normal(size=k)
adapter = W0 @ x + (alpha / r) * B @ (A @ x)
merged  = (W0 + (alpha / r) * B @ A) @ x
print(np.allclose(adapter, merged), np.linalg.matrix_rank(B @ A))   # True 2

The parameter math

A full update to a d × k matrix has d × k parameters. The LoRA pair has r × (d + k). For example, for a square 4,096 × 4,096 matrix:

Rank rTrainable parameters (LoRA)Full matrixLoRA as % of full
18,19216,777,2160.049%
865,53616,777,2160.39%
16131,07216,777,2160.78%
64524,28816,777,2163.1%

This is per adapted matrix; multiply by the number of matrices you target across all layers. The fraction shrinks further for bigger matrices: at 8,192 × 8,192, rank 8 is 0.195%. The paper's striking finding was how low r can go: it reports that a rank of 1 or 2 was enough in its GPT-3 experiments, suggesting the update fine-tuning needs really is low-rank.

The settings you'll actually choose

In Hugging Face's PEFT library, LoRA is configured with a LoraConfig. The documentation describes the main parameters:

ParameterWhat it controls
rThe rank of the update matrices. Lower rank means smaller matrices and fewer trainable parameters.
lora_alphaThe scaling factor α. The update is scaled by α/r.
target_modulesWhich modules (for example attention projections) get LoRA matrices.
biasWhether bias parameters are trained: "none", "all" or "lora_only".

The workflow PEFT describes is: load the base model, create a LoraConfig, wrap the model with get_peft_model(), and train the result with your usual training loop. Only the LoRA matrices receive gradient updates.

QLoRA: LoRA on a 4-bit base model

LoRA reduces the trainable parameters, but the frozen base model still has to sit in GPU memory. QLoRA (Dettmers et al., 2023) stores that frozen model in 4-bit precision, using a data type the paper calls 4-bit NormalFloat (NF4), and backpropagates through it into the LoRA adapters. The paper reports fine-tuning a 65-billion-parameter model on a single 48 GB GPU while preserving the task performance of full 16-bit fine-tuning. For a rough idea of how much memory a model's weights need at different precisions, see how much VRAM you need to run an LLM.

Merging, swapping and shipping adapters

After training you have two options.

  • Merge: compute W₀ + (α/r)BA once and save a normal model. PEFT's documentation says merge_and_unload() merges the adapter weights into the base model so it can be used as a standalone model, with no added inference latency. The merged model is as large as the original.
  • Keep separate: ship the small adapter file and load it on top of the base model at runtime. One base model can serve many tasks by swapping adapters, which is the storage win the LoRA paper emphasises. PEFT also provides merge_adapter() and unmerge_adapter() to switch back and forth.

An adapter only works with the exact base model it was trained on. A LoRA trained on one model's weights is meaningless on a different model, even one with the same architecture and size.

If you plan to run the result locally with llama.cpp or Ollama, one route is to merge first and then convert and quantize the merged model; see what is LLM quantization and GGUF.

When LoRA is the right tool

  • Good fit: teaching a consistent output format, a house style or tone, a classification or extraction task, a domain's vocabulary, or behaviour that prompting alone can't hold reliably.
  • Poor fit: keeping the model up to date with changing facts, or answering from a specific document set with citations. Retrieval does that better; see fine-tuning vs RAG.
  • Consider first: better prompts and a few examples in the prompt. They cost nothing to try, and if they work, you don't need to train anything.

Whatever you train, hold out a set of test examples the model never saw during training and compare the fine-tuned model against the base model with a good prompt. If the adapter doesn't clearly beat that baseline, it isn't worth maintaining.

Frequently asked questions

What rank should I use for LoRA?
Start small. The LoRA paper found that a rank as low as 1 or 2 was enough in its GPT-3 experiments. Higher rank means more trainable parameters and a larger adapter file. Increase it only if a lower rank underfits your task in evaluation.
Does LoRA make the model slower at inference?
Not if you merge it. The adapter's two matrices can be multiplied together and added into the original weights, giving a normal model with no extra computation. Hugging Face PEFT does this with merge_and_unload(). Keeping the adapter separate adds a small amount of computation but lets you swap adapters.
What is the difference between LoRA and QLoRA?
LoRA trains small adapter matrices on top of a frozen model stored in 16-bit precision. QLoRA does the same but stores the frozen base model in 4-bit precision during training, which cuts memory much further. The QLoRA paper reports fine-tuning a 65-billion-parameter model on a single 48 GB GPU.
Can LoRA teach a model new facts?
It can only learn what is in its training examples, and whatever it learns is frozen at training time. LoRA is mostly used to teach a format, style, tone or task. For facts that change or must be cited, retrieval (RAG) is usually the better tool, and the two can be combined.

Sources