liveBook · Manning

Tokenizer

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

A text-processing component that converts text to token IDs and back, including handling of special tokens and unknown words in this section.

A tokenizer converts text into token IDs for model input and converts predicted IDs back into readable text. It uses a vocabulary that maps unique tokens to integers in both directions. section 2.3 section 5.1.1

A practical tokenizer follows this data flow:

  • Build a vocabulary by tokenizing training text, removing duplicates, sorting tokens, and assigning integer IDs. section 2.3
  • Encode text by splitting it into words and punctuation, then mapping each token through the vocabulary. section 2.3
  • Decode IDs by reversing the mapping, joining tokens, and removing spacing inserted before punctuation. section 2.3
  • Handle unseen words with <|unk|> and separate unrelated documents with <|endoftext|>. section 2.4

Encoding through GPT

The GPT pipeline encodes input text into token IDs, processes those IDs into logits, converts selected logits into new IDs, and decodes the result into text. A GPT-2 tokenizer and utility functions place the IDs in a batch-shaped tensor before generation. section 5.1.1

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

How deep it goes
Implements vocabulary construction and simple encode/decode classes, while deferring tokenizer internals beyond these mechanics.
Examples
Uses an Edith Wharton passage, an unknown Hello token, document separators, and GPT-generated token sequences.
What it assumes
Assumes basic text processing and Python while teaching vocabulary mapping and token-ID flow from first principles.
What you can do after
Build a simple tokenizer, handle unknown words and document boundaries, and connect token IDs to GPT generation.
How it connects
Ties tokenization to vocabularies, special tokens, GPT models, logits, and text generation.

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.