Concepts / Introduction to Low-Rank Adaptation (LoRA)

Introduction to Low-Rank Adaptation (LoRA)

A LoRA setup involves two linked decisions: which weight matrices receive LoRA and what rank each factorization uses.

  • Programming

Setup as a Form With Two Fields

Low-Rank Adaptation, usually called LoRA, is a way of finetuning a large model by training a small number of extra parameters instead of every weight in the model. Before you can use it, you have to configure it. A common first impression is that this is like flipping a switch: turn LoRA on, and the model adapts. The more accurate picture is that setting up LoRA is closer to filling out a form with two fields that interact.

The first field asks which weight matrices in the model should receive a LoRA adaptation. The second field asks what rank each of those low-rank factorizations should have. Both answers shape how much the model can learn during finetuning and how many parameters you end up training. This article explains the reasoning behind both fields. The goal is not to memorize one recommended configuration, but to understand why the choices matter and how a practitioner narrows them down when hardware limits the number of trainable parameters.

What do you think happens?

Suppose your hardware only lets you train two attention matrices. Which two does the source suggest choosing?

Reveal answer
Answer:

The query matrix and the value matrix.

The source says that if only two attention matrices can be chosen, the query and value matrices generally yield the best results. It phrases this as a general tendency rather than a guarantee, so it is a starting point for a budget-limited setup rather than a rule to apply blindly. The rest of this article explains why the choice of matrices and the choice of rank both matter.

Tracing a Frozen Weight Plus an Adapter

LoRA does not change the original model as a single block. It is applied to individual weight matrices, and each matrix can be chosen or skipped independently. Picture one weight matrix $W$ in the model. In a LoRA setup, $W$ stays frozen. Instead of retraining it directly, the training process learns a change to it, and that change is represented by two smaller matrices. The output of the adapted layer is the frozen path plus the low-rank path, so the frozen weight still does its original job while the adapter contributes a learned adjustment.

Input xfrom the previous layerFrozen weight Wnot trainedAdapted outputfrozen path plus adapterpathMatrix Ar by k, trainedMatrix Bd by r, trainedfrozen pathadapter pathlow-rank bottleneckoriginal outputlearned change
What happens to the input when a weight matrix has a LoRA adapter attached? The input travels both through the frozen original weight and through the two smaller adapter matrices, and the two results are combined.

Read the diagram as a state change. The frozen matrix $W$ never moves during finetuning. The two adapter matrices are the only parts that learn. Their shapes come from the factorization: one matrix is $d \times r$ and the other is $r \times k$, where $r$ is the rank. A small $r$ means fewer trainable numbers, and the adapter's two small matrices together hold far fewer parameters than the full $d \times k$ matrix they stand in for.

The First Field: Choosing Which Matrices Receive LoRA

LoRA has been tried on other kinds of networks, including convolutional neural networks, but it has been used most heavily on transformer models. Within a transformer, the most common targets are the four weight matrices inside the attention modules. These are the query matrix, the key matrix, the value matrix, and the output projection matrix.

Attention moduleinside a transformerQuery matrixQKey matrixKValue matrixVOutput projectionmatrixOcontainscontainscontainscontains
Where do the four commonly targeted matrices sit? They all belong to the same attention module, and LoRA is most often applied to each of them.

The reason matrix choice matters is the budget. In an ideal world you would attach LoRA to all four attention matrices and let training sort out the rest. In practice, hardware memory often sets a ceiling on how many trainable parameters you can afford. Once that ceiling is fixed, the question changes from what is possible to what is best within the budget: given a fixed number of trainable parameters, which matrices should receive LoRA in order to get the strongest performance?

The Second Field: Choosing the Rank

Once you have chosen matrices, you still need to decide the rank of each factorization. The source calls rank a critical parameter that affects performance. Rank is the size of the bottleneck between the two adapter matrices: it sets how many dimensions the learned change passes through. In the GPT-3 175B experiments the source describes, the rank used for the LoRA factorization was 2, and that setting produced the strong results reported for the all-four-matrices configuration.

The two fields should be read together rather than one at a time. A large rank applied to few matrices and a small rank applied to many matrices both spend parameters, so the two dials trade against each other under a fixed budget. The diagram below shows how the two decisions feed into the same total.

