Inferensys

Glossary

Subword Tokenization

Subword tokenization is a text segmentation method that breaks words into frequently occurring subunits (prefixes, stems, suffixes) to handle out-of-vocabulary words and reduce vocabulary size for machine learning models.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
MULTIMODAL DATA TRANSFORMATION

What is Subword Tokenization?

A core technique in natural language processing for converting text into model-ready inputs.

Subword tokenization is a text segmentation method that breaks words into frequently occurring subunits—such as prefixes, stems, and suffixes—to create a model's vocabulary. This approach strikes a balance between word-level and character-level tokenization, enabling models to handle out-of-vocabulary (OOV) words by decomposing them into known sub-units. It is the foundational step for models like BERT and GPT, directly reducing vocabulary size and improving efficiency on multilingual and technical corpora where novel word forms are common.

Common algorithms include Byte-Pair Encoding (BPE), WordPiece, and SentencePiece, which learn merge rules from a training corpus. By representing rare words as combinations of common subwords (e.g., 'unhappiness' as 'un', 'happi', 'ness'), this method provides a compact and flexible vocabulary. This is critical for multimodal data transformation pipelines, ensuring text from diverse sources is consistently encoded for joint processing with other modalities like audio or video in unified architectures.

ALGORITHM COMPARISON

Key Subword Tokenization Algorithms

Subword tokenization bridges the gap between character- and word-level segmentation. These algorithms create vocabularies of frequently occurring subunits, enabling models to handle out-of-vocabulary words efficiently. Below are the core algorithms that power modern NLP.

01

Byte-Pair Encoding (BPE)

Byte-Pair Encoding (BPE) is a data compression algorithm adapted for tokenization. It starts with a base vocabulary of individual characters and iteratively merges the most frequent adjacent pair of symbols (bytes or characters) to create new subword units.

  • Process: Begins with character-level tokens, then performs merge(‘A’, ‘B’) -> ‘AB’ for the most frequent pair (A, B) in the corpus. This repeats until a target vocabulary size is reached.
  • Key Use Case: Originally used in GPT-2 and many early transformer models. It effectively balances vocabulary size and sequence length.
  • Example: The word "unhappiness" might be tokenized as ["un", "happ", "iness"] if "un", "happ", and "iness" are learned subwords.
02

WordPiece

WordPiece is a subword tokenization algorithm used by models like BERT. It is similar to BPE but uses a different, likelihood-based merging criterion.

  • Process: Starts with a base vocabulary of characters. Instead of merging the most frequent pair, it merges the pair that maximizes the likelihood of the training data when added to the vocabulary. This is often implemented by scoring pairs with the formula: score = (freq_of_pair) / (freq_of_first * freq_of_second).
  • Key Use Case: The default tokenizer for BERT and its derivatives. It often produces linguistically intuitive subwords.
  • Example: The word "playing" might be split into ["play", "##ing"], where ## indicates a subword suffix.
03

Unigram Language Model

The Unigram Language Model tokenization algorithm starts with a large seed vocabulary and iteratively prunes it down, in contrast to BPE/WordPiece's bottom-up merging.

  • Process:
    1. Initialize with a large vocabulary (e.g., all words and common substrings).
    2. Train a unigram language model to estimate the probability of each subword.
    3. Iteratively remove the subwords that cause the smallest increase in the overall loss (perplexity) of the corpus.
  • Key Advantage: Provides a probability score for each tokenization, allowing for multiple segmentation possibilities. This is useful for uncertainty modeling.
  • Key Use Case: Used in SentencePiece (with the --model_type=unigram flag) and models like ALBERT.
05

Byte-level BPE (GPT Family)

Byte-level BPE is a variant used by OpenAI's GPT models (from GPT-2 onward). It applies BPE not on Unicode characters but on bytes, creating a truly universal base vocabulary.

  • Process: The input text is first encoded into UTF-8 bytes. The BPE merging algorithm is then run on this sequence of 256 possible byte values.
  • Key Advantage: The vocabulary is small (256 base bytes + merges), guaranteeing that any string can be tokenized without an <UNK> token. It elegantly handles any language, emoji, or special formatting.
  • Consideration: Sequences become longer (as many bytes per character), but this is offset by the transformer's ability to handle long contexts. It is the foundation of tokenizers like OpenAI's tiktoken and Hugging Face's GPT2Tokenizer.
