Let's build the GPT Tokenizer

Andrej Karpathy · Intermediate ·🧠 Large Language Models ·2y ago

Key Takeaways

The video demonstrates building the GPT Tokenizer from scratch, covering the Byte Pair Encoding algorithm, tokenization issues, and implementation details. It also explores the importance of tokenization in Large Language Models and its potential problems.

Original Description

The Tokenizer is a necessary and pervasive component of Large Language Models (LLMs), where it translates between strings and tokens (text chunks). Tokenizers are a completely separate stage of the LLM pipeline: they have their own training sets, training algorithms (Byte Pair Encoding), and after training implement two fundamental functions: encode() from strings to tokens, and decode() back from tokens to strings. In this lecture we build from scratch the Tokenizer used in the GPT series from OpenAI. In the process, we will see that a lot of weird behaviors and problems of LLMs actually trace back to tokenization. We'll go through a number of these issues, discuss why tokenization is at fault, and why someone out there ideally finds a way to delete this stage entirely. Chapters: 00:00:00 intro: Tokenization, GPT-2 paper, tokenization-related issues 00:05:50 tokenization by example in a Web UI (tiktokenizer) 00:14:56 strings in Python, Unicode code points 00:18:15 Unicode byte encodings, ASCII, UTF-8, UTF-16, UTF-32 00:22:47 daydreaming: deleting tokenization 00:23:50 Byte Pair Encoding (BPE) algorithm walkthrough 00:27:02 starting the implementation 00:28:35 counting consecutive pairs, finding most common pair 00:30:36 merging the most common pair 00:34:58 training the tokenizer: adding the while loop, compression ratio 00:39:20 tokenizer/LLM diagram: it is a completely separate stage 00:42:47 decoding tokens to strings 00:48:21 encoding strings to tokens 00:57:36 regex patterns to force splits across categories 01:11:38 tiktoken library intro, differences between GPT-2/GPT-4 regex 01:14:59 GPT-2 encoder.py released by OpenAI walkthrough 01:18:26 special tokens, tiktoken handling of, GPT-2/GPT-4 differences 01:25:28 minbpe exercise time! write your own GPT-4 tokenizer 01:28:42 sentencepiece library intro, used to train Llama 2 vocabulary 01:43:27 how to set vocabulary set? revisiting gpt.py transformer 01:48:11 training new tokens, example of prompt compression 0
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

This video teaches how to build the GPT Tokenizer from scratch, covering the Byte Pair Encoding algorithm and implementation details. It also explores the importance of tokenization in Large Language Models and its potential problems. By the end of this video, you will be able to build and understand the GPT Tokenizer.

Key Takeaways
  1. Understand the basics of tokenization and its importance in LLMs
  2. Implement the Byte Pair Encoding algorithm
  3. Build the GPT Tokenizer from scratch
  4. Use the tiktoken library to handle special tokens
  5. Train a new tokenizer using the sentencepiece library
💡 Tokenization is a critical component of LLMs, and understanding its implementation details is essential for building and optimizing these models.

Related Reads

📰
TAI #214: Kimi K3 Brings Open Weight Closer to the Frontier
Kimi K3 brings open weight closer to the frontier, and Fable disproves an 87-year-old math conjecture, showcasing AI's capabilities in math and science
Medium · LLM
📰
MCP Explained: How AI Learns to Use the Real World
Learn how AI uses the Model Context Protocol to interact with the real world and leverage external resources
Medium · LLM
📰
What is the Model Context Protocol (MCP)?
Learn about the Model Context Protocol (MCP), an open standard for connecting language models to tools and data through a consistent interface
Dev.to AI
📰
Green Is Not Correct — field notes from an AI orchestrator, end of a long shift
Learn from an AI orchestrator's field notes at the end of a long shift, exploring the complexities of AI operations
Medium · LLM

Chapters (21)

intro: Tokenization, GPT-2 paper, tokenization-related issues
5:50 tokenization by example in a Web UI (tiktokenizer)
14:56 strings in Python, Unicode code points
18:15 Unicode byte encodings, ASCII, UTF-8, UTF-16, UTF-32
22:47 daydreaming: deleting tokenization
23:50 Byte Pair Encoding (BPE) algorithm walkthrough
27:02 starting the implementation
28:35 counting consecutive pairs, finding most common pair
30:36 merging the most common pair
34:58 training the tokenizer: adding the while loop, compression ratio
39:20 tokenizer/LLM diagram: it is a completely separate stage
42:47 decoding tokens to strings
48:21 encoding strings to tokens
57:36 regex patterns to force splits across categories
1:11:38 tiktoken library intro, differences between GPT-2/GPT-4 regex
1:14:59 GPT-2 encoder.py released by OpenAI walkthrough
1:18:26 special tokens, tiktoken handling of, GPT-2/GPT-4 differences
1:25:28 minbpe exercise time! write your own GPT-4 tokenizer
1:28:42 sentencepiece library intro, used to train Llama 2 vocabulary
1:43:27 how to set vocabulary set? revisiting gpt.py transformer
1:48:11 training new tokens, example of prompt compression
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →