AI ENGVisual Encyclopedia

MODULE 2: DATA AT WEB SCALE · SCENE 08

Building the vocabulary

Byte-pair encoding grows a vocabulary from raw bytes — merge by merge, with rules that shape every token.

RAW TEXT: "thinking"VOCAB TOKENS: 8
"t""h""i""n""k""i""n""g"

Byte-Pair Encoding merges frequent adjacent character pairs into single vocabulary tokens.

TECHNICAL BREAKDOWNModule 2: Data Engineering & Curation at Web-Scale

Tokenizer Design & Byte-Pair Encoding (BPE)

Tokenizers convert raw text strings into discrete integer sequences for model ingestion. Modern BPE tokenizers operate directly on UTF-8 bytes to guarantee zero out-of-vocabulary (OOV) tokens while maximizing compression efficiency.

Byte-Level BPE

Starting with a base vocabulary of 256 raw UTF-8 bytes, the algorithm iteratively counts adjacent symbol pairs across a massive text corpus and merges the most frequent pair into a new token.

Vocabulary Size Trade-offs

Small vocabs (32k) save memory in the embedding/LM head layer but result in longer sequence lengths per document. Large vocabs (128k+) shorten token sequences by ~15%-20%, speeding up attention and generation.

Pre-Tokenization Regex

Regex splitting (such as tiktoken's cl100k pattern) prevents BPE from merging across numbers, punctuation, or word boundaries, preventing weird token mergers.

MATHEMATICAL FORMULATION · BPE PAIR FREQUENCY MERGE
Pair_selected = argmax_(p_A, p_B) Count( p_A, p_B )

Find the most frequent pair of adjacent tokens in the corpus and assign it a fresh vocabulary index.

REAL-WORLD PRODUCTION ENGINEERING
  • Llama 3 expanded vocabulary from Llama 2's 32k to 128k tokens, achieving ~15% better text compression and significantly improving non-English performance.