liveBook · Manning

Byte pair encoding

As explained in Build a Large Language Model (From Scratch)

A subword tokenization method that iteratively merges frequent symbol pairs to build a vocabulary for text representation.

Byte pair encoding (BPE) tokenizes text into subword units by repeatedly merging frequent symbol pairs to build a vocabulary. It sits between raw text and the token IDs that feed GPT-style transformer models, reducing the need for unknown-word tokens. section 2.0 section 2.9

The tokenization flow has three practical properties:

  • Represents familiar words with vocabulary entries while decomposing unfamiliar words into smaller subwords or individual characters. section 2.5
  • Avoids relying on an <|unk|> token, allowing the tokenizer to represent any encountered word. section 2.5 section 2.4
  • Produces integer token IDs that are later converted into embeddings for neural-network processing. section 2.9

Using the GPT-2 tokenizer

With tiktoken, select the GPT-2 encoding, call encode on text containing <|endoftext|> and an unfamiliar word, then call decode to reconstruct the text. The GPT-2 vocabulary contains 50,257 entries, with <|endoftext|> assigned ID 50256. section 2.5

Figure 2.11 BPE tokenizers break down unknown words into subwords and individual characters. This way, a BPE tokenizer can parse any word and doesn’t need to replace unknown words with special tokens, such as <|unk|>.
figure

Shows how BPE decomposes unknown words into subwords and individual characters instead of replacing them with <|unk|>. section 2.5

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains subword fallback and demonstrates encode/decode usage, but delegates efficient algorithm implementation to tiktoken.
Examples
Demonstrates tiktoken with GPT-2, <|endoftext|>, an unfamiliar place name, encoded IDs, and decoded text.
What it assumes
Assumes basic text preprocessing and token concepts, while introducing subwords, token IDs, and embeddings within the LLM pipeline.
What you can do after
Apply it via tiktoken to encode text into GPT-compatible IDs and decode those IDs back into text.
How it connects
Connects tokenization with token IDs, embeddings, GPT models, transformer architecture, and out-of-vocabulary handling.

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 Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.