LoRA setuptwo linked decisionsWhich matrices totargetfor example Q, K, V, OTrainable parametercountlimited by hardwareRank of eachfactorizationfor example 2requiresrequiresshapesshapes
How do the two decisions relate? Choosing which matrices to target and choosing each rank both determine how many parameters get trained, so they must be balanced together.

Reading the Reference Result

The source describes how Hu and colleagues handled this trade-off when finetuning GPT-3 175B. They set their trainable parameter budget at 18M, which is about 0.01 percent of the model's total parameters. That budget was enough to apply LoRA to a selected set of matrices, and the choice of which set mattered for the results.

The specific comparison the source reports is this. Applying LoRA to all four attention matrices with rank equal to 2 gave the best performance on the WikiSQL and MultiNLI benchmarks. WikiSQL is a benchmark built around translating questions into database queries, and MultiNLI is a natural language inference benchmark. Because this result was strong, it is the reference point for this configuration.

Choosing a Pair Under a Tight Budget

Sometimes the budget only allows two attention matrices instead of four. The source gives a practical fallback for this case: if you can choose only two attention matrices, the query and value matrices generally yield the best results. This is why the two-matrix case lands on that pair. The comparison below sets out the decision in the same way you would reason about it on your own setup.

Worked example

Picking two matrices with a fixed budget

Your hardware allows LoRA on only two of the four attention matrices, and you keep the rank at 2 as in the reference experiment. Which two matrices do you choose?

  1. 1
    Check the budget

    All four matrices would exceed the trainable parameter ceiling, so you must narrow the target set to two.

  2. 2
    Recall the stated tendency

    The source says that with only two attention matrices, the query and value matrices generally yield the best results.

  3. 3
    Keep the rank fixed

    With the matrix pair chosen, the rank stays at 2 so you can compare against the reference setup on a like-for-like basis.

  4. 4
    Treat the advice as a starting point

    The source phrases this as a general tendency, so you should verify it on your own task rather than assume it always holds.

Result

Choose the query matrix and the value matrix, with rank 2 as the starting setting, and evaluate whether the result is acceptable for your task.

SetupMatrices targetedWhat the source says
Full referenceQuery, key, value, and output projectionGave the best WikiSQL and MultiNLI results with rank 2
Two-matrix fallbackQuery and valueGenerally yields the best results when only two can be chosen
Other two-matrix pairsAny other pair, such as query and keyThe source does not rank these pairs, so test them rather than assume

Matrix choices under different budgets, using only what the source states

Mistakes Beginners Make With the Two Fields

  • Common mistake

    Treating LoRA as a single on-off switch

    Why it fails

    Setup involves two linked fields, which matrices receive LoRA and what rank each factorization uses. Changing one changes the effect of the other.

    Do this instead

    Decide the matrix set and the rank together, and check how many trainable parameters the combination produces.

  • Common mistake

    Copying the reference configuration without thinking

    Why it fails

    The all-four-matrices, rank-2 setup is the result from specific experiments on specific benchmarks. The source presents it as a reference point, not a universal formula.

    Do this instead

    Use the reference as a starting point, then test whether your budget and task support the same choice.

  • Common mistake

    Assuming the matrix choice applies to one layer only

    Why it fails

    LoRA is usually applied uniformly by matrix type, so selecting the query matrix applies it to every query matrix in the model.

    Do this instead

    Remember that each matrix-type choice is broad and affects the whole network.

  • Common mistake

    Assuming a large rank is always better, without considering how many matrices it is spread across

Practice Choosing a Configuration

MEDIUM

Your budget allows exactly two attention matrices to receive LoRA. Name the pair you would choose first, state the rank you would start with, and explain in one sentence why your choice depends on the trade-off between matrix choice and rank.

Hints
  • Look for the sentence in the source about choosing only two attention matrices.
  • The source names rank 2 as the setting used in its reference experiments.
  • Think about whether spending the budget on more matrices or on a larger rank changes the total parameter count.

Key Takeaways

  • LoRA setup involves two linked decisions: which weight matrices receive LoRA and what rank each factorization uses.
  • LoRA is most commonly applied to the query, key, value, and output projection matrices in transformer attention modules, and is usually applied uniformly by matrix type.
  • When only two attention matrices can be chosen, the query and value matrices generally yield the best results, while the reference result applied LoRA to all four with rank 2.
  • Matrix choice and rank trade against each other under a fixed trainable parameter budget, so they should be read together rather than one at a time.

Related concepts

More concepts starting with I