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.
Glossary
Subword Tokenization

What is Subword Tokenization?
A core technique in natural language processing for converting text into model-ready inputs.
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.
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.
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.
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.
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:
- Initialize with a large vocabulary (e.g., all words and common substrings).
- Train a unigram language model to estimate the probability of each subword.
- 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=unigramflag) and models like ALBERT.
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
tiktokenand Hugging Face'sGPT2Tokenizer.
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.
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.
| Feature | Word-Level | Character-Level | Subword (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 |
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.
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.
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Subword tokenization is a core technique within a broader ecosystem of data transformation methods. These related concepts are essential for preparing diverse data types—text, audio, video, sensor data—for machine learning models.
Byte-Pair Encoding (BPE)
Byte-Pair Encoding (BPE) is the foundational, data-driven algorithm behind most modern subword tokenizers. It starts with a base vocabulary of individual characters and iteratively merges the most frequently occurring pair of symbols in the training corpus to create new subword units.
- Key Mechanism: The algorithm runs for a pre-defined number of merge operations, building a vocabulary that balances character-level flexibility with word-level efficiency.
- Real-World Use: BPE is the core of tokenizers for models like GPT (OpenAI) and RoBERTa (Meta), enabling them to handle complex morphology and out-of-vocabulary words effectively.
WordPiece Tokenization
WordPiece is a subword tokenization algorithm similar to BPE but with a different merging criterion. Instead of merging the most frequent pair, it merges the pair that maximizes the likelihood of the training data when added to the vocabulary.
- Key Difference: This likelihood-based approach can lead to a vocabulary that is more optimized for the language model's training objective.
- Primary Application: WordPiece is famously used in BERT and its derivatives. Tokens often start with
##to indicate they are a subword continuation (e.g.,##ing).
SentencePiece
SentencePiece is a language-agnostic tokenization toolkit that implements subword units (using BPE or unigram language models) directly on raw text, treating whitespace as just another character.
- Key Advantage: It does not require pre-tokenization (splitting by whitespace/punctuation), making it highly effective for languages without clear word boundaries (e.g., Japanese, Chinese) or for processing code and noisy text.
- Widespread Use: It is the tokenizer of choice for models like T5 (Google) and LLaMA (Meta), providing robust performance across many languages.
Unigram Language Model Tokenization
The Unigram Language Model approach is a probabilistic method for subword tokenization. It starts with a large seed vocabulary (e.g., all words plus subwords) and iteratively prunes it down by removing the units that least affect the overall likelihood of the corpus.
- Key Mechanism: It evaluates the contribution of each subword unit, allowing for the creation of a vocabulary of a precise target size.
- Use Case: This method is particularly useful when a specific vocabulary size is a hard constraint. It's an option within the SentencePiece toolkit and is used in models like ALBERT.
Data Tokenization (General)
Data Tokenization is the overarching process of breaking down any raw data stream into discrete, processable units (tokens). While subword tokenization is specific to text, the concept generalizes across modalities.
- Cross-Modal Examples:
- Audio: Converting a waveform into frames or mel-spectrogram patches.
- Video: Dividing a video into frames or spatiotemporal patches.
- Code: Splitting source code into lexical tokens (keywords, operators, identifiers).
- Unified Principle: All tokenization aims to create a finite vocabulary of atomic units from which a model can learn patterns and generate coherent outputs.
Chunking Strategy
A Chunking Strategy is a systematic method for segmenting long, continuous data into coherent blocks that fit within a model's finite context window. It works in concert with tokenization.
- Relationship to Tokenization: First, text is tokenized into subwords. Then, a chunking strategy (e.g., sliding window, semantic boundary detection) groups these tokens into sequences of appropriate length.
- Critical for Long Context: Effective chunking is essential for processing long documents, audio recordings, or video with models like transformers, ensuring that related information stays together within a single processing window.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us