AI-340 · Lesson 7 of 12 · 25 min

Pretrained language models and scaling

Until about 2018, most NLP systems were trained from scratch for each task on labeled data, with perhaps pretrained word embeddings (Lesson 2) as input. The transformer (Lesson 6) made a different approach practical: pretrain a large model on vast amounts of unlabeled text with a self-supervised objective, then adapt it to many tasks. This transfer learning approach transformed the field within two years and led directly to today's large language models. This lesson explains the three main families of pretrained models (encoders such as BERT, decoders such as GPT, and encoder-decoders such as T5), their pretraining objectives, how they are adapted to tasks, the data they are trained on, and the scaling laws that relate model size, data and compute to performance.

Self-supervised pretraining

Self-supervised learning creates training targets from the text itself, so any text can be used without labels. The model is trained to predict hidden or future parts of the input, and in doing so learns syntax, word meanings, facts about the world and patterns of reasoning that turn out to be useful for many tasks. Three objectives dominate:

  • Causal (autoregressive) language modeling: predict each token from the tokens before it, \(\max \sum_t \log P(x_t \mid x_{<t})\), the same objective as the n-gram models of Lesson 1. It is used by decoder-only models such as the GPT family.
  • Masked language modeling: replace a random 15% of tokens with a mask symbol (or random tokens) and predict the originals from both left and right context. It is used by encoder models such as BERT.
  • Span corruption (denoising): replace random spans of text with sentinel tokens and train an encoder-decoder to generate the missing spans, as in T5.
The three families of pretrained transformer language models.
FamilyExamplesPretraining objectiveAttentionBest suited to
Encoder-onlyBERT, RoBERTa, DeBERTamasked language modelingbidirectionalclassification, tagging, retrieval embeddings
Decoder-onlyGPT series, Llama, Mistral, Claudecausal language modelingcausal (left to right)generation, chat, few-shot tasks; now general-purpose
Encoder-decoderT5, BART, mT5span corruption or denoisingbidirectional encoder, causal decodertranslation, summarization, text-to-text tasks

Encoders see the whole input at once, which suits understanding tasks; decoders can generate text naturally. Since around 2020, decoder-only models have become dominant because, at large scale, a single generative model can perform almost any task specified in text, while encoders remain the workhorses for efficient classification, tagging and embedding models used in search (Lesson 9).

BERT and fine-tuning

BERT (2018) was pretrained with masked language modeling on books and Wikipedia, plus a next-sentence prediction task later found unnecessary. Its input begins with a special [CLS] token, whose final hidden state summarizes the sequence, and pairs of sentences are separated by [SEP]. To adapt BERT to a task, a small output layer is added and the whole network is fine-tuned on labeled data for a few epochs: a classification layer on [CLS] for sentence classification (Lesson 3), a per-token layer for tagging, or start and end pointers for extractive question answering (Lesson 11). BERT set new records on almost every NLP benchmark of its time, often with only thousands of labeled examples, because most of the needed knowledge came from pretraining.

Successors improved the recipe: RoBERTa trained longer on more data and dropped next-sentence prediction; DeBERTa improved attention with separate content and position representations; ELECTRA replaced masked prediction with detecting which tokens were replaced by a small generator, using every token for training and learning more efficiently; and distilled models such as DistilBERT compressed BERT for faster inference (Lesson 10).

GPT and in-context learning

The GPT series took the decoder route. GPT-2 (2019) showed that a large causal language model could perform tasks such as summarization and translation without task-specific training when prompted appropriately. GPT-3 (2020), with 175 billion parameters, demonstrated in-context learning: given a few examples of a task in the prompt (few-shot prompting), the model performs the task on a new input, with no change to its weights. Performance improved steadily with scale, and abilities such as arithmetic and multi-step reasoning appeared to emerge in larger models. Subsequent work turned these pretrained models into assistants through instruction tuning and reinforcement learning from human feedback (Lesson 8).

T5 and the text-to-text framework

T5 (2019) cast every task as converting input text to output text: "translate English to French: ...", "summarize: ...", "cola sentence: ..." for grammatical acceptability, and so on, with a single encoder-decoder model and one training objective. The T5 paper also systematically compared pretraining objectives, architectures, data sets and model sizes, an influential study of what matters. Multilingual versions such as mT5 extended the approach to 101 languages.

Pretraining data

Pretrained models learn from very large text collections: web crawls such as Common Crawl, filtered and deduplicated (for example C4, RefinedWeb, FineWeb), books, Wikipedia, scientific papers, code repositories, and conversational data. Modern large models train on trillions of tokens. Data quality has proved as important as quantity:

  • Filtering removes boilerplate, spam, very short or machine-generated pages, using heuristics and quality classifiers.
  • Deduplication removes repeated documents and passages, which waste compute and increase memorization of training data.
  • Mixtures balance web text with high-quality sources, code and multilingual data, with weights tuned by experiment.
  • Safety and privacy filtering remove personal information and harmful content.
  • Contamination checks ensure that evaluation benchmarks have not leaked into training data, which would inflate scores (Lesson 12).

Training data also raise legal and ethical questions about copyright, consent and compensation, which are the subject of litigation and policy debate in several countries.

Counting parameters and compute

A transformer layer with model dimension \(d\) has about \(4d^2\) parameters in its attention projections and \(8d^2\) in a feed-forward block with hidden size \(4d\), so about \(12d^2\) per layer, plus a token embedding matrix of size vocabulary × \(d\). Training a model with \(N\) parameters on \(D\) tokens takes roughly \(6ND\) floating-point operations: about \(2N\) per token for the forward pass and \(4N\) for the backward pass.

Approximate parameter counts for well-known model configurations, and the compute and time to train a 7B-parameter model on 2 trillion tokens.
def transformer_params(layers, d, vocab, ff_mult=4):
    attn = 4 * d * d                                    # query, key, value and output projections
    ffn = 2 * ff_mult * d * d                           # two feed-forward matrices
    return layers * (attn + ffn) + vocab * d            # plus the token embedding matrix (biases and norms ignored)

models = {"BERT-base (encoder)": (12, 768, 30522), "GPT-2 small (decoder)": (12, 768, 50257), "GPT-2 XL": (48, 1600, 50257), "a 7B decoder": (32, 4096, 32000)}
for name, (L, d, V) in models.items():
    print(f"{name:22s}: about {transformer_params(L, d, V) / 1e6:7.0f} million parameters")

# Training compute and time: about 6 FLOPs per parameter per token
N, tokens = 7e9, 2e12
flops = 6 * N * tokens
gpu_flops = 400e12 * 0.40                               # a GPU at 400 TFLOP/s peak and 40% utilization
print(f"7B model on 2 trillion tokens: {flops:.1e} FLOPs = {flops / gpu_flops / 3600 / 24:,.0f} GPU-days, or {flops / gpu_flops / 3600 / 24 / 1024:.0f} days on 1,024 GPUs")
Output
BERT-base (encoder)   : about     108 million parameters
GPT-2 small (decoder) : about     124 million parameters
GPT-2 XL              : about    1555 million parameters
a 7B decoder          : about    6574 million parameters
7B model on 2 trillion tokens: 8.4e+22 FLOPs = 6,076 GPU-days, or 6 days on 1,024 GPUs

The approximation reproduces BERT-base (110 million) and GPT-2 small (124 million) closely; real 7B models differ in details such as gated feed-forward layers but have similar totals. Training a 7B model on 2 trillion tokens takes about \(8 \times 10^{22}\) operations, roughly a week on a thousand modern GPUs, while frontier models use 100 to 1,000 times more compute.

Scaling laws

Studies from 2020 onward found that a language model's loss on held-out text falls smoothly and predictably as a power law in model size, data size and compute, over many orders of magnitude. The Chinchilla study (2022) fitted the loss as

\[L(N, D) = E + \frac{A}{N^\alpha} + \frac{B}{D^\beta}\]

where \(E\) is the irreducible loss of natural text and the other terms shrink as parameters \(N\) and training tokens \(D\) grow. For a fixed compute budget \(C \approx 6ND\), the loss is minimized by scaling parameters and data roughly in proportion. The study's headline conclusion was that compute-optimal training uses about 20 tokens per parameter, far more data than earlier models such as GPT-3 had used.

Compute-optimal model size and data under the Chinchilla loss fit, and the loss of a GPT-3-like allocation compared with the optimum for the same compute.
import numpy as np

# Chinchilla-style loss fit: L(N, D) = E + A / N^alpha + B / D^beta (constants from Hoffmann et al., 2022)
E, A, B, alpha, beta = 1.69, 406.4, 410.7, 0.34, 0.28
loss = lambda N, D: E + A / N ** alpha + B / D ** beta

for C in (1e21, 1e23, 1e25):                            # training compute in FLOPs, using C = 6 N D
    Ns = np.logspace(7, 13, 2000)
    Ds = C / (6 * Ns)
    L = loss(Ns, Ds)
    k = np.argmin(L)
    print(f"compute {C:.0e} FLOPs: best N = {Ns[k]:.2e} parameters, D = {Ds[k]:.2e} tokens ({Ds[k] / Ns[k]:.0f} tokens per parameter), loss {L[k]:.3f}")
# A model trained on far fewer tokens than optimal for its size (similar to GPT-3)
N, D = 175e9, 300e9
C = 6 * N * D
Ns = np.logspace(9, 12, 3000)
best = Ns[np.argmin(loss(Ns, C / (6 * Ns)))]
print(f"175B parameters on 300B tokens: loss {loss(N, D):.3f}; the same compute with {best:.2e} parameters on {C / (6 * best):.2e} tokens: loss {loss(best, C / (6 * best)):.3f}")
Output
compute 1e+21 FLOPs: best N = 1.82e+09 parameters, D = 9.16e+10 tokens (50 tokens per parameter), loss 2.329
compute 1e+23 FLOPs: best N = 1.46e+10 parameters, D = 1.14e+12 tokens (78 tokens per parameter), loss 2.005
compute 1e+25 FLOPs: best N = 1.17e+11 parameters, D = 1.43e+13 tokens (122 tokens per parameter), loss 1.845
175B parameters on 300B tokens: loss 2.002; the same compute with 2.45e+10 parameters on 2.14e+12 tokens: loss 1.954
Plot of predicted loss against model size in parameters on a logarithmic axis, for three fixed compute budgets of 10 to the 21, 23 and 25 FLOPs. Each curve is U-shaped: too small a model is limited by capacity, too large a model is limited by the data it can be trained on within the budget. The minimum of each curve, marked with a dot, moves to larger models as compute grows, and the minimum loss falls.
For each compute budget, loss is minimized at a particular model size: too small underfits, too large is undertrained.

A GPT-3-like allocation (175 billion parameters on 300 billion tokens) achieves a worse loss than a model about seven times smaller trained on seven times more tokens with the same compute. The exact optimal ratio depends on how the law is fitted (this particular fit suggests more tokens per parameter at larger budgets than the 20 commonly quoted), but the qualitative lesson changed practice: models are now trained on far more data. In practice, models are often trained well beyond the compute-optimal point ("overtrained"), because a smaller model trained on more data is cheaper to run at inference time, where most lifetime compute is spent.

Scaling laws describe the average next-token loss, not specific abilities. Downstream task performance usually improves with lower loss, sometimes abruptly, though apparent emergent abilities partly reflect the choice of discontinuous evaluation metrics. Scaling of pretraining is now complemented by scaling of post-training and of inference-time computation, such as models that reason through longer chains of thought (Lesson 8).

Worked problem

Compute-optimal sizing with the 20:1 rule

A team has a budget of \(6 \times 10^{22}\) FLOPs. Using \(C = 6ND\) and \(D = 20N\), what model size and data size are compute-optimal?

Show solution
  1. \(C = 6N(20N) = 120N^2\), so \(N = \sqrt{6 \times 10^{22}/120} = \sqrt{5 \times 10^{20}} = 2.2 \times 10^{10}\).
  2. \(D = 20N = 4.5 \times 10^{11}\) tokens.

