Which loss function does supervised fine-tuning actually minimize?
Supervised fine-tuning minimizes token-level cross-entropy on the response tokens, the same objective the base model was pretrained with, just on a smaller curated set.
Imagine teaching someone to copy a famous handwriting sample one letter at a time. At each letter you cover the answer, ask them to guess the next stroke, then reveal the correct one. The lesson scores how surprised they were by the actual letter: very surprised is a big penalty, perfectly expected is a small one. They keep practicing until their guesses match the handwriting closely. SFT works the same way with words. The model reads a prompt, then one word at a time tries to predict the next word in the ideal response. The loss is the surprise score, summed over every word in the response. The system and user prompts are not scored, only the answer is, because the answer is what you are teaching the model to write.
Detailed answer & concept explanation~9 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: name token level cross-entropy with masking, write the formula, explain teacher forcing, contrast with DPO and PPO losses, and call out the chat-template masking bug as the common production failure mode.
Real products, models, and research that use this idea.
- Hugging Face TRL SFTTrainer wraps PyTorch CrossEntropyLoss with an ignore-index of -100 for prompt positions, the standard recipe used to fine-tune Llama 4 and Mistral on instruction data.
- Axolotl exposes a train_on_inputs flag that toggles whether prompt tokens contribute to the loss, defaulting to false so only response cross-entropy is minimized.
- DeepSeek V4 distillation recipes report training loss as mean next-token cross-entropy averaged across the response positions of the curated student dataset.
- Unsloth tutorials walk users through inspecting the loss mask alignment to confirm the chat template did not silently grade the system prompt during a Gemma 4 SFT run.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is it important that the SFT loss exactly matches the pretraining loss objective?
QHow does loss masking on prompt tokens affect convergence and what changes if you grade prompts too?
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.
Conflating the SFT loss with the DPO pairwise loss or the PPO KL term. SFT is plain next-token cross-entropy on a single ideal response, with no pair, no reference, and no reward model.
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.