Skip to content
0:00 / 0:00

#tokenizer#bytepairencoding#gpt

Let's build the GPT Tokenizer

Andrej Karpathy

1.7M subscribers • 6 videos on Hallway

YouTube

Intermediate. Assumes working knowledge.

1.2M views2 years ago5 recommendations

AI and machine learningComputer scienceTransformers and LLMsAlgorithms

Andrej Karpathy builds a GPT-style tokenizer from scratch, explaining Byte Pair Encoding and how tokenization issues cause common LLM limitations.

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: intro: Tokenization, GPT-2 paper, tokenization-related issues tokenization by example in a Web UI (tiktokenizer) strings in Python, Unicode code points Unicode byte encodings, ASCII, UTF-8, UTF-16, UTF-32 daydreaming: deleting tokenization Byte Pair Encoding (BPE) algorithm walkthrough starting the implementation counting consecutive pairs, finding most common pair merging the most common pair training the tokenizer: adding the while loop, compression ratio tokenizer/LLM diagram: it is a completely separate stage decoding tokens to strings encoding strings to tokens regex patterns to force splits across categories tiktoken library intro, differences between GPT-2/GPT-4 regex GPT-2 encoder.py released by OpenAI walkthrough special tokens, tiktoken handling of, GPT-2/GPT-4 differences minbpe exercise time! write your own GPT-4 tokenizer sentencepiece library intro, used to train Llama 2 vocabulary how to set vocabulary set? revisiting gpt.py transformer training new tokens, example of prompt compression multimodal [image, video, audio] tokenization with vector quantization revisiting and explaining the quirks of LLM tokenization final recommendations ??? :) Exercises: - Advised flow: reference this document and try to implement the steps before I give away the partial solutions in the video. The full solutions if you're getting stuck are in the minbpe code github.com/karpathy/minbpe/blob/master/exercise.md Links: - Google colab for the video: colab.research.google.com/drive/1y0KnCFZvGVf_odSfcNAws6kcDD7… - GitHub repo for the video: minBPE github.com/karpathy/minbpe - Playlist of the whole Zero to Hero series so far: youtube.com/watch?v=VMj-3S1tku0&list=PLAqhIrjkxbuWI23v9cThsA… - our Discord channel: discord.gg/3zy8kqD9Cp - my Twitter: twitter.com/karpathy Supplementary links: - tiktokenizer tiktokenizer.vercel.app - tiktoken from OpenAI: github.com/openai

Why it's here

5 people recommend this

What people say. Highly recommended as the definitive, clearest deep dive into how LLM tokenizers work and why they cause odd model behaviors.

craniumon Hacker News • Recommends

On “Probably pay attention to tokenizers”

I finally understood the weirdness of tokenizers after watching the video Andrej Karpathy made: "Let's build the GPT Tokenizer" ( link to 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.

moozillaon Hacker News • Recommends

On “Ask HN: Did you encounter any leap year bugs today?”

…- 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: link to 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.…

Chironoon Hacker News • Recommends

On “Artificial intelligence is losing hype”

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: link to this video

zellynon Hacker News • Recommends

On “Why Does Claude Speak Byzantine Music Notation?”

Andrej Karpathy goes into quite some depth about how tokenization works here: link to 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 conve…

vismit2000on Hacker News • Recommends

On “UTF-8 is a brilliant design”

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: link to this video

anonymouse008on Hacker News • Mentions

On “Claude 3 model family”

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 link to this video

dist-epochon Hacker News • Mentions

On “Chronos: Learning the Language of Time Series”

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: link to this video

pquki4on Hacker News • Mentions

On “Notebooks Are McDonalds of Code”

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: link to this video