Save this video free

Let's build the GPT Tokenizer

Andrej Karpathy · 2:13:35 · Watch on YouTube

These notes were made with YouTube Collector. Paste any YouTube link and get the same: a summary, chapters and a searchable transcript.

Overview

Andrej Karpathy details the intricacies of tokenization in large language models, explaining its necessity and the "foot guns" it introduces. He covers the Byte Pair Encoding (BPE) algorithm, its implementation, and how it's used in GPT-2, GPT-4, and SentencePiece tokenizers, highlighting issues like non-English language performance, arithmetic difficulties, and "solid gold Magikarp" token anomalies, while also discussing special tokens and vocabulary size considerations.

Key takeaways

Chapters

0:00 Introduction to Tokenization and its Importance
0:20 Character-Level Tokenization vs. Subword Tokenization
7:00 Tokenization Issues: Spelling, Arithmetic, and Language Bias
10:00 Interactive Tokenizer Demo (tiktoken.app)
11:51 Case Sensitivity and Contextual Tokenization
18:45 Python Code Tokenization and Efficiency
20:29 Comparing GPT-2 and GPT-4 Tokenizer Vocabulary Sizes
24:17 Understanding Unicode Code Points
29:00 Why Not Use Raw Unicode Code Points as Tokens?
30:17 UTF-8 Encoding and its Limitations for Tokenization
37:13 Introduction to Byte Pair Encoding (BPE)
45:01 Implementing BPE: Training the Tokenizer
58:56 BPE Training Loop and Vocabulary Size
1:05:23 Tokenizer as a Separate Pre-processing Stage
1:10:43 Implementing BPE Decoding: Tokens to Text
1:20:23 Implementing BPE Encoding: Text to Tokens
1:34:16 BPE Algorithm Basics and Implementation Refinements
1:35:37 GPT-2 Tokenizer: Regex Chunking and Merging Rules
1:59:00 OpenAI's tiktoken Library for Inference
2:10:28 Special Tokens: End-of-Text and Conversation Delimiters

Keep these chapters and the full searchable transcript in your own library.

Transcript excerpt

0:00 hi everyone so in this video I'd like us to cover the process of tokenization in large language models now you see here that I have a set face and that's because uh tokenization is my least favorite part of working with large language models but unfortunately it is necessary to understand in some detail because it it is fairly hairy gnarly and there's a lot of hidden foot guns to be aware of and a lot of oddness with large language models typically traces back to tokenization so what is tokenization now in my previous video Let's Build GPT from scratch uh we actually already did tokenization but we did a very naive simple version of tokenization so when you go to the Google colab for that video uh you see here that we loaded our training set and our training set was this uh Shakespeare uh data set now in the beginning the

0:48 Shakespeare data set is just a large string in Python it's just text and so the question is how do we plug text into large language models and in this case here we created a vocabulary of 65 possible characters that we saw occur in this string these were the possible characters and we saw that there are 65 of them and then we created a a lookup table for converting from every possible character a little string piece into a token an integer so here for example we tokenized the string High there and we received this sequence of tokens and here we took the first 1,000 characters of our data set and we encoded it into tokens and because it is this is character level we received 1,000 tokens in a sequence so token 18

1:39 47 Etc now later we saw that the way we

This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.

Related notes

Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Andrej Karpathy.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.