One task, trillions of times
Pretraining means training a model, from random weights, to predict the next token across an enormous pile of text. The loss is the cross-entropy from the probability lesson: how surprised the model was by each real next token. That is the whole task. Everything else a model can do grows out of getting very good at it.
| Text so far | To guess the next token well, the model needs |
|---|---|
| The capital of Pakistan is | Facts about the world |
| 2 + 3 × 4 = | Arithmetic, and the order of operations |
| def is_even(n): return n % 2 == | How Python works |
| She hadn’t slept for two days, so she was | Common sense about people |
| Il pleut, alors je prends mon | French |
From each paper or model card. 15 trillion tokens is about 11 trillion English words: far more than a person could read in thousands of lifetimes.
From web pages to training tokens
Most text comes from the web, usually starting from Common Crawl, a free archive that has been saving billions of web pages since 2008. Raw pages are mostly menus, adverts, duplicates and junk, so labs run them through a long pipeline.
- CrawlBillions of pages, in snapshots taken over many years
- Extract the textStrip the HTML, menus and buttons
- Identify the languageA small classifier labels every page
- Remove duplicatesSame URL, near-identical pages (MinHash), and lines repeated across many pages
- Filter for qualitySimple rules, then classifiers trained to spot good pages
- Remove unsafe and personal dataAdult sites, pages full of addresses and phone numbers
- Mix and tokenizeDecide how much web, code, maths and each language to use
Hugging Face did the same work in the open. FineWeb is 15 trillion tokens from 96 Common Crawl snapshots. Then Llama-3-70B scored a sample of pages from 0 to 5 for how educational they were, a small classifier learned those scores, and only pages scoring 3 or more were kept. The result, FineWeb-Edu, is 1.3 trillion tokens, less than a tenth of FineWeb, and models trained on it do dramatically better on knowledge and reasoning tests.
Should each piece of text stay in the training data?
-
A clear explanation of how vaccines work
Answer: Keep
-
The same cookie banner, found on a million pages
Answer: Remove
-
The 499 extra copies of a news article posted on 500 sites
Answer: Remove
-
Well-commented open-source Python code
Answer: Keep
-
A page listing people’s home addresses and phone numbers
Answer: Remove
-
A long list of keywords made to trick search engines
Answer: Remove
-
A maths textbook chapter with worked examples
Answer: Keep
Only 8 % of Llama 3’s tokens were not English. This is one reason models are often weaker in Urdu, Bengali or Hausa: they saw far less of them. Qwen3 trained on 119 languages and dialects.
When the web runs out: PDFs and rewriting
Good text is limited, and the biggest labs have already used most of the clean web. So they look for more in two places.
- Documents that are hard to read. For Qwen3, Alibaba fine-tuned its vision model Qwen2.5-VL to pull the text out of huge numbers of PDF files.
- Synthetic data. Models write training text for other models: Qwen used its maths and coding models to generate maths and code data. Microsoft’s phi-1 (2023), a 1.3B coding model, was trained partly on textbooks and exercises written by an AI.
| How the same knowledge text was used | Accuracy on a fact quiz (SimpleQA) |
|---|---|
| The original text, read 10 times | 23.76 % |
| Rewritten once, read 10 times | 27.39 % |
| Rewritten 10 different ways, each read once | 28.94 % |
Moonshot asked a model to rephrase knowledge-heavy texts in different styles and from different angles, checking each rewrite against the original. Reading many versions taught more than rereading one.
Scaling laws: how big, and for how long?
In 2020, a team at OpenAI found that a model’s loss falls along a smooth curve, a power law, as you add parameters, data or compute, with some trends holding across more than seven orders of magnitude. That made AI plannable: train small models, fit the curve, and predict how good a huge one will be before spending the money.
C ≈ 6 × N × DC is computation in operations, N the parameters (active ones, for MoE), D the training tokens. The 6 is 2 for the forward pass and 4 for the backward pass, as in the backprop lesson.
Llama 3.1 405B: 6 × 405 × 10⁹ × 15.6 × 10¹² ≈ 3.8 × 10²⁵Exactly the figure in Meta’s paper.
But how should a fixed budget be split between a bigger model and more data? In 2022 DeepMind trained over 400 models and found that both should grow together: double the model, double the data. The rule of thumb is about 20 tokens per parameter. Their 70B model Chinchilla, trained on 1.4 trillion tokens, beat their 280B model Gopher, which had used the same compute on fewer tokens.
Try the real runs, then make your own. Watch how fast the GPU-days add up.
This interactive part needs JavaScript.
Quick check
Training in stages
A pretraining run is not one flat pass. The data mix and settings change as it goes, usually from broad to sharp.
- A broad foundation. Qwen3’s first stage used about 30 trillion tokens of general text, at a context of 4,096 tokens.
- Knowledge and reasoning. The second stage shifted towards knowledge-heavy text in science, maths and code.
- Long context. The third stage trained on long documents, raising the context from 4,096 to 32,768 tokens. DeepSeek-V4 went 4K → 16K → 64K → 1M.
- Annealing. At the very end, the learning rate drops towards zero while the best data is mixed in. Llama 3 did this over its final 40 million tokens, then averaged several saved copies of the model.
Thousands of GPUs, one model
A 405B model in BF16 is 810 GB before any training memory is added, and an H100 holds 80 GB. So the model and the work are split up in several ways at once.
| What is split | Used for | |
|---|---|---|
| Data parallel | Each group of GPUs reads different batches; their gradients are averaged | Using more GPUs to go faster |
| Tensor parallel | Each big matrix is cut into pieces on different GPUs | Layers too big for one GPU; needs the fast links inside one server |
| Pipeline parallel | Different layers on different GPUs; small batches flow through like an assembly line | Very deep models |
| Expert parallel | Different experts on different GPUs | Mixture-of-experts models |
| Context parallel | One long sequence is split across GPUs | Long-context training |
Llama 3 405B combined four of these on up to about 16,000 H100 GPUs, which reached 38–43 % of their theoretical peak speed.
What comes out: a base model
The result of pretraining is a base model. It knows a great deal, but it is not an assistant: it continues documents. Give it a question, and it may well continue with more questions, because on the web questions often come in lists.
Base model
- Continues any text in the style it sees
- “What is the capital of France?” might go on with “What is the capital of Spain?”
- Most useful to researchers and for fine-tuning
- Released by many labs: DeepSeek-V3-Base, Kimi K2 Base, Qwen3 base models
Chat or instruct model
- Answers, follows instructions, uses tools
- “What is the capital of France?” gets “Paris.”
- Made from the base model by post-training
- What you use in apps and agents
Turning a base model into a helpful assistant takes much less computation than pretraining, but a great deal of care. That is the next lesson.
Remember
- Pretraining teaches a model, from random weights, to predict the next token over trillions of tokens. Nearly all its knowledge comes from here.
- Raw web pages go through a pipeline: extract, identify the language, remove duplicates, filter for quality, remove unsafe and personal data, then mix.
- Training compute is about 6 × N × D. Chinchilla: about 20 tokens per parameter is compute-optimal, but small models are trained far longer to be cheap to run.
- When good text runs out, labs extract PDFs and write synthetic or rephrased data.
- Training is split with data, tensor, pipeline, expert and context parallelism across thousands of GPUs, and failures happen daily.
- The output is a base model that continues text. Post-training turns it into an assistant.
Words to know
- Pretraining
- The first and largest training stage: next-token prediction on trillions of tokens.
- Base model
- The model straight after pretraining. It continues text; it does not follow instructions.
- Common Crawl
- A free, public archive of billions of web pages, the starting point for most training data.
- Deduplication
- Removing repeated pages and lines, so the model does not memorise them.
- Quality classifier
- A small model that scores pages, so that only good ones are kept.
- Data mix
- The share of each kind of data: web, code, maths, each language.
- Synthetic data
- Training text written by an AI model, not by people.
- Scaling law
- A smooth formula that predicts a model’s loss from its size, data and compute.
- Compute-optimal
- The split between model size and data that gives the lowest loss for a fixed training budget.
- Annealing
- The end of training, when the learning rate falls and the best data is mixed in.
- Data parallelism
- Many copies of the model, each training on different data, averaging their gradients.
- Checkpoint
- A saved copy of the model during training, so a crash does not lose everything.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Scaling Laws for Neural Language Models
The first big scaling-law paper. Figure 1 is three straight lines on log scales that changed the field.
-
Training Compute-Optimal Large Language Models (Chinchilla)
The 20-tokens-per-parameter result. Table 3 gives the optimal sizes for different budgets.
-
The FineWeb Datasets: Decanting the Web for the Finest Text Data at Scale
The most open account of a modern web data pipeline, with every step tested.
-
The Llama 3 Herd of Models
Section 3 covers data, scaling laws, infrastructure and failures in rare detail.
-
DeepSeek-V3 Technical Report
Section 3 is a masterclass in efficient training: FP8, DualPipe and cross-node communication.
-
Kimi K2: Open Agentic Intelligence
Section 2 covers data rephrasing and the MuonClip optimizer that trained 15.5 trillion tokens without a loss spike.
-
SmolLM2: When Smol Goes Big
How a small model is trained on 11 trillion tokens in stages, fully documented.
Check yourself
6 questions. Tap the answer you think is right.
0 of 6 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonPost-training: from text predictor to assistant