Bookmarks

LoRA — LoRA method

Low-Rank Adaptation

A way of further training language models in which the weights of the base model are frozen and only small low-rank adapter matrices are trained.

Also: lora, LoRA adapter, low-rank adaptation

checked 9 Nov

All terms

LoRA addresses the main problem of fine-tuning large models: storing and updating billions of weights for every task is expensive. Instead, the base model stays unchanged, and everything learned during fine-tuning is two small matrices for each selected one. Hu and co-authors proposed the method in 2021; today it is one of the common ways of adapting open models to a task or a style.

How it works

Full fine-tuning changes every weight of the model. The LoRA hypothesis: when a model adapts to a new task, the change in its weights has a low “intrinsic rank”, so it can be compressed into the product of two narrow matrices. For a weight matrix W0, an update is added in the form of B·A, where B has the size d×r, A — r×k, and the rank r is much smaller than the dimensions of the matrix.

textForward pass of a layer with LoRA: the adapter output is added to the layer’s original output
W0·x + ΔW·x = W0·x + B·A·x

During training, W0 is frozen and only A and B receive gradients. Matrix A is initialised at random and matrix B with zeros, so at the start of training the adapter changes nothing. The update is scaled by α/r, where α (alpha) is a constant. In practice, LoRA is added to the attention matrices of a transformer (Wq, Wk, Wv, Wo).

Savings in numbers

The method’s paper gives this calculation for GPT-3 175B: with rank r=4 on the attention query and value matrices, the fine-tuning checkpoint shrinks roughly 10,000-fold — from 350 GB to 35 MB. Training memory drops from 1.2 TB to 350 GB, because frozen weights need no optimiser states or gradients.

WhatFull fine-tuningLoRA
Trainable weightsall the weights of the modelthe A and B matrices of the adapters, usually under one percent of the total
Training memoryhigh: weights + gradients + optimiser stateslower: gradients and optimiser only for the adapters
Task checkpointa copy of the whole modeltens of megabytes of adapters
Switching taskload another copy of the modelswap the adapters on top of a single base model
Inference slowdownnonenone: the adapter can be merged into the base model’s weights

Rank and merging into the weights

The main hyperparameter is the rank r: the higher it is, the more trainable parameters there are and the more expressive the adapter. In the method’s paper, on GPT-3 even r=1 and r=4 gave good results; in practice values from single digits to hundreds are taken — for example, 8, 16 or 64. The second parameter is the scale α. An adapter is trained for a particular base model, so published adapters come with an indication of which model they belong to.

After training, the update B·A can be merged into the base weights: W = W0 + B·A — then the adapter disappears and the model runs without any slowdown. To switch task, B·A is subtracted and a new adapter added. Adapters are small, which makes them easy to publish: the base model with open weights is downloaded once, and the task-specific fine-tunes are files of tens of megabytes.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • fine-tuning

    Fine-tuning in the broad sense changes all or many of the model’s weights; LoRA is a specific fine-tuning method that touches only small adapter matrices.

  • quantisation

    Quantisation compresses already-trained weights into lower-precision bits; LoRA does not compress weights but reduces the number of trainable parameters during fine-tuning.

In depth in the references

where the topic is covered in detail

Appears in the references