AI Hints
Home›Generative AI Tutorial›Customising and Running Models
Generative AI · Lesson 51 of 77

Fine-tuning with LoRA

Adapting a huge model cheaply.

⏱ 2 min read▶ Video at 1:56:02🎓 Free
Watch this lesson in the Generative AI courseWatch on YouTube ↗Full Generative AI course

Updating every weight of a huge model needs a huge computer. But researchers found a shortcut, that changes only a tiny part, and works nearly as well. It is called LoRA, and it has made fine tuning possible for almost anyone. In this lesson, you will understand the idea.

Fine-tuning with LoRA
From the video: Fine-tuning with LoRA
  • Updating every weight is expensive
  • A clever shortcut
  • Fine-tuning for almost anyone

The idea behind LoRA

Here is the idea. Freeze the original model, so none of its billions of weights change. Next to certain layers, add two tiny extra matrices, called adapters. During fine tuning, only these small adapters learn. They act like a thin lens, on top of the frozen model, that changes its behaviour. The adapters are so small, that they may be a thousand times smaller than the model itself.

  1. Freeze the modelbillions of weights stay fixed
  2. Add tiny adapterssmall extra matrices
  3. Train only the adaptersfast and cheap
  4. Plug them ina small file changes behaviour
Train a thin lens, not the whole model
The idea behind LoRA
From the video: The idea behind LoRA

Why LoRA is a big deal

LoRA brings real benefits. Training needs far less memory, so it runs on a single graphics card. It is much faster, and cheaper. The adapter is a small file, a few megabytes, so you can keep many adapters, one for each task, or customer, and swap them in, on the same base model. And the original model is never damaged.

Full fine-tuning

  • Updates every weight
  • Needs many powerful GPUs
  • Large, costly files
  • High risk of damaging the model

LoRA

  • Trains tiny adapters only
  • Runs on a single GPU
  • Adapter files of a few megabytes
  • Base model stays safe

A fine-tuning project, step by step

A typical project has five steps. Collect a few hundred to a few thousand high quality examples, of the behaviour you want. Format them, as instruction and answer pairs. Train a LoRA adapter, while watching the validation score. Test it, against examples it has never seen. And compare it with a well prompted base model. If the improvement is not clear, you may not need the fine tune at all.

  1. Collect exampleshundreds to thousands, high quality
  2. Format pairsinstruction and ideal answer
  3. Train the adapterwatch validation
  4. Test and compareagainst a well-prompted base
Quality of examples matters most

Summary

Good. LoRA freezes the big model, and trains small adapters, which makes fine tuning cheap, fast, and flexible. Success depends on a small set of high quality examples, and honest testing. Next, how to run models on your own computer, with quantization.

Key takeaways
  • LoRA: freeze the model, train small adapters
  • Adapters are tiny, swappable files
  • Quality of examples matters most
Next up: Running Models Locally — quantization and small models