Unit 3 · Training

Lesson 09 · Available to read

Pretraining: Learning from Internet-Scale Data

How a base model is made: self-supervised next-token training on trillions of tokens, measured with perplexity.

Pretraining is the most expensive phase in an LLM's life: weeks or months on thousands of accelerators, consuming most of the total compute. It is also conceptually the simplest. Take an enormous amount of text and run the training loop from Lesson 4 with one objective — predict the next token.

The objective

Pretraining is self-supervised: the text supplies its own answers. For a sequence of tokens, the input is the sequence and the target at each position is the token that comes next — the same sequence shifted one place to the left.

Each position in a training sequence predicts the next token: inputs 'The cat sat on the' have targets 'cat sat on the mat'.
Each position in a training sequence predicts the next token: inputs 'The cat sat on the' have targets 'cat sat on the mat'.

Thanks to the causal mask (Lesson 6), a single forward pass computes predictions for every position at once, each seeing only the tokens before it. A 4,096-token sequence therefore provides 4,096 training examples in one go. The loss is the average cross-entropy across all positions:

loss = −(1/n) Σᵢ ln P(tokenᵢ₊₁ | token₁ … tokenᵢ)

During training the model always conditions on the real previous tokens, not its own guesses — a setup called teacher forcing.

The recipe in practice

A modern pretraining run combines several ingredients:

  • Data — trillions of tokens of filtered web pages, books, code, scientific papers and more (Lesson 10), tokenized and packed into fixed-length sequences.
  • Batches — each optimisation step processes millions of tokens at once, spread across many devices.
  • Optimiser — almost always a variant of AdamW (Adam with weight decay).
  • Learning-rate schedule — a short warm-up from near zero, then a slow decay (often cosine-shaped) over the rest of training. Many labs finish with a phase of especially high-quality data at a low learning rate ("annealing" or mid-training).
  • Mixed precision — most arithmetic in 16-bit (bfloat16) or even 8-bit formats to save memory and time.
  • Parallelism — the model and data are split across thousands of chips: data parallelism (each group gets different batches), tensor parallelism (individual matrix multiplications split across chips) and pipeline parallelism (different layers on different chips).
  • Checkpoints and babysitting — runs save their state regularly, because hardware fails and loss spikes happen; engineers roll back and skip bad data or lower the learning rate.

Watching the loss

At initialisation the model guesses uniformly over its vocabulary, so the loss starts near ln(V) — about 11 for a 50,000-token vocabulary. It falls steeply at first (learning common words and grammar), then slowly for a very long time (facts, rare patterns, reasoning).

Pretraining loss falls quickly from about 11 at random initialisation towards about 2, with occasional spikes.
Pretraining loss falls quickly from about 11 at random initialisation towards about 2, with occasional spikes.

Perplexity

Loss values are hard to interpret, so we often report perplexity:

perplexity = e^(average cross-entropy loss)

Perplexity is the size of a uniform choice that would be equally uncertain. A loss of 2.0 gives perplexity e² ≈ 7.4: on average the model is as unsure as if it were picking among about seven equally likely tokens. Lower is better; a perfect model would score 1.

Perplexity is great for comparing models with the same tokenizer on the same text. Across different tokenizers it can mislead, since a token means something different for each.

What comes out: the base model

The result of pretraining is a base model (also called a foundation or pretrained model). It has absorbed:

  • grammar and style in many languages,
  • an enormous amount of world knowledge (up to the knowledge cutoff of its data),
  • programming languages and common algorithms,
  • patterns of argument, explanation and step-by-step reasoning.

But it has one instinct: continue the document. Ask it a question and it may answer — or it may produce more questions, because on the internet a question is often followed by other questions.

Asked a question, a base model continues it like a document (more quiz questions); an assistant answers it.
Asked a question, a base model continues it like a document (more quiz questions); an assistant answers it.

You can steer a base model by framing the prompt as the start of a document whose natural continuation is what you want — for example, a Q&A page with a few solved examples before your question. This is few-shot prompting (Lesson 14). But to get a dependable assistant, base models go through fine-tuning and alignment (Lessons 11–12).

Why it works so well

Next-token prediction is a remarkably rich task. To keep improving across trillions of diverse tokens, the cheapest strategy for the model is to build reusable internal representations: of syntax, of entities and their properties, of cause and effect, of how code executes. Pretraining is where nearly all of an LLM's knowledge and most of its raw capability come from; later stages mostly shape how that capability is expressed.

The takeaway

Pretraining runs the training loop over trillions of tokens of raw text, scoring every position's next-token prediction with cross-entropy; the result is a base model — measured by perplexity — that knows a great deal but simply continues documents rather than following instructions.

Practice

0 of 7 attempted

  1. 1Explain in your own words

    Explain why a single 2,048-token training sequence provides 2,048 training examples, and what makes it possible to compute them all in one forward pass.

    Saved in this browser

  2. 2Calculate

    A model's average cross-entropy on a test set is 1.5 nats. What is its perplexity? Another model scores perplexity 3.0 on the same text with the same tokenizer — which is better?

    Saved in this browser

  3. 3Calculate

    Roughly what loss would you expect from a freshly initialised model with a 128,000-token vocabulary? Why?

    Saved in this browser

  4. 4Multiple choice

    Why does a base model often respond to a question with more questions?

  5. 5Recall

    Name four practical ingredients of a modern pretraining run besides the data itself.

    Saved in this browser

  6. 6Apply

    Write a prompt that would get a base model (not an assistant) to translate "Where is the train station?" into Spanish, by framing it as a document.

    Saved in this browser

  7. 7Explain in your own words

    What is a knowledge cutoff, and why does every pretrained model have one?

    Saved in this browser

See what unlocked