Level 11 · AdvancedLesson 11.1 42 min read 10 question quiz 11 marks of written practice

How Large Language Models Are Built

What makes a model large

A large language model (LLM) is a decoder-only transformer, exactly as in Level 10, trained to predict the next token on an enormous amount of text. "Large" describes three things at once: the number of parameters, the number of training tokens, and the compute needed to train it.

YearModelParametersTraining tokens
2020GPT-3175 billion300 billion
2021Gopher280 billion300 billion
2022Chinchilla70 billion1.4 trillion
2023Llama 27 to 70 billion2 trillion
2024Llama 38 to 405 billionover 15 trillion

Turning one of these into a helpful assistant takes three stages:

Building a language model assistantThree stages left to right, each a 3D block under a drum of data whose size shows how much there is. Pretraining, in blue: next-token prediction on trillions of tokens of web pages, books and code, using most of the compute; it produces a base model. Instruction tuning, in violet: supervised fine-tuning on example conversations, a prompt and an ideal reply, using a small fraction of the compute. Preference tuning, in pink: RLHF or DPO on people's comparisons of answers. Below, asked what the capital of France is, the base model continues with more quiz questions; the instruction-tuned model answers that it is Paris; the preference-tuned model answers and offers more help.From a transformer to an assistant: three stages of trainingtrillions of tokensweb, books, codePretrainingnext-token predictionmost of the computeexample conversationsprompt + ideal replyInstruction tuningsupervised fine-tuninga small fractionpeople's comparisonsanswer A or answer B?Preference tuningRLHF or DPOa small fractionbase modelassistantAsked “What is the capital of France?”, the model replies:“What is the capital ofGermany? What is the …”“The capital of Franceis Paris.”“The capital of France isParis. Want to know more?”
Knowledge comes from pretraining; the later stages shape how it is used.

Stage 1: pretraining

Pretraining is next-token prediction on as much good text as can be found: web pages, books, encyclopaedias, scientific papers and program code. Before training, the data is cleaned: duplicates are removed, low-quality pages are filtered out, and attempts are made to strip personal information and harmful content.

A pretraining data mixA donut chart of the share of training text sampled from each source for Meta's LLaMA models, 1.4 trillion tokens in all: CommonCrawl web pages 67%, C4 cleaned web text 15%, GitHub program code 4.5%, Wikipedia 4.5%, books 4.5%, arXiv science papers 2.5%, and StackExchange questions and answers 2%. A row of steps shows how raw text is cleaned before training: remove duplicates, filter low quality, strip personal data, tokenise.What a model reads: the training mix of Meta's LLaMA (2023)1.4 trilliontokensCommonCrawlweb pages67%C4cleaned web text15%GitHubprogram code4.5%Wikipediaencyclopaedia4.5%Bookstwo book collections4.5%arXivscience papers2.5%StackExchangequestions and answers2%Before training, the text is cleaned:raw textremove duplicatesfilter low qualitystrip personal datatokenise
Over four fifths of the text came from the web, after heavy filtering.

Every position in every document is a training example, because the label is simply the next token. No human has to label anything, which is why this is called self-supervised learning. A trillion-token dataset is a trillion examples.

To predict the next token well across that much text, a model has to absorb grammar, facts, styles, and patterns of reasoning. Predicting the end of "The capital of France is" needs a fact; predicting the end of a proof needs some maths. The result is a base model: it continues text brilliantly, but it is not yet an assistant.

Counting the compute

Training a transformer with N parameters on D tokens takes about

Training compute

operations ≈ 6 × N × D (2 × N × D for the forward passes, about 4 × N × D for the backward passes)

For GPT-3: 6 × 175 billion × 300 billion ≈ 3.2 × 1023 operations. A modern GPU might sustain about 1014 operations per second in training, so that is around 3.2 × 109 seconds: about 100 years on one GPU, or roughly 36 days on 1,000 GPUs working together.

That is why frontier models are trained on clusters of thousands of GPUs for months, and why the electricity and hardware cost millions of pounds. The model, its training data and its optimiser state are split across the machines in several ways at once, which Level 14 returns to.

