Inferensys

Glossary

Byte-Pair Encoding (BPE)

A data compression algorithm adapted for NLP that iteratively merges the most frequent pairs of bytes or characters to build a subword vocabulary, balancing vocabulary size 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 data compression algorithm adapted for natural language processing that constructs a subword vocabulary by iteratively merging the most frequent adjacent pairs of bytes or characters in a training corpus.

Byte-Pair Encoding (BPE) is a subword tokenization algorithm that initializes a vocabulary with all unique characters in a corpus and then iteratively merges the most frequent adjacent symbol pair. This process builds a vocabulary of common subwords, enabling the model to represent rare or unseen words as sequences of known fragments, effectively balancing vocabulary size against the risk of out-of-vocabulary (OOV) tokens.

Originating as a compression technique, BPE was popularized in NLP by the subword-nmt library and forms the basis of tokenizers like GPT's tiktoken. Unlike purely word-based tokenization, BPE segments morphology like "un" + "believ" + "able", allowing models to infer semantic relationships between word forms. The number of merge operations is a hyperparameter that directly controls the final vocabulary size.

Subword Tokenization

Key Characteristics of BPE

Byte-Pair Encoding (BPE) is a data compression algorithm adapted for NLP that iteratively merges the most frequent pairs of bytes or characters to build a subword vocabulary. It balances vocabulary size and out-of-vocabulary words, making it a foundational component of modern embedding model selection.

01

Iterative Merge Algorithm

BPE starts with a base vocabulary of individual characters and iteratively merges the most frequent adjacent pair of symbols. This process builds a vocabulary of subword units, allowing the model to represent rare or unseen words by decomposing them into known fragments. Key steps:

  • Initialize vocabulary with all unique characters in the corpus.
  • Count the frequency of adjacent symbol pairs.
  • Merge the highest-frequency pair into a new symbol.
  • Repeat until the desired vocabulary size is reached.
  • Example: 'low' + 'er' → 'lower' if 'low' and 'er' are frequent.
Iterative
Algorithm Type
Frequency
Merge Criterion
02

Open-Vocabulary Solution

BPE elegantly solves the out-of-vocabulary (OOV) problem inherent in word-level tokenization. By operating on subword units, any unseen word can be represented as a sequence of known subwords. Benefits:

  • No more 'UNK' tokens for rare words.
  • Handles morphologically rich languages effectively.
  • Generalizes to new compounds and technical jargon.
  • Example: 'unhappiness' → 'un' + 'happiness' or 'un' + 'hap' + 'pi' + 'ness'.
Zero
OOV Rate
03

Vocabulary Size Control

The final vocabulary size is a hyperparameter chosen before training. The number of merge operations directly determines the vocabulary size, allowing engineers to trade off between token granularity and model embedding matrix size. Trade-offs:

  • Smaller vocab: Shorter sequences, but more characters per token, potentially losing semantic meaning.
  • Larger vocab: Captures more common words and morphemes as single tokens, improving efficiency, but increases the embedding layer's memory footprint.
  • Typical sizes range from 8k to 50k tokens for large language models.
8k-50k
Typical Vocab Size
04

Deterministic Encoding

Once the merge rules are learned from the training corpus, the tokenization process is entirely deterministic. A given input string will always be segmented into the exact same sequence of subword tokens. Process:

  • Apply the learned merge rules in order of their creation.
  • Greedily merge pairs starting from the most frequent.
  • This determinism is crucial for reproducible model inference and consistent embedding generation.
  • Contrasts with stochastic tokenization methods like BPE-Dropout.
Deterministic
Encoding Nature
06

Byte-Level BPE

A variant, Byte-Level BPE (BBPE), operates on raw bytes (UTF-8 encoding) rather than Unicode characters. This creates a truly universal base vocabulary of 256 bytes, guaranteeing that any sequence of bytes, including emojis or corrupted text, can be tokenized without an unknown token. Advantages:

  • Universal coverage: Handles any input, regardless of language or encoding.
  • No pre-tokenization: Eliminates the need for language-specific pre-tokenization rules.
  • Used in models like GPT-2 and RoBERTa.
256
Base Vocab Size
TOKENIZATION STRATEGY COMPARISON

BPE vs. Other Tokenization Methods

A technical comparison of Byte-Pair Encoding against alternative subword, word-level, and character-level tokenization strategies for modern NLP pipelines.

FeatureByte-Pair Encoding (BPE)WordPieceUnigram LMSentencePieceCharacter-Level

Core Algorithm

Iteratively merges most frequent byte/character pairs

Merges pairs based on likelihood maximization

Starts with large vocabulary, prunes lowest-probability tokens

Wraps BPE or Unigram; treats input as raw byte sequence

Splits text into individual characters

Vocabulary Size

Configurable (typically 30K-50K)

Configurable (typically 30K)

Configurable; naturally prunes to optimal size

Configurable; language-agnostic

Fixed and small (alphabet size)

Handles Unknown Words

Language Agnostic

Subword Granularity

Data-driven; merges frequent sequences

Likelihood-driven; prefers linguistically plausible splits

Probabilistic; retains multiple segmentation candidates

Same as underlying algorithm (BPE or Unigram)

None; atomic character units

Training Speed

Fast

Moderate

Slower (requires iterative pruning)

Fast (BPE) to Moderate (Unigram)

None required

Segmentation Consistency

Deterministic

Deterministic

Probabilistic (multiple segmentations possible)

Deterministic

Deterministic

Sequence Length (Avg Tokens per Word)

2-3 tokens

2-3 tokens

2-3 tokens

2-3 tokens

5-10 characters

Out-of-Vocabulary Rate

< 0.1%

< 0.1%

< 0.1%

< 0.1%

0%

Primary Use Case

GPT models, RoBERTa, LLaMA

BERT, DistilBERT

T5, XLNet, mT5

LLaMA, Mistral, multilingual models

Character-aware CNNs, spelling correction

Memory Footprint (Embedding Table)

Moderate

Moderate

Moderate

Moderate

Minimal

Pretokenization Required

Lossless Reconstruction

BYTE-PAIR ENCODING

Frequently Asked Questions

Explore the mechanics of Byte-Pair Encoding (BPE), the dominant subword tokenization algorithm that balances vocabulary size with the ability to represent open-ended language, forming the bedrock of modern large language models.

Byte-Pair Encoding (BPE) is a data compression algorithm adapted for natural language processing that constructs a subword vocabulary by iteratively merging the most frequent adjacent pairs of bytes or characters in a training corpus. It begins with a base vocabulary of all unique characters, treating the text as a sequence of these symbols. The algorithm then scans the corpus, counts all adjacent symbol pairs, and merges the single most frequent pair into a new symbol. This process repeats for a predefined number of merge operations, building a vocabulary of common words, subwords, and rare characters. The result is a deterministic set of merge rules that can segment any new word into known subword units, effectively eliminating the problem of out-of-vocabulary words while keeping the vocabulary compact.

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.