Dive into Deep Learning · §20.6
Tokenizers at Scale
unigram model · premium · bits per byte · vocabulary size · extension · shards
The problem
Tokens for 488 Belebele passages, relative to English (the premium):
| language | GPT-2 | Qwen3 | Qwen3.5 | o200k_base |
|---|---|---|---|---|
| German | 2.14 | 1.56 | 1.31 | 1.31 |
| Hindi | 7.43 | 4.45 | 2.01 | 1.56 |
| Thai | 9.00 | 2.57 | 1.21 | 1.95 |
| Chinese | 3.20 | 1.06 | 0.95 | 1.28 |
A premium of 2 means twice the compute and half the context for the same content.
Goals
Running example: Belebele (488 parallel passages, 10 languages), a FineWeb-2 slice of 4.5 MB per language, Qwen3-0.6B-Base.
Where this fits
| step | section |
|---|---|
| get text | Document Extraction from Web Crawls |
| choose text | Quality Filtering |
| Deduplication and Decontamination | |
| mix text | Data Mixtures |
| make text | Synthetic and Rephrased Data |
| cut text | Tokenizers at Scale |
The unigram model
Pieces x with probabilities p(x); a word w has segmentations \mathbf{s} = (x_1, \ldots, x_k):
P(\mathbf{s}) = \prod_{m} p(x_m), \qquad P(w) = \sum_{\mathbf{s}} P(\mathbf{s})
Lattice: positions 0..n are nodes, an edge i \to j for every piece w_{i:j}; a path is a segmentation.
path for ▁sports |
log-probability |
|---|---|
▁s · port · s |
−18.5 |
▁sp · ort · s |
−19.3 |
Training
f_j = \sum_{i<j} f_i\, p(w_{i:j}), \qquad g_i = \sum_{j>i} p(w_{i:j})\, g_j, \qquad P(w) = f_n
N_x = \sum_w c_w \sum_{w_{i:j} = x} \frac{f_i\, p(w_{i:j})\, g_j}{P(w)}, \qquad p_{\textrm{new}}(x) = \frac{N_x}{\sum_y N_y}
Checks
| check | result |
|---|---|
| forward-backward vs all segmentations (200 strings) | equal to 10^{-14} |
| EM log-likelihood, 6 rounds | rises every round |
| our Viterbi on SentencePiece’s pieces, 1,000 lines | 1,000 identical |
| our trainer vs SentencePiece, 4,096 pieces | 75% shared |
SentencePiece’s default nmt_nfkc rewrites text: x² → x2, fi → fi, ABC → ABC. Normalize to NFC yourself and pass identity.
Subword regularization
Draw \mathbf{s} with probability \propto P(\mathbf{s})^{\beta}: \beta = 0 uniform, \beta \to \infty Viterbi.
| segmentation (a FineWeb-Edu line, ODC-By) | |
|---|---|
| Viterbi | ▁computer ▁scientists ▁have ▁release d ▁over ▁three |
| sample | ▁ com p u ter ▁scientists ▁have ▁ r e l e a s ed ▁ over ▁three |
| sample | ▁computer ▁scientists ▁have ▁release d ▁over ▁t h r e e |
Train on a fresh sample each time a sentence is read: the model must recognize a word however it is cut.
Check
▁s|port|s has log-probability −18.5, ▁sp|ort|s −19.3. Viterbi always picks the first. At \beta = 0.2, how much more often is it sampled than the second?
Answer. The ratio of probabilities is e^{0.8} \approx 2.2; raised to \beta = 0.2 it is e^{0.16} \approx 1.17. Nearly a coin toss, which is why samples at 0.2 look so ragged.
Language weighting
Byte-level BPE, 16,384 pieces, languages weighted \propto n_i^{\alpha} by FineWeb-2 document count n_i (English: German’s count):
| \alpha = 1 | \alpha = 0.3 | \alpha = 0 | |
|---|---|---|---|
| Devanagari pieces | 93 | 399 | 574 |
| premium Hindi | 2.42 | 1.91 | 1.77 |
| premium Swahili | 1.88 | 1.34 | 1.04 |
| premium Russian | 1.30 | 1.38 | 1.42 |
A fixed vocabulary is divided among languages. The split rule \p{L}+ also cuts हिन्दी into ह·िन·्द·ी before any merge.
The fair measure
L: loss per token (nats), \bar b: bytes per token.
\textrm{bpb} = \frac{L}{\bar b \ln 2}
Small LMs on the same 45 MB, 100 held-out documents:
| tokenizer | perplexity | bytes/token | bpb |
|---|---|---|---|
| SP unigram | 214 | 3.99 | 1.94 |
| SP BPE | 218 | 4.01 | 1.94 |
| HF byte-level BPE | 243 | 4.00 | 1.98 |
Results at T1 scale: 7.3M-parameter LMs, one epoch over 45 MB
| result | |
|---|---|
| unigram − BPE, average of 10 languages | +0.002 bpb [0.000, 0.004] |
| SentencePiece − byte-level BPE | about −0.02 bpb |
| largest unigram/BPE effect, one language | 3.6% of its bpb (Thai) |
| premium spread across languages, one tokenizer | 1.5× to 1.8× |
| Hindi premium, byte-level vs SentencePiece BPE | 1.77 vs 1.32 |
At this scale a tokenizer decides how many tokens a language costs far more than how well it is predicted.
Vocabulary size
A: non-embedding parameters, d: width, tied embeddings.
C(V) = \frac{6\,(A + Vd)}{\bar b(V)} \ \text{FLOP per byte}
\frac{d \log C}{d \log V} = \underbrace{\frac{Vd}{A + Vd}}_{s(V)} - \underbrace{\frac{d \log \bar b}{d \log V}}_{\varepsilon(V)} = 0 \;\Rightarrow\; \boxed{V^* = \frac{\varepsilon}{1-\varepsilon}\cdot\frac{A}{d}}
Larger models and multilingual text (larger \varepsilon) want larger vocabularies.
Measured on held-out text
One BPE of 65,536 pieces cut to its first V ids; s(V) at d = 1{,}024, A \approx 302M.
| V | bytes/token, English | \varepsilon, English | \varepsilon, 10 languages | s(V) |
|---|---|---|---|---|
| 4,096 | 3.30 | 0.19 | 0.22 | 0.014 |
| 16,384 | 4.09 | 0.11 | 0.18 | 0.053 |
| 32,768 | 4.36 | 0.07 | 0.16 | 0.100 |
| 65,536 | 4.53 | 0.04 | 0.14 | 0.182 |
Released small models put 26% to 34% of their parameters in the vocabulary matrix; one tokenizer serves a whole model family.
Check
A 24-block model with d = 1{,}024, so A = 12 \cdot 24 \cdot 1024^2, and a constant elasticity \varepsilon = 0.1. What is V^*?
Answer. A/d = 294{,}912, and V^* = \tfrac{0.1}{0.9} \cdot 294{,}912 = 32{,}768. The minimum is flat: half or twice that size costs only a few percent more.
Numbers and code
| tokenizer | 12345678 |
digits/token | code tokens/line |
|---|---|---|---|
| GPT-2 | 123·45·678 | 2.12 | 19.25 |
| o200k_base | 123·456·78 | 2.07 | 10.63 |
| Qwen3 | 1·2·3·4·5·6·7·8 | 1.00 | 10.67 |
| reference | 1·2·3·4·5·6·7·8 | 1.00 | 17.64 |
The reference tokenizer’s longest whitespace piece is two spaces: it reads Python 65% more expensively than Qwen3’s.
Extending a vocabulary
| tokenizer | Hindi bytes per token |
|---|---|
| Qwen3 | 2.79 |
| Qwen3 + 4,096 Hindi pieces | 5.28 |
| Qwen3.5 | 6.13 |
New rows of the shared embedding matrix:
No ASCII in new pieces: 1,000 English lines keep their ids.
Step 0, before training
| arm | new-token mass, Hindi | Hindi bpb | English bpb |
|---|---|---|---|
| control (no new pieces) | 0.62 | 0.8225 | |
| random | 0.2% | 2.39 | 0.8225 |
| mean | 0.01% | 2.89 | 0.8225 |
| decomp | 73% | 1.48 | 0.8225 |
Identical mean rows share one logit: no hijacked softmax, but no mass for the new tokens either. Decomp’s logit is the mean of its pieces’ logits.
T1 scale: Qwen3-0.6B-Base, 150 steps × 8 × 1,024 tokens
Phase 1 (50 steps): new rows only, lr 3\times10^{-3}. Phase 2 (100 steps): all, lr 10^{-5}.
| Hindi bpb | step 0 | 50 | 150 |
|---|---|---|---|
| control | 0.63 | 0.63 | 0.63 |
| random | 2.38 | 1.25 | 1.03 |
| mean | 2.88 | 1.03 | 0.92 |
| decomp | 1.49 | 0.96 | 0.85 |
At 1.2M tokens the extension halves Hindi’s tokens but is still 0.2 bpb behind. The body rate is set so that the control, with nothing new to learn, stays flat.
One-way doors
| field | value |
|---|---|
| tokenizer | byte-level BPE, 32,768 pieces, NFC, single digits |
| special ids | 0 end of text (after every document), 1 pad, 2-3 chat, 4-31 reserved; no BOS |
.bin / .idx |
little-endian ids (uint16 up to 65,536) / uint32 offsets; a document’s range includes its EOS |
| check | retraining on 80,000 documents reproduces the SHA-256 |
| data | 4.0B FineWeb-Edu tokens in 40 shards; validation 2^{23} tokens |
2.5% of validation documents share a 50-token span with a training shard: boilerplate.
Check
A web page about tokenizers contains the end-of-text string, <|endoftext|>, as text. What goes wrong if the tokenizer encodes it as id 0?
Answer. The document ends in the middle, the shard’s EOS invariant breaks, and the model learns that the string ends documents. The reference tokenizer encodes it as ordinary tokens, all with ids ≥ 32.
Recap
Next: streaming these shards into pretraining.