Q&A-22How does BPE subword tokenization decide what an LLM can see and produce, and what breaks when the tokenizer is mismatched to the model or the domain?
Answer
BPE is a greedy compression algorithm. Start from a base alphabet (256 bytes for GPT-2/Llama, characters for the original Sennrich formulation) count adjacent pairs over the training corpus, merge the most frequent pair, and repeat until you hit the target vocabulary size V. The ordered merge list is the tokenizer; encoding new text replays those merges. So V is one hyperparameter that fixes three things at once: the model's input alphabet, the size of the embedding matrix and output softmax, and the average tokens per word (fertility).
Trace on lowest: base chars l o w e s t. Corpus merges in order: (e,s)→es, (es,t)→est, (l,o)→lo, (lo,w)→low, (low,est)→lowest. Five merges turn six characters into one token. Rare words get fewer merges, so they stay split.
Variants differ in base alphabet and merge rule:
| scheme | base | merge rule | used by |
|---|---|---|---|
| byte-level BPE | 256 bytes | most frequent pair | GPT-2, GPT-3, Llama 3 |
| WordPiece | chars | pair maximizing training-data likelihood | BERT |
| Unigram LM | seed subwords | prune by likelihood loss | SentencePiece, T5 |
Byte-level BPE has no UNK: any UTF-8 string is representable, but unseen scripts fall back to 2–4 bytes per character. SentencePiece runs on raw text with ▁ marking spaces, so it is language-agnostic and reversible.
What V buys and costs, with real numbers. GPT-2 small: V=50,257, d_model=768, so the embedding matrix is 50,257×768 ≈ 38.6M parameters. About 31% of the 124M model. Llama 3: V=128,256, d_model=4096, so 525M parameters in embeddings, ~6.5% of the 8B model. Larger V shortens sequences (cheaper attention, more text per context window) but grows the embedding and LM head and gives rare tokens fewer gradient updates.
Rare words, numbers, code, non-English. A rare word is not OOV; it is split into fragments the model must compose. Numbers are the sharp edge: BPE merges digits by corpus frequency, so 1234 may be one token while 12345 is three, and the same number can tokenize differently depending on surrounding whitespace. A real cause of arithmetic errors. Code tokenizes well for common keywords, but long identifiers and indentation split unpredictably. Non-English fertility varies 2–5×: GPT-2's English-heavy vocab spends roughly 15 byte-tokens on こんにちは世界 where Llama 3's 128k vocab spends about 5. Same sentence, 3× the context and cost.
Mismatch. The tokenizer is not interchangeable: token IDs index specific rows of the embedding matrix. Loading a model with a different tokenizer: wrong vocab file, a fine-tune that resized embeddings without retraining them, a domain tokenizer swapped in: produces fluent-looking garbage because every ID maps to the wrong vector. Detect it by hashing the tokenizer vocab and comparing against the model card, and by checking fertility on a held-out domain sample before deployment. Domain mismatch without a hard error is subtler: a legal or genomic corpus tokenized by an English web tokenizer can double or triple token counts, blowing context budgets and cost. Fixing it means retraining the tokenizer and resizing plus retraining the embedding and LM head: a real training run, not a config change.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.