Byte Pair Encoding › Vocabulary Size: The Core Tradeoff

Michael BrenndoerferUpdated January 24, 20264 min read
Language AI HandbookAll chapters
Part 5 of 67Subword Tokenization
Chapter 29Byte Pair Encoding › Vocabulary Size: The Core Tradeoff
Reading section 8 of 14 · Vocabulary Size: The Core Tradeoff · 4 minSection 8 of 14 · 4 min

Book contents

Section 8 of 14 · 4 min read

Byte Pair Encoding › Vocabulary Size: The Core TradeoffOne section · Sections in this chapter8 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Vocabulary size is where practitioners most often need to make a principled choice. The tradeoff operates along two axes simultaneously: sequence length and parameter count. Getting this balance right matters for training efficiency, inference cost, and downstream model quality.

Sequence length decreases as vocabulary size grows. A vocabulary of 1,000 tokens might encode a word like "computational" as 7 pieces, while a vocabulary of 50,000 might represent it as a single token. Shorter sequences are cheaper to process in transformers because attention is quadratic in sequence length. Every token you eliminate from the average sequence reduces inference cost. For models that process long documents, this effect is particularly significant: a 10% reduction in average sequence length translates directly to a roughly 20% reduction in attention computation.

Embedding parameters increase with vocabulary size. Each token requires a row in the embedding matrix. For a model with hidden dimension 4,096, a vocabulary of 50,000 tokens requires a 50,000 ×\times 4,096 matrix with about 200 million parameters, just for the input embeddings. Doubling the vocabulary doubles this cost. For rare tokens that appear only a handful of times during training, these parameters may be poorly learned, effectively wasting capacity.

Out[27]:
Visualization
Dual-axis line chart showing tokens per sentence decreasing and embedding parameters increasing as vocabulary size grows from 1000 to 100000.
Sequence length and embedding parameter count as a function of vocabulary size. Increasing vocabulary size reduces average tokens per sentence (blue curve, left axis), reducing compute cost for attention, but linearly increases embedding parameters (red dashed curve, right axis). The shaded green region marks the 30k-50k range used by most production monolingual English models, representing a practical balance between sequence efficiency and parameter economy.

The practical sweet spot for most monolingual English models is 30,000 to 50,000 tokens. GPT-2 and RoBERTa both use approximately 50,000. BERT uses 30,522 (through WordPiece, a BPE variant). Models targeting multiple languages or code often use larger vocabularies. GPT-4's tokenizer uses over 100,000 tokens to accommodate the linguistic diversity of its training data.

Vocabulary sizes for popular language models. Most monolingual English models converge on 30k-50k tokens. Models targeting broader coverage (multilingual, code, long-context) trend toward 100k+.
ModelTokenizerVocabulary Size
BERTWordPiece30,522
GPT-2BPE50,257
GPT-3BPE50,257
GPT-4BPE~100,277
LLaMA 2SentencePiece BPE32,000
LLaMA 3BPE128,256
RoBERTaBPE50,265
T5SentencePiece Unigram32,100

The trend toward larger vocabularies in more recent models is not accidental. As models scale up and are deployed more broadly, the cost of a large embedding matrix becomes relatively smaller compared to the attention and feedforward layers, while the benefit of fewer tokens per sequence becomes relatively larger. LLaMA 3's jump to 128,256 tokens reflects both the model's multilingual ambitions and the practical observation that at billion-parameter scale, the embedding table is a small fraction of total parameters.