What is LoRA (low-rank adaptation)?
LoRA (low-rank adaptation) is a fine-tuning method that freezes a pretrained model's weights and trains a small low-rank update next to selected weight matrices. Each update is the product of two thin matrices, B and A, so a layer computes W₀x + BAx. Only B and A are trained, which is usually well under 1% of the model's parameters.
LoRA is the most common way to adapt large language models and image generators on modest hardware. It is used to fine-tune open models such as Llama and Mistral for chat, coding, or a company's own data, and to train style and character adapters for Stable Diffusion. Because each adapter is a few megabytes, one base model can serve many tasks, and the adapter can be merged into the weights so inference runs at the original speed.
Key ideas
- LoRA freezes the pretrained weights and trains a low-rank update ΔW = BA beside each chosen matrix.
- For a d × d weight, LoRA trains 2dr parameters instead of d², a large saving when r is small.
- B starts at zero, so training begins from the exact pretrained model.
- A trained adapter can be merged into the weights, so inference costs nothing extra.
- One base model can serve many tasks by swapping adapters of a few megabytes each.
- QLoRA pairs LoRA with a 4-bit frozen base to fine-tune very large models on one GPU.
Short takes on this topic
Common questions
What rank should I use for LoRA?
Ranks from 4 to 64 are common, and 8 or 16 is a typical starting point. A harder task or a larger gap from the pretraining data can justify a higher rank. If the adapter underfits, raising the rank or adding more target layers usually helps more than training longer.
What does lora_alpha do?
The update is scaled by α / r before it is added to the frozen output. Changing α acts much like changing the adapter's learning rate. Common settings are α equal to r or twice r.
Is LoRA as good as full fine-tuning?
On the tasks in the original paper, including GLUE with RoBERTa and DeBERTa and several GPT-3 benchmarks, LoRA matched or beat full fine-tuning. Later studies found a gap on tasks that need a lot of new knowledge, such as continued training on code or math, where LoRA learns less but also forgets less of what the base model knew.
Can LoRA be used for image models?
Yes. In Stable Diffusion, LoRA adapters are usually trained on the attention layers of the denoising network to teach a style, a character, or an object from a few dozen images. The resulting files are small enough to share and stack, which is why they became a common format in image-generation communities.
Related explainers
- Transfer Learning — Standing on the shoulders of pretrained giants
- Singular Value Decomposition — Factor any matrix into rotation, scaling, and rotation — the Swiss army knife of linear algebra
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- RLHF — Align language models with human preferences using reward models and policy optimization
- How Self-Attention Works — Query, key, value — the core of self-attention
- Linear Algebra — Vectors, matrices, and the language of data
References
- Hu et al. (2021), "LoRA: Low-Rank Adaptation of Large Language Models"
- Dettmers et al. (2023), "QLoRA: Efficient Finetuning of Quantized LLMs"
- Houlsby et al. (2019), "Parameter-Efficient Transfer Learning for NLP"
Last updated