The Problem It Solves
BPE (Byte Pair Encoding) is a subword tokenization algorithm for text data processing, and it's very common in natural language processing. Its core goal is to address the problems of overly large vocabularies and handling rare words.
At its core, it's about using a small character vocabulary to handle a wide range of unknown word variations and combinations.
If we tried to enumerate every language and word in the world, we'd need an enormous mapping table. BPE solves this problem, and it also reduces memory usage and improves model efficiency.
How BPE Works
BPE builds a more compact vocabulary by gradually merging frequently occurring character or subword pairs. Initially, it treats each character in the text as a separate symbol. It then generates higher-level subword units through the following steps:
- Count frequencies: Count how often every character pair (two consecutive characters) appears in the current text.
- Merge the most frequent character pair: Find the most frequent pair and merge it into a new subword.
- Update the text: Replace the original pair with the new subword throughout the text.
- Repeat: Keep repeating these steps until you've generated the target number of subwords or reached the target vocabulary size.
In this way, BPE can gradually build from the character level up to the subword and word levels. Because it's based on statistics, high-frequency words are easier to merge, while low-frequency words remain as subwords.
BPE in Action
# pip install tiktoken
from importlib.metadata import version
import tiktoken
tokenizer = tiktoken.get_encoding("gpt2")
text = (
"Hello, do you like tea? <|endoftext|> In the sunlit terraces"
"of someunknownPlace."
)
integers = tokenizer.encode(text, allowed_special={"<|endoftext|>"})
print(integers)
# [15496, 11, 466, 345, 588, 8887, 30, 220, 50256, 554, 262, 4252, 18250,
# 8812, 2114, 286, 617, 34680, 27271, 13]
strings = tokenizer.decode(integers)
print(strings)
# Hello, do you like tea? <|endoftext|> In the sunlit terraces of
# someunknownPlace.
In the example above, you can see that <|endoftext|> is encoded as 50256, and GPT-2's tokenizer has a maximum vocabulary size of 50257. The word someunknownPlace also gets encoded correctly.
The algorithm behind BPE splits words that aren't in its predefined vocabulary into smaller subword units or even individual characters, which lets it handle out-of-vocabulary words.
Here's an example showing how BPE handles Akwirw ier.

CleanShot 2024-10-08 at 5 .03.54@2x.png
Quick Summary
Put simply, it builds a vocabulary by repeatedly merging frequent characters into subwords and then merging frequent subwords into words. For example, BPE first adds all individual characters (like “a,” “b,” and so on) to the vocabulary. In the next stage, it merges character combinations that often appear together into subwords. For instance, “d” and “e” can merge into the subword “de,” a combination common in many English words such as “define,” “depend,” “made,” and “hidden.” Merging is driven by a frequency threshold.