The Tokens and Embeddings board shows text cut into pieces before a model sees it. This lesson builds the cutting machine yourself.
A developer in India sends the two words “नमस्ते दुनिया” to a language model API. It is 13 characters. The bill counts 13 tokens. The English “Hello world” is 11 characters and the bill counts 2 tokens. Same meaning, six and a half times the pieces.
Nobody chose that on purpose. It comes from a small program, the tokenizer, and from the text it was built on. By the end you will be able to write that program in about 40 lines of Python. You will train it on text, use it to cut new text, and explain why numbers, Hindi and one strange word called SolidGoldMagikarp all behave the way they do.
The history (optional)
In 1994 Philip Gage described byte pair encoding in The C User Journal, as a way to shrink files. In August 2015 Rico Sennrich, Barry Haddow and Alexandra Birch at Edinburgh used the same idea to cut words into pieces for machine translation (published at ACL 2016). On 5 February 2023 Jessica Rumbelow and Matthew Watkins posted “SolidGoldMagikarp (plus, prompt generation)” on LessWrong. It described words that made GPT models behave strangely. Their follow-up posts traced those tokens back to Reddit usernames. Nothing later in this lesson depends on these dates.
Before you start. In Tokens & Embeddings you saw that a model reads numbers, so text is cut into tokens and each token gets a row of numbers. You also saw that common text is cheap and rare text is expensive. In What the Model Sees (one string, every turn, from the top) you saw that cost and context size are counted in tokens. Here you build the part that decides where the cuts go. You need only basic Python.
Lesson 59 of 83 in AI
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.