Explain SFT's loss and the role of prompt-token masking
Describe what the SFT loss computes for a single (prompt, response) training example. What does 'completion-only loss' or 'prompt masking' mean, and what failure mode does it prevent?
SFT is next-token cross-entropy over the prompt-plus-response sequence. Prompt masking sets prompt targets to -100 so only response tokens train the model.
Picture a fill-in-the-blank worksheet where the question is already printed and you must write the answer. SFT grades the model on every blank it could fill. But you do not want credit for re-copying the printed question, you already have it. So a teacher draws a line through the question part and only marks the answer you wrote. Prompt masking is that line: it tells the grader to skip the question tokens and score only the response tokens. The model still reads the full question to understand context, it just earns a grade only on the words it was supposed to generate. That way all of the learning effort goes into shaping good answers, not into memorising prompts.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: cross-entropy recap + the logits-label shift + what prompt masking does with -100 + the wasted-gradient failure mode + reduction choices + framework defaults and pitfalls.
import torch.nn.functional as F
# logits: [B, T, V]; labels: [B, T] with -100 on prompt tokens
shift_logits = logits[:, :-1, :].contiguous()
shift_labels = labels[:, 1:].contiguous()
loss = F.cross_entropy(
shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),
ignore_index=-100, # prompt tokens skipped
)| Aspect | Full-sequence loss | Completion-only loss |
|---|---|---|
| Tokens scored | Prompt plus response | Response only |
| Prompt label | True next token | -100 (ignore_index) |
| Gradient use | Partly spent on prompts | All on response behaviour |
| Small-dataset quality | Slightly worse, slower | Faster, slightly better |
| Large-dataset gap | Shrinks (response dominates) | Still default, near free |
Real products, models, and research that use this idea.
- Hugging Face TRL's SFTTrainer enables completion-only loss via DataCollatorForCompletionOnlyLM, masking prompt tokens to -100 by default.
- Axolotl exposes train_on_inputs: false to mask instruction tokens, the standard recipe used to fine-tune Llama 4 and Qwen3 chat models.
- Unsloth's notebooks for fine-tuning Llama 4 and Gemma 2 ship with response-only loss masking built from the chat template.
- PyTorch's nn.CrossEntropyLoss uses ignore_index=-100, the sentinel every SFT stack writes into prompt-token label positions.
- Meta's Llama 4 and DeepSeek V4 post-training pipelines apply completion-only SFT before preference optimisation like DPO.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does the choice between token-mean and sample-mean reduction change what SFT optimises?
QWalk through exactly how the logits-and-labels shift is implemented, and what breaks if it is off by one.
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Saying SFT uses a special loss. It is plain next-token cross-entropy, the same objective as pretraining. The only change is which tokens contribute.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.