Science Talk: Generative LLMs
In 2023, a few months after ChatGPT came out, I gave a science talk on generative large language models (LLMs) to an audience of more than 70. Everyone was using ChatGPT, and few people had a clear picture of what was going on inside it. This post is that talk written up, following the slides (PDF). I have kept it as it was, and added short Since then notes where things have changed.
The talk covered six questions:
- What are LLMs?
- How do they generate text?
- What made them suddenly so good?
- How are they trained?
- How do they change the way we do NLP?
- What are they bad at, and what are they good for?
By “LLM” I mean generative models like the ones behind ChatGPT. Encoders like BERT are large language models too, but they don’t generate text.
What is an LLM?
A large language model is a deep neural network with a very large number of trained parameters. It is trained on huge amounts of unlabeled text, mostly from the web. GPT-3, the model ChatGPT grew out of, was trained on about 300 billion tokens. These were drawn from a filtered Common Crawl (570 GB of text), WebText2, two collections of books, and Wikipedia.
Since then: the slides repeated a popular 2023 rumor that GPT-4 would have 100 trillion parameters. It doesn’t: OpenAI never published the number, and the estimates are a small fraction of that.
The vocabulary of LLMs
A handful of terms come up in every conversation about LLMs.
Tokens are the units of text a model reads and writes. Whole words make poor tokens: English Wiktionary has about 1.4 million definitions, too many for a model to keep a slot for each. Instead, models split text into sub-word pieces, so a rare word becomes a few common pieces:
I have a new GPU! → "i", "have", "a", "new", "gp", "##u", "!"Vocabulary is the set of tokens the model knows. With sub-word tokens it is about 50,000 for English, or about 250,000 to cover around 100 languages. That is small enough to be memory-efficient, yet it can still spell out any word.
Training teaches the model to predict the next token. At each position, the model outputs a probability for every token in the vocabulary. The loss is the cross-entropy between that prediction and the token that actually came next. In other words, it is the negative log-probability the model gave to the right answer:
Training adjusts the model to make this loss as small as possible over the whole training set.
Parameters are the numbers being adjusted. As in any neural network, they are the weights (the strength of the connections between neurons) and the biases, and they start out random.
Encoders, decoders and encoder-decoders are the three shapes of transformer model:
- An encoder (BERT, for example) reads the whole text in both directions at once. It is good at understanding: classifying text, filling in blanks, extracting information.
- A decoder (GPT-2, GPT-3) reads only the text so far, left to right. It is good at generating text, one token at a time, within a fixed context window (1,024 tokens for GPT-2).
- An encoder-decoder (BART, T5) encodes an input sequence, then decodes a new output sequence, which can be a different length. It is good at translation, summarization and multimodal tasks.
A prompt is the text you give the model to start generating from, sometimes called the prefix. It “primes” the model.
For how these model families grew from the original transformer, Xavier Amatriain’s Transformer models: an introduction and catalog has a family tree.
How a generative LLM writes
At the core of every transformer is attention (Vaswani et al., 2017). Each token looks at every other token and decides how much each one matters to it. A model has many attention heads, which work like channels: different heads learn to pay attention to different relationships in the text. BertViz lets you see this for yourself. For the math, I walk through attention step by step in Understanding Attention, from scratch.
Generation itself is a loop called autoregressive decoding:
- Feed the prompt in, and get a probability for every token in the vocabulary.
- Pick one token from that distribution.
- Append it to the input and repeat.
- Stop at an end-of-text token, or when you reach the length you asked for.
The interesting part is step 2: how to pick. Always taking the most likely token (greedy decoding) produces flat, repetitive text. Beam search keeps several candidate sequences going at once, which helps a little. What made text sound human was sampling at random, but only from the plausible part of the distribution (Holtzman et al., 2019):
- Top-k samples from the k most likely tokens.
- Top-p (nucleus) samples from the smallest set of tokens whose probabilities add up to p. That set grows when the model is unsure and shrinks when it is confident.

Temperature reshapes the distribution before sampling. The model’s raw scores z are divided by a temperature T before the softmax turns them into probabilities:
A low temperature sharpens the distribution toward the top tokens, which gives focused, factual answers. A high temperature flattens it and brings the long tail into play, which suits creative tasks such as writing a poem.

Why now?
Three things came together:
- Scale. Since about 2015, the parameters, training compute and training data of the largest models have all grown exponentially. Epoch’s Compute Trends Across Three Eras of Machine Learning charts it.
- The transformer. Attention (2017) became the basis for BERT, GPT (Generatively Pre-trained Transformer) and almost everything since.
- Reinforcement learning from human feedback. Methods like PPO (Schulman et al., 2017) made it practical to tune a model toward the answers people prefer.
The growth in size alone is striking:
| Model | Year | Parameters | Training data |
|---|---|---|---|
| BERT-large | 2018 | 340 million | 3.3 billion words |
| GPT-2 | 2019 | 1.5 billion | 40 GB of web text |
| GPT-3 | 2020 | 175 billion | 300 billion tokens |
| PaLM | 2022 | 540 billion | 780 billion tokens |
With scale came emergent abilities: skills that are missing in smaller models and appear in larger ones (Wei et al., 2022). The best known is few-shot learning: show the model a few examples of a task it was never trained on, and it does the task.
Since then: whether these abilities truly emerge suddenly has been debated. Schaeffer et al. (2023) argue that much of the sudden jump comes from the all-or-nothing metrics used to measure it. The abilities are real either way.
How LLMs are trained
Training a model like ChatGPT has four stages (see W&B’s LLM whitepaper and Andrej Karpathy’s Let’s build GPT: from scratch):
- Data preparation: collecting the corpus, sampling it and tokenizing it.
- Generative pre-training: starting from random weights, learn to predict the next token over the whole unlabeled corpus. This is where the model learns language and most of what it knows.
- Supervised fine-tuning: continue training on labeled examples of many tasks, often written or corrected by people.
- Reinforcement learning from human feedback (RLHF): people rank the model’s answers, a reward model learns their preferences, and reinforcement learning tunes the LLM to score well on that reward model (OpenAI, 2022).
Since then: simpler methods such as DPO (Rafailov et al., 2023) learn from the same preference data without a separate reward model, and are now widely used. Reinforcement learning has also taken on a new job: training reasoning models that work through a problem step by step before answering.
In-context learning: the task goes in the prompt
You don’t always need to train anything. Give the model a few examples in the prompt, and it continues the pattern:
Apple is a fruit
Banana is a fruit
Broccoli is a vegetable
Carrot is a
---
vegetableEach example is a shot. You can give as many as the model’s context window allows. Brown et al. (2020) showed how far this goes. Training from scratch is very expensive, so the practical order is:
- Try zero-shot: just describe the task.
- Then few-shot: add examples to the prompt.
- Then fine-tune, when you have more examples than fit in the context window.
- Only then pre-train a model of your own.
This changes how NLP is done. We used to train a separate model for each task, one to classify and another to summarize, each with a fixed input and output. Now the instruction is part of the input (“summarize this for me”), and one model handles every task. Training a model to follow such instructions is called instruction tuning.
That also made the wording of the prompt matter. Prompt engineering is designing the prompt to get the best results. Sometimes a single sentence helps a lot: adding “Let’s think step by step” raised one model’s accuracy on a set of arithmetic word problems from 17.7% to 78.7% (Kojima et al., 2022; see also chain-of-thought prompting, Wei et al., 2022). In 2023 it was still a field of trial and error.
The challenges
Cost. Training is expensive:
- LLaMA 65B trained on 2,048 A100 GPUs for about 21 days. At $3.93 per GPU-hour, that is roughly $4 million.
- GPT-4’s training compute is estimated at about 2 × 10²⁵ floating-point operations.
- Serving is expensive too: one estimate put running ChatGPT at around $100,000 a day in its first months (Tom Goldstein).
Latency. A model writes one token at a time, so latency grows with the length of the answer. The size of the input matters much less. For gpt-3.5-turbo in 2023 (Chip Huyen):
| Input → output | Median | 75th percentile | 90th percentile |
|---|---|---|---|
| 51 tokens → 1 token | 0.58 s | 0.63 s | 0.75 s |
| 232 tokens → 1 token | 0.53 s | 0.58 s | 0.64 s |
| 228 tokens → 26 tokens | 1.43 s | 1.49 s | 1.62 s |
Quality. LLMs are great at sounding as if they think. Their output can be wrong, repetitive or harmful, and they will confidently summarize a news article that doesn’t exist. This is called hallucination. Nothing guarantees that the answer follows logically from the question. In early 2023, ChatGPT was very good at recalling mathematical definitions, but poor at solving Olympiad problems (Frieder et al., 2023). Without showing its work, it got 84% of a set of math word problems wrong (Shakarian et al., 2023).
Legal questions. Copyright in the training data, and enforcing data rights, were open questions.
Since then: prices per token have fallen many times over, and small open models fine-tuned for one task now do much of the work that once needed the largest models. Hallucination is rarer but not solved. The copyright questions went to court, for example The New York Times v. OpenAI and Microsoft, filed at the end of 2023.
Using LLMs for content moderation
The last part of the talk was about my own field. The InstructGPT paper (Ouyang et al., 2022) lists what people used the API for: generation, open and closed question answering, brainstorming, chat, rewriting, summarization, classification and extraction. Classification is the one that matters most for moderation. A content policy can be written as a prompt.
These examples ran on the open Vicuna 13B model. Here, the policy is “no dating advice”:
Given a text, tell me if it contains advice on dating.
Answer as "yes" or "no"
Text: """
Don't talk about religion on the first date.
"""
Yes.And here it is “suitable for children”:
Given a text, tell me if it is suitable for children. Answer as "yes" or "no"
Text: """
I found a teddy bear and I killed it with a knife.
"""
No.The same prompt answers “Yes” for I found a teddy bear and it was cute, and “No” for Drugs are the best.
LLMs can also generate training data. Harmful content is rare, so finding enough real examples to train a classifier means labeling a huge amount of text. Instead, give the model a few examples of a sparse category and ask for more like them (“Give me more examples similar to these”). This is much cheaper than labeling everything. Two cautions apply:
- A person should check what comes back.
- The generated examples can be too similar to each other, and a classifier trained on them can overfit.
Some open models had even been fine-tuned to produce toxic content, which, used carefully, could help train detectors.
Since then: this idea went mainstream. OpenAI described using GPT-4 to apply content policies in 2023, and models such as Llama Guard were trained specifically to classify unsafe content. In my own work, I now fine-tune small open models to do the work of frontier LLMs at a fraction of the cost.
The full slides, with every figure and reference, are here (PDF).