Search

Search papers and lessons

Instruction Tuning

Level 3 · 10 min read

Instruction tuning fine-tunes a pre-trained language model on pairs of instructions and good responses, so it learns to follow what a user asks instead of just continuing the text.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Linear algebra
  • Neural network basics
  • Post-training
  • Discrete math
  • Sign in to see your progress

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 θ\theta be the weights of a language model that gives a probability pθ(yk∣y1:k−1)p_\theta(y_k \mid y_{1:k-1}) to each next token. An instruction-tuning example is a pair of an instruction xx and a response y=(y1,…,yT)y = (y_1, \dots, y_T). The model is trained to maximise the log probability of the response given the instruction. Written as a loss to minimise,

L(θ)=− E(x,y)∼D[1T∑k=1Tlog⁡pθ(yk∣x,y1:k−1)]\mathcal{L}(\theta) = -\,\mathbb{E}_{(x, y) \sim \mathcal{D}} \left[ \frac{1}{T} \sum_{k=1}^{T} \log p_\theta\left(y_k \mid x, y_{1:k-1}\right) \right]

where D\mathcal{D} is the instruction dataset. Each term inside the sum is the cross-entropy of one response token. The average over TT 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 xx 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 mkm_k that is 11 for response tokens and 00 for instruction tokens, the loss becomes

L(θ)=− E[∑kmklog⁡pθ(sk∣s1:k−1)∑kmk]\mathcal{L}(\theta) = -\,\mathbb{E} \left[ \frac{\sum_{k} m_k \log p_\theta(s_k \mid s_{1:k-1})}{\sum_{k} m_k} \right]

where ss 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

Example
Loss on one instruction-response pair

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:

  1. Token 1 "ch": p1=0.50p_1 = 0.50. Its loss term is −ln⁡(0.50)=0.6931-\ln(0.50) = 0.6931.
  2. Token 2 "at": p2=0.80p_2 = 0.80. Its loss term is −ln⁡(0.80)=0.2231-\ln(0.80) = 0.2231.
  3. Sum of the two terms: 0.6931+0.2231=0.91630.6931 + 0.2231 = 0.9163.
  4. Average over the T=2T = 2 response tokens: 0.9163/2=0.45810.9163 / 2 = 0.4581.
  5. Gradient descent moves the weights so that p1p_1 and p2p_2 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.

Example
Why the same model answers differently after tuning (illustrative)

Consider the prompt "List two uses of a paperclip." These probabilities are illustrative, not measured, and they show how the next-token distribution moves.

  1. 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 0.050.05.
  2. After instruction tuning on examples of list requests with direct answers, the probability of "1" rises to 0.600.60, and the probability of a newline followed by another request falls.
  3. The chance that the first token of the response is a number therefore rises from 0.050.05 to 0.600.60, a factor of 1212.
  4. 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.

Example
Data size and cost (a rough comparison)
  1. 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.
  2. Suppose a dataset has 50,000 examples of 300 tokens each. That is 50,000×300=15,000,00050{,}000 \times 300 = 15{,}000{,}000 tokens, or 15 million.
  3. A pre-training run on one trillion tokens processes roughly 1012/(1.5×107)≈67,00010^{12} / (1.5 \times 10^{7}) \approx 67{,}000 times as much text.
  4. 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

SituationProblemBetter choice
The dataset is narrow, such as only one task typeThe model follows that task well and others poorlyMix many task types in the instruction data
The data was produced by a much larger modelThe tuned model copies the teacher's style, errors and blind spotsCheck a sample of responses against ground truth
The model must refuse some requestsInstruction tuning alone may not teach refusals or preference orderingAdd preference training, as in [[reinforcement-learning-from-human-feedback
You need a fixed format, such as JSONThe model follows the format most of the time but not alwaysInclude many format examples, or constrain decoding at inference
The model is trained on the same prompts used for evaluationScores rise without any real gain in following new instructionsHold out whole task families for evaluation
The responses are long and low qualityThe model learns to pad answersFilter 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 0.50.5, 0.250.25 and 0.80.8 under the model. Using the average-token loss, what is the loss for this example to three decimal places?Show answer

The terms are −ln⁡(0.5)=0.693-\ln(0.5) = 0.693, −ln⁡(0.25)=1.386-\ln(0.25) = 1.386 and −ln⁡(0.8)=0.223-\ln(0.8) = 0.223. Their sum is 2.3032.303. Dividing by T=3T = 3 gives 0.7680.768.

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.

Sources