# Mission 19.2: Can I fine-tune on my laptop?

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/einsatz-19-2

Belongs to [Chapter 19: Fine-tuning in brief](https://llm.yoga/en/19-fine-tuning.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Felix**, sales engineer:

> “I wanted to train an 8B model to translate requests from our contact form into our CRM schema. Then I read that full fine-tuning of an 8B model needs about 128 GB. My MacBook has 24 GB. I guess that’s that.”

“We’d need a data center for that” is a sentence that ends AI projects early; you should be able to check whether it’s true. 8B means eight billion parameters, the learned numbers in the model. Fine-tuning trains a finished model further with your own examples; full fine-tuning can change every one of these numbers.

**Question 1 (prediction):** What takes up most of the 128 GB?

- A) The eight billion parameters themselves.
- B) Helper values that training keeps for every parameter.
- C) The training examples, which all sit in memory at the same time.

## What’s behind it

In full fine-tuning, training keeps track of two more things for every number in the model: how strongly and in which direction it should change, the **gradient**, and what the optimizer remembers about it, the optimizer being the procedure that takes the steps. For the common Adam optimizer, Microsoft’s ZeRO paper counts 2 bytes for the weight, 2 for the gradient and 12 for the optimizer: 16 bytes per parameter. For eight billion, that’s about 128 GB, without the intermediate results of training.

**LoRA** (Low-Rank Adaptation) turns this around. The weights stay frozen. Next to a weight matrix, a big table of numbers, you place two narrow strips: one as tall as the matrix, one as wide as it, both only r numbers thick. r is called the **rank**; 4 to 64 is usual. So one strip has rows times r numbers, the other r times columns. Only the strips are trained, and only they incur the 16 bytes.

**QLoRA** also keeps the frozen weights in 4 bits, about half a byte per parameter (chapter 9). For eight billion, that leaves about 5 GB, plus the strips and the intermediate results. On the Mac, the mlx-lm tool automatically does QLoRA when the model is in 4 bits. What comes out is an adapter, a file with only the strips, which only works on top of this exact base model.

For Felix, this means: the 128 GB apply to full fine-tuning. The mlx-lm guide trains a 7B model on a Mac with 32 GB, with one example per step and only four adapted layers. For 24 GB, chapter 19 recommends starting with a 4B model in 4 bits. On the test Mac, a short run with a 0.6B model needed 1.1 GB, and the adapter was 5.8 MB.

### In more detail: the math and numbers from the papers

Full fine-tuning according to ZeRO: 2 + 2 + 12 = 16 bytes per parameter. The optimizer’s 12 bytes are a copy of the weights in FP32 (4 bytes) and two moments of 4 bytes each. 8 billion × 16 bytes = 128 GB.

LoRA on a matrix with R rows and C columns: strip B has R × r numbers, strip A r × C, together r × (R + C). Their product B · A is as big as the matrix. After training, you can merge it in permanently; then the model is as fast as before.

Llama 3.1 8B, LoRA with rank 8 only on query (4,096 × 4,096) and value (4,096 × 1,024) as in the LoRA paper: 65,536 + 40,960 = 106,496 per layer, times 32 layers 3,407,872, so 0.042 percent of the 8.03 billion. At 16 bytes, that’s about 55 MB.

The LoRA paper (Hu et al., 2021) showed on GPT-3 with 175 billion parameters: memory in training 350 instead of 1,200 GB, and an adapter with rank 4 was 35 MB instead of 350 GB for a full copy of the model. QLoRA (Dettmers et al., 2023) trained a 65B model on a GPU with 48 GB; full fine-tuning in 16 bits would have needed over 780 GB.

## Try it

**Rank 16 on a feed-forward matrix**

**Task:** Felix applies LoRA with rank 16 to a matrix of Llama 3.1 8B. Work out what percentage of this matrix’s numbers he trains that way. Two decimal places.

**From the config.json of Llama 3.1 8B**

