Concepts / Cross-Entropy Loss in Language Model Training

Cross-Entropy Loss in Language Model Training

Masking changes which positions the loss is allowed to see. It does not change the text the model receives.

  • Programming

Why Instruction Tokens Enter the Loss

Imagine you are preparing a dataset to fine-tune a large language model so that it follows instructions. Each training example has three parts: an instruction that describes a task, an optional input the task operates on, and the response you want the model to produce. Once the text is assembled, the model is asked to predict the tokens that come next, one position at a time, and it is adjusted whenever its predictions drift from the expected tokens.

That setup raises an uncomfortable question. The instruction is part of the text the model sees, so it is also part of what the model is asked to predict. If nothing special is done, a share of the training effort goes toward reproducing the wording of instructions, which is not the skill you actually want the model to acquire. Instruction masking is a technique for steering that effort away from the prompt and toward the answer.

What do you think happens?

In a training example where the instruction is masked, which positions keep their real token IDs in the target, and which are overwritten?

  • The instruction positions keep their IDs and the response positions are overwritten

  • The response positions keep their IDs and the instruction positions are overwritten

  • Every position is overwritten with -100

  • Only the padding positions are overwritten

Reveal answer
Answer:

The response positions keep their real IDs, and the instruction positions are overwritten with -100.

Instruction masking replaces the target token IDs for the instruction with -100, the ignore_index value, so the cross-entropy loss is computed only over the generated response. Padding masking is a related but separate step.

Tracing One Training Example

To see the mechanism, follow one short example through the target sequence. Suppose the formatted example contains an instruction that reads Translate to French, an input word Hello, and a response Bonjour. The numbers below are illustrative placeholders, not real vocabulary IDs. Before masking, every position in the target holds its real token ID. After instruction masking, the instruction positions are overwritten with -100, while the response positions keep their IDs.

Translate-100 (ignored)to-100 (ignored)French-100 (ignored)Hello-100 (ignored)BonjourID 2214 (counted)
Which positions in the token sequence keep their real IDs and which are overwritten with -100, so that the loss ignores them?

Read the diagram as a rule about the target alone. The three instruction positions and the input word are overwritten, so they contribute nothing to the loss. Only the response token Bonjour keeps its ID and is graded. The illustrative ID 2214 stands in for whatever real ID the tokenizer assigns to that word. The rule is not about deleting text: the same tokens still exist in the input that the model reads.

Input Text Versus Target Text

Preparing a training example involves two parallel versions of the same formatted text. The first is the input text, which is tokenized and fed to the LLM during training. The second is the target text, which is what the model's predictions are compared against. Before masking, both versions are essentially identical, and the target is simply the input shifted so that each position is paired with the token that should follow it.

Input textfull formatted exampleLLMreads all tokensCross-entropy lossresponse tokens onlyTarget textinstruction overwrittenwith -100tokenizedpredictionsexpected tokens
How do the input sequence and the target sequence line up position by position, and where do they differ once the instruction is masked?

The two versions share positions. Position by position, the input and the target match until masking begins to overwrite the target. The model's predictions come from the input, while the comparison is made against the target. Because the instruction positions are overwritten in the target, the loss has no expected value to compare against at those positions, so they do not count toward learning. The model still reads them as context for the positions that follow.

Definition

The input text is the full formatted example the model reads during training. The target text is the same sequence in which the positions to be ignored have been overwritten, and it is what the model's predictions are compared against when computing the loss.

How the -100 Ignore Index Works

When a position in the target is masked, its token ID is replaced by the value -100, which is the ignore_index value. The name describes its role: a loss calculation configured to ignore that index skips the position entirely. Because the replacement happens in the target and not in the input, the model still reads the masked tokens as context for the positions that follow them.

Key point

Masking changes which positions the loss is allowed to see. It does not change the text the model receives.

Masking is closely related to a more common step: masking padding tokens. Padding is added so that training examples in a batch share the same length, and those padding positions are usually masked for the same reason. Instruction masking extends that idea to a meaningful part of the content itself. It is therefore a choice about which positions the loss is allowed to see, not a change to the text the model reads.

Best practice

When you enable instruction masking, verify two things separately: the input you feed the model still contains the full prompt, and the target has -100 exactly at the positions you intend to ignore. Confusing these two is the most common setup error.

What the Gradient Sees With and Without Masking

Compare the two training setups on the same example. Without masking, every target position has a real ID, so the loss pushes the model to predict the instruction wording as well as the response. With masking, only the response positions carry a real ID, so the loss is shaped only by how well the model predicts the answer. The main motivation is a shift in what the model is rewarded for: with masking on, the model is trained to focus on producing accurate responses rather than memorizing the instructions it is given.

The source identifies reduced overfitting as the benefit masking can offer. Overfitting is the general problem of a model fitting the training data too closely at the expense of generalizing. Masking does not guarantee that outcome; it changes the signal, and the effect has to be judged by how the trained model performs.

Mistakes About Masking

  • Common mistake

    Believing masking removes the instruction from the model's input

    What it looks like

    Assuming the instruction is stripped from the training text so the model never sees it.

    Why it fails

    The model still reads the full formatted example. Masking changes the target, not the input.

    Do this instead

    Keep the instruction in the input and overwrite only its target positions with -100.

  • Common mistake

    Assuming masked tokens provide no context

    What it looks like

    Thinking the model forgets masked instruction tokens when predicting response tokens.

    Why it fails

    The masked tokens are still read as context for the positions that follow them.

    Do this instead

    Treat masking as a loss-scope decision: the tokens are visible to the model but not graded.

  • Common mistake

    Confusing instruction masking with padding masking

    What it looks like

    Treating all -100 positions as simply padding added to equalize batch lengths.

    Why it fails

    Padding masking is the common, related step. Instruction masking extends the same idea to meaningful content.

    Do this instead

    Recognize that both use -100, but instruction masking is a deliberate choice about which content is graded.

  • Common mistake

    Treating masking as required for every fine-tuning run rather than as an optional technique to test and compare

The Open Debate on Masking

Masking is a reasonable default to reason about, but it is not settled. The source notes that researchers are divided on whether masking instructions is universally beneficial during instruction fine-tuning. It cites a 2024 paper by Shi et al., titled Instruction Tuning With Loss Over Instructions, which demonstrated that not masking the instructions can benefit LLM performance.

FeatureMasked instructionsUnmasked instructions
What the loss coversOnly the generated responseInstruction and response tokens
What the model is trained to focus onProducing accurate responsesPredicting the full formatted text, including instruction wording
Motivation the source givesReduced overfitting and less memorization of instructionsShi et al. (2024) showed that not masking can help performance
Current state of knowledgeResearchers are divided; not universally beneficialResearchers are divided; not universally beneficial

Two training setups compared on the features the source discusses

The source's own example leaves masking off and treats turning it on as an optional exercise for readers. That choice is a useful reminder that the technique is a tool you can enable, test, and compare, rather than a requirement built into every fine-tuning run.

Practice: Predict the Loss Positions

MEDIUM

A training example is formatted as an instruction, an optional input, and a response. Instruction masking is applied to the instruction. Without running any code, state which target positions still count toward the cross-entropy loss, and explain whether the model can still read the instruction when predicting the response.

Hints
  • Ask which positions are overwritten with -100 in the target.
  • Remember that the input text is not changed by masking.
  • Consider whether a masked token can still serve as context for later positions.

Key Takeaways

  • Instruction masking replaces the target token IDs for the instruction with -100, the ignore_index value, so the cross-entropy loss is computed only over the generated response.
  • The model still reads the full formatted example, including the instruction, because masking changes the target and not the input.
  • Masking is motivated by reduced overfitting: the model is trained to focus on accurate responses rather than memorizing instructions.
  • Researchers are divided on whether masking is universally beneficial, and a 2024 study by Shi et al. showed that not masking can help performance.

Related concepts

More concepts starting with C