AI engineering paper · 2022

LoRA

Hu, Shen, Wallis, Allen-Zhu, Li, Wang, Wang and Chen · ICLR 2022

How LoRA fine-tunes a huge model by training a pair of thin matrices beside each adapted weight matrix, why a merged adapter adds no serving delay, and what later work changed.

LoRA leaves W0 alone and trains B and A. At rank 4 for one GPT-3 matrix, that is 98,304 trained numbers beside 150,994,944 frozen ones.

You’ll learn to

  • Explain how LoRA writes the change to a weight matrix as the product of two thin matrices, and why that is enough for many tasks.
  • Count the trainable numbers for a real model, GPT-3 175B, and work out the storage and memory this saves.
  • Explain why a merged adapter adds no delay at serving time, and when you would keep it separate.
  • Say which of the paper’s choices still hold, and which later work changed: target layers, rank, scaling and learning rate.

The idea in short

A language model is mostly large grids of numbers, called weight matrices. Fine-tuning means training the model further on your own examples, so those numbers change. Done to every number, it needs a full copy of the model for every task, plus a lot of memory while training.

LoRA freezes the original numbers and learns only the change, written as the product of two thin matrices. For GPT-3 that is about 0.01% as many trainable numbers. The change can be added into the original weights after training, so the result runs exactly as fast as the model you started with.

The problem

Take a model that has already learned from a huge amount of text, and you want it to do one job well: write SQL from a plain-English question, summarize support chats, follow your company’s tone. The usual route is : you train the model a little further on examples of the job. Training adjusts the model’s learned numbers, called its weights or parameters (the three words mean the same thing here), so a fine-tuned model is a changed copy of the original.

The plain version, where every number is allowed to change, is called . For a small model that is fine. For the paper’s target, GPT-3 with 175 billion numbers, it hurts in three places.

  • Storage. Each task needs its own full copy. At two bytes per number, which is our arithmetic and matches the paper’s 350 GB figure, one copy of GPT-3 is 350 GB. The paper’s own example is 100 tasks. Full copies come to about 35 TB.
  • Training memory. Training stores more than the weights. For every number it trains it also keeps a gradient (which way to adjust that number) and the running averages that the Adam training method uses to choose step sizes. Those averages are called the . With both, the paper puts GPT-3 175B training at 1.2 TB of GPU memory (VRAM).
  • Switching. To serve a different task, you load another 350 GB.

A model with 175 billion numbers is an extreme case. The shape of the problem is the same for smaller ones, because the gradients and optimizer state add to the weights. That remark is ours, not the paper’s.

So the question the paper asks is: can we adapt a large model to a task by training only a tiny number of new values, and lose nothing in quality or in speed at serving time? Others had tried. The next section says why those attempts did not satisfy the authors.

Why the earlier shortcuts fell short

The paper names two families of earlier methods. Each has a cost the authors wanted to avoid. Both come under the name : keep the model frozen and train only a small number of extra values.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

About the paper

LoRA: Low-Rank Adaptation of Large Language Models by Hu, Shen, Wallis, Allen-Zhu, Li, Wang, Wang and Chen, ICLR 2022.

Read the original paper

More AI engineering papers

All papers