Skip to main content

Exercises — Training examples

Six questions: alignment, causality, packing, and padding

← Back to the lesson: Training examples

Check your understanding

1. Input-target shift

For stream [4, 7, 2, 9] and input length 3, what is the first input-target pair?

Hint

Each target is the token immediately following the input token at the same position.

Show the answer

Input [4, 7, 2], target [7, 2, 9]. Both sequences have the same length; the target is shifted by one index in the source stream.

2. Teacher forcing and causality

Why does teacher forcing not expose future tokens?

Hint

Distinguish the choice of prefix supplied to the model from the rule that determines which positions each token can access.

Show the answer

Teacher forcing supplies the observed prefix from the data. The causal mask still restricts each position to previous tokens and the current token. Conditioning on reference data therefore does not remove the causal constraint; without it, a future target could leak into the computation.

3. Attention mask and loss mask

How do attention masks and loss masks differ?

Hint

One acts on the dependencies used to compute representations; the other acts on the terms aggregated into the objective.

Show the answer

An attention mask determines which positions may be consulted. A loss mask determines which targets contribute to the objective. They are distinct: masking a position in attention does not guarantee that its label is excluded from cross-entropy.

4. Logits axes

If logits have shape \([B,L,V]\), what do the three axes represent?

Show the answer

Batch, sequence position, and candidate vocabulary token. Each position in each sequence has a score for each of the \(V\) candidate tokens.

Apply the mechanisms

5. Packing: is a document boundary enough?

Two documents are concatenated:

  • Document A: [A1, A2, EOS]
  • Document B: [B1, B2, EOS]

The packed stream is therefore [A1, A2, EOS, B1, B2, EOS]. Inputs and targets are constructed mechanically by shifting the stream by one token.

  1. Write the input and target sequences.
  2. With an ordinary causal mask, which positions can the representation at the B1 position attend to?
  3. Does the EOS token by itself prevent access to document A?
  4. If the objective is to treat B as independent of A, what additional rules must be defined?
Hint

Separate three mechanisms: target shifting, visibility defined by the attention mask, and inclusion of targets in the loss. A separator token is data in the sequence, not a masking rule.

Show the reasoned answer

The mechanical shift gives:

  • Input: [A1, A2, EOS, B1, B2]
  • Target: [A2, EOS, B1, B2, EOS]

With an ordinary causal mask, the representation at the B1 position can attend to earlier positions, including A1, A2, and EOS (as well as B1 itself, under the usual causal convention). Causality prevents access to the future; it does not isolate documents.

No: EOS marks a boundary in the stream, but does not by itself change the attention visibility matrix.

To make B independent of A, define a block-diagonal attention mask so B positions cannot attend to A. If the positional mechanism uses absolute position IDs, also decide whether they reset at the document boundary; the attention mask alone does not specify that. Handle the target transition explicitly too: under the mechanical shift above, the output associated with A’s EOS position is trained to predict B1. If that cross-document prediction is not part of the objective, exclude that target from the loss or define a target-construction/reset mechanism that respects the boundary.

Conclusion: the marker, attention mask, and loss mask answer three different questions. Their combination depends on the objective; there is no universal packing or masking policy.

6. Padding: does the attention mask alone exclude PAD from the loss?

Two examples of different lengths are batched and right-padded to five positions.

Example input_ids targets
A (3 tokens) [11, 12, 13, PAD, PAD] [12, 13, 14, PAD, PAD]
B (5 tokens) [21, 22, 23, 24, 25] [22, 23, 24, 25, 26]

The padding component of the attention mask is [1,1,1,0,0] for A and [1,1,1,1,1] for B; the causal mask remains a separate constraint. The per-position losses before masking are:

  • A: \([0.2, 1.1, 0.7, 4.0, 3.0]\)
  • B: \([0.5, 0.4, 0.6, 0.9, 0.6]\)
  1. Construct the expected loss mask if only real targets should contribute.
  2. Calculate the valid-token mean.
  3. Calculate the naive mean over all ten positions and explain why it is incorrect for this objective.
  4. Is a correct attention mask sufficient if PAD labels are still passed to cross-entropy?
Hint

There are three valid targets for A and five for B. For a valid-token mean, the denominator is the number of retained targets, not the total number of padded positions.

Show the reasoned answer

The loss mask is [1,1,1,0,0] for A and [1,1,1,1,1] for B. It retains eight targets.

The sum of valid losses is:

[ 0.2+1.1+0.7+0.5+0.4+0.6+0.9+0.6=5. ]

The valid-token mean is therefore:

[ _{}==0.625. ]

The naive mean also includes the two PAD losses from A:

[ _{}==1.2. ]

It optimizes a different objective, which here depends on the losses assigned to padding positions. The attention mask determines which positions may be consulted; it does not automatically remove PAD labels from the loss function. A consistent loss mask or explicit ignored-label convention is required, followed by reduction over the number of valid targets.

Note

Scope of these exercises. The scenarios are conceptual and illustrate possible policies. They do not prescribe a universal packing format, cross-document mask, or reduction convention; those choices must be defined by the training pipeline and objective.

Language: Français · English