Transformers/Large Language Models: Pretraining and Scaling

Lesson 5.42,203 words

Large Language Models: Pretraining and Scaling

A language model's next-token distribution is only as good as the parameters behind it. This part is where those parameters come from: self-supervised pretraining on web-scale text with teacher forcing and cross-entropy, the scaling laws that make test loss a predictable power law in parameters, data, and compute, the KV cache that keeps long-context inference affordable, and how a finished model is evaluated by perplexity and benchmarks — closing with the Kaplan, Chinchilla, GPT-3, and emergence papers behind the scaling story.

╌╌╌╌

This builds on Large Language Models, which assembled the inference side: the language-modeling head that turns a top-block hidden state into a distribution over the vocabulary, autoregressive generation, and the decoding strategies that read text out of that distribution. All of it presumed a well-trained model. This part supplies the training, and the scaling results that govern it.

Pretraining

None of the above learns anything. The distribution is only as good as the parameters , and those come from pretraining — the self-supervised phase where the model reads enormous quantities of raw text and is trained to predict the next token at every position.1

The objective needs no human labels, which is what makes web-scale training possible. Take any document, and at every position the correct answer is simply the word that actually comes next. This is self-supervision: the data is its own answer key. The loss at position is the cross-entropy between the model's distribution and the one-hot vector for the true next word , which reduces to the negative log-probability the model assigned to that word:2

The training loss over a sequence is the average of this over all positions:

Two properties make this tractable at scale. During training the model is fed the true prefix at every step rather than its own guesses — teacher forcing — which decouples the positions.3 And because the transformer computes every position's output independently (unlike an RNN, whose recurrence forces a serial pass), a whole training sequence is processed in parallel, one forward pass scoring the next-token prediction at every position at once.

Self-supervised pretraining. A raw text sequence is shifted by one to form its own targets; the transformer predicts the next token at every position in parallel, and the loss is the average cross-entropy against the true next word.

The second requirement is data. Pretraining corpora are assembled from web crawls, books, code, and reference text, filtered and deduplicated, and tokenized into subword units so that a fixed vocabulary can represent any string, including words it has never seen. The scale is the point: hundreds of billions of tokens, so that predicting the next word forces the model to absorb syntax, factual associations, and enough of the statistics of the world to make the prediction. The representations that fall out are the ones reused when the model is fine-tuned or prompted for a downstream task.

Scaling laws

The empirical result behind large-scale pretraining is that test loss falls predictably as the model grows. Across many orders of magnitude, the loss is a power law in each of three resources — the parameter count , the dataset size , and the compute budget — whenever the other two are not the bottleneck:

with small positive exponents . A power law is a straight line on log-log axes, which is what makes it useful: measure the loss at a few affordable scales, fit the line, and extrapolate to predict the loss of a model too expensive to have trained yet. Training runs can then be budgeted in advance.

Scaling laws. On log-log axes, test loss falls as a straight line in parameters, data, and compute, ; each curve is labeled at its right end. Starving any one resource bends its curve flat early (dashed), since the neglected term dominates the loss.

Because the loss depends on all three, a fixed compute budget poses an allocation question: spend it on more parameters or more data? Early (Kaplan) scaling work suggested pushing model size hard; the later Chinchilla result corrected it, finding that parameters and data should grow together — roughly in proportion — and that most large models had been badly undertrained, too big for the number of tokens they saw.4 The compute-optimal frontier is the balance point: for a given , the pairing of and that minimizes loss. Off that frontier, one resource is wasted while the other bottlenecks the loss.

The reason scaling laws matter beyond bookkeeping is that some capabilities do not appear gradually. As loss crosses certain thresholds, models begin to do things — multi-step arithmetic, following an instruction from a few examples in the prompt — that smaller models could not do at all. These emergent abilities, and the related phenomenon of in-context learning, where a model performs a new task from examples given in the prompt without any weight update, are what make the scaled-up next-token predictor feel qualitatively different from the n-gram it descends from. We take them up in the next lesson.

Context length and the KV cache

The transformer attends over its entire context, and the cost of that is quadratic: attention compares every token against every other, so a context of length costs on the order of to process. The context length — the maximum prefix the model can attend over — is therefore both a capability (longer context means more to condition on) and a cost driver.

