Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Explainers/Research Papers

LoRA explained: fine-tune a big AI model cheaply

LoRA freezes a big model and learns each weight change as two thin matrices, cutting trainable numbers about 10,000 times on GPT-3 175B.

Derpx067 Sept · 6 min read
Contents
The idea in one sentenceHow it worksStep 1: what a weight matrix is, and what rank meansStep 2: the low-rank updateStep 3: the number countStep 4: which matrices get adaptedStep 5: why a small rank can workStep 6: merging, so there is no extra cost when using the modelWhat the paper foundWhat to be careful aboutSources
0% read · 6 min leftBack to top ↑
On this page0%
Contents
The idea in one sentenceHow it worksStep 1: what a weight matrix is, and what rank meansStep 2: the low-rank updateStep 3: the number countStep 4: which matrices get adaptedStep 5: why a small rank can workStep 6: merging, so there is no extra cost when using the modelWhat the paper foundWhat to be careful aboutSources

A big language model has billions of numbers. To teach it a new job, the usual method (fine-tuning) changes all of them and saves a full new copy for every job. For a model with 175 billion numbers, that is hundreds of gigabytes per job. LoRA is a way to teach the same job while changing and storing only a tiny extra piece.

Paper

LoRA: Low-Rank Adaptation of Large Language Models. Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen (Microsoft). arXiv, June 2021 (version 2, October 2021). arxiv.org/abs/2106.09685

The idea in one sentence

Freeze the original model, and learn the change to each weight matrix as the product of two thin matrices.

The authors guess that the change needed for a new job is simple, so it can be written with far fewer numbers than the original matrix has. The paper tests this guess and finds it holds well on the tasks they tried.

How it works

Step 1: what a weight matrix is, and what rank means

A weight matrix is a grid of numbers. A layer of the model multiplies its input vector xxx by this grid to get an output: h=Wxh = Wxh=Wx. A big model is mostly many such grids. In the paper, W0W_0W0​ is a frozen pre-trained grid with ddd rows and kkk columns.

The rank of a grid is how many truly independent rows it has. A rank-1 grid is built from one pattern: every row is just a multiple of one row. Toy example (made up to show the idea):

  • Take a column B=(1,2,3)B = (1, 2, 3)B=(1,2,3) and a row A=(4,5,6)A = (4, 5, 6)A=(4,5,6).
  • Multiply them to get a 3 by 3 grid with 9 numbers: rows (4,5,6)(4, 5, 6)(4,5,6), (8,10,12)(8, 10, 12)(8,10,12), (12,15,18)(12, 15, 18)(12,15,18).
  • Each row is a multiple of the first, so this grid has rank 1. We described 9 numbers using only 6.

Step 2: the low-rank update

Full fine-tuning learns a change ΔW\Delta WΔW with the same size as W0W_0W0​. LoRA instead forces the change to be a product of two thin grids (paper, section 4.1):

W0+ΔW=W0+BA,B∈Rd×r, A∈Rr×k, r≪min⁡(d,k)W_0 + \Delta W = W_0 + BA, \qquad B \in \mathbb{R}^{d \times r},\ A \in \mathbb{R}^{r \times k},\ r \ll \min(d,k)W0​+ΔW=W0​+BA,B∈Rd×r, A∈Rr×k, r≪min(d,k)

Here rrr is the rank you choose, usually a small number. The layer computes h=W0x+BAxh = W_0 x + BAxh=W0​x+BAx. Both parts get the same input and their outputs are added. Only AAA and BBB are trained. W0W_0W0​ never changes.

Before training, AAA is filled with random numbers and BBB with zeros, so BABABA is zero and the model starts exactly as the original. The change is also multiplied by α/r\alpha / rα/r, where α\alphaα is a constant. The paper says this scaling reduces the need to retune settings when you change rrr.

Step 3: the number count

A d×kd \times kd×k grid has dkdkdk numbers. BBB and AAA together have r(d+k)r(d + k)r(d+k).

Real-size example (our arithmetic): take d=k=4096d = k = 4096d=k=4096 and r=8r = 8r=8.

  • Full change: 4096×4096=16,777,2164096 \times 4096 = 16{,}777{,}2164096×4096=16,777,216 numbers.
  • LoRA: 8×(4096+4096)=65,5368 \times (4096 + 4096) = 65{,}5368×(4096+4096)=65,536 numbers.
  • That is about 0.4% of the full count.

For GPT-3 175B (96 layers, d=12,288d = 12{,}288d=12,288) the paper adapts two matrices per layer with r=4r = 4r=4. Our check: 4×(12,288+12,288)=98,3044 \times (12{,}288 + 12{,}288) = 98{,}3044×(12,288+12,288)=98,304 per matrix, times 2 matrices, times 96 layers, equals 18,874,368. The paper calls this its 18M budget.

Full change to one 4096 x 4096 matrix16777216
LoRA at r = 865536
Trainable numbers for one matrix (our arithmetic)

Step 4: which matrices get adapted

A Transformer attention block has four weight matrices: WqW_qWq​ (query), WkW_kWk​ (key), WvW_vWv​ (value) and WoW_oWo​ (output). It also has two in its feed-forward part. The paper adapts only the attention ones, and in most experiments only WqW_qWq​ and WvW_vWv​. It left the feed-forward matrices to future work.