06

Comparison & Trade-offs

Choosing an algorithm involves key engineering trade-offs between vocabulary coverage, sequence length, and linguistic intuition.

  • Vocabulary Size vs. Sequence Length: A larger subword vocabulary leads to shorter, more efficient sequences but risks overfitting. A smaller vocabulary (e.g., byte-level) leads to longer sequences but perfect coverage.
  • Merging Strategy: BPE (frequency-based) is simple and fast. WordPiece (likelihood-based) can yield more meaningful merges. Unigram (pruning-based) is more computationally intensive but provides probabilistic segmentations.
  • Pre-tokenization: Most algorithms (BPE, WordPiece) require a pre-tokenization step (splitting on whitespace/punctuation). SentencePiece and Byte-level BPE avoid this, offering greater flexibility for multilingual and noisy text.
  • Implementation: For production, robust libraries like Hugging Face Tokenizers (which supports all major algorithms) or SentencePiece are essential.
TEXT SEGMENTATION

Tokenization Method Comparison

A comparison of fundamental text segmentation methods used to convert raw text into discrete units (tokens) for machine learning models, highlighting trade-offs in vocabulary size, out-of-vocabulary handling, and sequence length.

FeatureWord-LevelCharacter-LevelSubword (BPE/WordPiece/Unigram)

Granularity

Whole words

Individual characters

Variable-length subword units (prefixes, stems, suffixes)

Vocabulary Size

Large (50k-500k+)

Very small (< 1k)

Moderate (10k-50k)

Out-of-Vocabulary (OOV) Handling

❌ Fails; maps to <UNK>

✅ Always succeeds

✅ Partial; composes OOV words from known subwords

Sequence Length

Short (1 token per word)

Very long (many tokens per word)

Moderate (1-4 tokens per word)

Semantic Preservation

High (tokens are meaningful)

Very low (tokens lack meaning)

Moderate (common words intact; rare words split)

Common Use Cases

Early NLP models, bag-of-words

Character-level RNNs, morphology-heavy languages

Modern transformers (BERT, GPT), machine translation

Example: 'unhappiness'

['unhappiness']

['u', 'n', 'h', 'a', 'p', 'p', 'i', 'n', 'e', 's', 's']

['un', '##happi', '##ness']

Primary Limitation

Poor generalization to rare/OOV words

Long sequences obscure word-level semantics; inefficient learning

Splits can be linguistically arbitrary; requires training on corpus

SUBWORD TOKENIZATION

Implementation in Major Models & Libraries

Subword tokenization is a foundational text preprocessing step implemented across all major language models and frameworks. The specific algorithm and vocabulary are critical architectural choices that directly impact model performance on downstream tasks.

06

Subword Regularization & Sampling

Advanced implementations go beyond deterministic tokenization. Subword Regularization (e.g., as implemented in SentencePiece's Unigram mode) introduces noise during training to improve model robustness.

  • Method: Instead of always using the single best segmentation, the model randomly samples from possible subword segmentations based on their probability.
  • Benefit: This acts as a powerful data augmentation technique, exposing the model to multiple segmentations of the same word and improving generalization, especially for morphologically rich languages or noisy text.
  • Use Case: Prominently used in training models like T5 and multilingual BERT variants.
SUBWORD TOKENIZATION

Frequently Asked Questions

Subword tokenization is a core technique in Natural Language Processing (NLP) for converting text into model-understandable units. This FAQ addresses common technical questions about its algorithms, trade-offs, and role in modern multimodal architectures.

Subword tokenization is a text segmentation method that decomposes words into frequently occurring subunits—such as prefixes, stems, and suffixes—to create a model's vocabulary. It works by statistically analyzing a training corpus to identify the most common character sequences, then using these learned subword units to segment new text. Popular algorithms like Byte-Pair Encoding (BPE) start with a base vocabulary of individual characters and iteratively merge the most frequent adjacent pairs into new subword tokens. This creates a vocabulary that balances the granularity of characters with the efficiency of whole words, enabling the model to handle a vast number of words, including those not seen during training (out-of-vocabulary words), by breaking them into known sub-components.

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.