The cost is clearest in generation. Naively, producing token re-runs attention over the whole prefix , and doing that at every step repeats enormous work. The KV cache removes the repetition: attention needs a key and a value vector for each past token, and those never change once computed, so the model stores them. Generating a new token then computes attention using the one new query against all cached keys and values, and appends its own key and value to the cache. Generation cost per token becomes linear in the context rather than quadratic, at the price of memory that grows with the context. This is why long contexts are expensive to serve.

Evaluation

How good is a language model? Two families of answer.

Perplexity measures the model against held-out text directly. It is the inverse probability the model assigns to a test set, normalized by length — equivalently, the exponentiated average negative log-probability per token:5

Perplexity is the model's weighted average branching factor: how many words, on average, it is choosing between at each step. Lower is better — the model is less surprised by real text. It is the training objective itself, evaluated on held-out data, which makes it the natural intrinsic metric, with one caveat: perplexities are only comparable across models that share a vocabulary and tokenization, and an improvement in perplexity does not guarantee an improvement on any downstream task.

Put numbers on it. Suppose a four-token test string the cat sat down receives per-token probabilities from the model. The average negative log-probability is nats, and perplexity is its exponential, . The same number arrives from the other definition: the geometric mean of the four probabilities is , and perplexity is its reciprocal, . The interpretation is direct: this model is as uncertain, on average, as if it were choosing uniformly among words at each step. A model that assigned every token probability would have perplexity (no uncertainty); a model that assigned uniform probability to a -word vocabulary would have perplexity . Halving perplexity means the model is, in the branching-factor sense, twice as sure of the next word.

Perplexity of the test string "the cat sat down". Per-token probabilities are turned into per-token surprisals (negative log-probabilities); their average, exponentiated, is the perplexity 4.73 — the effective number of equally likely choices per step.

Benchmarks measure the model against tasks instead of text. A generative model producing free text needs task-level scoring: for translation and summarization, overlap metrics like chrF (a character-n-gram F-score) and the older word-based BLEU compare the output against human references, while embedding-based metrics like BERTScore match tokens by cosine similarity to reward paraphrase rather than exact overlap.6 Beyond single tasks, large models are reported on suites of question-answering, reasoning, and knowledge benchmarks — the coarse but comparable numbers that track progress across the field. Neither family is sufficient alone: perplexity is faithful but task-blind, benchmarks are task-aware but easy to overfit.

Scaling laws, GPT-3, and emergence

The scaling story above is drawn from three papers worth naming precisely, because their claims are often blurred together.

The scaling laws (Kaplan et al., 2020). Scaling Laws for Neural Language Models established the power-law form empirically: over seven orders of magnitude, test loss falls as in parameters and analogously in data and compute, with exponents around and for their setup.7 Their headline recommendation, given a fixed compute budget, was to spend most of it on model size and comparatively little on more data — train very large models on relatively few tokens and stop before convergence.

The Chinchilla correction (Hoffmann et al., 2022). Training Compute-Optimal Large Language Models re-ran the analysis more carefully and reached the opposite allocation: parameters and training tokens should scale in roughly equal proportion, about tokens per parameter at the compute-optimal point.8 To demonstrate it they trained Chinchilla, a -billion-parameter model, on trillion tokens, and it outperformed the -billion-parameter Gopher despite being four times smaller — because Gopher, like most models of its era, had been badly undertrained for its size. The result changed practice: compute-optimal models are smaller and trained on more tokens.

Kaplan vs. Chinchilla allocation of a fixed compute budget. Kaplan (2020) put most of a budget into parameters and undertrained; Chinchilla (2022) found the loss-minimizing point grows parameters N and tokens D together (about 20 tokens per parameter).

GPT-3 and in-context learning (Brown et al., 2020). Language Models Are Few-Shot Learners trained a -billion-parameter decoder-only transformer and showed that, at that scale, the model performs new tasks from examples placed in the prompt with no gradient update — the in-context learning the last section named.9 Give it a few input-output pairs as context and it infers the pattern and continues it. This is not learning in the weight-update sense; the demonstrations live only in the forward pass. GPT-3 made few-shot prompting a headline capability and is the reason the next lesson can treat prompting as an adaptation method on par with fine-tuning.

