Supervised Fine-Tuning
A model that can continue text may not respond in the way a task requires. Fine-tuning on examples of prompts and desired responses gives it demonstrations of the behaviour we want.
10 sections · 10 questions · 4 coding challenges
The arrow keys move between sections too.
Why Does a Text Predictor Need Examples of Following Instructions?
BOTIE
The 2022 InstructGPT work used demonstrations written by labelers to fine-tune a pretrained model before later preference-based training. That supervised stage gave explicit examples of the response behaviour the system was intended to produce.
Supervised fine-tuning continues training with input-output pairs chosen for a task. The desired response supplies the target tokens. We are adapting an existing model's behaviour rather than learning language entirely from scratch. The demonstrations therefore matter: their coverage, correctness, and style influence what the adapted model learns, and performance still needs checking on unfamiliar requests.
How Do We Teach a Text Predictor to Respond the Way We Need?
A model trained to continue text may continue a question with another question, a quoted conversation, or an answer in an unsuitable format. We want to show it what responding to our kind of request should look like.
Supervised fine-tuning starts with an already trained model and examples of desired behavior. Each example contains a prompt and a target response. We train on those pairs so the model becomes more likely to produce the demonstrated kind of response.
Decide what behavior the examples should teach
Suppose a model should complete a short prefix and stop at an explicit end token. A training pair supplies both that prompt and the desired completion. For an assistant, the prompt might instead contain an instruction and the target might be a carefully written answer.
Prompt
- <start>
- the
- cat
Desired response
- sat
- <end>
The loss mask and the attention mask solve different problems
The worked example makes that distinction visible through per-token losses and derivatives. It isolates the objective rather than training a complete assistant.
After understanding full-parameter fine-tuning, we can ask how LoRA reduces the number of parameters that need updates.
Explore the full example
Compare response-only scoring with scoring all targets. The prompt stays available in both cases. Inspect which target derivatives become zero when prompt predictions are excluded from the loss.
Calculating the example…
Part 1. Define the Behavior Before Training
1. Start with a pretrained model and target responses
A pretrained language model has learned one thing, which is to continue text. Nothing in that training says a question should be followed by its answer, or that an answer should stop when it is complete. A question followed by another question is also a plausible continuation. What the model is missing is a demonstration of the continuation we want.
Supervised fine-tuning continues training from existing parameters using examples selected for a task or behavior. A prompt-response pair might demonstrate answering a question, extracting fields, summarizing a passage, or following a required output format.
The target response is labeled data: it specifies what the model should be encouraged to produce. More examples are not automatically better if their responses conflict, contain errors, or fail to represent the intended use.
This page follows one pair all the way through. The prompt is a start token and two words, and the response is one word and an end token.
Prompt: <start> the cat
Response: sat <end>Small as it is, the pair demonstrates two behaviors. The first is which word should follow the prompt. The second is that the response then stops. The end token is part of the target, so a model that produces sat and carries on has not reproduced the demonstrated response.
That is why the behavior has to be defined before training and not discovered afterward. Training pulls the model toward whatever the responses contain, and it cannot tell an intended pattern from an accidental one. If some responses end with the end token and others trail off, both are being taught. If two responses answer the same kind of prompt in different formats, the model is pulled toward each in turn. Deciding what a good response looks like, including its format and where it stops, is the part of fine-tuning that happens before any gradient is computed. The collection of pairs is where that decision is written down.
2. Keep the prompt visible while scoring the response
The prompt needs to remain available because it tells the model what response is appropriate. The causal attention mask prevents a response position from reading future response tokens. A separate loss mask decides which next-token predictions contribute to the training objective.
Response-only loss excludes prompt-token predictions while including the assistant response targets. Some training setups score additional positions; this is an explicit objective choice rather than a universal requirement. The checkbox exposes that choice.
The two masks are easy to confuse, because both are rows of zeros and ones laid over the same sequence. They answer different questions.
| Mask | The question it answers | In the pair above |
|---|---|---|
| Causal attention mask | What may this position read? | itself and everything before it |
| Loss mask | Is this prediction graded? | only the predictions of sat and <end> |
Neither mask does the other’s job. Changing the loss mask leaves what every position reads exactly as it was.
Why leave the prompt predictions ungraded? In use, the prompt is written by someone else and handed to the model. The model is never asked to produce it. Grading the predictions of the two prompt words, the and cat, would spend part of every update on teaching the model to guess the wording of prompts, which is not the behavior being defined. The response predictions are the ones the model will have to make by itself, so those are the ones this objective grades.
The prompt still does its work either way. The prediction of sat is made by a position that has read the whole prompt, and the prediction of the end token is made by a position that has read the prompt and sat. Removing the prompt would leave those predictions with nothing to respond to.
Part 2. Calculate Response-Only Loss
3. Align the mask with targets, not input positions
The toy prompt ends at cat, and the target response is sat followed by an end token. Remember that a next-token target is shifted one position beyond its input. The output at the cat position predicts the first response token.
Input positions: <start> the cat sat
Target positions: the cat sat <end>
Loss mask: 0 0 1 1A prompt token can therefore appear at an input position whose output is scored. Masking every output merely because its input token belongs to the prompt would wrongly exclude the first response prediction.
Laying the four predictions out one per row makes the alignment easier to see. Each row is one position. It reads the prefix on the left and is asked for the target beside it.
| Prefix the position has read | Target it predicts | The target belongs to | Scored |
|---|---|---|---|
| <start> | the | the prompt | no |
| <start> the | cat | the prompt | no |
| <start> the cat | sat | the response | yes |
| <start> the cat sat | <end> | the response | yes |
The four next-token predictions of the toy pair. The mask follows the third column.
The row that predicts sat is the one that goes wrong. Its input token, cat, is a prompt token, and its target, sat, is a response token. A mask built from the input row would mark that position as prompt and switch it off.
Mask by input token (wrong): 0 0 0 1
Mask by target token (right): 0 0 1 1The wrong mask keeps only the prediction of the end token. The prediction it throws away is the decision about how the response begins, which is the first thing a fine-tuned model has to get right.
4. Average over the included targets
Let m indicate whether a target is scored, and p give the probability assigned to that target. Divide by the number of included positions, not the full sequence length.
L = −Σₜ [mₜ log pₜ(yₜ)] / Σₜ mₜ
For this response-only mask:
L = [−log p(sat | <start> the cat)
−log p(<end> | <start> the cat sat)] / 2The live example fills in the numbers. Its vocabulary has seven tokens. At every position it assigns the target a logit of 1 and each of the other six tokens a logit of zero, which is where its slider starts. Softmax turns those seven logits into probabilities, and the token loss is the negative logarithm of the target’s share.
p(target) = e¹ / (e¹ + 6) ≈ 0.312
Token loss = −log(0.312) ≈ 1.165
Response-only mean = (1.165 + 1.165) / 2 ≈ 1.165
All-targets mean = (1.165 + 1.165 + 1.165 + 1.165) / 4 ≈ 1.165The two means agree, and that is a property of this example and not of the mask. Every target was assigned the same score, so every token loss is the same and any mean of them is 1.165. The denominator still matters. Dividing the two included losses by the sequence length of four would report a different number.
(1.165 + 1.165) / 4 ≈ 0.583That is half the true mean, and it would shrink again with every token added to the prompt, although the response had not changed. Dividing by the count of included targets keeps the loss a statement about the response alone.
Where the mask does show is in the derivatives, which are what training acts on. The derivative of the mean loss with respect to a scored target’s own logit is its probability minus one, divided by the number of included targets. For an excluded target it is zero.
Response-only: (0.312 − 1) / 2 ≈ −0.344
All targets: (0.312 − 1) / 4 ≈ −0.172| Target | Token loss | Derivative, response only | Derivative, all targets |
|---|---|---|---|
| the | 1.165 | 0 | −0.172 |
| cat | 1.165 | 0 | −0.172 |
| sat | 1.165 | −0.344 | −0.172 |
| <end> | 1.165 | −0.344 | −0.172 |
The derivative of the mean loss with respect to each target’s own logit, at an assigned target logit of 1. The two derivative columns are the two settings of the live example’s checkbox.
A negative derivative says that raising the logit lowers the loss, so descent raises it. Under response-only scoring the two response targets are each pulled twice as hard as under all-targets scoring, because the same mean is now shared between two positions and not four. The two prompt targets are not pulled at all. The whole update goes toward the response.
The derivative with respect to an excluded position’s output logits is zero. This does not mean prompt embeddings or earlier hidden states receive no gradient: scored response positions can depend on them through attention and other shared computation.
The example uses assigned logits and calculates the objective only. Updating a real model requires backpropagating this loss to the selected parameters, followed by optimizer steps.
Check yourself
Questions on Parts 1 and 2
Question 1 of 5
Under response-only loss the prompt still enters the model, and the position that predicts sat has read all of it.
Part 3. Adapt Without Confusing the Different Routes
5. Reproduce the target mask in NumPy
The SDK accepts one boolean per target row and rejects an all-excluded batch because its mean would have no denominator.
import numpy as np
from oop_ml.numpy.modern import TokenCrossEntropy
logits = np.zeros((4, 7))
targets = np.array([1, 2, 4, 6])
scored = np.array([False, False, True, True])
result = TokenCrossEntropy().measure(logits, targets, scored)
print(result.value)
print(result.gradient)All 28 logits in that snippet are zero, so every position spreads its probability evenly over the seven tokens. Both things it prints can be worked out before running it.
p(target) = 1 / 7 ≈ 0.143
Loss = −log(1 / 7) = log 7 ≈ 1.946
Derivative at a scored target = ((1 / 7) − 1) / 2 ≈ −0.429
Derivative at each other token = (1 / 7) / 2 ≈ 0.071The printed gradient has one row for each of the four positions and one column for each of the seven tokens. The rows for the two prompt targets are all zero. In each of the two response rows the target’s entry is −0.429 and the other six entries are 0.071, so the row sums to zero. Read as an instruction to an optimizer, a response row says to raise the target’s logit and lower the other six by the same total amount. A prompt row says nothing.
Real training also needs an unambiguous chat or completion format, boundary tokens, padding handling, held-out examples, and an optimizer. These define the concrete learning task around the objective.
6. Separate fine-tuning, prompting, and retrieval
Fine-tuning changes parameters. Prompting supplies instructions or demonstrations as input for the current response. Retrieval supplies selected source passages as additional input. These approaches can be combined, but they modify different parts of the system.
| Route | What it changes | How long the change lasts |
|---|---|---|
| Fine-tuning | the model’s parameters | every later request |
| Prompting | the input, by adding instructions or demonstrations | the current response |
| Retrieval | the input, by adding selected source passages | the current response |
Three ways to change what a model produces. Only fine-tuning leaves the model itself different afterward.
The toy behavior shows how the routes differ. A prompt could ask for one more word and then a stop, and it would have to ask again on every request. Fine-tuning trains on pairs like the one above until the model does that without being asked, which costs a training run and removes the instruction from every later prompt. Retrieval does not fit this behavior at all, because a habit of formatting is not a fact that some source passage states. It suits answers that live in documents.
Supervised fine-tuning imitates demonstrated target responses. Preference training uses comparisons or feedback about outputs and introduces a different training objective. A few demonstrations of good behavior do not guarantee that behavior on unfamiliar inputs.
Imitation has a blind spot, and the loss in Part 2 shows where it is. Every term asks how much probability the model gave the one demonstrated token. A different response that would have served the reader equally well earns no credit, because it is not the response in the pair. Preference training exists for the cases where it is easier to say which of two outputs is better than to write the single best one.
Evaluate both the target task and capabilities the original model should retain. Repeated training on a narrow dataset can overfit or weaken other behavior. Full-parameter updates are one option; the next lesson describes a smaller trainable update.
Keep in mind
Nothing on this page was trained. The example assigns logits and computes the objective and its derivatives, which is the part of fine-tuning that can be checked by hand. A real model’s logits all come from shared parameters, so pulling on the response targets also moves everything else those parameters produce. That is the reason to evaluate capabilities the examples never mention.
The InstructGPT paper uses supervised demonstrations as one training stage before preference-based stages. This lesson isolates supervised fine-tuning. Training Language Models to Follow Instructions with Human Feedback.
Continue with Low-Rank Adaptation.
Check yourself
Questions on Part 3
Question 1 of 5
Why does the SDK reject a batch in which every target row is excluded?
Try it with Botie
Build with Botie
Write and run Python