4.2.1Tokenization & Language Modeling

Tokenization fundamentals

3,257 words15 min readdifficulty · medium2 backlinks

What is a Token?

Why not just use words?

  1. Rare words problem: "antidisestablishmentarianism" appears once in training data → model never learns it properly
  2. Out-of-vocabulary (OOV): New words ("COVID-19") didn't exist when model trained
  3. Morphology: "run", "running", "ran" are treated as completely separate → no shared understanding
  4. Vocabulary explosion: English has ~170K words, but infinite variations with proper nouns, typos, emoji

Why not just use characters?

  1. Sequence length explosion: "Hello" = 5 tokens vs1 word token → 5× longer sequences
  2. Computational cost: Transformer complexity is O(n²) in sequence length
  3. Long-range dependencies: Harder for model to connect "H" + "e" + "l" + "o" into meaning

The Tokenization Process

Step 1: Normalization

What happens: Clean and standardize text before splitting

Original: "  Hello,  WORLD! 🌍 "
Normalized: "hello, world! 🌍"

Common operations:

  • Lowercase conversion (optional)
  • Unicode normalization (NFC, NFD, NFKC, NFKD)
  • Whitespace triming
  • Accent removal (optional)

Why this matters: "café" vs "cafe" vs "café" (different unicode) should ideally map to same tokens.

Step 2: Pre-tokenization

What happens: Split text into "words" that won't be split further

Input: "Don't stop learning!"
Pre-tokenized: ["Don", "'", "t", "stop", "learning", "!"]

Common rules:

  • Split on whitespace
  • Separate punctuation
  • Keep contractions together OR split them

Why needed: Gives tokenizer algorithm sensible boundaries to work within.

Step 3: Token Splitting (The Core Algorithm)

This is where subword algorithms like BPE, WordPiece, or Unigram operate. Let's derive Byte Pair Encoding (BPE) from first principles:

Step 4: Numericalization

What happens: Map each token string to a unique integer ID

Vocabulary: {"<pad>": 0, "<unk>": 1, "hello": 100, "world": 250 ...}
Tokens: ["hello", "world"]
Token IDs: [100, 250]

This is a simple dictionary lookup. Special tokens:

  • <pad> (ID 0): Padding for batching
  • <unk> (ID 1): Unknown/rare tokens (fallback)
  • <bos> / <eos>: Beginning/end of sequence
  • <mask>: For masked language modeling

Encoding and Decoding

Key Properties of Good Tokenization

Figure — Tokenization fundamentals

Common Tokenization Algorithms

Algorithm Strategy Used By
BPE (Byte-Pair Encoding) Merge most frequent pairs iteratively GPT-2, GPT-3, RoBERTa
WordPiece Similar to BPE, but maximizes likelihood BERT, DistilBERT
Unigram Start with large vocab, prune low-probability tokens T5, ALBERT
SentencePiece Treats text as raw byte stream, language-agnostic T5, XLNet, many multilingual models

Why different algorithms?

  • BPE: Simple, greedy, deterministic
  • WordPiece: More principled (likelihood-based), slightly better for BERT-style models
  • Unigram: Allows probabilistic tokenization (multiple valid splits), used in Japanese/Chinese
  • SentencePiece: Handles languages without clear word boundaries, no pre-tokenization needed
Recall Explain to a 12-Year-Old

Imagine you're teaching a robot to read, but the robot only understands numbers, not letters or words.

The problem: You show the robot the sentence "I love pizza," but it just sees squiggly shapes. It needs a translator.

Bad solution1: Break everything into letters: I → 9, (space) → 0, l → 12, o → 15, v → 22, e → 5... Now "love" is [12, 15, 22, 5]. The sentence becomes a list of 13 numbers! That's really long, and the robot has to remember that "l + o + v + e" means something about feelings—super hard!

Bad solution 2: Make every possible word a number: "I" → 1, "love" → 2, "pizza" → 3. Great! Short list: [1, 2, 3]. But what if tomorrow you say "I adore pizza"? The robot doesn't know "adore" because we never taught it that number. And English has like 170,000 words—too many numbers to remember!

Smart solution (tokenization): Break words into common PIECES. Like Lego blocks!

  • Common words stay together: "I" → 1, "love" → 2, "pizza" → 3
  • Rare words get broken into pieces: "adore" → "ad" (17) + "ore" (18)
  • Now if the robot learned "ad" from "advent" and "ore" from "explore," it can understand "adore" even though it's new!

How we choose pieces: We look at lots of sentences and find pairs of letters that appear together ALL THE TIME. Like "th," "ing," "ed." Those become blocks. We keep combining until we have about 50,000 different blocks—enough to build any word, but not too many to remember.

Magic moment: When the robot reads "I'm running to eat pizza," it knows "running" = "run" + "ing" and "eating" = "eat" + "ing," so it understands the pattern even if it's only seen "running" before, not "eating"!

Connections

  • 4.2.02-BPE-algorithm - Deep dive into Byte Pair Encoding
  • 4.2.03-WordPiece-and-SentencePiece - Alternative tokenization algorithms
  • 4.3.01-Word-embedings - Tokens → vectors (next step)
  • 4.1.05-Vocabulary-and-OV - Why vocabulary management matters
  • 5.1.02-Transformer-architecture - Where tokenized sequences are processed
  • 4.2.08-Multilingual-tokenization - Handling non-English text
  • 3.4.03-Sequence-length-and-padding - Dealing with variable-length tokenized sequences

#flashcards/ai-ml

What is a token in NLP? :: The basic unit of text that a language model processes. Can be a complete word, subword, character, or byte. The model's vocabulary is the set of all possible tokens.

Why use subword tokenization instead of word-level?
(1) Handles rare/unseen words via composition (2) Reduces vocabulary size (3) Captures morphology (prefixes/suffixes) (4) No out-of-vocabulary problem (5) More efficient than character-level

What are the 4 steps of tokenization? :: (1) Normalization (clean text) (2) Pre-tokenization (split into words) (3) Token splitting (apply subword algorithm like BPE) (4) Numericalization (map to integer IDs)

What is the core idea of BPE?
Start with character vocabulary. Iteratively find the most frequent consecutive pair and merge it into a new token. Repeat until target vocabulary size. Frequent patterns become single tokens; rare words are character combinations.
What is the typical vocabulary size for modern LMs?
30K-100K tokens. Balance between coverage (fewer <unk> tokens) and efficiency (smaller embedding matrix, faster softmax).
Why does GPT-2 use the Ġ character?
Marks tokens that had a space before them. Preserves whitespace information for perfect reversibility when decoding token IDs back to text.
What is token fertility?
Average number of tokens per word. Typical BPE fertility is 1.3-1.5. Lower = more efficient tokenization (shorter sequences for same text).
What happens if vocabulary is too large?
(1) Huge embedding matrix (more parameters) (2) Rare tokens don't train well (sparse updates) (3) Expensive softmax over large vocabulary (4) Slower training and inference
What is the difference between BPE and WordPiece?
BPE merges most frequent pairs (gredy). WordPiece merges pairs that maximize likelihood of training data (more principled). Both achieve similar results; WordPiece used in BERT, BPE in GPT models.
Why is tokenization important for model performance?
(1) Token boundaries limit what can be learned (2) Poor tokenization → long sequences → higher compute cost (3) Many <unk> → model loses information (4) Domain mismatch hurts (e.g., code vs natural language) (5) Affects multilingual capability

Concept Map

motivates

produces

collected into

causes OOV and explosion

causes long sequences

balances tradeoff

runs

step 1

step 2

step 3

uses

Neural nets need numbers

Tokenization

Token

Vocabulary 30K-100K

Word-level

Char-level

Subword tokens

Tokenization Pipeline

Normalization

Pre-tokenization

Token Splitting

BPE / WordPiece / Unigram

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho yaar, tokenization ka core idea bahut simple hai — neural networks sirf numbers samajhte hain, text nahi. Toh humein raw text ko chote-chote units mein todna padta hai jinhe "tokens" kehte hain, aur phir un tokens ko numbers mein map karte hain. Ye ek bridge ki tarah hai text aur model ke beech. Ab yahan ek balance ka game hai: agar hum pura word ko ek token banate hain toh vocabulary explode ho jati hai (english mein lakhon words, plus typos aur naye words jaise "COVID-19" jo training ke time exist hi nahi karte). Aur agar hum har character ko alag token banate hain toh sequence bahut lambi ho jati hai — "Hello" 5 tokens ban jayega instead of 1 — aur transformer ka cost O(n²) hota hai, matlab lamba sequence = zyada computation.

Isliye smart solution hai subword tokenization, jismein sabse popular algorithm hai BPE (Byte Pair Encoding). Iska intuition ekdum natural hai: shuru mein sab kuch character-level pe hota hai, phir hum baar-baar sabse frequent consecutive pair dhundhte hain aur unhe merge karke ek naya token bana dete hain. Ye process repeat hota rehta hai jab tak humein desired vocabulary size na mil jaye. Iska magic ye hai ki common words automatically single token ban jaate hain (kyunki wo frequent hote hain), jabki rare words character combinations ki tarah rehte hain — isse OOV (out-of-vocabulary) problem solve ho jati hai. Morphology bhi capture hoti hai, jaise "run", "running", "ran" ka common part share ho jata hai.

Ye topic tumhare liye important kyun hai? Kyunki har modern LLM — ChatGPT ho ya koi bhi — pehle tumhara input tokenize karta hai. Tokenization pipeline (normalization → pre-tokenization → splitting → numericalization) samajhna zaroori hai kyunki yahi decide karta hai ki model kitna efficiently text ko process karega aur naye words ke saath kaise deal karega. Agar tum future mein NLP ya AI mein kaam karna chahte ho, toh ye foundation hai — iske bina baaki sab language modeling concepts adhure lagenge.

Go deeper — visual, from zero

Test yourself — Tokenization & Language Modeling

Connections