Concepts / Subword Tokenization and Vocabulary Design

Subword Tokenization and Vocabulary Design

Byte Pair Encoding is a tokenization method used in natural language processing, including in GPT-2.

  • Programming

Why a Tokenizer Has to Choose Its Pieces

Language models do not read text as whole sentences. They read a sequence of tokens, and something has to decide where one token ends and the next begins. That something is the tokenizer. Its choices affect how much text fits into a model's input, how common words and rare words are represented, and how the model handles spellings it has never seen before.

Byte Pair Encoding, usually abbreviated BPE, is one of the tokenization methods used in natural language processing. It is the method behind the tokenizer used to train GPT-2. This article builds a mental model of what BPE does and why it is designed the way it is. Once that model is clear, the implementations and interactive demonstrations you meet later will make sense.

What do you think happens?

Suppose the most frequent adjacent pair in a set of text is the letter l followed by the letter o. What new unit does BPE create when it merges that pair?

Reveal answer
Answer:

The new unit is lo, a single symbol built from the two letters.

BPE replaces the adjacent pair with one new unit. Here the two characters l and o become the single unit lo, which is then available as a building block for later merges.

Words, Characters, and Subword Units

There are two simple ways to cut text, and each fails in a different way. If a tokenizer treats every whole word as one token, it needs a huge vocabulary, and any word it has not seen during training has no entry at all. If it treats every character as one token, the vocabulary stays small, but the sequences become long and the individual units carry little meaning on their own.

Subword units sit between these two extremes. Common words can stay whole, while rare or unfamiliar words are assembled from smaller pieces that the vocabulary already contains. The name of the method points to its core operation. A tokenizer built this way works on pairs of adjacent symbols and combines frequent pairs into single units. Repeating that combination gradually builds a vocabulary of units that are longer than one character and shorter than a whole word. Because these units are pieces of words, BPE belongs to the family of subword tokenization methods.

The Training and Application Stages

A BPE tokenizer is best understood as a pipeline with two distinct stages. Training builds the vocabulary. Application uses that finished vocabulary to split new text. The two stages are separate: the vocabulary is produced once during training, and afterward it is applied to whatever text needs to be tokenized.

During training, the method starts from a basic set of symbols and repeatedly merges the most frequent adjacent pair into a new symbol. Each merge adds one entry to the vocabulary. During application, new text is cut using the vocabulary that training produced. The source material places BPE in this sequence of stages, with raw text going in and token IDs coming out for the model to read.

Key point

The heart of the method is a loop. Each pass looks at the current sequence of units, finds the pair that occurs most often, and replaces that pair with a single unit. Because the merged unit becomes a building block for later merges, longer units can emerge from shorter ones over many passes.

Tracing One Merge Step

The example below is deliberately small. Real training runs over a large body of text and performs many merges, but the rule at each step is the same: find the most frequent adjacent pair, merge it, and record the new unit. Imagine a tiny training text made of the words low, low, and lower. We start with every character as its own symbol.

Worked example

Counting pairs and performing the first merge

Starting from single characters, find the most frequent adjacent pair in the training text low, low, lower, and show how one merge changes the sequence of the word lower.

  1. 1
    Write the starting symbols

    Each word begins as a list of single characters. The word low is l, o, w. The word lower is l, o, w, e, r.

  2. 2
    Count adjacent pairs across the text

    The pair l followed by o appears in each of the three words, so it occurs 3 times. The pair o followed by w also occurs 3 times. The pair w followed by e occurs once, and the pair e followed by r occurs once.

  3. 3
    Pick the most frequent pair

    The pairs l o and o w tie at 3 occurrences. This example merges l o, which is one of the two tied choices. The rule only requires choosing a most frequent pair, so either tied pair is valid for this illustration.

  4. 4
    Replace the pair with one new unit

    The adjacent symbols l and o are replaced by the single symbol lo. The new unit is recorded in the vocabulary, so the vocabulary now holds lo alongside the single characters.

  5. 5
    Compare the token sequence before and after

    The word lower changes from five symbols, l o w e r, to four symbols, lo w e r. The sequence is shorter, and it now contains a unit that is longer than one character.

