Sliding Window Approach for Generating Input-Target Pairs
The chain this article follows is: tokenized data goes in, a window slides across it, and input-target pairs come out. The pairs are then what the model is trained on.
Starting From a Tokenized Story
Before a language model can learn from a text, that text has to be converted into a form the training process can use. Earlier steps in this chapter covered how raw text is broken into tokens. This article picks up at that point. You already have a sequence of tokens, and the question is how to turn that sequence into the examples a model actually trains on.
A useful starting point is a small program that reads the full text of a short story called The Verdict, converts the raw string into a list of token IDs, and prints how many tokens the list contains. The length it prints is treated as the total number of tokens in the training set. Notice what this program leaves behind: a tokenized list, not the raw text. That tokenized list is what the data loader works with, and the rest of this article explains why.
The chain this article follows is: tokenized data goes in, a window slides across it, and input-target pairs come out. The pairs are then what the model is trained on.
Why Next-Word Prediction Trains the Model
An LLM learns by predicting the next word in a piece of text. Training does not hand the model a finished answer key. Instead, the training data is cut into pairs. One part is an input block, which is the portion of the text the model sees. The other part is a target, which is the word that comes immediately after that input block. The model's job is to predict the target from the input. Each time its prediction is compared against the actual next token, the model gets a learning signal.
Because every position in the text offers a new input and a new target, one piece of text can supply many training examples. The rest of the article shows how a sliding window generates those examples systematically.
Tracing the Token List Before Windowing
Before any window moves, the source removes the first 50 tokens from the tokenized text. It does this only to make the passage used in later steps more interesting. The remaining tokens form the sequence the rest of the process works on. Think of the token list as a long row of discrete pieces, each with a fixed position.
What do you think happens?
The source drops the first 50 tokens from the tokenized text. What does the token sequence look like afterwards?
Reveal answer
The sequence starts at what was originally token 51. Tokens 1 through 50 are no longer part of the sequence that later windows will cover.
Removing a prefix from a list leaves the remaining items in their original order, starting at the first item that was not removed. The source does this only to pick a more interesting passage for later steps.
What an Input-Target Pair Contains
An input-target pair is a training example made of two parts. The input block is what the model is allowed to read. The target is the single next word it must predict. Input-target pairs are the training examples used for LLM training, and they are generated from tokenized data.
The pairing has a second half that matters just as much. Words that come after the target are masked out during training, so the model cannot use them as hints. The model only ever sees the input block and is scored on whether it predicts the next word. The same token sequence can therefore be split into many different splits, and each split gives the model a different input and a different target.
The diagram shows one sample split three ways. The input block is the part the model reads. The target is the single token directly after it. Everything beyond the target stays hidden. The sliding window is simply a systematic way of producing many such splits from one long token sequence.
Sliding the Window Across Six Tokens
The source uses a sliding window approach to fetch input-target pairs from the training dataset. Picture a fixed-size frame placed over the token sequence. The frame covers an input block, and the token directly after the frame is the target. Each window position produces one input-target pair. Moving the frame one step along the sequence produces the next pair.
To make the mechanics easy to follow, consider an invented sequence of six tokens, named tok1 through tok6, with a window that holds three input tokens. The window always has the same width. The first window covers tok1, tok2, and tok3, and its target is tok4.
input: tok1 tok2 tok3 | target: tok4
The window covers positions 1 to 3 as input. The token at position 4 is the target. Tokens 5 and 6 are beyond the target and are hidden.
input: tok2 tok3 tok4 | target: tok5
The window has moved forward by one token. It now covers positions 2 to 4 as input, and the target is position 5. Token 1 has left the window, and token 6 is still beyond the target.
Generating all pairs from six tokens
Given the token sequence tok1, tok2, tok3, tok4, tok5, tok6, and a window that holds three input tokens, list every input-target pair the sliding window produces.
- 1Place the first window
Cover tok1, tok2, and tok3 with the frame. The token directly after the frame is tok4, so the first pair is input tok1 tok2 tok3 with target tok4.
- 2Advance one token
Move the frame one position to the right. It now covers tok2, tok3, and tok4. The target is tok5, so the second pair is input tok2 tok3 tok4 with target tok5.
- 3Advance again
Move the frame once more. It now covers tok3, tok4, and tok5. The target is tok6, so the third pair is input tok3 tok4 tok5 with target tok6.
- 4Check for a fourth window
A fourth position would need a target after tok6, and no token exists there. The sequence ends, so the window stops.
Three pairs come out of six tokens: tok1 tok2 tok3 to tok4, tok2 tok3 tok4 to tok5, and tok3 tok4 tok5 to tok6. Consecutive windows overlap, which is why one token sequence can yield several pairs.
Where Tokenized Data Fits in the Pipeline
The window does not slide over raw characters or whole words on a page. It slides over tokens, which are the units the model already treats as discrete pieces. Tokenization here uses a BPE tokenizer, which splits text into subwords. The figure that shows input and target pairs leaves this step out on purpose, but the text must be tokenized before the LLM can process it.
Read the pipeline from left to right. The tokenized data is the source material, the sliding window is the mechanism that scans it, and the input-target pairs are the result that feeds LLM training. Tokenized data is the input to the process, and input-target pairs are its output. The source describes this procedure as the way a data loader fetches pairs from the training dataset, and the program that turns the file into a token list is the first stage of that loader.
Mistakes When Building Pairs
Common mistake Sliding the window over raw characters or words instead of tokens
What it looks likeUsing the characters of The Verdict as the sequence and forming windows from them
Why it failsThe source specifies that the window operates on tokens produced by a BPE tokenizer, because those tokens are the discrete units the model understands.
Do this insteadTokenize the text first, then move the window over the resulting token list.
Common mistake Treating the target as part of the input block
What it looks likeUsing tok1 tok2 tok3 tok4 as the input and tok5 as the target
Why it failsThe target is the single word the model must predict, and it sits immediately after the input block rather than inside it.
Do this insteadKeep the input block to the window width and place the target directly after it.
Common mistake Letting tokens past the target leak into training
Why it failsWords after the target are masked out so the model cannot use them as hints. Visible future tokens would let the model copy the answer.
Common mistake Expecting each window to produce unrelated pairs
What it looks likeAssuming the pair from window 2 shares nothing with window 1
Why it failsConsecutive windows overlap. Moving the frame one token at a time means most of the input carries over between neighboring pairs.
Do this insteadExpect overlap, and count pairs by window positions rather than by distinct tokens.
Tracing a Window on a New Sequence
Suppose a token sequence has eight tokens, tok1 through tok8, and the window holds three input tokens. Trace the window one step at a time. List the input block and target for each window position, and state how many pairs the sequence yields in total.
Hints
- Start with the window covering tok1 to tok3 and find the token just after it.
- Each step moves the frame one token to the right, so the input block drops its first token and gains the next one.
- The last valid window must still leave a token for the target. Count how far the frame can slide before the target would fall off the end.
Key Takeaways
- An LLM is pretrained by predicting the next word in a text, so its training data is built as input-target pairs: an input block the model reads and a target it must predict.
- Words after the target are masked out during training, so the model is scored only on its prediction from the input block.
- A sliding window moves a fixed-width frame across a token sequence, producing one input-target pair at each position, and consecutive windows overlap.
- Tokenized data is the input to pair generation, since the window operates on tokens produced by a BPE tokenizer rather than on raw text.