Welcome to our third lesson. In our previous discussion, we established that subword tokenization offers a robust and pragmatic solution to the challenges of representing text numerically, balancing vocabulary size, sequence length, and the ability to handle any word. Now, we will transition from the "what" to the "how."
This lesson focuses on Byte Pair Encoding (BPE), the algorithm used by models like GPT to create their subword vocabularies. We will dissect the iterative process BPE uses to learn these subwords directly from a raw text corpus. You will see how a simple data compression technique was ingeniously adapted to become a cornerstone of modern language models.
The Core Idea: Data Compression Meets NLP
At its heart, BPE is a greedy algorithm that iteratively finds the most frequent pair of adjacent tokens in a sequence and merges them into a single, new token. This process is repeated for a set number of merges, and the final collection of merged tokens, along with the initial characters, forms our vocabulary.
This high-level overview shows the key components of the BPE process, which we'll explore in detail.

A mind map illustrating the core concepts of Byte Pair Encoding: how it works, why it's used, and its applications.
Before we see how BPE is used for tokenization, let's first understand its origins as a pure data compression algorithm. The underlying logic is identical and provides a clean, intuitive entry point.
Lecture 8: The GPT Tokenizer: Byte Pair Encoding
The video "The GPT Tokenizer: Byte Pair Encoding" by Vizuara provides an excellent introduction to BPE's history.
Watch the segment from the history of BPE. Pay close attention to the simple example with the string aaabdaaabac. This clearly demonstrates the core iterative process: find the most common pair of bytes (characters), replace it with a new, unused byte, and repeat.
As you saw in the video, the algorithm is conceptually simple: find the most common pair and replace it. This compresses the data by reducing the total number of symbols. When we apply this to natural language processing, our goal isn't just to compress the text, but to learn a vocabulary from these merge operations. The sequence of merges itself becomes a set of learned subword tokens.
From Characters to Subwords: A Step-by-Step Walkthrough
Let's now walk through how BPE trains a tokenizer on a text corpus. The process consists of a few key steps: initializing a vocabulary, pre-processing the text, and then iteratively merging pairs.
To understand the motivation for subword tokenization and how BPE achieves it, it's helpful to quickly recap the pros and cons of word and character tokenization, and see the two guiding principles of subword methods.
Lecture 8: The GPT Tokenizer: Byte Pair Encoding
The same video provides a concise review of tokenization strategies and the principles of subword tokenization that BPE implements.
First, watch the section from comparing tokenization types for a refresher on the trade-offs. Then, watch from the rules of subword tokenization to understand the two main goals: keeping frequent words intact and splitting rare words into meaningful parts.
Now, let's apply these principles. The following video segment provides a detailed, step-by-step demonstration using a small corpus containing the words old, older, finest, and lowest. This example is invaluable for seeing the algorithm in action.
Lecture 8: The GPT Tokenizer: Byte Pair Encoding
This is the core of our lesson. You will see the BPE algorithm build a vocabulary from scratch.
Please watch this detailed walkthrough from the practical example. Follow each iteration closely: Initialization: Notice how the process starts with a frequency count of individual characters from the corpus. A special end-of-word token (</w>) is added to distinguish between subwords at the end of a word (like est in "lowest") and those in the middle (like est in "estimate"). Iterative Merging: Observe how in each step, the algorithm identifies the most frequent adjacent pair (e.g., e and s), merges them into a new token (es), adds es to the vocabulary, and updates the corpus representation. Vocabulary Growth: See how this process continues, building larger and more meaningful tokens like est and eventually old. This demonstrates how BPE automatically discovers linguistic roots and affixes based purely on frequency.
To solidify your understanding, the following textual resource walks through a very similar example. Given your background, you may find the accompanying Python code snippets particularly illuminating.
Byte Pair Encoding: Complete Guide to Subword Tokenization
This guide by Martin Brenndörfer provides a clear textual and visual explanation of the BPE training process.
Read the sections Learning Merge Rules and Visualizing the Merge Process. The code examples for count_pairs and merge_pair directly implement the logic you saw in the video, and the visualization shows how words like "lowest" are progressively built up from characters to subwords.
Modern BPE: Operating on Bytes
The examples we've seen operate on characters. Modern implementations, such as the one used for GPT-2, take this a step further and operate on bytes. This has a crucial advantage: the base vocabulary is fixed to the 256 possible byte values. Since any text in any language can be represented as a sequence of UTF-8 bytes, this approach completely eliminates the possibility of an "unknown" character. A multi-byte character (like an emoji or a character from a non-Latin script) is initially treated as several byte tokens, which the BPE algorithm can then learn to merge if they appear frequently.
Furthermore, modern BPE tokenizers use a sophisticated pre-tokenization step. Before BPE training begins, the text is split into "words" using a regular expression. This prevents merges from happening across word boundaries (e.g., merging the 'e' from "the" and the 'b' from "boy"). For a CS student like yourself, seeing the actual regex used in GPT-2 can be insightful.
Building a Fast BPE Tokenizer from Scratch
This article by Jun Yu Tan is an advanced deep-dive into optimizing BPE, but its initial sections provide a very clear explanation of modern, byte-level BPE and pre-tokenization.
Read the sections Algorithm & Naive Implementation and the pre-tokenization regex. Focus on the conceptual explanation of byte-level BPE and the breakdown of the regex pattern. Don't worry about the full implementation details; the key is to understand why these two modifications are made in practice.
This combination of byte-level operation and intelligent pre-tokenization makes BPE a highly effective and universal solution for preparing text for large language models.
Conclusion
In this lesson, we have demystified the process of Byte Pair Encoding. You've learned that BPE is fundamentally a data-driven algorithm that:
- Initializes a vocabulary with base units (characters or, more robustly, bytes).
- Scans a text corpus to find the most frequently occurring adjacent pair of tokens.
- Merges this pair into a new, single token and adds it to the vocabulary.
- Repeats this process for a predetermined number of merges, resulting in a vocabulary of subwords that efficiently represents the training data.
This iterative merging process is how LLMs learn a vocabulary that captures meaningful linguistic patterns (like roots and suffixes) without being explicitly taught them. It elegantly solves the tokenization trilemma, creating a vocabulary of manageable size that can represent any word while keeping sequence lengths computationally feasible.
We have now converted our raw text into a sequence of integer IDs. But these are still just arbitrary numbers. In our next lesson, we will explore token embeddings, the crucial step where we map these discrete token IDs to dense, continuous vectors that finally capture the semantic meaning our models can work with.