Result

After one merge, the vocabulary gains the unit lo, and the word lower is represented as lo, w, e, r instead of l, o, w, e, r.

Growing the Vocabulary Over Several Merges

A single merge is only the first pass. Once lo exists, it can take part in the next count. In the same tiny text, the pair lo followed by w now occurs 3 times, which is more than any other adjacent pair. Merging it produces the unit low, and the word lower becomes low, e, r. A unit that is longer than a single merge could have produced has now emerged from shorter pieces.

Key point

The vocabulary grows by one entry per merge. It starts from the basic symbols and ends with a mix of single characters and multi-character units. Real training repeats this loop many more times, but every pass uses the same count-then-merge rule.

Mistakes Beginners Make with BPE

  • Common mistake

    Thinking the vocabulary is written by hand

    What it looks like

    Assuming someone chose lo and low because they are meaningful English pieces.

    Why it fails

    BPE builds its units by counting adjacent pairs in the training text and merging the most frequent ones.

    Do this instead

    Treat each unit as the result of a frequency-driven merge, and expect the vocabulary to reflect the statistics of its training data.

  • Common mistake

    Confusing training with application

    What it looks like

    Trying to learn new merges every time a new sentence is tokenized.

    Why it fails

    Training builds the vocabulary once. Application uses that existing vocabulary to split new text.

    Do this instead

    Keep the two stages separate in your mind: build the vocabulary during training, then apply it to incoming text.

  • Common mistake

    Assuming a rare word cannot be represented

    What it looks like

    Believing that an unfamiliar word has no token at all because it is not in the vocabulary as a whole word.

    Why it fails

    Subword units let the tokenizer represent a rare word by combining smaller pieces that the vocabulary already holds.

    Do this instead

    Think of an unfamiliar word as a sequence of known pieces rather than as a missing entry.

  • Common mistake

    Believing that every merge is the same size; each merge combines exactly two adjacent units into one new unit

Where BPE Is Used and Where to Look Next

BPE was introduced for machine translation. The research paper that introduced this approach is titled Neural Machine Translation of Rare Words with Subword Units. It was published in 2015 by Sennrich et al. and is available on arXiv. The title states the motivation directly: rare words are the hard case, and subword units are the proposed way to handle them.

Today BPE is part of how language models are prepared to read text. It is the method behind the tokenizer used to train GPT-2, which is the concrete industry use the source material names. In that setting, the tokenizer sits between raw text and the model's input IDs.

The source material points to three concrete resources for moving from this concept to real work. Each suits a different goal, ranging from reading the original method to inspecting real code or trying a visual demonstration. The pack does not name those three resources, so check the course's resource list for their current names and links rather than relying on a memory of them.

Practice Tracing a Merge

MEDIUM

Using the same tiny training text of low, low, and lower, start from single characters. Count the adjacent pairs in the word lower, identify the most frequent pair in the whole text, merge it, and write the new token sequence for lower.

Hints
  • Count pairs across all three words, not only in lower.
  • The pair o w also appears three times, so it ties with l o at the first step.
  • After you merge lo, look for the pair that now occurs most often, which involves lo.

Key Takeaways

  • BPE is a subword tokenization method used in natural language processing, including as the tokenizer method behind GPT-2.
  • BPE builds subword units by repeatedly merging the most frequent adjacent pair of symbols into a new unit, and each merge adds one entry to the vocabulary.
  • Subword units sit between whole-word and character-level tokenization, so common words can stay whole while rare words are built from smaller known pieces.
  • Training builds the vocabulary once, and application uses that vocabulary to split new text.
  • The source material points to three open-source resources for inspecting or implementing BPE, which should be checked in the course's resource list.

Related concepts

More concepts starting with S