Inferensys

Glossary

Tokenization

Tokenization is the foundational NLP preprocessing step that segments a raw string of text into discrete units, or tokens, such as words or subwords, which serve as the numerical input for downstream machine learning models.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
FOUNDATIONAL NLP PREPROCESSING

What is Tokenization?

Tokenization is the foundational process of segmenting a raw string of text into discrete, meaningful units called tokens, which serve as the primary input for downstream natural language processing models.

Tokenization is the computational process of dissecting a continuous sequence of text into discrete units, or tokens. These tokens—which can be words, subwords, or individual characters—transform unstructured strings into a structured sequence that a machine learning model can digest. This step is mandatory because language models operate on numerical vectors, not raw text; the tokenizer's vocabulary defines the model's fundamental linguistic building blocks.

Modern architectures predominantly use subword tokenization algorithms like Byte-Pair Encoding (BPE) or WordPiece to balance vocabulary size against out-of-vocabulary issues. Unlike simple whitespace splitting, subword methods decompose rare words into frequent morphological fragments (e.g., 'tokenization' into 'token' and 'ization'), allowing the model to generalize morphological patterns. The output is a sequence of integer IDs mapped from the tokenizer's vocabulary, ready for the embedding layer.

CORE MECHANISMS

Key Features of Modern Tokenization

Modern tokenization has evolved beyond simple whitespace splitting into a critical preprocessing step that balances vocabulary size, out-of-vocabulary handling, and semantic granularity for downstream models.

01

Subword Segmentation

Modern tokenizers decompose text into subword units rather than whole words, striking a balance between character-level and word-level representations. This approach handles morphologically rich languages and rare terms by breaking them into known fragments. For example, the word 'unhappiness' might be segmented into 'un', 'happiness', or further into 'un', 'happi', 'ness'. This ensures the model never encounters a true out-of-vocabulary token, as any unknown word can be constructed from its constituent subword pieces.

50k-250k
Typical Vocabulary Size
03

WordPiece Tokenization

WordPiece, used by BERT and its derivatives, selects subword units based on a likelihood maximization objective rather than raw frequency. The algorithm evaluates all possible merges and chooses the one that maximizes the training data's likelihood under a unigram language model. This produces a vocabulary where each token contributes meaningfully to the probability of the sequence. Unlike BPE, WordPiece uses a special '##' prefix to denote tokens that are continuations of a word, such as 'play' and '##ing' for the word 'playing'.

30k
BERT Base Vocabulary
05

Special Tokens & Control Codes

Tokenizers inject reserved tokens into the vocabulary to control model behavior and structure input sequences. Common examples include:

  • [CLS]: A classification token prepended to sequences in BERT, whose final hidden state represents the aggregate sequence meaning.
  • [SEP]: A separator token used to delineate sentence boundaries in pair-input tasks.
  • [PAD]: A padding token ensuring all sequences in a batch have uniform length.
  • [MASK]: A placeholder token for masked language modeling objectives.
  • <|endoftext|>: A document boundary token used in GPT models to signal the end of a text segment.
06

Tokenization Mismatch & Glitch Tokens

A critical failure mode occurs when a model's tokenizer is mismatched from its training data distribution, leading to glitch tokens or degraded performance. For example, a tokenizer trained on English text may fragment a programming language's syntax into nonsensical subword units, destroying structural meaning. Similarly, anomalous token sequences can trigger undefined model behavior. SolidGoldMagikarp is a notorious example of a glitch token in GPT-2's vocabulary that caused nonsensical completions. Robust systems must ensure strict tokenizer-model alignment and monitor for anomalous token activations.

TOKENIZATION

Frequently Asked Questions

Clear, technical answers to the most common questions about the foundational NLP process of breaking text into discrete units for machine learning models.

Tokenization is the foundational natural language processing (NLP) step of segmenting a raw string of text into a sequence of discrete units called tokens. These tokens—which can be words, subwords, or individual characters—serve as the atomic input for downstream models. The process works by applying a specific algorithm, such as Byte-Pair Encoding (BPE) or WordPiece, to split text based on a pre-built vocabulary. For example, the sentence "The quick brown fox" might be tokenized into ["The", "quick", "brown", "fox"] in a word-level scheme, or ["The", "quick", "brown", "fox"] with a subword algorithm splitting an unknown word like "unhappiness" into ["un", "happiness"]. This conversion from human-readable text to machine-readable integer IDs is non-negotiable; a language model cannot process raw text directly. The tokenizer's vocabulary size and splitting rules directly control the model's ability to handle out-of-vocabulary (OOV) words and the overall sequence length, making it a critical architectural decision.

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.