Exercises — Vocabulary size tradeoffs
Two facts we reuse everywhere (both derived in the parent note):
Level 1 — Recognition
Exercise 1.1
A tokenizer uses tokens and embedding dimension . How many parameters does the embedding table hold?
Recall Solution
WHAT we do: Apply directly — no sequence, no attention involved, just the shelf of vectors. WHY only this: The embedding table cost never depends on or on how long documents are. It is one vector per token slot, full stop.
Exercise 1.2
Text is 1000 characters long. A byte-level tokenizer averages 1 token per character; a BPE tokenizer averages 4 characters per token. Give the sequence length for each.
Recall Solution
Byte-level: one token per character, so . BPE: each token swallows 4 characters, so WHAT this shows: a bigger vocabulary (BPE has thousands of merges) produces shorter sequences. That is the whole tension of this chapter in one line.
Exercise 1.3
True or false: "Doubling the sequence length doubles the self-attention cost."
Recall Solution
False. Attention cost . Replace with : Doubling quadruples attention cost. The square is the reason short sequences matter so much.
Level 2 — Application
Exercise 2.1
Model A: , . Model B: , . How many extra embedding parameters does B carry over A?
Recall Solution
WHAT we do: the shared factors out, so subtract the vocab sizes first. WHY factor out: it exposes the clean rule — extra embedding cost = (extra tokens) . This is the left-hand side of the parent's breakeven inequality.
Exercise 2.2
A 1000-character document. Vocab A gives average token length ; vocab B (larger) gives . By what factor does the attention cost drop when moving A → B?
Recall Solution
Sequence lengths: Attention cost , so the ratio is WHAT it means: the bigger vocab makes attention about cheaper — a 36% cut — even though tokens only got 25% longer. The square amplifies the gain.
Exercise 2.3
Batch size , layers , sequence , scores stored in float16 (2 bytes). Compute the attention score matrix memory (scalars only, one per layer per batch item).
Recall Solution
WHAT we count: each layer produces an table of scalar scores. Step by step: WHY no : the scores are single numbers, not -vectors. Multiplying by here is the classic blunder (see below).
Level 3 — Analysis
Exercise 3.1
Using the breakeven inequality from the parent note, where is the number of batches processed. Cancel and solve for the minimum at which the large vocab wins, given , , , .
Recall Solution
WHAT we do: appears on both sides, so it cancels — the winner does not depend on embedding dimension here. Inequality becomes WHAT it means: means the large vocab wins after the very first batch (). The one-time parameter overhead (96000 units) is tiny next to the per-batch activation savings (2.04M units per batch). See the figure — the amber savings line overtakes the flat white overhead almost immediately.
Exercise 3.2
For Chinese text: character-level gives , ; subword gives , . Embedding dim . Compare (a) embedding tables and (b) attention costs (as ratios).
Recall Solution
(a) Embedding table ratio: (b) Attention cost ratio (character ÷ subword): The tradeoff in words: subword pays more in fixed embedding memory but character-level pays more in per-input attention compute. For a model that processes lots of text, the recurring cost usually dominates → subword wins for Chinese, matching the parent note.
Exercise 3.3
The parent claims (average characters per token) is sublinear in : "doubling raises by 20–30%." If at and grows 25% per doubling, estimate at .
Recall Solution
WHAT we do: is two doublings (, then ). WHY sublinear matters: grew but only grew . You pay linearly for a bigger table but only get diminishing sequence shortening — the core reason vocabularies plateau around 32K–128K instead of ballooning.
Level 4 — Synthesis
Exercise 4.1
Design decision. A model budget allows 65M embedding parameters. With , what is the largest vocabulary you can afford? Then state one downside of using that whole budget on vocabulary.
Recall Solution
WHAT we do: invert . Downside: at 127K tokens, many slots hold rare tokens seen only a handful of times in training. Their embedding vectors get few gradient updates, stay noisy, and generalize poorly — the exact failure mode the parent's final [!mistake] warns about. A smaller, well-trained vocab can outperform a bloated one.
Exercise 4.2
Combine both cost laws into a single total memory expression for one batch, then decide which term to attack first when is large. Use , , , , , float16.
Recall Solution
Embedding (fixed): (bytes for the stored table in fp16). Attention scores (per batch): . Which term to attack: the attention term (805 MB) dwarfs the embedding term (76.8 MB) by more than , and it grows with . Attack sequence length first — via a bigger/smarter vocab, or the memory tricks in 5.1.03-Memory-optimization-techniques — before shrinking the embedding table.
Level 5 — Mastery
Exercise 5.1
Full end-to-end verdict. A multilingual system (see 6.2.01-Multilingual-models) chooses between:
- Option S: , average .
- Option L: , average .
Shared: , , , float16, and the team runs 50,000 training batches. Compute (a) extra embedding memory of L, (b) per-batch attention savings of L, (c) total savings across all batches, and (d) the final verdict.
Recall Solution
(a) Extra embedding memory (one-time, fp16 bytes): (b) Per-batch attention-score savings: (c) The per-batch saving already exceeds the one-time overhead on the first batch (1792 MB > 446 MB). Across 50,000 batches the recurring activation saving is the dominant story; the 446 MB parameter cost is paid once and forgotten. (d) Verdict: Option L wins on resources — provided (i) the 250K vocabulary genuinely reduces sequences to for your languages, and (ii) rare-token embeddings stay trainable. The recurring savings crush the one-time parameter overhead.
Exercise 5.2
Conceptual mastery — connect three chapters. Explain in 3 sentences why increasing helps 4.3.01-Transformer-attention-complexity but pressures 4.5.02-Embedding-layer-design, and how 4.2.02-Byte-pair-encoding positions the vocab in the middle.
Recall Solution
A larger lets each token absorb more characters, shrinking , and since attention scales as this shortening delivers quadratic compute relief. But that same larger inflates the embedding table linearly and dilutes training signal across many rare slots, straining embedding-layer design. BPE is the compromise engine: it greedily merges only the frequent substrings, so it buys most of the sequence-shortening benefit while keeping modest enough that every token still gets enough training exposure.
Recall Self-test: can you answer without looking?
Doubling multiplies attention cost by ::: 4 (because cost ) Embedding table parameter count formula ::: Why the attention score matrix has no factor ::: each score is a dot product — two -vectors collapse to one scalar The core reason large vocabularies plateau (don't grow forever) ::: grows sublinearly in , and rare-token embeddings train poorly