Scaling laws

In 2020, researchers at OpenAI published a striking result: the loss of a language model falls smoothly and predictably as you increase the parameters, the data or the compute, following a power law. On a log-log graph, the trend is a straight line, so a few small, cheap training runs predict how well a far bigger one will do.

Scaling lawsA graph of loss, from 1.6 to 4, against training compute on a log scale from 10 to the 18 to 10 to the 25 operations. Five curves, for models of 100 million, 1 billion, 10 billion, 100 billion and 1 trillion parameters, each fall as compute grows and then level off at a floor set by the model's size; bigger models start higher but reach lower. A dashed amber line along the lower edge of all the curves is the compute-optimal frontier, a smooth downward trend. Chinchilla, 70 billion parameters on 1.4 trillion tokens, sits on the frontier, below Gopher, 280 billion parameters on 300 billion tokens, which used about the same compute.Scaling laws: loss falls predictably as training compute grows22.533.54training compute, operations (log scale)10181019102010211022102310241025loss100M1B10B100B1TChinchillaGophereach curve is onemodel size, from100 million (100M)to 1 trillion (1T)the best size foreach budget: thefrontier
Curves from the loss formula fitted in the Chinchilla paper (2022): the frontier is the best you can do with each budget.

In 2022, DeepMind's Chinchilla paper showed that most large models had been undertrained: for a fixed compute budget, it is better to train a smaller model on more tokens, at roughly 20 tokens per parameter. Chinchilla, with 70 billion parameters and 1.4 trillion tokens, beat the 280 billion parameter Gopher, which had used about the same compute on only 300 billion tokens.

Many models now go far beyond 20 tokens per parameter on purpose. A smaller model is cheaper every time anyone uses it, so it is worth spending extra training compute to make it as good as possible: Llama 3's 8 billion parameter model saw about 1,900 tokens per parameter.

In-context learning

The GPT-3 paper was titled "Language Models are Few-Shot Learners". It showed that a large enough model can do a new task from a few examples written into the prompt, with no change to its weights:

PromptThe model continues
sea otter → loutre de mer; cheese → fromage; peppermint →menthe poivrée

This in-context learning gets better with scale, and some abilities seem to appear only once models are large enough. Researchers still debate how sudden these jumps really are: part of the suddenness turned out to come from how the abilities were scored.

Stage 2: instruction tuning

Ask a base model "What is the capital of France?" and it may well continue with more quiz questions, because that is what such a line is usually followed by on the web. It predicts text; it does not follow instructions.

Supervised fine-tuning (SFT), also called instruction tuning, continues training on tens of thousands to millions of example conversations: a prompt, then an ideal response, written or checked by people. The loss is the same next-token loss, applied only to the responses.

PromptBase modelAfter instruction tuning
What is the capital of France?What is the capital of Germany? What is the capital of Spain?The capital of France is Paris.

The model learns the format of being an assistant: answer the question, follow the instruction, stop when finished. Most of what it knows still comes from pretraining.

Stage 3: learning from preferences

Some qualities are easier to recognise than to write down: which of two explanations is clearer, kinder or more honest. Reinforcement learning from human feedback (RLHF) uses exactly that:

RLHFA loop. A prompt goes into the violet language model, which writes two answers: A, short and unclear, and B, clear and correct. A person compares them and picks B. The comparison trains an orange reward model to predict which answer wins. A pink arrow returns to the language model: reinforcement learning changes it so the reward model scores its answers higher, with a penalty for drifting far from the original model so that it cannot game the reward.Reinforcement learning from human feedbacka promptlanguagemodelanswer Ashort and unclearanswer Bclear and correcta person picks Brewardmodellearns to predictwhich answer winsreinforcement learning: change the model so the reward model scores its answers higherwith a penalty for drifting far from the original, so it cannot game the reward
People are better at judging answers than writing perfect ones, so RLHF learns from their judgements.
  1. The model writes several answers to the same prompt.
  2. People compare them and pick the better one.
  3. A reward model, another network, learns to predict which answers people prefer.
  4. The LLM is fine-tuned with reinforcement learning to write answers the reward model scores highly, with a penalty for drifting too far from the instruction-tuned model, so that it cannot game the reward model with strange text.

