Fine-tuning, RLHF & Alignment·Question 3 of 61Basic

What is loss masking in SFT, and why do you mask the prompt tokens?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Loss masking means computing the cross-entropy loss only over the tokens you want the model to learn to produce - the assistant response - while excluding prompt/system/user tokens from the loss. Mechanically, you set the label for masked positions to -100, which PyTorch's cross_entropy ignores:

labels = input_ids.clone()
labels[:prompt_len] = -100          # no loss on prompt tokens
# in multi-turn data: mask everything except assistant spans
Full worked answer with diagram

Loss masking means computing the cross-entropy loss only over the tokens you want the model to learn to produce - the assistant response - while excluding prompt/system/user tokens from the loss. Mechanically, you set the label for masked positions to -100, which PyTorch's cross_entropy ignores:

labels = input_ids.clone()
labels[:prompt_len] = -100          # no loss on prompt tokens
# in multi-turn data: mask everything except assistant spans

Why: without masking, gradient signal is dominated by whatever fills your prompts. The model spends capacity learning to predict user questions, document chunks, and boilerplate system prompts - none of which it will ever need to generate. Consequences of skipping it:

  • Wasted capacity and distribution shift - with long prompts and short answers (typical for RAG-style or extraction data), 90%+ of the loss can come from prompt tokens.
  • Prompt regurgitation - the model learns to echo instructions or context into its output.
  • Weaker instruction following - the response tokens, the thing you actually care about, get proportionally less gradient.

Going further: follow-up territory

Nuances a senior candidate adds: (1) In multi-turn data you mask everything except assistant turns - including earlier assistant turns is a deliberate choice (train on all turns vs. last turn only). (2) It's not always binary: for continued pretraining you don't mask at all, and some recipes train on prompts with a small weight when data is scarce. (3) When packing multiple examples into one sequence, masking must be per-example and you must prevent cross-example attention. (4) Attention masks and loss masks are different things - prompt tokens are still attended to; they just don't contribute loss.

Worth sketching. it makes the point that the attention mask and the loss mask are two different things.

flowchart LR
    A["Rendered sequence"] --> B["Prompt span"]
    A --> C["Response span + EOS"]
    B -->|"labels set to -100"| D["Excluded from loss"]
    C -->|"labels kept"| E["Cross-entropy"]
    B -->|"still visible to attention"| C
    E --> F["Gradient shapes only<br/>what the model must emit"]

What a good answer covers

  • Loss masking means computing the cross-entropy loss only over the tokens you want the model to learn to…
  • Why: without masking, gradient signal is dominated by whatever fills your prompts
  • The model spends capacity learning to predict user questions, document chunks, and boilerplate system…
  • Wasted capacity and distribution shift - with long prompts and short answers (typical for RAG-style or…
  • Prompt regurgitation - the model learns to echo instructions or context into its output

Key terms: Wasted capacity and distribution shift · Prompt regurgitation · Weaker instruction following

Interviewer follow-ups to expect

  1. How would you verify masking is correct before launching a run?

  2. In a multi-turn dataset, would you train on all assistant turns or only the final one - what's the tradeoff?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep