AI Compass
Compass

How a language model is trained

From raw text to a usable model: pre-training, supervised fine-tuning and alignment, with the orders of magnitude of each stage.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The three stages

StageWhat happensData volumeShare of cost
Pre-trainingPredict the next tokenTrillions of tokens~99 %
Fine-tuningLearn to follow instructions10,000 to 1 million examples~1 %
AlignmentLearn preferred behaviour10,000 to 500,000 comparisonsunder 1 %

What is actually learned in pre-training

The task is always the same: here is a piece of text, predict the next piece. To do that well, a model has to grasp grammar, retain facts, resolve references and recognise patterns of argument. None of it is taught separately.

The pre-training loss

Autoregressive cross-entropy

L(θ) = −(1/N) · Σᵢ log P( xᵢ | x₁…xᵢ₋₁ ; θ )

The loss is the mean negative log probability the model assigned to the token that actually followed.

xᵢ
the i-th token
θ
the model parameters
N
number of tokens in the batch

What is remarkable is that every token is simultaneously input and target. A text of a million tokens yields a million training examples with nobody annotating anything. That is why the task is feasible at this scale at all.

What is in the data

  • Deduplication is the single most effective quality step. Repeated texts get memorised.
  • Quality filtering beats sheer volume, measured on downstream tasks.
  • Order matters: high-quality data late in training has a stronger residual effect.
  • A language's share of the training data determines later quality in that language almost directly.

Scaling laws

Compute-optimal allocation

C ≈ 6·N·D optimal: D ≈ 20 · N

At a fixed budget, parameters and training tokens should grow in roughly equal proportion, which works out to about twenty tokens per parameter.

C
available compute budget in FLOPs
N
parameter count
D
number of training tokens

A 7-billion-parameter model would therefore be compute-optimal at 140 billion tokens. In fact such models are trained on 2 to 15 trillion tokens, far beyond the optimum.

The reason is a different objective. Compute-optimal minimises cost to a finished model. Anyone planning to run that model millions of times wants the smallest possible model at a given quality, and accepts a multiple of the training effort for it. Operating cost dominates the overall calculation.

Fine-tuning

Loss on the answer only

L = −(1/|y|) · Σ_{i∈y} log P( yᵢ | p, y₁…yᵢ₋₁ )

The loss covers only the answer tokens; the prompt is seen but not predicted.

p
the prompt tokens
y
the answer tokens

Forget that mask and you train the model to generate prompts too. The result is answers that repeat the question: a common and easily missed mistake.

Orders of magnitude for your own project

PlanEffortRealistic for
Own pre-training, 7 bn10⁵ to 10⁶ GPU hoursVery few organisations
Full fine-tuning, 7 bn100 to 1,000 GPU hoursA larger department
LoRA fine-tuning, 7 bn5 to 50 GPU hoursAny team
Prompting and RAG0 GPU hoursThe normal case

The last row is the right answer for the overwhelming majority of business tasks. See Fine-tuning in depth.

Related courses and sources

DatasetFreeEN

Common Crawl

The open crawl of the web that a large share of language model training data comes from. It shows concretely what a pre-training corpus actually contains.

For anyone asking where a model's knowledge comes from, and for the question of opt-out reservations.

PaperFreeEN

Language Models are Few-Shot Learners

The paper showing that examples in the prompt can replace fine-tuning. The origin of what is now called prompting.

For understanding why examples in the prompt can replace fine-tuning.

PaperFreeEN

Training Compute-Optimal Large Language Models

The calculation showing that most large models were trained on too little data. Data volume has not been a side issue since.

For anyone comparing model sizes who needs to know why data volume counts.

PaperFreeEN

Training Language Models to Follow Instructions

How a text continuation engine becomes an assistant. The paper behind alignment from human feedback.

For anyone asking how a text continuation engine becomes an assistant.

Was this page helpful?
How a language model is trained