In 2022, OpenAI reported that people preferred the answers of a 1.3 billion parameter model trained this way, InstructGPT, to those of the original 175 billion parameter GPT-3. Newer methods such as DPO (2023) learn directly from the preference pairs without a separate reward model, and the feedback can also come from AI models judging answers against written principles.

Since 2024, reasoning models have added another kind of reinforcement learning: on maths and programming problems whose answers can be checked automatically, the model is rewarded for reaching the correct answer, and it learns to write out long chains of reasoning before it answers.

Why language models make things up

A language model is trained to produce likely text, not true text. When it does not know something, the most likely-sounding continuation is often a confident, specific answer, because confident answers are common in its training data. This is called hallucination.

  • Facts that appeared rarely in training are stored weakly and are easily confused.
  • The model knows nothing after its training data was collected, its knowledge cutoff.
  • Unless it was trained to say "I don't know", it rarely does.

Defences include training models to admit uncertainty, letting them look things up (Lesson 11.2), asking them to cite sources, and checking anything important yourself.

Hands-on: the numbers behind LLMs

Training compute

Python
models = [("GPT-3", 175e9, 300e9), ("Gopher", 280e9, 300e9),
          ("Chinchilla", 70e9, 1.4e12), ("Llama 3 8B", 8e9, 15e12)]
gpu = 1e14          # operations per second one modern GPU might sustain in training

for name, n, d in models:
    flops = 6 * n * d
    gpu_years = flops / gpu / (365 * 24 * 3600)
    print(f"{name:11} {n / 1e9:5.0f}B parameters, {d / 1e12:5.2f}T tokens: "
          f"{flops:.1e} operations, {gpu_years:5.0f} GPU-years, {d / n:7.1f} tokens per parameter")

Fitting a scaling law

Five small training runs are enough to fit the straight line on log-log axes, and the line predicts runs a thousand times bigger.

Python
import math, random

random.seed(2)
compute = [1e18, 1e19, 1e20, 1e21, 1e22]                       # operations used by five small runs
loss = [60 * c ** -0.07 * random.uniform(0.98, 1.02) for c in compute]

# A power law is a straight line on log-log axes: log(loss) = log(a) - alpha * log(compute)
xs = [math.log10(c) for c in compute]
ys = [math.log10(v) for v in loss]
n = len(xs)
mx, my = sum(xs) / n, sum(ys) / n
slope = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sum((x - mx) ** 2 for x in xs)
intercept = my - slope * mx
print(f"fitted exponent alpha = {-slope:.3f}")
print(f"each 10x more compute multiplies the loss by {10 ** slope:.3f}")
for c in [1e23, 1e24, 1e25]:
    print(f"predicted loss at {c:.0e} operations: {10 ** (intercept + slope * math.log10(c)):.2f}")

A reward model from people's choices

This is the heart of RLHF in miniature. Each answer is described by three features, and people have compared pairs of answers. The model learns weights so that preferred answers score higher, using the same logistic idea as Level 4.

Python
import math

# Each answer described by three features: [correct, polite, length in hundreds of words]
answers = {"A": [1, 1, 0.8], "B": [1, 0, 0.5], "C": [0, 1, 1.2], "D": [0, 0, 0.3], "E": [1, 1, 2.5]}
# People compared two answers at a time and picked a winner: (winner, loser)
preferences = [("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"),
               ("A", "D"), ("E", "C"), ("A", "E"), ("B", "C")]

w = [0.0, 0.0, 0.0]
def reward(x):
    return sum(wi * xi for wi, xi in zip(w, x))

