Skip to content
mentoor

Topic 3 of 6 · 5 min read

Choosing vocabulary size trade-offs

Goal: Compare two vocabulary sizes for a stated corpus and parameter budget and justify which one to train.

Vocabulary size is a trade: a bigger vocabulary turns the same text into fewer tokens, but every extra token adds a row to the embedding table, a column to the output layer, and a token the model may rarely see. There is no best number, only a number that fits your data and budget.

Check yourself · 1 of 3

What is the main benefit of a larger BPE vocabulary?

Choose one answer

Why it matters

Cost and quality both move with it. Fewer tokens per document means shorter sequences, cheaper attention, more text in the context window and cheaper inference per character. More tokens means more parameters before the first transformer layer, and sparser training signal per token.

How to reason about it

  1. Count parameters. The input embedding table has vocab size times model width. Many models tie the output layer to it, others keep a second copy, which doubles the cost. For a model of width 768 and a vocabulary of 50,000, the table is 50,000 x 768 = 38.4 million parameters. Doubling the vocabulary adds another 38.4 million.
  2. Compare to the whole model. Those 38.4 million are a small slice of a billion-parameter model but a large slice of a 30-million-parameter model. Small models are hurt most by big vocabularies.
  3. Measure compression on your data. Train two candidates and compute characters per token on held-out text. Gains shrink as the vocabulary grows, and each doubling saves less than the last.
  4. Check coverage. A vocabulary trained on English leaves other languages and code in tiny pieces. If your users write Arabic or Hindi, or your data is mostly source code, a larger vocabulary or a corpus mix that includes them is what lowers their sequence length.
  5. Check the tail. Count how often the rarest tokens appear in the training data. Tokens seen a handful of times get poor embeddings.

Before and after

Weak: "Use a 100k vocabulary because big models do." Fixed: "Our model is 60M parameters, 90 percent English. At width 512, 100k tokens cost 51 million embedding parameters, almost the whole budget. A 16k vocabulary costs 8 million. Held-out text shows 3.6 characters per token at 16k versus 4.1 at 100k, so we take 16k and spend the saved parameters on layers."

Those numbers are from this made-up setup; yours will differ, which is the point of measuring.

What goes wrong

  • Copying a vocabulary size from a large model into a tiny one, so embeddings dominate the parameter budget.
  • Judging by compression alone. Better compression with many near-unseen tokens still gives weak embeddings.
  • Ignoring padding to hardware-friendly multiples. Sizes are often rounded to a multiple such as 64 for efficient matrix shapes. That is an engineering convenience, not a quality choice.
  • Forgetting languages and code. A vocabulary tuned on English only makes everything else longer, slower and costlier.

Examples

Two candidates for a 60M model

Model width 512, corpus mostly English.

Vocab Embedding params Chars per token (held-out)
16,000 8.2 million 3.6
100,000 51.2 million 4.1

The larger vocabulary compresses about 14 percent better but costs six times the embedding parameters, nearly the whole budget of a 60M model. Pick 16,000.

A product with code and Arabic

A 7B model will serve developers and Arabic readers. Training a tokenizer on English prose gives Arabic words in 4 to 6 pieces and indentation in many tokens. Adding code and Arabic to the tokenizer corpus and raising the vocabulary is what cuts their sequence length, and the embedding table is a tiny share of 7 billion parameters.

Key points

  • A bigger vocabulary shortens sequences but adds embedding and output parameters.
  • Embedding parameters equal vocabulary size times model width, doubled if the output layer is not tied.
  • Small models pay the most for large vocabularies because the table is a bigger share of the total.
  • Measure characters per token on held-out text; gains per doubling shrink as the vocabulary grows.
  • Multilingual and code-heavy corpora justify larger vocabularies because they otherwise fall into tiny pieces.

Common mistakes

  • Reusing a large model's vocabulary size for a small model and letting embeddings eat the parameter budget.
  • Picking a size by compression ratio alone without looking at how rarely the tail tokens occur.
  • Training the tokenizer on an English-only sample and then expecting good behaviour in other languages or code.
  • Forgetting to count the untied output layer when estimating parameter cost.

Check yourself · 2 of 3

A model has width 1,024 and a vocabulary of 50,000, with a separate output layer. About how many parameters do the input embeddings and the output layer take together?

Choose one answer

Check yourself · 3 of 3

A 30M-parameter model is hurting after you copy a 200k vocabulary from a big model. What is the likely cause and fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here