Input Token IDs and Batch Construction
Target token IDs tell the model what it should generate, and training compares the model's outputs against them to compute the loss.
Why a Batch Needs a Second Sequence
A custom collate function that only groups input token IDs into batches gets the model partway to being trainable. 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 answers one 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 applied to each sequence. The most useful way to understand it is to watch that operation happen to concrete token IDs, so the next section does exactly that. Keep the question in mind as you read.
Tracing the Shift on Concrete IDs
Take a small input sequence of four token IDs: 15, 42, 7, and 90. The target sequence is built in three moves. First, drop the first input ID (15). Second, shift every remaining ID one slot toward the front, so 42 moves into the first slot, 7 into the second, and 90 into the third. Third, append an end-of-text token to fill the last slot, because nothing in the input is left to move there.
The diagram shows the pattern. Each target position holds the token that comes next in the input. Target position 0 holds the input ID at position 1, target position 1 holds the input ID at position 2, and so on. The window that reads from the input advances by one position, so the target lines up with the input one step ahead. Written with a symbol, the rule for every position i that has a successor is $y_{i} = x_{i+1}$, where $x$ is the input and $y$ is the target. The last target position has no successor, so it is the special case the end-of-text token handles.
What do you think happens?
Given the input sequence 8, 3, 21, what is the target sequence under the shift-and-append rule?
Reveal answer
The target is 3, 21, 50256.
Drop the first ID (8), shift the rest left so 3 and 21 move forward, and append the end-of-text token to fill the final slot. The target keeps the same length as the input, which is 3.
Why Targets Make Training Possible
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, which is why the batching process must deliver targets alongside inputs.
The same alignment appeared earlier in pretraining. There, the model learns to predict the next token in a sequence, so 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. The shift-and-append procedure is therefore not a new idea. It is the next-token relationship written down for a whole batch.
How the Collate Function Delivers Both Batches
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.
Notice what the pipeline keeps constant. The batch still groups sequences the same way the earlier collate function did, and the target batch is produced from the same sequences, so row i of the target batch is always the shifted version of row i of the input batch. The training loop can then read both outputs in step.
Padding at the End of a Sequence
Batches often contain sequences of different lengths, so shorter ones are padded. Consider an input that already ends in end-of-text tokens: [5, 6, 50256, 50256, 50256]. Dropping the first ID and shifting left brings the 6 into the first slot. The padding entries of the input shift along with everything else, so the remaining slots hold end-of-text tokens. The final slot then receives a fresh end-of-text token, giving the target [6, 50256, 50256, 50256, 50256].
The final target position always receives an end-of-text token, even when the input already contains padding. That is why the target keeps the same length as the input. The rule is applied the same way whether the sequence ends in real text or in padding, so the collate function does not need a special case for padded sequences.
Mistakes When Deriving Targets
Common mistake Copying the input unchanged as the target
What it looks likeInput 15, 42, 7, 90 with target 15, 42, 7, 90Why it failsA target is supposed to be what comes next, so each position must hold the token one step ahead of the input at that position.
Do this insteadDrop the first ID and shift the remaining IDs forward, giving 42, 7, 90, then append the end-of-text token.
Common mistake Leaving the last target slot empty or dropping it
What it looks likeProducing a target of length 3 for an input of length 4
Why it failsInputs and targets must line up position by position, so the lengths have to match.
Do this insteadAppend an end-of-text token to fill the final slot, as the procedure requires.
Common mistake Treating padding in the input as something to skip
What it looks likeInput 5, 6, 50256, 50256, 50256 with target 6, 50256, 50256, 50256
Why it failsThe padded input still has five positions, so the target must still have five.
Do this insteadKeep the appended end-of-text token even when the input already ends in padding, giving 6, 50256, 50256, 50256, 50256.
Practicing the Target Procedure
Work out the target for the input sequence 4, 19, 0, 33, 8. Write out the target sequence, then check that it has the same length as the input and that its last entry is the end-of-text token.
Hints
- Start by dropping the first ID, which is 4.
- Shift the remaining IDs one slot toward the front, then fill the last slot with the end-of-text token 50256.
Take the input 5, 6, 50256, 50256 from a padded batch. Derive its target by hand, then explain in one sentence why the target still ends in an end-of-text token even though the input already contains padding.
Hints
- Apply the same drop, shift, and append steps used for the padded example earlier.
Key Takeaways
- Training compares the model's prediction at each position against a target token ID, and the resulting loss drives the weight updates.
- A target sequence is built by dropping the first input token, shifting the remaining tokens so each one moves into the slot before it, and appending an end-of-text token.
- The appended end-of-text token keeps the target the same length as the input, including sequences that already contain padding.
- The updated custom collate function returns both the input token IDs and the target token IDs, with the two batches aligned position by position.