How it works
Byte-pair encoding, or BPE, learns its vocabulary from a large text sample. It begins with single characters, finds the pair that occurs together most often, merges it into one new token, and repeats. Hugging Face’s documentation shows the idea with the words hug, pug, pun and bun: the pair u and g is the most frequent, so ug becomes a token, then un, and so on. Byte-level BPE starts from the 256 possible byte values, so any text can be encoded and no “unknown” token is needed.
SentencePiece is a library that applies BPE or a related method, Unigram, directly to raw text, spaces included, which suits languages such as Chinese and Japanese that do not separate words with spaces. At run time the tokenizer is just a lookup: text in, a list of numbers out. The model never sees the letters.