Inferensys

Glossary

Byte-Pair Encoding (BPE)

Byte-Pair Encoding (BPE) is a data compression algorithm repurposed as a subword tokenization method that iteratively merges the most frequent pairs of bytes or characters to build a vocabulary, effectively handling rare and out-of-vocabulary words.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
SUBWORD TOKENIZATION

What is Byte-Pair Encoding (BPE)?

Byte-Pair Encoding (BPE) is a subword tokenization algorithm that iteratively merges the most frequent adjacent pairs of bytes or characters in a text corpus to construct a fixed-size vocabulary, enabling language models to represent rare and out-of-vocabulary words as sequences of known subword units.

Byte-Pair Encoding (BPE) is a data compression algorithm repurposed for natural language processing that builds a vocabulary by starting with individual characters and repeatedly merging the most frequent adjacent symbol pair. This bottom-up process creates a set of subword units—common words remain intact while rare words decompose into meaningful fragments—allowing models to handle an open vocabulary without resorting to a single unknown token placeholder.

During tokenization, an input string is segmented by greedily applying the learned merge rules in order of frequency, splitting text into the longest possible subword tokens from the vocabulary. This deterministic procedure ensures that morphological variants and novel compounds are represented compositionally, striking a critical balance between the semantic richness of word-level modeling and the flexibility of character-level modeling.

Subword Tokenization

Key Characteristics of BPE

Byte-Pair Encoding (BPE) is a data compression algorithm repurposed as a subword tokenization method. It iteratively merges the most frequent pairs of bytes or characters to build a vocabulary, effectively handling rare and out-of-vocabulary words.

01

Iterative Merge Algorithm

BPE starts with a base vocabulary of individual characters or bytes. It then iteratively counts the frequency of adjacent symbol pairs in the training corpus and merges the most frequent pair into a new token. This process repeats for a predefined number of merge operations.

  • Start: Vocabulary = {a, b, c, d, ...}
  • Step 1: Find the most frequent adjacent pair (e.g., 'a' + 'b' = 'ab')
  • Step 2: Add 'ab' to the vocabulary and replace all occurrences of 'a b' with 'ab' in the corpus
  • Repeat: Continue until the target vocabulary size is reached
02

Open-Vocabulary Handling

The primary advantage of BPE is its ability to handle out-of-vocabulary (OOV) words. Since the vocabulary contains subword units, any unseen word can be represented by decomposing it into known subword tokens.

  • The word 'unhappiness' might be tokenized as ['un', 'happiness'] or ['un', 'happi', 'ness']
  • This is a critical improvement over word-level tokenization, which fails on novel words
  • It provides a lossless encoding for any input sequence, as it can always fall back to the base character or byte level
03

Vocabulary Size Control

The final vocabulary size is a hyperparameter chosen before training. The number of merge operations directly determines the size of the vocabulary.

  • A larger vocabulary means more common words are represented as single tokens, improving efficiency
  • A smaller vocabulary forces more subword segmentation, which can help with rare words but increases sequence length
  • Typical BPE vocabularies for large language models range from 30,000 to 100,000 tokens
04

Deterministic Encoding

Once the merge rules are learned from the training corpus, the tokenization process is completely deterministic. The algorithm applies the learned merges in the exact order they were created.

  • The same input text will always produce the exact same token sequence
  • This determinism is essential for reproducible model behavior and debugging
  • The merge rules are stored as an ordered list, often in a file like merges.txt
05

Handling Rare and Common Words

BPE naturally creates a frequency-based hierarchy of tokens. Common words and morphemes become single tokens, while rare words are broken into smaller subword units.

  • Common word: 'the' → single token [the]
  • Rare word: 'disestablishmentarianism' → multiple tokens [dis, establish, ment, arian, ism]
  • This dynamic granularity is what makes BPE so effective for morphologically rich languages and technical jargon
06

GPT and Modern LLM Usage

BPE is the foundational tokenization algorithm behind many state-of-the-art models, most notably the GPT series by OpenAI.

  • GPT-2 and GPT-3 use a variant of BPE with a vocabulary of ~50,000 tokens
  • The algorithm is often combined with byte-level encoding to handle any Unicode character without pre-processing
  • This widespread adoption makes understanding BPE essential for prompt engineering and context window optimization
BYTE-PAIR ENCODING

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the subword tokenization algorithm that powers modern large language models.

Byte-Pair Encoding (BPE) is a subword tokenization algorithm that iteratively merges the most frequent adjacent pairs of bytes or characters in a text corpus to construct a fixed-size vocabulary. Originally developed by Philip Gage in 1994 as a lossless data compression technique, BPE was repurposed for natural language processing by Sennrich et al. in 2016 to handle rare and out-of-vocabulary words. The algorithm begins by splitting every word into individual characters and appending a special end-of-word symbol. It then counts all adjacent symbol pairs across the entire corpus, identifies the most frequent pair, and merges that pair into a single new token. This merge operation is repeated for a pre-defined number of iterations or until the target vocabulary size is reached. The result is a vocabulary containing common whole words, frequent subword units like prefixes and suffixes, and individual characters for rare sequences. At inference time, an unseen word is tokenized by applying the learned merge rules in order, greedily combining characters into the longest known subword units. This ensures that every input can be represented without an <UNK> token, a critical property for open-vocabulary language modeling.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.