Next-Token Prediction in LLM Pretraining
Target token IDs tell the model what it should generate, and training compares the model's outputs against them to compute the loss.
Why the Model Needs a Second Sequence
A custom collate function that only groups input token IDs into batches gets the model partway to being trainable. The inputs tell the model what it is reading. Training also needs a second piece of information: what the model is supposed to produce at each position in that input. This article asks one specific question. Once you have a batch of input token IDs, how do you build the matching batch of target token IDs that the training loop will compare against?
The answer is a small, mechanical operation on each sequence. The best way to understand it is to watch that operation happen to concrete token IDs, so the sections below trace one sequence step by step before stating any general rule.
Tracing One Input Sequence Position by Position
Take the input token ID sequence [7, 12, 3, 9]. Each position in this input has a token ID. The target sequence must have the same length, one entry per input position. Ask yourself what the model should produce at position 0. The useful observation is that the token that comes next in the input, at position 1, is the token the model should be producing at position 0.
Read the diagram from top to bottom. The first input ID, 7, is dropped and does not appear in the target at all. The remaining IDs 12, 3, and 9 each move one slot to the left, so target position 0 holds 12, target position 1 holds 3, and target position 2 holds 9. The final target slot, position 3, has no input ID left to move into it, so an end-of-text token fills it. The target is therefore [12, 3, 9, 50256], which has the same length as the input.
What do you think happens?
Before reading on, predict the target for the input [4, 8, 15]. Use the rule: drop the first ID, shift the rest left, and append an end-of-text token.
Reveal answer
[8, 15, 50256]
The first ID, 4, is dropped. The 8 and 15 each move one position left, so they occupy target positions 0 and 1. The final slot has no input ID to move into it, so it receives the end-of-text token, 50256.
Why Training Works by Comparison
Targets matter because training works by comparison. The model produces a prediction for each position, and that prediction is scored against the target at the same position. The result of that comparison is the loss, and the loss drives the weight updates that make the model better. Without a target sequence there is nothing to compare against, so the batching process must deliver targets alongside the inputs.
The same alignment already appeared during pretraining. There, the model learns to predict the next token in a sequence, and the targets line up with the inputs but sit one position ahead. Instruction fine-tuning reuses that alignment: the target at each position is the token that should follow the input token at that position. Seeing the same logic in both settings is a good sign that the shift-and-append procedure is the standard way targets are formed here.
Handling Padding in Shifted Targets
Batches often contain sequences of different lengths, so shorter sequences are padded. The source's second example shows what happens when the input already ends in end-of-text tokens. The input is [5, 6, 50256, 50256, 50256] and the target is [6, 50256, 50256, 50256, 50256]. Dropping the first ID and shifting left brings the 6 into the first slot. Every remaining slot is filled by an end-of-text token, because the padding entries of the input shift along with everything else, and the final slot receives a fresh end-of-text token.
Mistakes When Building Target Sequences
Common mistake Keeping the first input ID in the target
What it looks likeInput [7, 12, 3, 9] with target [7, 12, 3, 9] or [7, 12, 3, 9, 50256]Why it failsThe target at position 0 should be the token that comes after the input token at position 0, which is the input ID at position 1, not the input ID at position 0.
Do this insteadDrop the first ID, so the target begins with 12 for this input.
Common mistake Leaving the last target slot empty or shortening the target
What it looks likeInput [7, 12, 3, 9] with target [12, 3, 9]Why it failsThe target must match the input length, and the final position has no next input token to copy.
Do this insteadAppend an end-of-text token so the target is [12, 3, 9, 50256].
Common mistake Assuming padding breaks the rule
What it looks likeInput [5, 6, 50256, 50256, 50256] with target [6, 50256, 50256, 50256, 50256]Why it failsPadding entries in the input still shift left like any other token, and the final slot still receives an end-of-text token.
Do this insteadApply the same drop, shift, and append procedure to padded sequences without special cases.
Common mistake Shifting the sequence right instead of left, which moves the drop to the wrong end
Returning Inputs and Targets from the Collate Function
The collate function built earlier produced one batch of input token IDs. The update does not change that job. It adds a second output, so the function hands back the input token IDs and the target token IDs together. Each sequence in the batch gets its target built by the shift-and-append procedure, and the two batches stay aligned position by position, so the training loop can compare each prediction with the right target.
Building Targets for a New Batch
Given the input token ID sequence [21, 4, 77, 6], write the target token ID sequence the collate function should produce for it. Then state where the end-of-text token lands and why.
Hints
- Start by dropping the first ID, 21.
- Shift the remaining IDs one position left, so 4 moves into target position 0.
- Count the target entries. There must be four, so the last slot needs a fresh end-of-text token.
Recap of the Target-Building Procedure
- 1Target token IDs tell the model what it should generate at each position, and training compares the model's predictions against them to compute the loss.
- 2To build a target, drop the first input token ID, shift the remaining IDs one position to the left, and append an end-of-text token so the length matches the input.
- 3The end-of-text token is always the final target entry, including for sequences that already contain padding.
- 4The updated custom collate function returns both the input token IDs and the target token IDs for each batch, with the two batches aligned position by position.
Key Takeaways
- Target token IDs tell the model what it should generate at each position, and training compares the model's predictions against them to compute the loss.
- To build a target, drop the first input token ID, shift the remaining IDs one position to the left, and append an end-of-text token so the length matches the input.
- The end-of-text token is always the final target entry, including for sequences that already contain padding.
- The updated custom collate function returns both the input token IDs and the target token IDs for each batch, with the two batches aligned position by position.