AI engineering paper · 2023

QLoRA

Dettmers, Pagnoni, Holtzman and Zettlemoyer · NeurIPS 2023

How QLoRA squeezes a large language model into 4 bits per number so that one graphics card can fine-tune it, and what later work changed.

The frozen model sits in 4 bits and is unpacked one layer at a time. Only the highlighted adapters are trained, which is how a 65-billion-weight model fits in under 48 GB instead of over 780 GB.

You’ll learn to

  • Explain how QLoRA stores the frozen model in 4 bits, yet still trains in 16 bits.
  • Work out how many bits a stored weight really costs, and how storing the rounding’s helper numbers in fewer bits cuts it.
  • Say where fine-tuning memory goes once the model is small, and how spilling to the computer’s main memory prevents crashes.
  • Weigh the paper’s chatbot claims, and name which parts of its recipe later work kept or changed.

Read first: LoRA

The idea in short

A language model is mostly a huge list of numbers, called weights, and each one is normally stored in 16 bits (a bit is a single 0 or 1, so more bits keep more detail). Fine-tuning, which means training the model further on your own examples, has to hold all of them in the memory of a graphics card, plus more for training. For a model with 65 billion weights, the authors of the 2023 QLoRA paper put that at over 780 GB.

Their method, QLoRA, freezes the original model and stores it with just 4 bits per weight, a rounding step called quantization. When a layer is needed, it is unpacked to 16 bits for the arithmetic, then dropped. Only small add-on pieces, called adapters, are trained, and they stay in 16 bits. Two further tricks trim what is left: one stores the rounding’s helper numbers in fewer bits, and one lets spare work spill into the computer’s main memory. The result: the same 65-billion-weight model fine-tunes in under 48 GB, which is one card.

The problem

In 2023, Dettmers, Pagnoni, Holtzman and Zettlemoyer published “QLoRA: Efficient Finetuning of Quantized LLMs”. From here, “the paper” means that one. It builds on , which our LoRA guide explains. Read that first if are new to you.

Start with what a model is made of. A language model is a stack of layers. Each layer takes a list of numbers, multiplies it by grids of learned numbers called matrices, and passes the result on. All those learned numbers are the model’s . A computer stores each number using some count of bits, the 0s and 1s of binary. The usual choice for training is 16 bits, which is 2 bytes (a byte is 8 bits). The weights live in the memory of a , the graphics card that does the arithmetic. This GPU memory is small and expensive. The paper talks about cards with 24 GB and 48 GB.

means training the model further on examples of your own job. It needs more than the weights. For each weight that is trained, it also keeps:

  • a : a number saying which way, and how far, to change that weight;
  • the : running averages the training method keeps for each trained weight to choose its step size.

Training also passes a learning signal backwards through the layers, a step called the backward pass, and that needs working memory for the results of each layer. The paper says that regular 16-bit fine-tuning of the 65-billion-weight LLaMA model (an openly available family of language models) needs more than 780 GB of GPU memory. That is many cards, in a server most teams do not have.

Weights alone, at 16 bits

65 billion × 2 bytes = 130 GB

Our arithmetic. This is the least a card must hold, before any training.

The paper’s total for fine-tuning

Over 780 GB

Our division: 780 GB ÷ 65 billion = 12 bytes per weight, six times the weights alone. The rest is the gradients, optimizer state and working memory above (our reading; the paper does not split the 780).

Weights alone, at 4 bits

65 billion × 0.5 bytes = 32.5 GB

Our arithmetic, ignoring a small extra cost the next sections count. A card with 48 GB could hold this, with room left.

Shrinking models with fewer bits was already common for running them. The paper says such methods “only work for inference and break down during training”. means using a finished model to answer, with no learning. LoRA had already cut the trained part to a sliver, but the frozen model still sat in 16 bits.

So the question answers is: can the frozen model be kept in 4 bits, with training done on top, and still match 16-bit fine-tuning in quality?

The design choices

The paper makes six choices. Five are about memory or reliability, and one is about quality. Each is explained in its own section below, so the table only gives the idea.

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

QLoRA: Efficient Finetuning of Quantized LLMs by Dettmers, Pagnoni, Holtzman and Zettlemoyer, NeurIPS 2023.

Read the original paper

More AI engineering papers

All papers