Concepts / Tokenization for LLMs

Tokenization for LLMs

Tokenization is the process of splitting text into smaller units called tokens.

  • Programming

Why Raw Text Is Not Ready for Training

A large language model learns from large amounts of text. However, the text in its raw form is not yet ready for training. Preparing text for large language model training is a required step before the model can learn from it. The model does not simply consume text as it was originally written; the text has to be transformed first.

This article focuses on the part of that preparation that comes right after tokenization. Tokenization is the process of splitting text into smaller units called tokens. Earlier steps cover how raw text is broken into tokens, so here we pick up at the point where you already have a sequence of tokens. The question is how to turn that sequence into the examples a model actually trains on.

Key point

Keep one chain in mind as you read: tokenized data goes in, a sliding window slides across it, and input-target pairs come out. Those pairs are what the model is trained on.

Tokenized Data as the Starting Point

The process described in the source has a clear input and a clear output. Tokenized data is the input. Input-target pairs are the output. Everything in between is the sliding window approach applied to the tokens.

The window does not slide over raw characters. It slides over the tokens. This is why tokenization comes first: the window operates on units the model already treats as discrete pieces. If the splitting step produced different tokens, the windows and the pairs built from them would also be different.

Raw textunpreparedTokenizationsplits text into tokensTokenized datathe input to pairgenerationSliding windowscans the tokensInput-target pairstraining examplesLLM traininglearns from the pairsprepared byproducesscanned byoutputsfeeds
How do raw text, tokenization, and pair generation connect in sequence to produce training examples?

Sliding a Window Across Tokens

An input-target pair is a training example. Each pair ties a piece of input to the target the model should learn to associate with it. The source defines pairs by their purpose, which is to supply training examples, rather than by one fixed recipe. The exact arrangement can vary, so the illustration below shows one possible arrangement and says so clearly.

The sliding window approach moves a window across the tokenized data to produce these pairs. The window covers a fixed number of consecutive tokens. At each position it yields a pair, and then it moves forward. Because consecutive window positions overlap, one token sequence can yield several pairs.

Worked example

Generating pairs from a six-token sequence

Take the token sequence: The, cat, sat, on, the, mat. Use a window that holds three tokens for the input. In this illustration, the target is the same three-token span shifted one position ahead. Find the pairs produced at the first two window positions. This arrangement is an illustrative choice made for this article; the source does not fix one recipe.

  1. 1
    Position 0

    The window covers The, cat, sat. The input is The, cat, sat. Shifted one position ahead, the target is cat, sat, on.

  2. 2
    Slide one step

    The window moves one token forward and now covers cat, sat, on. This step is the only change in the window.

  3. 3
    Position 1

    The input is cat, sat, on. Shifted one position ahead, the target is sat, on, the.

  4. 4
    Compare the two pairs

    Both pairs share the tokens cat and sat. This overlap is what lets a single token sequence produce several pairs.

Result

Position 0 gives the pair (The cat sat, cat sat on). Position 1 gives the pair (cat sat on, sat on the).

Theinputcatinput and targetsatinput and targetontargetthenot yet usedmatnot yet used
As the window moves one step at a time across a token sequence, which tokens form the input and which form the target at each position?

The diagram above shows the window at its first position. Tokens The, cat, and sat form the input. Tokens cat, sat, and on form the target. Tokens cat and sat appear in both, which is the overlap described above. The tokens the and mat are not reached yet.

One Window Step, Before and After

Now trace exactly what changes when the window advances by one position. Compare the state before the step with the state after it. Only the window contents, and therefore the generated pair, change. The token sequence itself stays the same.

Window start 0The cat satinputcat sat ontargetWindow start 1cat sat oninputsat on thetargetcoversshifted tocoversshifted to
What exactly changes in the token sequence, window contents, and generated pair when the window advances by one position?

Read the diagram as a before and after pair. Before the step, the window starts at position 0, its input is The cat sat, and its target is cat sat on. After one step, the window starts at position 1, its input is cat sat on, and its target is sat on the. The pair changes from one example to the next, while the six-token sequence it reads from stays unchanged.

Word Tokens Versus Subword Tokens

The source describes tokenization as splitting text into word tokens and subword tokens. A word-level split can keep a word as one token. A subword split can produce several smaller tokens for the same word. The splitting decision changes what the tokenized data looks like, and that in turn changes every pair generated from it.

The following example is an illustration written for this article, not taken from the source. It uses the made-up word unhappiness. Under a word-level split, the whole word stays a single token. Under a subword split, the same word becomes several smaller pieces. The exact subword boundaries depend on the tokenizer used, so the split shown here is one plausible example.

unhappinessone tokenunpiece 1happipiece 2nesspiece 3
How does the same word split differently under word-level tokenization versus subword tokenization, and what does each produce?
AspectWord tokensSubword tokens
Unit kept togetherA whole wordPieces smaller than a word
Example for unhappinessOne token: unhappinessSeveral tokens: un, happi, ness (illustrative)
Effect on the sequenceFewer, larger unitsMore, smaller units for the same text
Effect on windows and pairsWindows cover whole wordsWindows cover pieces of words

Word-level and subword-level splits of the same text

Byte Pair Encoding as a Tokenizer

Byte pair encoding, often abbreviated BPE, is described in the source as a more advanced way of tokenizing text. It is one method for producing tokens, and it belongs to the same tokenization step covered above. Its result feeds the same sliding window process that produces input-target pairs.

The source does not walk through BPE step by step, so this article does not state its internal rules. What matters at this stage is the change in state it produces. The text starts as a sequence of smaller units and ends as a sequence of tokens that the tokenizer has formed. Follow that change closely, comparing each state with the one before it, to see how the token sequence takes shape.

Mistakes Beginners Make with Pairs and Tokens

  • Common mistake

    Assuming the window slides over raw characters

    Why it fails

    The source states that the window slides over the tokens, not over raw characters.

    Do this instead

    Remember that tokenization comes first, and the window operates on the resulting tokens.

  • Common mistake

    Thinking each token sequence yields only one pair

    Why it fails

    Consecutive window positions overlap, so one sequence can yield several pairs.

    Do this instead

    Move the window one step at a time and produce a pair at each position.

  • Common mistake

    Treating the input and target as unrelated text

    Why it fails

    Each pair ties a piece of input to the target the model should learn to associate with it.

    Do this instead

    Read every pair as an input and the target linked to it, within the same window.

  • Common mistake

    Assuming every word becomes one token

    Why it fails

    A subword split can produce several smaller tokens for one word.

    Do this instead

    Check whether the tokenizer keeps words whole or splits them into subword pieces.

Check Your Understanding

EASY

Using the six-token sequence The, cat, sat, on, the, mat, and the same illustrative arrangement (input of three tokens, target shifted one position ahead), predict the pair produced when the window reaches position 2. Then describe in one sentence what changed between position 1 and position 2.

Hints
  • At position 2 the window covers sat, on, the.
  • Shift the input one position ahead to find the target.
  • Only the window contents change between steps; the sequence does not.

Key Takeaways

  • LLM training needs input-target pairs, which are training examples generated from tokenized data.
  • The sliding window approach moves across the tokens one step at a time, and consecutive positions overlap, so one token sequence can yield several pairs.
  • Text must be prepared before training, and tokenization splits that text into word tokens or smaller subword tokens, which affects every pair built from it.
  • Byte pair encoding is a more advanced way of tokenizing text; it changes how tokens are formed, while the window process that produces pairs stays the same.

Related concepts

More concepts starting with T