Tokenization Algorithm BPE
Byte Pair Encoding (BPE) is a subword tokenization algorithm: starting from characters, it repeatedly merges the most frequent adjacent symbol pairs, building a subword vocabulary that balances size and expressiveness. It underlies the tokenizers of mainstream models such as GPT, RoBERTa and BART.
Core Principles
BPE began as a 1994 data compression algorithm; in 2016 Sennrich et al. brought it to neural machine translation to resolve the dilemma of "too large a vocabulary" and "out-of-vocabulary (OOV) words": whole-word modeling explodes the vocabulary with many OOVs, while character-level modeling makes sequences too long.
Training is intuitive: split each word into characters with its frequency, count adjacent pairs, merge the most frequent into a new symbol recorded in a merge table, and repeat until the vocabulary reaches its target size (or merge count). Frequent words such as low and lower pass through l+o, lo, lo+w and low, and lower ends as low+er, so the algorithm learns English comparative morphology automatically.
Encoding greedily applies the learned merges in priority order (by rule priority, not appearance). Modern implementations such as HuggingFace tokenizers use a priority queue, training on hundreds of millions of words in minutes.
For multilingual and arbitrary-character coverage, Byte-level BPE starts from 256 byte values, so any text, including emoji and rare scripts, encodes with no OOV; it has been standard since GPT-2. The cost is that one Chinese character may split into several byte tokens, compressing worse than character-level tokenization.
Key Points
Vocabulary size is the core hyperparameter: Llama 3 uses 128k and GPT-4's cl100k_base about 100k; larger vocabularies shorten sequences but enlarge the embedding matrix and softmax compute.
The BPE family has variants: WordPiece (BERT) merges the pair that most raises language-model likelihood; Unigram LM (SentencePiece's other mode, used by T5 and ALBERT) starts large and prunes by probability, supporting sampled tokenization; SentencePiece treats spaces as symbols, language-agnostic and able to restore text losslessly.
Impact on downstream ability: tokenization affects token efficiency, multilingual fairness (low-resource languages split finer and cost more) and numerical reasoning (irregular digit splits harm arithmetic), so digit-by-digit splitting and forced whitespace rules have appeared.
The evolution runs from word, character and subword trade-offs to byte-level coverage, and then to tokenizer data mixes tuned for code, math and multilingual text.
Common Misconceptions
Misconception one: tokenization barely matters; vocabulary and splitting affect efficiency and abilities such as arithmetic and multilingual handling, a badly underestimated decision, and changing the tokenizer usually means retraining from scratch. Misconception two: merge count equals vocabulary size, which differs in byte-level versions (256 base bytes plus uncounted special tokens). Misconception three: a tokenizer trained on Chinese applied to other languages, where compression collapses and inference cost doubles. Misconception four: subword splitting only "saves tokens," ignoring its effect on numerical representation and whitespace sensitivity.
Application Scenarios
Tokenizer construction for large models, multilingual vocabulary design, code and math tokenization, neural machine translation, speech text post-processing, and token budget estimation for retrieval and embedding models.