for epoch in range(300):
    for win, lose in preferences:
        xw, xl = answers[win], answers[lose]
        p = 1 / (1 + math.exp(-(reward(xw) - reward(xl))))    # chance the model agrees with the person
        for i in range(3):
            w[i] += 0.1 * ((1 - p) * (xw[i] - xl[i]) - 0.01 * w[i])

print("learned weights: correct %.2f, polite %.2f, length %.2f" % tuple(w))
for name, x in sorted(answers.items(), key=lambda kv: -reward(kv[1])):
    print(f"answer {name}: reward {reward(x):5.2f}")

Nobody told the reward model that correctness matters most, or that rambling is bad. It worked both out from which answers people chose.

Try this

  1. Use 6 × N × D to estimate the compute for a 405 billion parameter model trained on 15 trillion tokens. How many GPU-years is that?
  2. Add a preference ("E", "B"), saying a long, correct, polite answer beats a short, correct, rude one. How do the learned weights change?

Level check

  • Describe pretraining, and explain why it is self-supervised
  • Estimate training compute with 6 × N × D, and turn it into GPU time
  • Explain what a scaling law shows, and the Chinchilla rule of about 20 tokens per parameter
  • Explain in-context learning
  • Describe instruction tuning and why a base model needs it
  • Describe the four steps of RLHF, including the reward model
  • Explain why language models hallucinate, and ways to reduce it

Key terms you must be able to define

Large language model (LLM)
A transformer with billions of parameters trained to predict the next token on vast amounts of text.
Pretraining
The first and largest stage of training: next-token prediction on trillions of tokens.
Base model
A pretrained model that continues text but has not been tuned to follow instructions.
Self-supervised learning
Learning from labels that come from the data itself, such as the next token of a text.
Training compute
The number of arithmetic operations used in training, about 6 × parameters × tokens for a transformer.
Scaling law
A power law showing how loss falls predictably as parameters, data and compute grow.
Compute-optimal
The best balance of model size and data for a compute budget: about 20 tokens per parameter.
In-context learning
Doing a new task from examples in the prompt, without changing the model's weights.
Supervised fine-tuning (SFT)
Further training on example conversations so the model responds as an assistant.
RLHF
Reinforcement learning from human feedback: tuning a model towards answers people prefer.
Reward model
A network trained on people's comparisons to score how good an answer is.
Hallucination
A fluent, confident statement that is false or unsupported.

Flashcards

DefineLarge language model (LLM)
AnswerA transformer with billions of parameters trained to predict the next token on vast amounts of text.
DefinePretraining
AnswerThe first and largest stage of training: next-token prediction on trillions of tokens.
DefineBase model
AnswerA pretrained model that continues text but has not been tuned to follow instructions.
DefineSelf-supervised learning
AnswerLearning from labels that come from the data itself, such as the next token of a text.
DefineTraining compute
AnswerThe number of arithmetic operations used in training, about 6 × parameters × tokens for a transformer.
DefineScaling law
AnswerA power law showing how loss falls predictably as parameters, data and compute grow.
DefineCompute-optimal
AnswerThe best balance of model size and data for a compute budget: about 20 tokens per parameter.
DefineIn-context learning
AnswerDoing a new task from examples in the prompt, without changing the model's weights.

Go further: notes for future AI engineers

Training once, serving forever

Training happens once, but inference, running the model to answer people, happens every time anyone uses it. For a popular model, the total compute spent serving users can exceed the training compute. That is why companies deliberately "over-train" smaller models far beyond the Chinchilla ratio: every extra point of quality in a small model is paid for once, and saved on every request.

Synthetic data

High-quality human text is running short, so models are increasingly trained on text generated by other models and then filtered, especially for maths and code, where answers can be checked. Done carelessly, training generation after generation on model output loses the rare and unusual parts of the original data. A 2024 paper in Nature called this model collapse.

Test-time compute

Reasoning models showed a second way to scale. Instead of only making the model bigger, let it think longer: spend more tokens of reasoning on harder problems. Accuracy on difficult maths and programming problems keeps rising with the amount of thinking allowed, so a model can trade answer speed for answer quality.

Misconceptions to avoid

