In 1984, someone named Mark V. Shaney started posting on net.singles Usenet (think 80s Reddit). According to one witness:
Mark was a member of a UseNet News group called net.singles, a users group chock full of dating tips, lonely heart chatter…Mr Shaney was a little goofy but he was always there. He chimed in with poetic opinions on romantic empathy: “A I’ve commented before, really relating to someone involves standing next to impossible.”
And he had a great Groucho Marx sense of humor: “One morning I shot an elephant in my arms and kissed him. So it was too small for a pill? Well, it was too small for a while.” And his idea of a good closing was: “Oh, sorry. Nevermind. I am afraid of it becoming another island in a nice suit.” ----Mark
Here’s something else Mark wrote: “I hope that there are sour apples in every bushel.” source
As you can see, Mark’s messages were odd but grammatically correct. He sounds like a person who has been awake for 40 hours wants to get off his chest about relationships.
Mark V. Shaney was a computer program. In fact, it was a fairly simple program designed by legendary programmer and author Rob Pike. It read text (a book and some Usenet postings) and created a table with two columns. The 1st column had each pair of words that appeared in the text next to one another, and the 2nd column had the word that came right after. To write a sentence, it looked up the last 2 words it had written, matched in the table, picked one of the words that had followed them in the table, added it, and repeated.
Here’s what it did. Suppose you “train” it on two sentences:
- “I am serious because it is critical.”
- “I am happy because it is funny.”
The program uses a sliding window that slides across words, 2 at a time and writes down what follows them. That is:
| Last 2 words | Followed by |
|---|---|
| I am | serious, happy |
| am serious | because |
| am happy | because |
| serious because | it |
| happy because | it |
| because it | is, is |
| it is | critical., funny. |
Now to generate new text, you pick a pair to start e.g. “I am”. It’s followed by 2 words in the table: “serious” and “happy”. Flip a coin and choose 1. Suppose “happy” wins. Now repeat with “am happy”, you get “because” (only choice). Keep going and you may get the following sentence:
I am happy because it is critical.
Notice that the output sentence wasn’t in your training example dataset of 2 lines (I use the word ‘training’ loosely here: training means running through each sentence and building the table above.) Your program generated something new. But what held Mark V. Shaney from taking over the world as an intelligent AI program in the 80s? Well, 2 things in my opinion (other than the fact it was created as a prank):
- Mark’s memory is severely limited to 2 words only. In generating the sentence, “I am happy because it is critical”, by the time Mark is picking “it is”, it has no memory that the sentence started with “I am serious”.
- Mark can only continue generating text it has seen before verbatim. E.g. If we ask Mark to generate starting with “He is” using our table above, Mark will fail because “He is” isn’t in the table.
And these problems fight with each other. Fix the 1st by giving Mark longer memory and the 2nd problem gets worse: with a 20 word window, almost no sequence typed by anyone will ever match in the table. You can’t algorithm your way out of these problems easily.
In some abstract sense, modern LLMs like ChatGPT or Bard work the same way. You give them some text, they predict the next word (token), append it to the sentence and repeat again. But the approach they take to predict the next word couldn’t be more different than Mark’s. Let’s look at how LLMs generate text in this post.
How LLMs guess the next word
Let’s massively simplify: strip everything away and think of an LLM as a single function. It takes a sequence of words (or tokens) as input and outputs a table of probabilities, one for every word it knows i.e. its vocabulary. The higher the probability the more likely it is that word is the one that comes next.
A quick word on ‘words’ and ‘tokens’: A token is a word in LLMs vocabulary. A token can be a word e.g. ‘cent’, part of a word ‘imeter’ or even punctuation.
Suppose you pass the following sequence of words as input to our LLM function:
The build broke on Friday because someone
The LLM function returns a table with one entry per word in its vocabulary: the word, and the probability that it goes next. If you sort it, the top few rows are the model’s best guesses; the rows at the bottom have probabilities so small they are nearly 0. The probabilities add to 100%. For our text above, we get the following made-up table:
| Next word | Probability |
|---|---|
| forgot | 14% |
| pushed | 11% |
| merged | 9% |
| changed | 7% |
| deployed | 6% |
| … | … |
| banana | 0.0001% |
Unlike Mark, the LLM isn’t looking up anything. Mark had a table of every word pair he’d ever seen, with the words that followed. An LLM has no such table and doesn’t store its training text. What it stores instead is billions of numbers (its parameters, or weights) that were selected during training. Send it “The build broke on Friday because someone” and it computes the table from scratch, even though it has almost certainly never seen that exact sentence before. That’s the difference between remembering (Mark) and generalizing (LLM).
More on tokens
Expanding on our earlier definition: a token is the primary unit which the model reads and writes. It can be a whole word, a piece of a word, or a punctuation mark. There’s no universal list of tokens. Each model builds its own vocabulary from its training data (before training starts), which is why the same sentence can be 12 tokens for one model and 15 for another. As a rule of thumb, common words get a token of their own and rare words get split into multiple.
Models use tokens rather than words for practical reasons. A fixed vocabulary of 30,000 to 100,000 can represent any string, including words the model has never seen, code, URLs and other languages.
Note: For developers, tokens matter because this is how OpenAI bills you: usage is priced on input and output tokens. Also, tokens define context limits.
How LLMs generate a complete sentence
In the previous section, we looked at how LLMs predict the next word (list of probabilities highest to lowest). But how does it generate poems, answers to multiple-choice questions, etc.
Here’s the algorithm. Note that we have covered steps 1-4 in the above section.
- Turn the input text into a list of tokens.
- Give the entire list to the model.
- The model returns a probability table: one number for every token in its vocabulary that it could produce next.
- Pick one token from the table.
- Add that token to the end of the list of input tokens.
- Stop if the token is special e.g. “end of text” or if the list has hit the length limit. Return the list.
- Otherwise, go back to step 2 with the longer list.
That’s it. Each pass through the loop produces exactly one token, and the model sees its previous output as part of the input on the next pass.
This is also known as autoregressive generation: each output becomes part of the next input. There are few consequences of it:
- The model has no plan. It just starts without deciding how the sentence would end.
- The system could stream tokens as they are generated in each step of the loop. This is how you see words appear one by one when you ask ChatGPT to improve your email to your manager.
- Cost grows with length. Because it appends new tokens to all the previously generated tokens and repeats, conversations get slower and more expensive towards the end, which is one reason why context limits exist.
- The loop ends when the model outputs an end-of-text token or the sequence reaches the max limit you defined. There’s no step where the model looks back at what it has written says ‘I’m done’ and stops. The end-of-text is just another token in the model’s vocabulary and gets its own probability in the table.
Now let’s look at one thing we hand-waved: how to pick the next token from the table of probabilities. While it may sound obvious to pick the token with the highest probability, in reality if we did that, we’d get very boring outputs.
Don’t always choose the highest probability
The obvious approach is to always choose the word with the highest probability (also known as greedy decoding). It’s a bad idea for LLMs: the output comes out bland and after a few sentences it starts to repeat itself over and over again as I’m sure you’ve seen. There’s research showing that even strong models degenerate when you maximize probability for every step. Greedy decoding is also why 2 people asking the same question would get the same answer every time, which is what the repetitive and boring version looks like and honestly if this is how ChatGPT worked, it wouldn’t have been as huge as it is.
The fix is to add some randomness. Instead of always choosing the top word (highest probability), the model rolls a weighted dice: a word with higher probability gets picked more and it’s possible to sometimes pick a word with lower probabilities.
How much randomness to add and how to add it? This is controlled by a parameter called temperature which you can pass to the model. For GPT, temperature ranges from 0 to 2. Pass it as 0 and the dice will always favor the top word (greedy decoding). Pass it as 2 and the answers will stop making sense. Pass it as somewhere in between 0 and 1, and you start getting better outputs.
Usually, a value from 0.7 to 1 works best in most cases. For some use cases e.g. if you are using GPT API for classification of some text documents, you’d want to use lower values or even 0 because you’d want the same answer (label) each time.
Note: This is what people refer to as “determinism” in LLMs. At lower values of temperature, the LLM outputs “mostly deterministic” text. At higher values, the models become “less deterministic”.
Where do the probabilities come from?
Mark V. Shaney got his from its table: look at every pair of words to find out what followed.
LLMs replace Mark’s table with a neural network (actually a very large and complex architecture of neural networks). Instead of storing what followed each exact context, the network has billions of adjustable numbers (called parameters) that turn any input into a probability table.
Training is conceptually simple: show it a chunk of text e.g. “the sky is …” have it predict the next word e.g. it might predict “blue” or “gray”, measure how wrong it was based on the answer in the training text, and then nudge it towards the right answer. Repeat this process over a staggering amount of text. GPT-3 has 175 billion parameters and was trained on ~300 billion tokens.
What I’ve described above is called pre-training. This is the first training step and you get a model that can complete text e.g. ‘the sky is ____’ => ‘the sky is blue’. This is not very useful as a Chatbot because it doesn’t follow instructions well. So the model goes through more rounds of smaller training called fine-tuning. There are two main steps.
-
First: the model is shown many examples of instructions and good responses (originally written by humans). E.g. model receives instruction such as “Write an email to my manager telling him I completed my Jira ticket” and it also sees what a good response looks like: “Dear Manager, I have completed my Jira ticket. Thank you. Yours Truly.”. This is called Supervised Fine-tuning (SFT).
-
Second: the model is asked to write several responses to the same instruction, e.g. 4 different versions of the email, and a human picks which one is better. These preferences are used to nudge the model towards the kind of answers people like and away from the ones they don’t. This is known as Reinforcement Learning from Human Feedback (RLHF).
These last two steps are what turned GPT from a text-completion model into ChatGPT - that can write your emails, generate code, write tech specs, PRDs and more.
So that’s the whole thing at a very high level. Hope you enjoyed the post and I sign off with Mark’s words from 1984: ‘I spent an interesting evening recently with a grain of salt.’