Efficient LLMs
November 7, 2026 · Updated November 16, 2026
Contents
LoRA (Low-Rank Adaptation)
In simple words, this is the method where a large language model learns a new task by training a small update instead of rewriting all its weights. [1]
The difficulty of fine-tuning an entire model is scale. Full fine-tuning stores the combined result as another complete set of weights. Updating every parameter of a large model requires substantial training memory, and each finished task produces another full-sized set of weights to store and deploy.
LoRA was developed to make this adaptation smaller: preserve the pretrained model, then learn only the compact changes required by the new task. It keeps the original matrix fixed and learns only the change using a much smaller representation.
It fine-tunes an LLM without changing its original weights. For each chosen weight matrix, it learns a small update written as the product of two thin matrices, and , with a low rank . Only those two matrices are trained for each task. So whenever something new needs to be learned, it comes as a small adapter instead of requiring a full copy of the model to be trained.
Full fine-tuning allows updates to flow into every model weight. This gives training broad freedom, but it also requires gradients and optimizer state for the full model and produces another full-sized set of weights for the task.
LoRA changes only the destination of the update. The same examples, loss, and backpropagation are used, but the pretrained weights are marked as frozen. Gradients update the small LoRA parameters instead, so the task is stored as an adapter rather than another copy of the model.
Transformer Layers LoRA Can Change
A transformer carries each token as a list of learned features called a hidden state. Linear layers appear throughout every transformer block, especially inside attention and the feed-forward network. They transform those features using weight matrices.
A typical Llama-style Transformer block contains two main components: self-attention and a feed-forward network (FFN). Self-attention uses q_proj, k_proj, v_proj, and o_proj. The FFN uses gate_proj, up_proj, and down_proj.
Each of these seven projections has a weight matrix that LoRA can adapt.
In Attention Projections
| Matrix | Purpose |
|---|---|
q_proj (Query) | What information am I looking for? |
k_proj (Key) | What information can other tokens be matched by? |
v_proj (Value) | What information do other tokens provide? |
o_proj (Output) | How do I combine and return the retrieved information? |
Applying LoRA to q_proj changes how the model searches for relevant information. Applying LoRA to v_proj changes the information carried between tokens. This is why query and value projections are common starting points for LoRA. [1]
In FFN Projections
Attention enables tokens to communicate with each other. The FFN processes and transforms each token’s representation independently.
In a Llama-style gated FFN:
up_projexpands the hidden representation into a larger feature space.gate_proj, followed by the SiLU activation, controls which features are emphasized or suppressed through elementwise multiplication with theup_projoutput.down_projprojects the transformed features back to the original hidden dimension.
By applying LoRA to these matrices, you also allow the model to adjust how it transforms its internal features.
LoRA can be attached to selected projections in attention, the feed-forward network, or both. It adds a parallel weight update to those linear layers. The surrounding attention calculation, activation functions, normalization, and residual path remain unchanged.
Suppose you configure LoRA like this:
target_modules=["q_proj", "v_proj"]
Inside every Transformer block, LoRA adds trainable matrices A and B to the query and value projections.
How LoRA Computes an Update
LoRA doesn’t replace what the pretrained model already knows. It learns a small correction to the model’s existing behaviour.
Imagine a pretrained model has a weight matrix . When a linear layer receives a hidden state , a vector containing information about the input, it computes:
- : input hidden state
- : pretrained weight matrix
- : output of the linear layer
Instead of changing , LoRA keeps it frozen and trains two smaller matrices, and .
The first reduces the hidden state to a narrow intermediate width . The second restores the original output width.
The new calculation becomes:
- : what the pretrained model would normally output.
- : the correction learned by LoRA.
- : the final, adapted output.
A LoRA adapter is therefore not a second language model. It is the collection of these small update matrices across the selected layers. Several adapters can share the same frozen base model because each stores only its own corrections.
In the original LoRA formulation, is initialized with small random values and is initialized to zero. The initial adapter update is therefore zero. [1]
On the first backward pass, can receive a gradient because is generally nonzero. The gradient for initially passes through zero-valued , so starts changing after moves away from zero.
Matrix Rank and LoRA
A matrix is a rectangular table of numbers. Its shape tells us how many input features it receives and how many output features it produces. A weight matrix with shape maps a hidden state with features to one with features.1
The rank of a matrix describes how many independent directions its transformation can express. A full-rank matrix can use every available direction. A low-rank matrix is more restricted because some rows or columns can be produced from combinations of others.
- Rank 2 (full rank): A 2D square becomes a parallelogram, preserving two independent directions.
- Rank 1 (low rank): The square collapses onto a line, leaving just one independent direction.
- Rank 0: Everything collapses into a single point, leaving zero independent directions.2
Source: My Datascience Journey, Determinants.
The product below has the same outer shape as the pretrained weight matrix , so it can be added to that weight. Its rank cannot exceed the inner dimension . This is the bottleneck: the update has at most independent output directions.
Multiplication by can expand the number of coordinates, but it cannot recreate independent directions lost when passing through the narrow -dimensional bottleneck. This is the mathematical reason two smaller matrices form a low-rank update.
Consider a linear layer with 4,096 input features and 4,096 output features. A freely trainable update would contain parameters. LoRA sends the update through a much narrower space instead.
With , the two adapter matrices contain only 65,536 parameters for this layer:
| Update | Trainable parameters |
|---|---|
| Full update matrix | |
| LoRA matrices and |
That is , or about 0.39% as many trainable parameters as a full update matrix. The frozen base weights are still present; these counts compare the parameters trained for this layer.
- Full update parameters
- 16,777,216
- LoRA parameters (A + B)
- 65,536
At rank 8, LoRA trains 0.391% as many parameters as the full update, or 256× fewer.
Full update: dout × din. LoRA: r × (din + dout).
Visualization inspired by TensorTonic’s LoRA rank comparison.
Footnotes
-
A weight matrix transforms a list of numbers from one size into a list of numbers of another size. Suppose and . The input has three features, and the output has two. The weight matrix has two rows and three columns:
Each row of produces one output feature:
Output feature Calculation Result First Second So the product is:
Three input numbers become two output numbers. ↩
-
The numbers in the blue grids at the top are the entries of the matrices that control how the shapes below are transformed.
Each matrix takes a point and produces a new point .
1. Rank 2: Full rank
The numbers tell us:
The two columns, and , represent two independent directions. That’s why the square becomes a parallelogram rather than collapsing.
2. Rank 1: Low rank
Notice that both columns are identical!
Because , every output point lies on the same line.
That’s why the square collapses into a line.
3. Rank 0: Zero rank
Every input becomes:
The entire square collapses into a single point.
The key idea: The numbers determine how the transformation moves points. The rank tells us how many independent output directions remain. ↩
Bibliography
- Edward J. Hu et al., “LoRA: Low-Rank Adaptation of Large Language Models,” arXiv preprint, 2021.