Byte pair encoding

From Wikipedia, the free encyclopedia

Byte pair encoding[1][2] (BPE) or digram coding[3] is a simple and robust form of data compression in which the most common pair of contiguous bytes of data in a sequence are replaced with a byte that does not occur within the sequence. A lookup table of the replacements is required to rebuild the original data. The algorithm was first described publicly by Philip Gage in a February 1994 article "A New Algorithm for Data Compression" in the C Users Journal.[4]

A variant of the technique has shown to be useful in several natural language processing (NLP) domains, for applications such as tokenisation, as seen in Google's SentencePiece[5] and OpenAI's GPT-3.[6] Here, the goal is not data compression, but tokenisation of text in a given language to produce a variable sequence of terms from a fixed-size vocabulary of tokens. Typically, most words will be encoded as a single token, while rare words will be encoded as a sequence of a few tokens, where these tokens represent meaningful word parts. This translation of text into tokens can be found by variants of byte pair encoding, such as subword units.[7]

Byte pair encoding lends itself to NLP tasks due to its simplicity and speed; BPE is suitably effective for the tokenisation of terms, does not require large computational overheads, and remains consistent, making it reliable.

Byte pair encoding example[edit]

Byte pair encoding operates by iteratively replacing the most common contiguous sequences of characters in a target piece of text with unused 'placeholder' bytes. The iteration ends when no sequences can be found, leaving the target text effectively compressed. Decompression can be performed by reversing this process, querying known placeholder terms against their corresponding denoted sequence, per a lookup table. In the original paper, this lookup table is encoded and stored alongside the compressed text.


Suppose the data to be encoded is


The byte pair "aa" occurs most often, so it will be replaced by a byte that is not used in the data, such as "Z". Now there is the following data and replacement table:


Then the process is repeated with byte pair "ab", replacing it with "Y":


The only literal byte pair left occurs only once, and the encoding might stop here. Alternatively, the process could continue with recursive byte pair encoding, replacing "ZY" with "X":


This data cannot be compressed further by byte pair encoding because there are no pairs of bytes that occur more than once.

To decompress the data, simply perform the replacements in the reverse order.

See also[edit]


  1. ^ Gage, Philip (1994). "A New Algorithm for Data Compression". The C User Journal.
  2. ^ "A New Algorithm for Data Compression". Dr. Dobb's Journal. 1 February 1994. Retrieved 10 August 2020.
  3. ^ Witten, Ian H.; Moffat, Alistair; Bell, Timothy C. (1994). Managing Gigabytes. New York: Van Nostrand Reinhold. ISBN 978-0-442-01863-4.
  4. ^ "Byte Pair Encoding". Archived from the original on 2016-03-26.
  5. ^ "google/sentencepiece". Google. 2021-03-02. Retrieved 2021-03-02.
  6. ^ Brown, Tom B.; Mann, Benjamin; Ryder, Nick; Subbiah, Melanie; Kaplan, Jared; Dhariwal, Prafulla; Neelakantan, Arvind; Shyam, Pranav; Sastry, Girish; Askell, Amanda; Agarwal, Sandhini (2020-06-04). "Language Models are Few-Shot Learners". arXiv:2005.14165 [cs.CL].
  7. ^ Sennrich, Rico; Haddow, Barry; Birch, Alexandra (2016-08-12). "Neural Machine Translation of Rare Words with Subword Units". Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. Association for Computational Linguistics (ACL): 1715–1725. doi:10.18653/v1/P16-1162.