LoRA and Adapters Explained: Cheap Fine-Tuning Without Retraining the Entire Model
Edited by: Editors llmnet.nl | Reading time: approx. 7 minutes
Adapting large language models (LLMs) to specific tasks was, until recently, a costly and complex endeavor. If you wanted to teach a model how to summarize medical texts or how to code in a specific programming language, you had to retrain the entire model. This method is unaffordable for most companies and individual developers.
Fortunately, the introduction of PEFT (Parameter-Efficient Fine-Tuning) has changed the landscape. The most popular technique within this category is LoRA, which stands for Low-Rank Adaptation. With LoRA, you can adapt a model for a fraction of the computing power. In this article, we explain exactly how LoRA and so-called 'adapters' work, why they have become the industry standard, and how they compare to other techniques.
The Problem of Full Fine-Tuning
Before we can understand LoRA, we need to look at how full fine-tuning works. A neural network consists of billions of parameters, also called 'weights'. With full fine-tuning, you adjust all the weights in the network during the learning process.
This process requires a massive amount of video memory (VRAM). This is because you not only need to load the model's weights into memory, but also the so-called 'gradients' (the calculated error margins) and the status of the 'optimizer' (the algorithm that updates the weights, such as AdamW). A rough estimate is that full fine-tuning requires three to four times more VRAM than the actual size of the model. A model with 7 billion parameters, which takes up about 14 GB of storage, quickly requires dozens of gigabytes of VRAM to train. As a result, you are forced to rent heavy, expensive cloud infrastructure.
What is LoRA (Low-Rank Adaptation)?
LoRA solves this memory problem in an elegant way. Instead of adjusting all the original billions of weights of the model, LoRA freezes the original, pre-trained model. Nothing is changed at the base.
In addition, LoRA injects small, extra layers into the network, which we call adapters. During the training phase, only the parameters in these tiny adapters are trained and updated. The massive amount of base weights remains untouched.
The mathematics behind the magic
Without diving too deep into linear algebra, LoRA utilizes 'matrix decomposition'. A large matrix of weights can seem very complex, but much of that complexity is not needed to learn a specific new task. LoRA replaces adjusting a giant matrix (for example, 4096 by 4096 numbers) with training two much smaller matrices (for example, 4096 by 8 and 8 by 4096). When you multiply these two small matrices together, you get an update that resembles the large matrix, but cost much less computing power to train.
The Practical Benefits of LoRA
Using adapters offers massive benefits for developers, data scientists, and AI enthusiasts:
- Lower hardware requirements: Because you only need to store the optimizer state and gradients for the small adapter, the required VRAM drops drastically. This suddenly makes training your own models feasible on a single, powerful graphics card in a desktop, or a local home HPC setup. You are no longer dependent on a data center.
- Faster training times: Because significantly fewer parameters need to be calculated and updated, the training process is much faster.
- Small file size: A saved adapter is often no larger than 50 to 300 Megabytes, while the base model is dozens of Gigabytes in size. You can easily share these small adapters over the internet or store them locally.
- Hot-swapping: In a production environment, you can keep one large base model (such as Llama or Mistral) in memory and load a different small adapter per request. This is called 'hot-swapping' and is extremely cost-effective.
How Adapters Work: Hyperparameters (r and alpha)
When you train an adapter, you need to configure a few settings (hyperparameters). The two most important ones are the 'rank' (r) and alpha.
The Rank (r)
The parameter r determines the size or 'capacity' of the adapter. A higher rank means the adapter has more parameters and can theoretically learn more complex patterns. A lower rank trains faster and uses less memory. A common rule of thumb, not a measurement, is to start with a rank of 8 for simple tasks, and potentially increase this to 16, 32, or 64 for more complex data representations.
Alpha
The alpha parameter is a scaling factor. It determines how heavily the weights of the trained adapter weigh on top of the base model. Another rule of thumb, not a measurement, is to set the alpha value to exactly double the rank (so if r=8, you choose alpha=16). This helps ensure stability during training.
QLoRA: LoRA with Quantization
Although LoRA solves the memory problem for training updates, the base model still needs to fit into the VRAM. For giant models, even that is sometimes too much to ask. This is where QLoRA (Quantized LoRA) comes into play.
With QLoRA, the base model is loaded in a compressed format, usually 4-bit (quantization). This compresses the base model enormously, minimizing its footprint in the VRAM. A normal (higher precision) LoRA adapter is then trained on top of this compressed base model. QLoRA makes it possible to fine-tune models with 70 billion parameters on a set of consumer graphics cards, which is an absolute breakthrough in accessibility.
Comparing LoRA with Prompt Engineering and RAG
When should you fine-tune with LoRA, and when are other techniques sufficient? Not every problem requires a trained model.
| Technique | Best Application | Complexity |
|---|---|---|
| Prompt Engineering | Giving short instructions and guiding simple tasks via the chat interface. | Low |
| RAG (Retrieval-Augmented Generation) | Giving the model access to external knowledge, such as company documents or a recent database. As is often shown in practice, Retrieval-Augmented Generation works excellently for feeding a model up-to-date facts. | Medium |
| LoRA Fine-Tuning | Deeply embedding writing style, tone, or a specific output format (such as JSON) into the model's behavior. | High |
| Full Fine-Tuning | Teaching a model new concepts from scratch in a language it does not know at all. | Very High |
Note: LoRA is relatively poor at learning new, hard facts. Use adapters to guide behavior and format, and use RAG for factual knowledge.
Merging Adapters
Once your training script is ready, you own the saved LoRA adapter. During the phase where you actually use the model (inference), you can choose to load the adapter dynamically, as described earlier.
However, you can also choose to permanently merge the adapter into the base model. The small matrices are then mathematically added to the large matrices of the base model. The result is a new, standalone model that is exactly the same size and runs just as fast as the original base model, but permanently possesses the new capabilities. This process prevents the adapter from causing any latency during inference.
Getting Started with LoRA Yourself
Want to experiment yourself? The process basically consists of three steps:
- Collect a set of high-quality examples (input and desired output).
- Choose a suitable open-source model. Check our overview of base models in our guide for the best choices at the moment.
- Use frameworks like Hugging Face
PEFTor specialized tools like Unsloth to write and run your training script.
LoRA has drastically lowered the barriers around artificial intelligence. It gives developers the power to build personalized, high-quality AI applications without the astronomical costs of the past.