Answer: About 22 billion parameters trained on about 450 billion tokens. If the model will serve heavy inference traffic, a smaller model (say 8 billion parameters) trained on more tokens may be the better choice overall, accepting a slightly higher training loss for much cheaper inference.

Adapting pretrained models

Pretrained models are adapted to tasks in several ways, from most to least expensive:

  • Full fine-tuning: update all weights on task data; most accurate for a fixed task but requires storing a copy of the model per task.
  • Parameter-efficient fine-tuning: update a small number of added parameters, such as low-rank adapters (LoRA), keeping the base model frozen (Lesson 10).
  • Instruction tuning: fine-tune on many tasks phrased as instructions, so the model follows new instructions zero-shot (Lesson 8).
  • Prompting and in-context learning: no weight changes; the task is specified in the input.
  • Retrieval augmentation: supply relevant documents at inference time instead of storing knowledge in weights (Lesson 9).

Common mistakes

  • MistakeUsing a decoder-only chat model for high-volume classification by default.

    InsteadConsider a fine-tuned encoder, which is cheaper and often as accurate.

  • MistakeIgnoring data quality and deduplication.

    InsteadFilter and deduplicate; quality strongly affects results and memorization.

  • MistakeTraining the largest model the budget allows.

    InsteadBalance parameters and tokens, and account for inference costs.

  • MistakeEvaluating on benchmarks that may be in the training data.

    InsteadCheck for contamination and use held-out or new evaluations.

  • MistakeTreating scaling laws as predictions of specific abilities.

    InsteadThey predict average loss; abilities must be evaluated directly.

Summary and key terms

  • Self-supervised pretraining on unlabeled text with causal, masked or span-corruption objectives learns broadly useful knowledge.
  • Encoders (BERT) suit understanding tasks; decoders (GPT) suit generation and in-context learning; encoder-decoders (T5) suit text-to-text tasks.
  • Fine-tuning adapts pretrained models with small labeled data sets.
  • Pretraining data are filtered, deduplicated, mixed and checked for contamination, raising legal and ethical questions.
  • A layer has about \(12d^2\) parameters; training costs about \(6ND\) FLOPs.
  • Scaling laws relate loss to parameters, data and compute; compute-optimal training balances them, while inference costs favor smaller, overtrained models.
Self-supervised learning
Learning from targets derived from the input data itself.
Causal language modeling
Predicting each token from the preceding tokens.
Masked language modeling
Predicting masked tokens from surrounding context.
Fine-tuning
Continuing training of a pretrained model on task data.
In-context learning
Performing a task from examples in the prompt without changing weights.
Text-to-text
Framing every task as generating output text from input text.
Deduplication
Removing repeated documents or passages from training data.
Contamination
Evaluation data appearing in training data.
Scaling law
A power-law relation between loss and model size, data or compute.
Compute-optimal
The allocation of parameters and tokens minimizing loss for a given compute budget.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Which objective does BERT use?

    Show answer

    Masked language modeling.

  2. Which family dominates general-purpose assistants?

    Show answer

    Decoder-only models.

  3. What fraction of tokens does BERT mask?

    Show answer

    About 15%.

  4. Parameters per layer for \(d = 1{,}024\) (attention and FFN only)?

    Show answer

    \(12 \times 1{,}024^2 \approx 12.6\) million.

  5. Training FLOPs for 1B parameters on 20B tokens?

    Show answer

    \(1.2 \times 10^{20}\).

  6. Compute-optimal tokens for a 3B model at 20 tokens per parameter?

    Show answer

    60 billion.

  7. Why do labs overtrain smaller models?

    Show answer

    Inference is cheaper for smaller models.

  8. What does the term \(E\) represent in the Chinchilla law?

    Show answer

    The irreducible loss of natural text.

  9. Name two pretraining data quality steps.

    Show answer

    Filtering, deduplication, mixing, contamination checks.

  10. What is in-context learning?

    Show answer

    Performing a task from prompt examples without weight updates.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz