Topic 02 of 62 · Text in, prediction out
Vocabulary size and tokenizer fertility
EasyWhat are the systems and modeling trade-offs of increasing vocabulary size (say from 32K to 128K+)?
1 · Start with the intuition
Making the vocabulary larger packs more characters into each token (lowering **fertility**, the average number of tokens per word). Shorter token sequences mean faster prefill, fewer decode steps to generate the same text, and more real content fitting inside a fixed context window—especially for code and non-English languages. The price you pay is in memory and tail-token sparsity: the input embedding and output LM head matrices grow as `V × d`, the output logit tensor `[B, T, V]` becomes a massive activation in training, and rare tokens in a huge vocabulary get fewer gradient updates.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Measure compression (fertility)
Compute tokens-per-word or bytes-per-token across every target language and code domain.
Next, we will look at: Budget embedding + LM head weights. Move on when this operation makes sense.
Visualizing the behavior
Parameters in one V × d matrix at d = 4,096
Derived3 · The governing equation
Embedding + LM head params = V · d (tied) or 2 · V · d (untied) Logit activation memory = B · T · V · bytes_per_element Sequence length T = Raw_UTF8_bytes / (mean_bytes_per_token)
Notation: V is vocabulary size and d is model hidden width. Fertility is defined as tokens per word (lower is more compressed); its inverse is words per token. Bytes per token is a separate measure of the same compression (higher is more compressed), not the inverse of fertility.
Why did frontier models move from V = 32,000 (Llama 1/2) to V = 128,256 (Llama 3) or 151,936 (Qwen)? At 7B–70B scale, an extra ~400M embedding parameters is a small fraction of total parameters, while compressing sequences by 15–20% (and 30–50% on multilingual/code text) directly speeds up both training and inference! For a tiny 300M on-device model, however, an untied 128K vocabulary would consume more parameters than all the Transformer layers combined—unless embeddings are tied or shrunk.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Assuming a larger vocabulary only adds static parameter memory—in practice, the `[B, T, V]` output logit tensor and its gradient during training are often what cause OOMs when scaling V.
In practice · implementation & engineering rule of thumb
At large vocabularies (V ≥ 128K), materializing the full [B, T, V] FP32 logit tensor can take 16+ GB by itself. Use chunked cross-entropy or fused linear-cross-entropy (e.g., Liger-Kernel) so full logits are never materialized at once.
# Memory-saving chunked cross-entropy over the sequence/batch dimension:
def chunked_ce(hidden, lm_head, targets, chunk_size=1024):
flat_h, flat_y = hidden.reshape(-1, hidden.size(-1)), targets.reshape(-1)
losses = [
F.cross_entropy(lm_head(flat_h[i:i+chunk_size]).float(), flat_y[i:i+chunk_size], reduction="sum")
for i in range(0, flat_h.size(0), chunk_size)
]
return torch.stack(losses).sum() / (flat_y != -100).sum()Go one level deeper
Follow-ups that often come after a correct first answer.
- Swapping or extending a tokenizer
- Adding new domain tokens to a pretrained model requires resizing the embedding and LM head matrices. Initialize each new token’s row as the mean of the embeddings of the existing subwords that spell it so the initial loss does not spike.
- Compare models across tokenizers in bits per byte
- Per-token loss rewards tokenizers that split text into more pieces, because each tiny piece is easier to predict. Normalizing total negative log2 likelihood by raw UTF-8 byte count (bits per byte) makes evaluations tokenizer-agnostic.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy does tying the input embedding and output LM head weights help a 500M model much more than a 70B model?
Show follow-up reasoning
At d = 1,536 and V = 128,000, one V × d matrix is ~197M parameters—nearly 40% of a 500M model’s entire parameter budget, so sharing one matrix between input and output saves huge memory. In a 70B model, a single V × d matrix is ~1% of total parameters, and untying them lets input and output representations specialize.