What it is
A training record holds far more than the reply to be learned: a system prompt, perhaps several earlier turns, and the user's question. Loss masking means the training error — the loss — is computed only on the tokens of the target reply. The model reads every token, and the context shapes every prediction it makes, but it is never scored on predicting the context itself.
Howmasked␣domasked␣Imasked␣resetmasked␣mymasked␣passwordmasked?maskedOpenp 0.62loss 0.48␣Settingsp 0.81loss 0.21␣→p 0.35loss 1.05␣Securityp 0.90loss 0.11How it works
A language model is trained by predicting each next token. At every position it outputs a probability for every token in its vocabulary, and the loss at that position is the negative logarithm of the probability it gave to the correct token: near 0 when it was confident and right, large when it was confident and wrong. The record's loss is the average over the positions that count.
loss for one record = average over target tokens of −ln p(correct token | everything before it)
context tokens: read, label = -100 → ignored by the loss
target tokens: read and scored → averaged into the lossWithout masking, the average would run over every token in the record. With masking, context positions are left out. Trainers do this by giving each context token a special label — -100 in PyTorch-based libraries such as Hugging Face Transformers — that the loss function is told to ignore.
- A record has 40 context tokens (system prompt and question) and 4 target tokens: “Open”, “ Settings”, “ →”, “ Security”.
- The model gives the correct target tokens probabilities of 0.62, 0.81, 0.35 and 0.90. Their losses, −ln p, are 0.48, 0.21, 1.05 and 0.11.
- The record's loss is their average: (0.48 + 0.21 + 1.05 + 0.11) ÷ 4 ≈ 0.46.
- The 40 context tokens add nothing. The update will mostly raise the probability of “ →”, the token the model was least sure of.
Why mask at all
- The signal goes where you want it. Your targets are the behaviour you chose. Without masking, much of the training effort would go into predicting users' questions and repeating the system prompt.
- The model does not learn to imitate users. Training on user turns teaches the model to write like the people asking — typos and all — which can leak into its answers.
- Long context does not drown short answers. In a record with 2,000 tokens of history and a 60-token reply, an unmasked loss would be about 97% about the history.
Is masking always better?
Masking is the standard choice and the default in most training services and libraries. There are exceptions: a 2024 study by Shi and colleagues found that also training on the instructions can help when instructions are long and outputs short, or when there are very few examples. For fine-tuning on real conversations, where user turns are not the behaviour you want to reproduce, masking the context remains the sensible default.
Masked does not mean irrelevant
A masked token is not graded, but it is still read, and the model learns to produce the target given that context. If the context contains a wrong statement that the target goes along with, the model learns to go along with it. If the system prompt in training differs from the one used later, the learned behaviour may not carry over. Masking narrows what is scored; it does not make the rest of the record unimportant.
Common mistakes
- Assuming that because context is not scored, its quality does not matter.
- Reading a low training loss as proof of quality when the targets are short and easy.
- Comparing loss values between datasets with very different target lengths.
- Expecting the model to learn something that only ever appears in user messages.
References and further reading
External sources open in a new tab.
- 01Instruction Tuning With Loss Over InstructionsShi et al., 2024 · arXiv · arxiv.orgWhen training on the prompt as well as the answer helps.
- 02SFT TrainerHugging Face TRL docs · huggingface.coThe completion-only and assistant-only loss options in practice.
- 03Causal language modelingHugging Face Transformers docs · huggingface.coNext-token prediction and how labels are prepared for it.
- 04Supervised Fine-TuningHugging Face LLM Course · huggingface.coFine-tuning on chat data, step by step.