Emergent abilities (Wei et al., 2022). Emergent Abilities of Large Language Models documented tasks — multi-step arithmetic, word unscrambling, certain benchmarks — where performance stays at chance across many model sizes and then rises sharply past a scale threshold, rather than improving smoothly.10 The claim is contested: Schaeffer et al. (2023) argued that some apparent emergence is an artifact of discontinuous metrics (exact-match scoring hides the smooth improvement a soft metric would reveal), and that under continuous metrics the jumps often vanish.11 The honest summary is that scaling reliably lowers loss as a power law, that some downstream metrics improve smoothly and others appear to jump, and that how much of the jumping is real versus a measurement artifact is still an open question. State the phenomenon, not more than the evidence supports.

The whole recipe

The parts summarize in one sentence. A stack of transformer blocks with a language-modeling head estimates ; trained by self-supervised next-token prediction on web-scale text, its loss falls as a power law in size, data, and compute; and at inference a decoding strategy reads text back out of the distribution. One architecture, one objective, and scale. The deep-learning course's LLM lesson works the same pipeline from the modeling side, and the alignment step that turns this fluent-but-unhelpful next-token predictor into an assistant — RLHF — is where reinforcement learning re-enters the story. What pretraining leaves us with is a model that continues text; the next lesson is about making it do what we ask.

Footnotes

  1. Jurafsky & Martin, §9.9.1 — Pretraining: first train a transformer language model on a large corpus in a self-supervised way, then add and fine-tune a task head; the self-supervised objective incorporates rich information about language.
  2. Jurafsky & Martin, §9.3: the language-modeling loss is cross-entropy against the one-hot true next word, reducing to , the negative log-probability the model assigns to the correct next token, averaged over the sequence.
  3. Jurafsky & Martin, §9.3 (Teacher Forcing): during training the model is always given the correct history to predict , rather than its own previous outputs; transformers score every position in parallel.
  4. Jurafsky & Martin, §10.7.3 and the scaling-law literature it cites: test loss falls as a power law in parameters, data, and compute; the compute-optimal balance (Chinchilla) grows model size and training tokens together rather than favoring size alone.
  5. Jurafsky & Martin, §3.2.1 — Perplexity: the inverse probability of the test set normalized by length, equal to the exponentiated per-token cross-entropy and interpretable as the weighted average branching factor; lower is better.
  6. Jurafsky & Martin, §10.8 — MT Evaluation: character-overlap metrics like chrF and the word-based BLEU compare output to human references, and embedding-based metrics like BERTScore match tokens by cosine similarity to credit paraphrase.
  7. Kaplan, McCandlish, Henighan, Brown, Chess, Child, Gray, Radford, Wu, Amodei (2020), Scaling Laws for Neural Language Models. Establishes that language-model test loss follows smooth power laws in parameters, dataset size, and compute over many orders of magnitude, with small exponents; recommends, for a fixed compute budget, allocating most of it to model size and training large models on relatively little data without training to convergence.
  8. Hoffmann, Borgeaud, Mensch, et al. (2022), Training Compute-Optimal Large Language Models (Chinchilla), NeurIPS 2022. Finds that model size and training tokens should be scaled in roughly equal proportion (about 20 tokens per parameter) at the compute-optimal frontier; the 70B Chinchilla, trained on 1.4T tokens, outperforms the 280B Gopher, showing prior large models were substantially undertrained.
  9. Brown, Mann, Ryder, et al. (2020), Language Models Are Few-Shot Learners (GPT-3), NeurIPS 2020. A 175B-parameter decoder-only transformer that performs many tasks from a few in-context demonstrations with no gradient update (in-context / few-shot learning), demonstrating that task specification can move from fine-tuning into the prompt at sufficient scale.
  10. Wei, Tay, Bommasani, et al. (2022), Emergent Abilities of Large Language Models, TMLR 2022. Documents tasks on which performance remains near chance across smaller scales and rises sharply beyond a threshold model size, rather than improving smoothly with scale.
  11. Schaeffer, Miranda, Koyejo (2023), Are Emergent Abilities of Large Language Models a Mirage?, NeurIPS 2023. Argues that many reported emergent jumps are artifacts of discontinuous, all-or-nothing metrics (e.g. exact match); under continuous or per-token metrics the same capabilities often improve smoothly and predictably with scale.

╌╌ END ╌╌