ElemedexA pocket encyclopedia of AI architectures

Glossary (TM) › Language and language models

TM051

Byte-pair encoding (BPE)

Also: BPE

In Spanish: Codificación por pares de bytes (BPE)

A tokenization algorithm that starts from characters (or bytes) and repeatedly merges the most frequent adjacent pair until the desired vocabulary size is reached.

Byte-pair encoding was born as a data-compression technique (Gage, 1994): replace the most frequent pair of bytes with a new byte, and repeat. Sennrich et al. (2016) adapted it to word segmentation for machine translation: start with a vocabulary of characters, count the adjacent symbol pairs in a corpus, merge the most frequent one into a new symbol, and repeat; the number of merges is the only hyperparameter (2016, p. 1717). The learned merges are then applied, in order, to any word, even an unseen one: the unknown lower gets segmented as low + er (2016, p. 1718).

Tiny example. Corpus: canta ×4, cantan ×3, canto ×2, manta ×1 (Spanish “sings”, “they sing”, “I sing”, “blanket”). The most frequent pair is a n (13 times), merged into “an”; then an t (10) → “ant”; then c ant (9) → “cant”; then cant a (4) → “canta”. After four merges, canta is a single token, cantan = cant + an, canto = cant + o, and manta = m + ant + a.

The example shows what a linguist cares about. cant|o matches a morphological analysis; cant|an isolates the root but fuses the theme vowel with the person ending; m|ant|a is not morphological at all. BPE looks for compression, not morphemes, and finds morphemes only when they are frequent. There is a distant kinship with the distributional procedures by which Harris (1955) proposed to find morpheme boundaries, but here the criterion is frequency alone.

GPT-2 introduced a byte-level variant: the base vocabulary has only 256 symbols, so any Unicode string can be represented with no “unknown” token. To avoid wasteful merges such as dog., dog! and dog?, merging across character categories was forbidden (Radford et al., 2019, sec. 2.2); the final vocabulary had 50,257 tokens (Alammar & Grootendorst, 2024, p. 49). GPT-4, Llama 2 and StarCoder2 also use BPE (Alammar & Grootendorst, 2024, pp. 50–54); BERT uses WordPiece, a related algorithm (Alammar & Grootendorst, 2024, p. 43). See tokenization.

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Gage, P. (1994). A new algorithm for data compression. The C Users Journal, 12(2), 23–38. §
  • Harris, Z. S. (1955). From phoneme to morpheme. Language, 31(2), 190–222. https://doi.org/10.2307/411036 §
  • Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., & Sutskever, I. (2019). Language models are unsupervised multitask learners [Technical report]. OpenAI. https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf §
  • Sennrich, R., Haddow, B., & Birch, A. (2016). Neural machine translation of rare words with subword units. In Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers) (pp. 1715–1725). Association for Computational Linguistics. https://doi.org/10.18653/v1/P16-1162 §