Low-rank adaptation (LoRA)
Low-rank adaptation (LoRA) is a fine-tuning method that freezes a model's original weights and trains a small set of added matrices, sharply cutting the cost of customising large models.
Fine-tuning a large model the ordinary way means updating every one of its parameters, which takes a lot of memory and produces a full-size copy of the model for every task. LoRA takes a cheaper route. It freezes the original weights and inserts small trainable matrices into each layer of the transformer. Instead of rewriting a huge weight matrix, it learns the change to that matrix as the product of two thin matrices. That change is said to have low rank, which is where the name comes from.
The method was introduced in June 2021 in LoRA: Low-Rank Adaptation of Large Language Models by Edward Hu and colleagues, who released their code through Microsoft's GitHub. Compared with fully fine-tuning the 175 billion parameter GPT-3 with the Adam optimiser, they reported that LoRA cut the number of trainable parameters by 10,000 times and the GPU memory needed by three times, while matching or beating full fine-tuning on quality for RoBERTa, DeBERTa, GPT-2 and GPT-3. Unlike earlier adapter methods, it adds no extra delay when the model is running.
In practice this changes who can customise a model. A LoRA adapter is a small file that sits alongside a base model, so one copy of the base model can serve many specialised versions. Hugging Face's Diffusers documentation notes that LoRA weights for image models come to a few hundred megabytes, which makes them easy to store and share, and the method is supported for models such as Stable Diffusion XL. An adapter can also be merged back into the base weights so the result runs as a single standalone model.
LoRA has spawned variants. The best known is QLoRA, published in May 2023 by Tim Dettmers and colleagues, which trains LoRA adapters on top of a base model compressed to 4-bit precision. They reported fine-tuning a 65 billion parameter model on a single 48GB GPU while preserving the task performance of 16-bit fine-tuning.
LoRA makes customisation cheaper; it does not make it automatic. The adapter can only learn what the training examples show it, so the quality of a LoRA depends on the data behind it, and it inherits the strengths and flaws of the base model it was trained on.
Full fine-tuning updates every weight in the model and produces a complete new copy. LoRA freezes the original weights and trains only small added matrices, so the output is a compact adapter. The LoRA paper found quality on par with or better than full fine-tuning on the models it tested, at a fraction of the trainable parameters. It is a small adapter file trained to steer an image model towards a particular subject or style without retraining the whole model. Because the files are relatively small, often a few hundred megabytes, they are easy to share and load on top of a compatible base model. QLoRA combines LoRA with quantisation. The base model is stored in 4-bit precision and frozen, and LoRA adapters are trained on top. Its 2023 paper reported fine-tuning a 65 billion parameter model on a single 48GB GPU, which brought large-model fine-tuning within reach of far smaller budgets. Not necessarily. The original paper notes that LoRA adds no extra inference delay because the learned matrices can be merged into the base weights. If an adapter is kept separate so it can be swapped in and out, there can be a small overhead, which merging removes.Common questions
What is the difference between LoRA and full fine-tuning?
What is a LoRA in Stable Diffusion and other image tools?
What is QLoRA?
Does LoRA slow a model down?
Search for "LoRA fine-tuning", "QLoRA" or "LoRA vs full fine-tuning" for how the method works and when it is used. The most reliable sources are the original papers on arXiv and the documentation of libraries that implement LoRA, such as Hugging Face PEFT and Diffusers. Treat quality comparisons in tutorials with care, because results depend heavily on the base model, the rank chosen and the training data.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.