With the number budget fixed at 18M on GPT-3, the paper compares ways to spend it (Table 5):

Adapted matricesRankWikiSQL accuracy (%)MultiNLI accuracy (%)
WqW_qWq​ only870.491.0
WkW_kWk​ only870.090.8
WvW_vWv​ only873.091.0
WqW_qWq​, WvW_vWv​473.791.3
WqW_qWq​, WkW_kWk​, WvW_vWv​, WoW_oWo​273.791.7

Spreading a small rank over more matrices beat putting a larger rank in one. The paper's own words are that adapting more matrices is preferable to adapting a single type with a larger rank.

Step 5: why a small rank can work

The paper builds on earlier work (Aghajanyan et al., 2020) showing that pre-trained models can still learn well when training is squeezed into a small subspace. The authors guess the weight change has a low "intrinsic rank" too. Their evidence is Table 6: on GPT-3, ranks from 1 to 64 give almost the same accuracy when both WqW_qWq​ and WvW_vWv​ are adapted (WikiSQL 73.4 at r=1r = 1r=1, 73.8 at r=8r = 8r=8, 73.5 at r=64r = 64r=64). They also found that the main directions learned at r=8r = 8r=8 and r=64r = 64r=64 overlap strongly. This is evidence, not proof, and it was measured on a few tasks.

Step 6: merging, so there is no extra cost when using the model

W0W_0W0​ and BABABA are both d×kd \times kd×k. So after training you can compute W=W0+BAW = W_0 + BAW=W0​+BA once and use WWW as an ordinary weight matrix. The model has the same shape and the same speed as before. To switch jobs, subtract BABABA and add a different B′A′B'A'B′A′.

Toy check with the earlier numbers: for x=(1,0,1)x = (1, 0, 1)x=(1,0,1), Ax=4+6=10Ax = 4 + 6 = 10Ax=4+6=10, then B(Ax)=(10,20,30)B(Ax) = (10, 20, 30)B(Ax)=(10,20,30). Multiplying by the merged grid BABABA gives the same (10,20,30)(10, 20, 30)(10,20,30).

AdaptersAdapter layers: extra layers stay in the model, so each request does more work
→
LoRALoRA merged: W = W0 + BA, same shape and same work as the original

What the paper found

Compared with fully fine-tuning GPT-3 175B (abstract and Table 4):

  • Trainable numbers: 10,000 times fewer.
  • GPU memory during training: 3 times less (1.2 TB down to 350 GB).
  • Saved file for r=4r = 4r=4 on WqW_qWq​ and WvW_vWv​: about 35 MB instead of 350 GB.
  • Training speed: 25% faster on GPT-3 175B.
GPT-3 175B methodTrainable numbersWikiSQL (%)MNLI-m (%)SAMSum R1/R2/RL
Full fine-tuning175,255.8M73.889.552.0/28.0/44.5
Adapters (largest)40.1M73.291.553.2/29.0/45.1
LoRA4.7M73.491.753.8/29.8/45.9
LoRA37.7M74.091.653.4/29.2/45.1

LoRA matches or beats full fine-tuning on these three tasks. The paper also reports on-par or better results on RoBERTa, DeBERTa and GPT-2. The WikiSQL numbers vary by about plus or minus 0.5% between runs, so small gaps there mean little.

What to be careful about

  • The paper tested language tasks such as classification, SQL generation and summarization. It does not show that LoRA matches full fine-tuning on every task. It says hard tasks may need more trainable numbers.
  • The "intrinsic rank is low" idea is a hypothesis backed by experiments, and the paper itself calls the mechanism "far from clear".
  • If you merge BABABA into WWW, it is not simple to serve requests for different jobs in one batch. You can skip merging, at some cost in speed.
  • The choice of which matrices to adapt is based on rules of thumb, not theory. The authors list a principled method as an open question.
  • LoRA still needs the full base model in memory when you use it. Only the per-job files shrink: 100 jobs take about 350 GB + 35 MB x 100, not 100 x 350 GB.
Key takeaways
  • LoRA freezes the model and learns the change as BABABA, two thin matrices of rank rrr.
  • Trainable numbers fall from dkdkdk to r(d+k)r(d + k)r(d+k). On GPT-3 175B that is 10,000 times fewer and 3 times less training memory.
  • W0+BAW_0 + BAW0​+BA can be merged once, so using the model costs nothing extra.
  • Small ranks (even 1 to 8) were enough on the paper's tasks, and adapting more matrices beat a larger rank in fewer.

Sources

  • Hu, Shen, Wallis, Allen-Zhu, Li, Wang, Wang, Chen. LoRA: Low-Rank Adaptation of Large Language Models, arXiv 2021. All numbers above come from this paper unless marked as our arithmetic.
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Research Papers · 7 minChinchilla explained: how big should an AI model be?Research Papers · 5 minMamba explained: an alternative to TransformersResearch Papers · 6 minFlashAttention explained: making AI attention much faster
Start here
01Why do AI chatbots make things up?02How does Raft work? Getting computers to agree03Mamba explained: an alternative to Transformers