Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to your next lesson on adapting Large Language Models.
In our last session, we drew a clear line between In-Context Learning (ICL), where we guide a model through its prompt, and Fine-Tuning, where we update the model's internal weights. We concluded that full fine-tuning, while powerful for teaching a model new skills and behaviors, is incredibly resource-intensive, requiring massive amounts of GPU memory and storage for each new task.
Today, we'll explore the solution to this challenge. This lesson focuses on Parameter-Efficient Fine-Tuning (PEFT), a family of techniques that makes fine-tuning accessible and manageable. Our main goal is to explain the concept of PEFT and dive deep into the LoRA method, one of the most popular and effective approaches used today. We'll cover what LoRA is, why it works, and the elegant mathematical idea that powers it.
The Challenge of Full Fine-Tuning
Let's start by reinforcing why full fine-tuning is so costly. A model like Llama 3 70B has 70 billion parameters. During training, you need to store not only the model's weights but also their gradients and the optimizer states for every single parameter. For an optimizer like Adam, this means storing two additional values (momentum and variance) for each weight, effectively tripling the memory requirement for the parameters alone.
For a 70B parameter model using 16-bit precision (2 bytes per parameter), the weights alone are 140 GB. The full training state can easily exceed 420 GB of GPU VRAM, which is beyond the capacity of all but the most powerful, industrial-scale hardware setups. Storing a new, fine-tuned version of the model for every task is equally impractical.

