Fine-tuning
A pretrained model, also called a base model, is a model that has been trained on a large general dataset. Fine-tuning is training a pretrained model further on a smaller dataset, for one task or for answering in a conversation.
For example, we can fine-tune a language model pretrained on trillions of tokens of web text on 1,000 support tickets and their answers. After this, the model answers new tickets in the same format. A small dataset is enough, because it only has to teach what differs between the tasks.
In practice, fine-tuning is usually used to teach a format, a tone or a narrow task. Facts that change often are instead given in the prompt, the text that the model reads before it answers, because each change would need a new round of training.
11.1Training from pretrained parameters
When we fine-tune a model, we use the steps of gradient descent (see Chapter 2), but we start from the pretrained parameters instead of random values.
As an example, consider a model that predicts from two inputs and , with the pretrained parameters and . The new task has one example: the input , with the answer 4. The loss is , and with a learning rate of 0.25, one step adds to both and . The following table shows this step from the pretrained parameters and from zero:
| from pretrained | from zero | |
|---|---|---|
| before | 2, 1 | 0, 0 |
| after | 2.5, 1.5 | 2, 2 |
| after | 4 | 4 |
| after | 1 | 0 |
Both models now give the answer 4. They differ in , which the example says nothing about. Since the step adds the same amount to and , the value of does not change. It remains 1 in the fine-tuned model, and it is 0 in the model trained from zero, a value that no data has taught.
The following picture shows the two steps in the plane of the parameters:
Each step moves the parameters in the direction (1, 1), which is perpendicular to the line . Thus, each model stops at the point of the line that is nearest to its start.
The step also changes the answer for the input , from 2 to 2.5. The new example does not contain this input. If an old task needs the answer 2 for this input, the fine-tuned model now fails it.
Catastrophic forgetting is this loss of an old skill. A small learning rate and few steps make it smaller, but they also make the model learn the new task less.
11.2Instruction tuning
A pretrained model continues a text (see Chapter 9). For example, when the text is a question, a pretrained model can continue it with more questions instead of an answer1.
Instruction tuning is fine-tuning on many requests with good answers, so that the model answers a request instead of continuing it. Each example is usually stored in a chat format, where, in most current models, special tokens mark the start and the end of each turn. For example, a support conversation can be stored as the following text:
<|system|>You are a support assistant.<|end|>
<|user|>How do I change my password?<|end|>
<|assistant|>Open Settings and choose Security.<|end|>
Each special token is one entry of the vocabulary, the list of all tokens that the model knows (see Chapter 5). Some tokenizers, the programs that turn text into tokens, also make it from the same text typed by a user, so the program that builds the prompt has to check user text for these names. The names differ between models, and the above names are only an example.
The training usually counts the loss (see Section 1.3) only on the tokens of the assistant turns2. The model learns to write answers, and it also learns to write <|end|> when an answer is complete. The program that runs the model asks it for one token at a time (see Chapter 7), and it stops when the model writes this token.
The system turn holds the instructions of the people who build the product (see Chapter 18).
Instruction tuning needs far fewer examples than the pretraining. For example, the first step of training a model of 2022 used about 13,000 requests with answers that people wrote, and a later step trained the model from scores of its answers instead (see Chapter 12)3. One view is that the examples mostly teach the form of an answer4. It turns out that fine-tuning is a poor way to add new facts, because a model learns examples with new facts more slowly than the others5.
11.3Low-rank adaptation
A straightforward way to fine-tune a large model is to train all its parameters. However, training with Adam (see Section 2.4) stores a gradient and two running averages for each parameter. Thus, training needs at least four numbers per parameter instead of one. Each task also needs its own copy of the model.
A language model keeps most of its parameters in weight matrices, which multiply the vectors that the model works on (see Chapter 3). Low-rank adaptation (LoRA)6 is a technique that keeps these matrices fixed and trains an update for some of them. For a matrix of size , where is the length of these vectors, has size , has size and is small. For example, when and , the 8 parameters in and give the 16 values of the update:
The rank of a matrix is the largest number of its columns of which none is a combination of the others. For example, each column of the above update is a multiple of , so its rank is 1. In general, has rank at most , because each column of is a combination of the columns of . The example of Section 11.1 is similar: the new data moved the parameters in one direction only.
In a real model with and , and have 2 · 4096 · 8 = 65,536 parameters instead of the 4096 · 4096 = 16,777,216 of , which is 256 times fewer.
Initially, is zero, so the model is exactly the pretrained one. Only and need a gradient and two running averages, and each task stores only its own and . After training, we can also add to , and the merged model runs as fast as before.
It turns out that or less is often enough for a format or a narrow task, but full fine-tuning is much better for training on a large amount of code or math7.
The paper on InstructGPT shows such a continuation in its Figure 9: Training language models to follow instructions with human feedback.↩︎
The Llama 2 paper describes this choice: Llama 2: Open Foundation and Fine-Tuned Chat Models. Some training libraries count every token unless they are told otherwise.↩︎
The model was described in 2022: Training language models to follow instructions with human feedback. Its supervised fine-tuning used about 13,000 training prompts.↩︎
The view was stated and tested in 2023 with only 1,000 requests and answers: LIMA: Less Is More for Alignment.↩︎
The result was published in 2024: Does Fine-Tuning LLMs on New Knowledge Encourage Hallucinations?.↩︎
The method was published in 2021: LoRA: Low-Rank Adaptation of Large Language Models.↩︎
The comparison was published in 2024: LoRA Learns Less and Forgets Less.↩︎