Let's build the GPT Tokenizer
Andrej Karpathy5 HN recommenders · recommended in 5 months, 2024-2025
1.3M views · 2 years ago · lecture · evergreen
Andrej Karpathy builds the GPT tokenizer from scratch, implementing byte pair encoding along with encode and decode. He shows how many odd LLM behaviors trace back to tokenization, and covers Unicode byte encodings along the way.
Recommended as the best deep dive on tokenizers, worth the two hours for anyone working with LLMs.
1:56:20Let's build GPT: from scratch, in code, spelled out.
Andrej Karpathy
8.2M views • 3 years ago
19:29Glitch Tokens - Computerphile
Computerphile
353K views • 3 years ago
3:31:24Deep Dive into LLMs like ChatGPT
Andrej Karpathy
10.2M views • 1 year ago
42:40State of GPT | BRK216HFS
Microsoft Developer
785K views • 3 years ago
1:57:45The spelled-out intro to language modeling: building makemore
Andrej Karpathy
1.3M views • 4 years ago
1:31:13A Hackers' Guide to Language Models
Jeremy Howard
580K views • 3 years ago
27:14Transformers, the tech behind LLMs | Deep Learning Chapter 5
3Blue1Brown
11.6M views • 2 years ago
13:52Ch(e)at GPT? - Computerphile
Computerphile
617K views • 3 years ago
59:48[1hr Talk] Intro to Large Language Models
Andrej Karpathy
4.3M views • 2 years ago
8:25Large Language Models from scratch
Graphics in 5 Minutes
375K views • 4 years ago
20:39AI Language Models & Transformers - Computerphile
Computerphile
354K views • 7 years ago
57:21An Observation on Generalization
Simons Institute for the Theory of Computing
210K views • 3 years ago
25:57GPT3: An Even Bigger Language Model - Computerphile
Computerphile
446K views • 6 years ago
28:48LSTM is dead. Long Live Transformers!
Seattle Applied Deep Learning
540K views • 6 years ago
19:20GPT-4chan: This is the worst AI ever
Yannic Kilcher
1M views • 4 years ago
48:32Sparks of AGI: early experiments with GPT-4
Sebastien Bubeck
1.8M views • 3 years ago
1:11:41Lecture 1 | Natural Language Processing with Deep Learning
Stanford University School of Engineering
820K views • 9 years ago
36:02ChatGPT with Rob Miles - Computerphile
Computerphile
517K views • 3 years ago
30:10The Inside Story of ChatGPT’s Astonishing Potential | Greg Brockman | TED
TED
1.9M views • 3 years ago
1:22:38CS480/680 Lecture 19: Attention and Transformer Networks
Pascal Poupart
374K views • 7 years ago
8 comments from Hacker News
Karpathy's "Let's build the GPT Tokenizer" also contains a good introduction to Unicode byte encodings, ASCII, UTF-8, UTF-16, UTF-32 in the first 20 minutes: ▶ this video
Andrej Karpathy goes into quite some depth about how tokenization works here: ▶ this video tl;dr many of the LLMs use byte-pair encoding to create tokens. You take a set of documents, and then form tokens by repeatedly merging the most common pair of tokens. The initial set of tokens is 256 raw bytes. And the text is typically represented in utf-8. I expect that although the LLMs can understand arbitrarily but cleanly offset unicode code points by (eventually) noticing the final byte of each sequence, they would do markedly worse on actually processing and completing on them, because they will not have been reduced to the normal set of tokens. However, if the text is actually output cleanly converted, either in internal thinking tokens or in the beginning of the response, they should do fine. Understanding tokenization is surprisingly useful, even if that video seems awfully long to devote to such a tedious subject. Even Karpathy doesn't like it!
I finally understood the weirdness of tokenizers after watching the video Andrej Karpathy made: "Let's build the GPT Tokenizer" ( ▶ this video ). He goes through why we need them instead of raw byte sequences (too expensive) and how the Byte Pair Encoding algorithm works. Worth spending 2h for the deeper understanding if you deal with LLMs.
This is just an artefact of tokenisation though. The model simply isn’t ever shown the letters that make up words, unless they are spelled out explicitly. It sees tokens representing groups of words. This is a little like saying a human isn’t intelligent because they couldn’t answer your question that you asked in an ultrasonic wavelength. If you’d like to learn more this video is a great resource: ▶ this video
If there are complaints about code quality in a jupyter notebook, chances are that someone is doing it wrong -- either the author is using it for demonstration purpose, for production or near production environment, or the person looking at others' notebooks is taking them too seriously. Most code in jupyter notebook is bad, but that's fine, because nobody should waste time doing that unless they must reproduce the results or they are sharing the notebook with others. Notable example of high quality notebook: ▶ this video
Tokenisation turns a continuous signal into a normalized discrete vocabulary: stock "went up a lot", "went up a little", "stayed flat". This smooths out noise and simplifies matching up similar but not identical signals. > We tokenize text because text isn't numbers. Text is actually numbers. People tried inputting UTF8 directly into transformers, but it doesn't work that well. Karpathy explains why: ▶ this video
I'm convinced GPT is running separate helper functions on input and output tokens to fix the 'tokenization' issues. As in, find items of math, send it to this hand made parser and function, then insert result into output tokens. There's no other way to fix the token issue. For reference, Let's build the GPT Tokenizer ▶ this video
ChatGPT is specifically bad at these kinds of tasks because of tokenization. If you plug your query into link , you can see that"egregious" is a single token, so the LLM doesn't actually see any "e" characters -- to answer your question it would have had to learn a fact about how the word was spelled from it's training data, and I imagine texts explicitly talking about how words are spelled are not very common. Good explanation here if this still doesn't make sense: link , or check out Andrej Karpathy's latest video if you have 2 hours for a deep dive: ▶ this video IMO questions about spelling or number sense are pretty tired as gotchas, because they are all basically just artifacts of this implementation detail. There are other language models available that don't have this issue. BTW this is also the reason DALL-E etc suck at generating text in images.