liveBook · Manning

Mask

As explained in Build a Large Language Model (From Scratch)

A training-time method that prevents the model from attending to certain input positions, such as padding tokens.

Masking prevents padded positions from affecting training. In batched GPT inputs, shorter sequences receive padding tokens so every sequence has the same length; attention masking keeps those positions from being attended to. section 2.4

Loss masking is a related operation applied to target token IDs. Replacing padding targets with -100 makes PyTorch cross-entropy ignore them through its default ignore_index=-100 setting. The first end-of-text target remains active so the model can learn when a response ends. section 7.3

A batching pipeline applies masking after creating aligned inputs and targets:

  • Pad each sequence with token ID 50256, then form inputs by removing the final position and targets by shifting one position. section 7.3
  • Find target positions containing padding, preserve the first end-of-text target, and replace later padding targets with -100. section 7.3
  • Optionally replace instruction and input targets with -100 so loss trains the generated response rather than the prompt. section 7.3

Masked target batch

For inputs [[0, 1, 2, 3, 4], [5, 6, 50256, 50256, 50256]], aligned targets become [[1, 2, 3, 4, 50256], [6, 50256, -100, -100, -100]]. Cross-entropy ignores the -100 entries, while still learning the meaningful end-of-text target. section 7.3

Figure 7.5 The three-stage process for instruction fine-tuning an LLM. Next, we look at step 2 of stage 1: assembling the training batches.
figure

Use the instruction fine-tuning pipeline figure to locate masking within batch assembly and the surrounding training stages. section 7.3

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Implements padding, shifted targets, -100 replacement, and loss behavior, but leaves instruction masking optional.
Examples
Demonstrates masked target tensors, GPT end-of-text padding, and an exercise comparing instruction masking with unmasked training.
What it assumes
Assumes tokenization and batching, then builds target alignment and padding exclusion through concrete tensor operations.
What you can do after
Apply masking via a custom PyTorch collate function and verify that ignored targets leave cross-entropy unchanged.
How it connects
Connects padding tokens with attention, target token alignment, cross-entropy loss, and instruction fine-tuning.

Also explained in

Compare how 7 books explain mask →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.