Inferensys

Glossary

Tokenization

Tokenization is the preprocessing step that segments raw text into atomic units, such as words or subwords, which are then mapped to unique integer IDs for ingestion by a machine learning model.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
TEXT PREPROCESSING

What is Tokenization?

Tokenization is the foundational preprocessing step that segments raw text into atomic units, such as words or subwords, which can be mapped to integer IDs for model ingestion.

Tokenization is the algorithmic process of segmenting a raw text string into discrete atomic units called tokens. These tokens serve as the fundamental input vocabulary for language models. The process converts unstructured text into a structured sequence of integer IDs, bridging the gap between human-readable language and the numerical tensor operations required by neural networks. Common strategies include word-level, character-level, and subword tokenization, with the latter balancing vocabulary size against the ability to handle rare or unseen words.

Modern architectures predominantly rely on subword algorithms like Byte-Pair Encoding (BPE) or WordPiece, which iteratively merge frequent character pairs to construct a vocabulary of meaningful fragments. This approach ensures that unknown terms are decomposed into known constituent parts, eliminating out-of-vocabulary errors. The tokenizer's vocabulary is a critical artifact of the model; mismatching the tokenizer during fine-tuning or inference will corrupt the semantic representation, making the model's outputs incoherent.

FOUNDATIONAL PREPROCESSING

Core Characteristics of Tokenization

Tokenization is the critical preprocessing step that segments raw text into atomic units, such as words or subwords, that can be mapped to integer IDs for model ingestion. The chosen strategy directly impacts vocabulary size, handling of out-of-vocabulary words, and a model's ability to generalize across languages and domains.

01

Subword Segmentation

Modern tokenizers like Byte-Pair Encoding (BPE) and WordPiece break text into subword units. This balances vocabulary size with coverage, allowing models to represent rare or unseen words by composing known fragments.

  • Mechanism: Iteratively merges the most frequent pair of characters or bytes.
  • Benefit: Eliminates the concept of a true out-of-vocabulary token for many models.
  • Example: The word 'unhappiness' might be segmented into ['un', 'happiness'] or ['un', 'happi', 'ness'].
02

Vocabulary Construction

The tokenizer builds a fixed-size vocabulary of unique tokens from the training corpus. Each token is assigned a unique integer ID.

  • Size Trade-off: A larger vocabulary captures more whole words but increases model embedding parameters. A smaller vocabulary relies on more subword fragments, increasing sequence length.
  • Special Tokens: Includes control tokens like [CLS], [SEP], [PAD], and [MASK] to structure input for specific model architectures.
03

Encoding and Decoding

Tokenization is a two-way process essential for text generation.

  • Encoding: Converts a raw string into a sequence of integer IDs ("Hello world" -> [15496, 995]).
  • Decoding: Converts a sequence of integer IDs back into a human-readable string.
  • Lossless Requirement: A robust tokenizer must ensure that decode(encode(text)) returns the original text, preserving whitespace and casing.
04

Context Window Impact

The tokenizer's efficiency directly affects a model's context window utilization. A tokenizer that produces more tokens for the same text consumes the model's limited attention span faster.

  • Multilingual Inefficiency: Languages with non-Latin scripts often require 2-4x more tokens than English for the same semantic content.
  • Numerical Fragmentation: Poorly designed tokenizers can split a single number like 3.14159 into many separate digit tokens, wasting context.
05

Normalization and Pre-tokenization

Before segmentation, text undergoes crucial preprocessing steps.

  • Normalization: Standardizes text by applying rules like lowercasing, Unicode normalization (NFKC), and stripping accents.
  • Pre-tokenization: Splits text into initial word-level chunks using whitespace and punctuation rules. This step prevents subword merges from crossing word boundaries, a key difference between BPE and pure character-level models.
06

Domain-Specific Tokenization

General-purpose tokenizers often fail on specialized corpora. Domain adaptation of a tokenizer involves training a new vocabulary on target data.

  • Code: A tokenizer trained on natural language will fragment common code syntax like getElementById into many subwords. A code-specific tokenizer preserves these as single tokens.
  • Biomedical Text: Specialized tokenizers preserve chemical compound names and genetic sequences, preventing fragmentation of critical domain entities.
SUBWORD SEGMENTATION STRATEGIES

Tokenization Algorithm Comparison

Comparative analysis of the dominant subword tokenization algorithms used in modern embedding models and large language models, evaluated across vocabulary efficiency, handling of rare words, and language agnosticism.

FeatureByte-Pair Encoding (BPE)WordPieceUnigram Language Model

Core Mechanism

Iteratively merges the most frequent pair of bytes or characters

Merges pairs based on the highest increase in language model likelihood

Starts with a large vocabulary and prunes tokens with the lowest probability contribution

Training Objective

Frequency maximization

Likelihood maximization

Data likelihood maximization via loss minimization

Vocabulary Initialization

Base characters or bytes

Base characters

Heavily over-parameterized seed vocabulary

Handles Rare Words

Language Agnosticism

Reversible to Original Text

Primary Adopters

GPT models, RoBERTa

BERT, DistilBERT

T5, XLNet, SentencePiece

Segmentation Granularity

Tends toward larger subwords

Tends toward smaller subwords

Probabilistic; offers multiple segmentation candidates

TOKENIZATION

Frequently Asked Questions

Clear, technical answers to the most common questions about the preprocessing step that converts raw text into the integer IDs a model can ingest.

Tokenization is the foundational preprocessing step that segments a raw text string into a sequence of atomic units called tokens, which are then mapped to unique integer IDs from a predefined vocabulary for model ingestion. The process works by applying a trained tokenizer algorithm—such as Byte-Pair Encoding (BPE) or WordPiece—that scans the input text and splits it into subword units. For example, the word "unbelievably" might be segmented into ["un", "believ", "ably"]. These tokens are then looked up in a vocabulary file to produce a list of integers, which are converted into dense vectors by an embedding layer. This mechanism allows models to handle an open vocabulary, gracefully managing rare or unseen words by decomposing them into known subword fragments rather than resorting to a generic <UNK> token.

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.