explain formula for lora: low rank adaptation of large language models
- User
explain formula for lora: low rank adaptation of large language models
- Assistant
Introduction
LoRA (Low-Rank Adaptation) is a technique designed to efficiently fine-tune large language models (LLMs) by introducing low-rank updates to the weight matrices of neural networks. Instead of updating the entire weight matrix during fine-tuning—which can be computationally expensive and memory-intensive—LoRA proposes learning the difference between the pretrained weights and the fine-tuned weights through low-rank decomposition. This approach significantly reduces the number of trainable parameters and speeds up the fine-tuning process.
Core Concept
In neural networks, particularly in transformer-based language models, a significant number of parameters are concentrated in the weight matrices of linear layers. Fine-tuning these large models traditionally involves updating all these weights, which can be impractical for very large models.
LoRA addresses this by:
- Keeping the original pretrained weights frozen during fine-tuning.
- Introducing trainable low-rank matrices that approximate the weight updates needed for the new task.
Mathematical Formulation
Let's delve into the mathematical details of how LoRA implements low-rank adaptation.
-
Standard Weight Update Without LoRA:
During fine-tuning, the weight matrix ( W \in \mathbb{R}^{d \times k} ) of a linear layer is updated to ( W' ):
[ W' = W + \Delta W ]
Here, ( \Delta W ) represents the full-rank weight update learned during fine-tuning.
-
LoRA's Low-Rank Decomposition:
Instead of learning the full-rank ( \Delta W ), LoRA constrains ( \Delta W ) to be a low-rank matrix by decomposing it into the product of two smaller matrices:
[ \Delta W = B A ]
- ( A \in \mathbb{R}^{r \times k} )
- ( B \in \mathbb{R}^{d \times r} )
- ( r \ll \min(d, k) ) (i.e., ( r ) is much smaller than both ( d ) and ( k ))
The updated weight matrix becomes:
[ W' = W + B A ]
-
Forward Pass with LoRA:
In the forward pass, the output ( y ) of the linear layer with input ( x \in \mathbb{R}^{k} ) is computed as:
[ y = W x + \Delta W x = W x + B A x ]
This can be interpreted as adjusting the original output ( W x ) with an additional term ( B (A x) ) that accounts for task-specific adaptations.
Intuitive Understanding
-
Low-Rank Approximation: By constraining ( \Delta W ) to be low-rank, LoRA assumes that the pertinent changes needed for fine-tuning lie in a lower-dimensional subspace. This leverages the observation that parameter updates often have redundancy and can be represented compactly.