Mask the intended assistant prediction targets
Separate attention visibility from target supervision and normalize loss over the explicitly selected tokens.
On this page
The question for today
What you’ll understand
- Define an assistant-target mask after causal label alignment.
- Distinguish masked loss from hidden context.
- Calculate token-mean and example-mean objectives with different target counts.
Assistant-only supervised loss trains the model to produce the desired responses while still allowing it to attend to the prompt. A loss mask does not remove user tokens from context. The details matter: causal label shifting, role boundaries, padding, end-of-turn targets, and the normalization denominator can all change what is optimized. Inspect selected prediction targets directly rather than trusting a configuration name or a decoded conversation alone.
Visibility and supervision are separate
For a causal language model, a target token is predicted from preceding visible context. User and system tokens can remain in the attention context while their own prediction targets contribute no supervised loss. Padding commonly contributes neither useful context nor targets under the collator's defined behavior. Keep attention masking and label masking separate in the data structure. Masking an input position without considering the model's causal shift can accidentally supervise the wrong boundary token.
Define the mask over actual prediction targets after the trainer's alignment convention. Do not blindly search for the printed word assistant in text: it can appear inside user content, and control markers vary across templates. TRL 0.24.0 supports assistant-only loss when the conversational template provides assistant-token-mask information through its documented generation regions. That is a version-specific capability with template requirements, not a guarantee that every tokenizer or custom template yields a correct mask automatically.
Boundary policy and normalization change the task
Decide whether the assistant's end-of-turn token is supervised. Including it can teach the intended stopping behavior; excluding it defines a different objective and may be intentional in a specialized setup. Role-prefix tokens can also be inside or outside the supervised region depending on the exact template. Document the chosen target policy and test it. Do not assume a single universal EOS rule across model families or let padding share an ID in a way that silently masks real stopping targets.
A global mean over all selected tokens gives longer responses more weight than an equal mean of per-example means. Gradient accumulation and distributed averaging can introduce further denominator differences when microbatches contain unequal target counts. If the intended objective is a global token mean, sum compatible loss numerators and selected-token denominators under the trainer's supported reduction rather than averaging arbitrary local means. Zero-target examples need rejection or explicit handling so division by zero is not hidden.
Verify the effective denominator
Print or inspect a small batch with token IDs, labels, and masks at the exact trainer boundary. Check every assistant turn, user/system spans, padding, and terminal tokens. Run a tiny manually computed loss case before a long training job. During training, record selected target tokens and loss by task or response length so a lower aggregate loss cannot merely reflect a changed mixture or denominator. A fully masked answer may appear in context without teaching the intended response.
The function lab is a limited normalization contrast. Reset and set both a=1, x=1, predict y=1 and slope 2, then inspect. Reset and re-set a=0.5, x=1 to predict y=0.5. This exact quadratic changes a coefficient, not a language-model loss mask. It has no token probabilities, causal shift, or batches. Use the comparison to explain that scaling an objective changes optimization magnitude, while correct supervision also requires selecting the intended target positions rather than merely shrinking every term.
Work it through
Two responses, two different means
After correct masking and causal alignment, response A has two target tokens each with loss 2 nats. Response B has eight targets each with loss 0.5 nats. Assume no auxiliary losses.
A contributes summed loss 4 over two targets. B also contributes 4, but over eight targets. The total selected target count is 10.
The global token mean is(4+4)/(2+8)=0.8 nats/token. The equal-example mean is(2+0.5)/2=1.25.
Neither arithmetic is universally the right objective: they weight examples differently. Choose and record the intended normalization, including terminal-token inclusion and distributed/microbatch aggregation, rather than interpreting their values as interchangeable.
The same selected tokens can yield different aggregate objectives when normalization weights examples or tokens differently.
The living notebook
Explore the model
Interactive lab · function
Scaling differs from selecting targets
Reset, set a=1, x=1, predict y=1. Reset and re-set a=0.5, x=1, predict y=0.5. This fixed quadratic has no token masking, causal alignment, or language-model likelihood.
- y at chosen x
- 0.608
- Tangent slope · 2ax
- 1.35
- Signed area · ax³/3
- 0.182
This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forLoss scale, denominator, and target selection are distinct parts of the training objective.
Pause & reason
Check your understanding
Your turn
Try it yourself
A batch has twelve selected assistant-content targets and two supervised assistant end-of-turn targets. Their total negative log-likelihood is 21 nats. Compute the declared global mean and explain what changes if the terminal tokens are excluded.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
The supplied numerator includes terminal-token losses, so excluding them requires changing both terms consistently.
Compare with the solution
With all fourteen selected targets, mean loss is 21/14=1.5 nats/token. Excluding the terminal targets requires subtracting their losses from 21 as well as reducing the count to twelve. Without those individual losses the new mean cannot be computed. Dividing the old numerator by twelve would mix inconsistent masking and normalization.
Keep the idea
Take it with you
- Attention visibility and supervised targets are different masks.
- Causal alignment and end-of-turn policy must be inspected explicitly.
- Token-mean and equal-example objectives have different weighting semantics.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.