liveBook · Manning

Byte pair encoding

As explained in LLMs in Production

Byte pair encoding is a subword tokenization algorithm that iteratively merges frequent adjacent symbol pairs into larger vocabulary units.

Byte-pair encoding (BPE) converts text into subword units by repeatedly merging frequent adjacent symbol pairs. Training a tokenizer on task-specific data can reduce unknown tokens and preserve more encoded meaning than relying on a generic tokenizer. section 4.4

BPE occupies a practical middle ground between character- and word-based tokenization: it keeps dictionaries smaller than word tokenizers while retaining more semantics than character tokenizers. The best split strategy depends on the task, so tokenizer choices can create downstream consequences. section 4.4.1

Tune the tokenizer around the data and the model:

  • Set vocab_size to balance multilingual or unusual-token coverage against computational complexity. section 4.4.1
  • Set min_frequency to control which subwords enter the vocabulary; higher values filter rare tokens but can discard useful ones. section 4.4.1
  • Define special_tokens such as padding, unknown, mask, start, and end markers for model training. section 4.4.1
  • Choose BPE or SentencePiece according to the base or fine-tuned model and the project’s requirements. section 4.4.1

Training a custom BPE tokenizer

A minimal Python workflow gathers text files and trains ByteLevelBPETokenizer with a 52,000-token vocabulary, minimum frequency 2, and explicit special tokens. section 4.4.1

Table 4.3 Tokenization allows GOAT 7B to outperform GPT-4 in math
Prompt
GOAT 7B
GPT-4 1.7T
Correct
3978640188 + 42886272 =
4021526460
4,021,526,460
4,021,526,460
4523646 minus 67453156
–62929510
–63,930,510
–62,929,510
Calculate 397 × 4429
1758313
1,757,413
1,758,313
What is 8914/64?
139 R 18
139.15625
139.28125 Or 139 R 18

Use the arithmetic comparison to inspect how tokenization affects numerical reasoning and how different token boundaries can produce different answers. section 4.4.1

How LLMs in Production covers it

How deep it goes
Explains subword trade-offs and training parameters but defers detailed algorithm mechanics.
Examples
Uses custom Python tokenizer training, token ID output, digit-level tokenization, and arithmetic-comparison results.
What it assumes
Assumes a prepared text dataset and basic awareness that language-model inputs must become numbers.
What you can do after
Build it from scratch with the tokenizers library and configure vocabulary size, frequency thresholds, and special tokens.
How it connects
Connects BPE with subword tokenization, SentencePiece, WordPiece, Unigram, embeddings, and model fine-tuning.

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 LLMs in Production. The section links point to the parts of the book it draws on.