A matrix of the feed-forward layer: 4,096 rows (hidden_size) and 14,336 columns (intermediate_size).

Rank r: 16.

Share in percent

## Check

**Question 2 (check question):** Felix takes an 8B model that’s already in 4 bits and trains QLoRA. How much memory should he plan for the frozen weights?

- A) About 16 GB, 2 bytes per weight.
- B) Almost nothing, since frozen weights stay on the disk.
- C) About 5 GB, a bit over half a byte per weight.

**Question 3 (check question):** A colleague wants to use Felix’s result on her Mac. What does he send her?

- A) The adapter, a few megabytes; she loads it together with the same base model.
- B) The adapter; she puts it on a model she already has.
- C) The whole fine-tuned model, because all the weights have changed.

## Done

**What you can do now**

- You explain where the 128 GB for full fine-tuning of an 8B model come from, and that the optimizer takes up three quarters of it.
- With rank times rows plus rank times columns, you work out how many numbers a LoRA adapter trains on a matrix.
- If a customer says fine-tuning is only for data centers, you ask about model size and memory and show them the math for what QLoRA needs with a 4-bit model.

### Two cards for review

**Card 1:** LoRA with rank 8 on the value matrix of Llama 3.1 8B (4,096 × 1,024): how many numbers are trained?

- A) 4,194,304
- B) 40,960
- C) 32,768

**Card 2:** Why does QLoRA of an 8B model fit on a laptop, but full fine-tuning doesn’t?

- [Next mission: Are 80 examples enough?](https://llm.yoga/en/einsatz-19-3.md)
- [Try it on your Mac](https://llm.yoga/en/mac-19-2.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [Full fine-tuning, LoRA and QLoRA](https://llm.yoga/en/19-fine-tuning-2.md)
- [Exercise: a LoRA adapter on the Mac](https://llm.yoga/en/19-fine-tuning-3.md)
- [Chapter 9: what quantizing is](https://llm.yoga/en/09-quantisierung-1.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is B.
- A: In training, the parameters themselves need 2 bytes each, 16 GB in total, so only an eighth of the 128 GB.
- B, correct: Each parameter (2 bytes) comes with its gradient (2 bytes) and the optimizer state (12 bytes). Three quarters of the 128 GB belong to the optimizer.
- C: A few hundred examples are text, a few megabytes, and they’re processed in batches. The 128 GB depend on the number of parameters, not the number of examples.

**Share in percent** Answer: 16 × (4,096 + 14,336) = 294,912; divided by 4,096 × 14,336 = 58,720,256, that’s about 0.50 percent.

**Question 2:** The correct answer is C.
- A: 2 bytes per weight is what LoRA needs with weights in FP16, a 16-bit format. QLoRA keeps them in 4 bits, about half a byte per parameter: about 5 GB.
- B: Frozen only means they don’t change. The model computes with them in every training step, so they have to go into memory, about 5 GB at 4 bits.
- C, correct: 4 bits are half a byte, plus a little overhead. Add to that the strips and the intermediate results of training.

**Question 3:** The correct answer is A.
- A, correct: The adapter contains only the strips. It fits exactly the base model it was trained with, and the colleague loads that unchanged alongside it.
- B: The strips only fit the matrices they were trained on. On a different base model they make no sense; for a new model, you train again.
- C: That applies to full fine-tuning. With LoRA, the weights stay frozen; only the strips in the adapter have changed.

**Card 1:** Correct: B, 40,960 One strip has 4,096 × 8 numbers, the other 8 × 1,024: together 8 × (4,096 + 1,024) = 40,960. The whole matrix would have 4,194,304. 32,768 is only the tall strip.

**Card 2:** According to the ZeRO paper, full fine-tuning needs 16 bytes per parameter (weight, gradient, optimizer), about 128 GB for eight billion. With QLoRA, the weights stay frozen and in 4 bits, about 5 GB; the 16 bytes apply only to the narrow LoRA strips. What comes out is an adapter of a few megabytes.
