Understanding LoRA: Low-Rank Adaptation for Fine-Tuning
Refer from AI Engineering: Customizing LLMs for Business (Fine-Tuning LLMs with QLoRA & AWS),https://zerotomastery.io/courses/learn-fine-tuning-llms
1. What is LoRA?
LoRA (Low-Rank Adaptation) is a technique to fine-tune large models efficiently. Instead of updating a full weight matrix \(W \in \mathbb{R}^{k \times d}\), we keep \(W\) frozen and learn a low-rank update \(\Delta W = BA\), where:
- \(A \in \mathbb{R}^{r \times d}\) is a small trainable matrix,
- \(B \in \mathbb{R}^{k \times r}\) is another small trainable matrix,
- \(r \ll \min(k, d)\), where \(k\) is the input dimension of the layer and \(d\) is the output dimension.
The effective weight becomes:
so only \(A\) and \(B\) receive gradient updates.
2. Why Use LoRA?
- Full fine-tuning updates every weight tensor—expensive in memory and compute.
- LoRA reduces the number of trainable parameters by two or more orders of magnitude.
- In practice it achieves competitive downstream accuracy at a fraction of the cost.
3. Numerical Example
Let:
- input dim \(d = 4\), output dim \(k = 4\), rank \(r = 2\).
The initial matrices are:
The initial update matrix \(\Delta W\) and the effective weight matrix \(W_{\text{eff}}\) are calculated as:
3.1. A Single Gradient Update Step
Now, let's simulate one step of training. We need an input vector \(x\), a target output \(y_{\text{true}}\), a loss function \(L\), and a learning rate \(\alpha\).
- Input: \(x = [1, 0, 0, 0]\)
- Target: \(y_{\text{true}} = [1, 1, 1, 1]\)
- Loss Function: \(L = \frac{1}{2} \sum (y_i - y_{\text{true}, i})^2\) (Sum of Squared Errors)
- Learning Rate: \(\alpha = 0.1\)
-
Forward Pass. First, we compute the predicted output \(y = x W_{\text{eff}}\). With \(x = [1, 0, 0, 0]\), this simply selects the first row of \(W_{\text{eff}}\):
\[y = [1.1, 0.2, -0.7, 2.4] \] -
Compute Loss and Gradients. We calculate the loss and then backpropagate to find the gradients of the loss with respect to \(A\) and \(B\).
\[\text{Loss } L \approx 2.75 \]
Crucially, since \(W\) is frozen, we do not compute its gradient. The gradients are computed only for the trainable parameters:
Via backpropagation, we find the numerical gradients (values are approximate):
- Update Parameters. We update \(A\) and \(B\) using gradient descent. \(W\) is not updated.
With \(\alpha = 0.1\), the new matrices are:
The original weight matrix \(W\) remains completely unchanged.
- New Effective Weight. Finally, we can see how the effective weight matrix has changed due to the updates to \(A\) and \(B\) only.
After just one update, only the small matrices \(A\) and \(B\) have been modified, changing the overall behavior of the layer while keeping the massive original weight matrix \(W\) frozen.
4. How Training Works
- Forward: compute with \(W_{\text{eff}} = W + BA\).
- Compute loss.
- Back-propagate gradients only into \(A\) and \(B\).
- Update \(A\) and \(B\); keep \(W\) frozen.
5. Does LoRA Add New Layers?
No. A LoRA patch lives inside an existing nn.Linear layer.
- The layer's computation (during forward pass) is rewritten as \(y = (W + BA)x + b\), but the call-graph still contains just the original layer.
- \(A\) and \(B\) are lightweight matrices, not brand-new modules.
- At inference you can merge them once into \(W\) and discard the patch: \(W \leftarrow W + BA\).
Thus LoRA changes the parameters a layer holds, not the network topology.
6. LoRA Diagram

7. LoRA Parameter Savings Example
Assume:
- LLaMA-7B contains \(\approx 7\) billion parameters in total.
- We apply LoRA to its linear layers, which contain the vast majority of its parameters. The total number of weights in these layers is \(\approx 6.48\) billion.
- LoRA rank is fixed at \(r = 8\).
For a linear layer of shape \((k \times d)\) LoRA adds:
With a typical layer size of \(k = d = 4096\):
Assuming we patch 100 of the model's main linear layers (a common practice):
Perspective
LoRA therefore tunes about one-tenth of one percent of the weights it touches and below one-tenth of one percent of the full network—a truly dramatic savings in both memory and compute.
8. What Does Rank Mean in LoRA?
Think of the rank \(r\) as a dial that controls how much freedom the LoRA patch has.
Linear-algebra view. The rank of a matrix is the count of independent rows or columns, i.e. how many directions it can cover in space.
LoRA view. We replace a full (large) update matrix \(\Delta W \in \mathbb{R}^{k \times d}\) with a product of two skinny ones:
The middle dimension \(r\) is the bottleneck size—our dial.
- Small \(r \rightarrow\) very few new parameters, runs fast, but can only make coarse adjustments.
- Large \(r \rightarrow\) more parameters and compute, but can learn subtler changes.
In practice people pick:
choosing a higher value for bigger models or harder tasks.
9. Summary
- LoRA injects low-rank matrices, not new layers.
- Only the small patches (\(A, B\)) are trained; \(W\) stays frozen.
- Widely used on
nn.Linearlayers in transformers to enable rapid, memory-efficient adaptation. - Used for fine-tuning.

浙公网安备 33010602011771号