Avoid these
  • Thinking a chatbot looks facts up in a database. Unless it is given a search or retrieval tool, it generates answers from patterns stored in its weights.
  • Believing bigger is always better. For a fixed compute budget, a smaller model trained on more data can win.
  • Thinking RLHF teaches a model most of its knowledge. That comes from pretraining; RLHF mainly shapes how it responds.
  • Assuming a model knows about events after its training data was collected.
  • Confusing parameters with training tokens. Parameters are the model's own numbers; tokens are the text it learned from.
Fun fact

GPT-3, released in 2020, was trained on about 300 billion tokens. Four years later, Meta trained its Llama 3 models on more than 15 trillion: fifty times as much text.

Knowledge check

0 of 10 answered
1What is a base model trained to do during pretraining?
2Why is pretraining called self-supervised?
3Roughly how many operations does it take to train a transformer with N parameters on D tokens?
4What did the Chinchilla paper (2022) find?
5What does a scaling law describe?
6What is in-context learning?
7What does supervised fine-tuning teach a pretrained model?
8In RLHF, what does the reward model learn?
9Why is a penalty for drifting away from the instruction-tuned model used in RLHF?
10Why do language models sometimes state false things confidently?
Score 0 / 10

Check you could explain this

11 marks
1Describe the three stages used to turn a transformer into a chat assistant.5 marks
Describe

Mark scheme0 / 5
  • LPretraining: next-token prediction on a vast amount of text
  • LThis produces a base model that continues text but does not follow instructions
  • LSupervised fine-tuning / instruction tuning on example conversations or prompt and response pairs
  • LPreference tuning: people compare or rank answers
  • LA reward model learns the preferences and reinforcement learning tunes the model to score highly
  • LThe aim of the later stages is helpful, honest, harmless answers
Model answer

First, pretraining trains the transformer to predict the next token on trillions of tokens of text, which gives a base model that knows a great deal but simply continues text rather than following instructions. Second, supervised fine-tuning, or instruction tuning, trains it further on example conversations made of prompts and ideal responses, so it learns to behave as an assistant. Third, preference tuning such as RLHF has people compare pairs of the model's answers; a reward model learns to predict which answers people prefer, and reinforcement learning then tunes the model towards answers that score highly, making it more helpful and less harmful.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

2A company has a fixed compute budget for training a language model. Explain how scaling laws help it choose the model's size and the amount of training data.3 marks
Explain

Mark scheme0 / 3
  • LLoss falls predictably as a power law with compute, so small runs can predict large ones
  • LFor a fixed budget, model size and data should be balanced, about 20 tokens per parameter
  • LA model that is too large for its data is undertrained / wastes compute
  • LA smaller model trained on more data is cheaper to run afterwards
Model answer

Scaling laws show that loss falls smoothly, as a power law, as compute grows, so the company can run a few small, cheap experiments and predict the loss of the full-size model before spending the budget. The Chinchilla result shows that, for a fixed budget, parameters and training tokens should grow together, at roughly 20 tokens per parameter, because a model that is too big for its data is undertrained and wastes compute. The company might even choose a smaller model trained on more tokens than that, since a smaller model is cheaper every time it is used.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

3A model has 10 billion parameters and is trained on 200 billion tokens, using 500 GPUs that each sustain 1014 operations per second. Calculate the training time in days.3 marks
Calculate

Mark scheme0 / 3
  • LCompute = 6 × 1010 × 2 × 1011 = 1.2 × 1022 operations
  • LCombined speed = 500 × 1014 = 5 × 1016 operations per second
  • LTime = 2.4 × 105 seconds ≈ 2.8 days
Model answer

Training compute is about 6 × N × D = 6 × 10,000,000,000 × 200,000,000,000 = 1.2 × 1022 operations. Together the GPUs perform 500 × 1014 = 5 × 1016 operations per second. The time is 1.2 × 1022 ÷ (5 × 1016) = 240,000 seconds, and 240,000 ÷ 86,400 ≈ 2.8 days.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

Machine mark 0 / 11