This is the problem that Parameter-Efficient Fine-Tuning (PEFT) was designed to solve.
Parameter-Efficient Fine-Tuning (PEFT)
PEFT encompasses a collection of methods that aim to adapt a pre-trained model by training only a small fraction of its parameters, or by training a small set of new parameters, while keeping the vast majority of the original model's weights frozen.
There are several PEFT techniques, including:
- Adapter Layers: Inserting small, new neural network layers into the original model and training only them.
- Prefix Tuning: Adding a small set of trainable vectors (a "prefix") to the input of the model.
- Low-Rank Adaptation (LoRA): The method we will focus on today.
The key idea behind all PEFT methods is to achieve performance comparable to full fine-tuning but with a tiny fraction of the computational and storage cost.
LoRA: The Core Idea
LoRA, which stands for Low-Rank Adaptation, proposes a simple yet brilliant approach. Instead of directly changing the pre-trained weights , we keep them frozen and learn the change in weights, , as a separate, much smaller component.
During fine-tuning, the forward pass through a layer is modified. The output for an input is calculated as:
Here, is the output from the original, frozen model, and is the output from our newly trained "update" component.
To understand this core concept, let's watch a short video that introduces the problem and LoRA's high-level solution.
What is LoRA? Low-Rank Adaptation for finetuning LLMs EXPLAINED
The video "What is LoRA?" by AI Coffee Break with Letitia provides a clear introduction to why LoRA is needed and how it works conceptually.
Watch the first two minutes of the video (00:00 - 03:04). The first part (until 01:40) recaps the problem with fine-tuning. The second part explains LoRA's core idea of freezing the original weights and training a separate update matrix.
This raises a crucial question: if has the same dimensions as , haven't we just doubled the parameters? How is this more efficient? This is where the "low-rank" part of the name becomes critical.
The "Magic" of LoRA: Low-Rank Decomposition
The foundational hypothesis of the LoRA paper is that the change in weights, , needed to adapt a model to a new task has a low "intrinsic rank". In simpler terms, the update doesn't need to be complex; it can be represented by a much simpler, lower-dimensional transformation.
Given your background in linear algebra, you know that a matrix's rank represents the number of linearly independent columns (or rows) it has. A low-rank matrix contains redundant information, meaning its transformations can be described by a smaller number of basis vectors.
LoRA leverages this by not learning the large matrix directly. Instead, it approximates as the product of two much smaller, "thin" matrices:
where:
Wis ad x kmatrix.Ais ad x rmatrix (a "down-projection").Bis anr x kmatrix (an "up-projection").ris the rank, a small hyperparameter (e.g., 8, 16, 64) that we choose.ris much smaller thandandk.
The number of trainable parameters is now the sum of the parameters in A and B, which is . This is dramatically smaller than the parameters in the original .
So, the forward pass becomes:
During training, only the matrices A and B are updated. All the original weights W remain frozen.
To get an intuitive feel for this from the inventor of LoRA himself, let's watch another short clip.
What is Low-Rank Adaptation (LoRA) | explained by the inventor
In this video, Edward Hu, who led the invention of LoRA, explains the intuition behind using rank to control the expressivity of the update matrix.
Watch the segment from 01:31 to 02:54. Pay attention to how he frames fine-tuning with two questions and how reducing the rank r restricts the possible transformations, thus reducing the number of parameters.
For a more detailed textual explanation of the underlying matrix properties, the following article is an excellent resource.
LoRA - Intuitively and Exhaustively Explained
The article "LoRA - Intuitively and Exhaustively Explained" on Towards Data Science provides a great breakdown of the linear algebra concepts that make LoRA work.
Read the sections titled "2) Parameter Change Compression" and "The Core Idea Behind LoRA". These sections explain the concepts of linear independence, rank, and matrix factorization, and tie them directly to LoRA's strategy of learning the factors of the change matrix.
The Key Advantages of LoRA
The low-rank decomposition strategy gives LoRA several powerful benefits:
- Massive Reduction in Trainable Parameters: As seen, we train far fewer parameters. This reduces the GPU VRAM needed for gradients and optimizer states, making it possible to fine-tune large models on consumer-grade GPUs.
- Small and Portable Checkpoints: Since we only need to save the small
AandBmatrices for each task, a fine-tuned adapter might only be a few megabytes instead of hundreds of gigabytes. This makes it easy to store and share many different task-specific adapters. - No Additional Inference Latency: This is a crucial and non-obvious advantage. While training involves two parallel paths ( and ), once training is complete, the update can be merged back into the original weights. We can compute
W_merged = W + B * Aonce and deploy this single, fused matrix. The model's architecture remains identical to the original, so there is no latency penalty at inference time. This is a significant advantage over methods like Adapters, which add extra layers to the model. - Easy Task Switching: You can load the base model and dynamically load, unload, or even combine different LoRA adapters on the fly, making it highly efficient to serve multiple specialized models.
The inventor of LoRA explains the "no inference latency" benefit very clearly.
What is Low-Rank Adaptation (LoRA) | explained by the inventor
Let's return to Edward Hu's video to see his explanation of LoRA's key benefits, particularly the absence of inference latency.
Watch the clip from 04:50 to 06:07. Focus on how the A and B matrices are merged back into the original weights after training, ensuring inference is just as fast as the base model.
Conclusion
You now have a solid theoretical understanding of parameter-efficient fine-tuning and the LoRA method. This technique has revolutionized the way we adapt and specialize large models, making it a fundamental skill for any AI engineer.
Key Takeaways:
- PEFT is a set of techniques to fine-tune large models by only updating a small subset of parameters, drastically reducing computational and storage costs.
- LoRA is a PEFT method that freezes the pre-trained weights and injects a trainable, low-rank update.
- The core mechanism of LoRA is approximating the weight update matrix as the product of two much smaller matrices, .
- The rank
ris a key hyperparameter that controls the number of trainable parameters and the expressiveness of the update. - LoRA's key advantages include huge memory savings, small checkpoint sizes, and no added inference latency due to the ability to merge weights after training.
Preview of the Next Lesson
Theory is great, but putting it into practice is even better. In our next lesson, we will move from concept to code. You will learn how to apply LoRA to adapt an LLM to a specific task using the HuggingFace PEFT library, taking your first practical step into the world of efficient fine-tuning.