liveBook · Manning

Byte pair encoding

As explained in How Large Language Models Work

A byte-pair encoding tokenizer segments text by repeatedly merging frequent adjacent symbol pairs into tokens.

Byte-pair encoding (BPE) is a tokenizer technique that turns text into reusable word pieces. Rather than relying on hand-written rules, it learns common adjacent symbol sequences from data and adds them to a token vocabulary. section 2.2.3 section 2.6

A simplified BPE process works like this:

  • Start with individual letters or bytes as tokens. section 2.2.3
  • Count adjacent pairs and find a frequent combination, such as “n” followed by “g.” section 2.2.3
  • Merge that pair into a new subword token, add it to the vocabulary, and repeat. section 2.2.3
  • Stop when the vocabulary reaches a chosen size or no useful common pairs remain. section 2.2.3

From letters to “ing”

Suppose “n” and “g” frequently occur together. BPE merges them into “ng.” If “i” commonly precedes that new token, it can merge “i” and “ng” into “ing.” The tokenizer can then represent familiar word parts compactly instead of storing every complete word. section 2.2.3

Figure 2.5 A simplified byte pair encoding algorithm for creating tokens: first, find the most frequent pair of characters “ng.” Next, replace all instances of “ng” with a placeholder token “T,” and add “ng” to the vocabulary. Repeat the process until no common byte pairs remain.
figure

Follow the simplified merge diagram from the frequent pair “ng” through replacement, vocabulary addition, and repetition. section 2.2.3

BPE is efficient but not intuitive. A longer string can produce fewer tokens than a shorter one when the longer form is frequent. For example, “I’m running” can be one token shorter than “I’m runnin,” because “running” may have a learned token while “runnin” is split into pieces such as “run” and “nin.” Different tokenizers can therefore produce different counts, creating practical software risks. section 2.2.4

How How Large Language Models Work covers it

How deep it goes
Explains iterative pair merging and token-count effects, but stops short of implementation code or detailed computation.
Examples
Uses the “ng” to “ing” merge and contrasts tokenization of “I’m running” with “I’m runnin.”
What it assumes
Assumes only basic familiarity with text and explains tokens, subwords, vocabulary, and merging from the beginning.
What you can do after
Evaluate token counts and recognize why frequent word forms, unusual spellings, and tokenizer differences can affect software behavior.
How it connects
Connects subword segmentation with tokenization, normalization, vocabulary, Unicode, and language-model computation.

Also explained in

Compare how 11 books explain byte pair encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Large Language Models Work. The section links point to the parts of the book it draws on.