TL;DR
Instruction tuning is further training of a pre-trained language model on a collection of examples, each made of an instruction and a good response to it. A base model learns to continue text, so it may answer a question with another question. After instruction tuning, it learns that the instruction is the thing to respond to. The method is ordinary supervised Fine-Tuning, but the data is chosen to teach the model to act as an assistant.
Where you'll see it: the step that turns a raw base model into a chat assistant, and the step that teaches models to follow formats, languages and task descriptions they were not trained on directly.
Intuition
Continuing text is not answering
A pre-trained language model has read a large amount of text, and it is good at one thing: predicting what comes next. Give it the start of a web page and it writes a plausible rest of the page. Give it "What is the capital of France?" and it might continue with a quiz, a list of similar questions, or a forum reply that mocks the question. All of these are plausible continuations of text that looks like the prompt.
What the user wants is different. They want the model to treat the prompt as a request and produce the answer. That is a change of behaviour, not a change of knowledge. The model already knows the capital of France. It has not learned that when a request appears, the right next text is the response.
Instruction tuning teaches that. The training data is a set of examples such as "Instruction: list three primary colours. Response: red, blue and yellow." The model sees many instructions and well-formed responses, and it adjusts its weights to make those responses more likely. After enough examples, it generalises: it follows instructions of kinds that were not in the data.
flowchart LR
PT[Pre-trained base model] --> SFT[Supervised training on instruction and response pairs]
DATA[Instruction dataset] --> SFT
SFT --> IT[Instruction-tuned model]
IT --> RL[Optional preference training]
RL --> CHAT[Assistant behaviour]
The diagram shows the usual order. Instruction tuning is the supervised step. Preference training, covered in Reinforcement Learning from Human Feedback (RLHF), is a later step that refines which of several good responses is preferred.
The math
Let be the weights of a language model that gives a probability to each next token. An instruction-tuning example is a pair of an instruction and a response . The model is trained to maximise the log probability of the response given the instruction. Written as a loss to minimise,
where is the instruction dataset. Each term inside the sum is the cross-entropy of one response token. The average over puts short and long responses on a similar scale, though many implementations sum instead of averaging, which changes the effective learning rate.
The one design choice that matters most is which tokens the loss covers. The instruction is conditioning, not a target. In a typical implementation, the loss is applied only to response tokens, so the model is not pushed to reproduce the user's wording. Written with a mask that is for response tokens and for instruction tokens, the loss becomes
where is the full sequence formed by concatenating instruction and response. The model still reads the whole sequence; only the target positions change.
Instruction tuning uses the same autoregressive objective as pre-training. What changes is the data distribution and the masking. That is why it is cheap compared with pre-training: the dataset is much smaller, and the model starts from weights that already contain most of the knowledge.
Worked examples
Take the instruction "Translate to French: cat" and the response "chat". Suppose the tokenizer splits the response into two tokens, "ch" and "at". The instruction tokens are masked out. The model gives these probabilities to the two response tokens. These probabilities are illustrative:
- Token 1 "ch": . Its loss term is .
- Token 2 "at": . Its loss term is .
- Sum of the two terms: .
- Average over the response tokens: .
- Gradient descent moves the weights so that and rise. After the update, the model is more likely to answer "chat" to this instruction and to similar translation requests.
If the instruction tokens were not masked, the loss would also include the probabilities of "Translate", "to", "French" and "cat" given their prefixes. Those terms would train the model to reproduce the instruction, which wastes capacity on text the user already supplied.
Consider the prompt "List two uses of a paperclip." These probabilities are illustrative, not measured, and they show how the next-token distribution moves.
- Base model: after the prompt, it puts most of its probability on continuations such as "\n\nList two uses of a stapler." because web pages often list related items in a row. The probability of the token "1" that starts an answer is low, say .
- After instruction tuning on examples of list requests with direct answers, the probability of "1" rises to , and the probability of a newline followed by another request falls.
- The chance that the first token of the response is a number therefore rises from to , a factor of .
- The model's knowledge of paperclips did not change. Only the choice of continuation did.
This is the behaviour change that instruction tuning aims for. It is also why the same underlying weights can show very different behaviour before and after tuning.
- A pre-training corpus may contain trillions of tokens. An instruction dataset may contain tens of thousands of examples, each a few hundred tokens long.
- Suppose a dataset has 50,000 examples of 300 tokens each. That is tokens, or 15 million.
- A pre-training run on one trillion tokens processes roughly times as much text.
- Instruction tuning is therefore a small fraction of the compute of pre-training, and it can be run on a cluster that could not pre-train the model.
The exact numbers vary, but the ratio is the reason instruction tuning is practical for many groups who could not train a base model.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| The dataset is narrow, such as only one task type | The model follows that task well and others poorly | Mix many task types in the instruction data |
| The data was produced by a much larger model | The tuned model copies the teacher's style, errors and blind spots | Check a sample of responses against ground truth |
| The model must refuse some requests | Instruction tuning alone may not teach refusals or preference ordering | Add preference training, as in [[reinforcement-learning-from-human-feedback |
| You need a fixed format, such as JSON | The model follows the format most of the time but not always | Include many format examples, or constrain decoding at inference |
| The model is trained on the same prompts used for evaluation | Scores rise without any real gain in following new instructions | Hold out whole task families for evaluation |
| The responses are long and low quality | The model learns to pad answers | Filter or rank responses before training |
Failure modes seen in practice: the model becomes confident about instructions it cannot follow, because the training data rarely shows "I cannot do that"; it copies the surface style of the training responses, such as length and bullet lists, even when a shorter answer would serve better; and it can lose some of its general knowledge if the tuning is too long or the learning rate is too high. The InstructGPT paper also notes that its tuned models still make simple mistakes, so instruction tuning changes behaviour without making the model reliable.
History
Before 2020, the usual way to get a model to do a task was to fine-tune it on that task. In 2020 the GPT-3 paper showed that a large enough model could do many tasks from a prompt alone, with a few examples in the context (see In-Context Learning). But larger models did not automatically follow instructions well, and they often produced text that was untruthful or unhelpful.
In 2021 Wei and colleagues trained a model on a large collection of tasks, each written as a natural-language instruction, and showed that the tuned model could do new tasks in a zero-shot setting. The paper, usually called FLAN, made instruction tuning a named method and used the term directly. It also showed that adding more task types improved performance on tasks the model had not seen.
In 2022 the InstructGPT paper combined supervised instruction tuning with reinforcement learning from human feedback. The abstract reports that outputs from the 1.3 billion parameter InstructGPT model were preferred to outputs from the 175 billion parameter GPT-3 in human evaluations, despite having 100 times fewer parameters. That result made instruction tuning and preference training the standard recipe for assistant models.
Check yourself
QuestionRecall: what is the difference between a base language model and an instruction-tuned model, in one sentence?Show answer
A base model is trained to predict the next token of text in general, so it continues a prompt in whatever way fits the text; an instruction-tuned model has been trained on instruction and response pairs so that it treats the prompt as a request and answers it.
QuestionComputation: a response has three tokens with probabilities , and under the model. Using the average-token loss, what is the loss for this example to three decimal places?Show answer
The terms are , and . Their sum is . Dividing by gives .
QuestionWhy is the loss usually masked so that instruction tokens do not contribute, and what could go wrong without the mask?Show answer
The instruction is given to the model as context, not as something to reproduce. Without the mask, the loss would also train the model to predict the user's own words, which spends capacity on text that is supplied at inference. It can also teach the model to repeat instructions in its answers. The model still reads the instruction; only the loss excludes it.
QuestionWhy can a model's knowledge stay the same while its behaviour on the same prompt changes a lot after instruction tuning?Show answer
Instruction tuning changes which continuations the model prefers after a prompt of a given shape. The knowledge stored in the weights comes mostly from pre-training and is little changed by a small supervised run. What changes is the distribution over the first tokens of the response, so the model answers the request instead of continuing a web-like pattern.
QuestionApplied: a team has a strong base model and a budget for one week of GPU time. They want a support assistant that answers questions about their product. What would you do, and what would you check?Show answer
Build a few thousand instruction and response pairs from real support tickets, with answers checked by someone who knows the product. Instruction-tune the base model on those pairs with the response-only loss, and hold out a set of tickets to evaluate. Check that the assistant refuses or escalates questions outside the product's scope, since the training data may not show that behaviour on its own. Preference training can follow later if the answers need ranking.