Language Model Pretraining

Language model pretraining is the process of training a neural network on massive amounts of raw text — billions or trillions of words — using self-supervised learning objectives, so the model learns general-purpose representations of language before being adapted to any specific task. It matters because pretraining is what gives modern AI systems like GPT-4, Claude, and Gemini their remarkable ability to reason, write, translate, and code — capabilities that emerge from predicting text at scale. The counterintuitive fact: the model is never explicitly taught grammar, facts, or logic; it learns all of these implicitly, simply by getting good at guessing the next word.

The core insight

The "aha moment" for pretraining is this: predicting the next word is a deceptively powerful learning signal. To reliably predict what word comes next in "The Eiffel Tower is located in ___", a model must have encoded the fact that Paris is in France, that the Eiffel Tower is a landmark, and how sentence grammar constrains word choice — all from the single task of next-word prediction. Pretraining is what lets you train one general model on the internet's text and then cheaply adapt it to thousands of downstream tasks, instead of building a separate model for each task from scratch.

How It Works

Pretraining works by showing a language model enormous amounts of text and asking it to make predictions it can check against the actual data — no human labeling required. This is called self-supervised learning.

The Prediction Objective

The dominant pretraining objective for modern large language models is causal language modeling (also called autoregressive language modeling): given all the words so far, predict the next one.

1
L=−∑t=1Tlog⁡P(wt∣w1,w2,…,wt−1)\quad \mathcal{L} = -\sum_{t=1}^{T} \log P(w_t \mid w_1, w_2, \ldots, w_{t-1})

Explanation: The loss is the total "surprise" the model experiences over a sequence — we sum up how unlikely the model found each correct next word, and training tries to minimize this total surprise.

  • L\mathcal{L}: the loss — the number we want to make as small as possible during training
  • TT: the total number of tokens (words or word-pieces) in the sequence
  • wtw_t: the token at position tt that the model is trying to predict
  • P(wt∣w1,…,wt−1)P(w_t \mid w_1, \ldots, w_{t-1}): the probability the model assigns to the correct token, given everything before it
  • log⁡\log: we use the logarithm so that very small probabilities (bad predictions) produce very large penalties

An alternative objective used by BERT-style models is masked language modeling (MLM), where random tokens are hidden and the model predicts them from both left and right context.

2
LMLM=−∑i∈Mlog⁡P(wi∣w∖M)\quad \mathcal{L}_{\text{MLM}} = -\sum_{i \in \mathcal{M}} \log P(w_i \mid w_{\setminus \mathcal{M}})

Explanation: For each masked position, the model is penalized based on how surprised it is by the true token, using the full surrounding (non-masked) context.

  • M\mathcal{M}: the set of positions that were masked (typically ~15% of tokens)
  • w∖Mw_{\setminus \mathcal{M}}: all the tokens that were not masked

Perplexity: The Standard Yardstick

Researchers typically report perplexity rather than raw loss, because it is more interpretable:

3
Perplexity=eL/T\quad \text{Perplexity} = e^{\mathcal{L}/T}

Explanation: Perplexity is roughly "how many equally likely choices did the model feel it had at each step?" — a perplexity of 10 means the model was about as uncertain as if it were guessing among 10 equally probable words. A well-trained model on English text might reach a perplexity of 10–20 on held-out text. Random guessing over a vocabulary of 50,000 words would give a perplexity of 50,000. ---

The Training Loop in Code

Here is a stripped-down version of the pretraining loop to make the mechanism concrete: ```python import torch import torch.nn as nn

# Assume: model is a Transformer, optimizer is AdamW

# tokens: a batch of token IDs, shape [batch_size, sequence_length] def pretrain_step(model, tokens, optimizer):

# Inputs are all tokens except the last one inputs = tokens[:, :-1] # shape: [batch, seq_len - 1]

# Targets are all tokens except the first one (shifted by 1) targets = tokens[:, 1:] # shape: [batch, seq_len - 1]

# Forward pass: model predicts next-token logits logits = model(inputs) # shape: [batch, seq_len - 1, vocab_size]

# Compute cross-entropy loss (implements equation [1]) loss_fn = nn.CrossEntropyLoss()

# Flatten batch and sequence dims for loss computation loss = loss_fn( logits.reshape(-1, logits.size(-1)), # [batch(seq-1), vocab] targets.reshape(-1) # [batch(seq-1)] )

# Backward pass: compute gradients and update weights optimizer.zero_grad() loss.backward() optimizer.step() return loss.item() ``` The entire sophistication of GPT-4, Claude, and Llama lives in the `model` and the dataset — the training loop itself is this simple. ---

The Data Pipeline

Pretraining data is collected from:

SourceExamplesScale
Web crawlsCommon Crawl, C4Trillions of tokens
BooksBookCorpus, GutenbergBillions of tokens
CodeGitHubHundreds of billions of tokens
Scientific textarXiv, PubMedTens of billions of tokens
Curated webWikipedia, StackExchangeBillions of tokens

The data is tokenized using algorithms like Byte Pair Encoding (BPE), which splits text into subword units so rare words can still be represented: ```python

# Example: how a tokenizer splits text into tokens from transformers import GPT2Tokenizer tokenizer = GPT2Tokenizer.from_pretrained("gpt2") text = "Pretraining uses self-supervised learning." tokens = tokenizer.encode(text) decoded = [tokenizer.decode([t]) for t in tokens] print(tokens) # [4360, 34409, 3696, 2116, 12, 16668, 16439, 13] print(decoded) # ['Pre', 'train', 'ing', ' uses', ' self', '-super', 'vised', ' learning', '.'] ``` Notice "Pretraining" becomes three tokens: `Pre`, `train`, `ing`. This is how models handle words they have never seen whole. ---

researcher · Yoshua Bengio | b. 1964 | Neural Language Models

Yoshua Bengio, a professor at Université de Montréal and co-founder of Mila, published the landmark 2003 paper A Neural Probabilistic Language Model with colleagues, which introduced the idea of learning dense word embeddings jointly with a language model — a foundational precursor to modern pretraining. His work established that neural networks could generalize across words by representing them in a continuous space, so knowledge about one word could transfer to similar words. Bengio shared the 2018 Turing Award with Geoffrey Hinton and Yann LeCun; he has since become an outspoken voice on AI safety, a striking pivot from his earlier focus purely on capability research.

researcher · Alec Radford | b. 1990 | GPT Pretraining Framework

Alec Radford led the OpenAI team that published Improving Language Understanding by Generative Pre-Training (2018), introducing the original GPT model and establishing the pretrain-then-finetune paradigm that the entire field now follows. His follow-up works GPT-2 and GPT-3 demonstrated that scaling this simple approach — more data, more parameters, more compute — produced qualitatively new emergent capabilities. GPT-2's 2019 release was deliberately staged by OpenAI over concerns about misuse, one of the first times an AI lab publicly grappled with the dual-use problem of a text generation model.

Figure
Causal Language Model Pretraining — Data Flow
Causal Language Model: Predict the Next Token Input: The cat sat on the mat ← target Transformer (Self-Attention + FFN) Each token attends to all previous tokens (causal mask) Output: P(cat) P(sat) P(on) P(the) P(mat) P(?) Loss = −log P(correct token) averaged over all positions
Figure
Scale vs. Capability: The Pretraining Scaling Curve
Pretraining Pipeline: From Raw Text to Capable Model Raw Text Web, books, code, papers Tokenize BPE / WordPiece → token IDs Pretrain Minimize loss over trillions of tokens Base LLM General-purpose language model Capabilities learned implicitly from prediction: Grammar World Facts Reasoning Code Emergent Skills ✦ All learned with zero human labels — only next-token prediction ✦ Emergent skills appear unpredictably at scale

Intuition

The Mental Model

Imagine you are learning a new language not from a textbook, but by reading every book, article, forum post, and recipe in that language — billions of pages — with one rule: whenever you see a word blacked out, you have to guess what it is from context. At first you guess randomly. Over time, you start noticing patterns: certain words follow certain other words, names go with places, verbs follow nouns in predictable ways, technical jargon clusters in certain domains. After enough practice, you would have internalized grammar, common facts, writing styles, and domain-specific knowledge — not because anyone taught you these explicitly, but because getting good at the guessing game required learning them. This is exactly what a language model does during pretraining. The "neural network" is the guesser. The "weights" of the network are its brain — billions of numerical parameters that get slightly adjusted every time it makes a wrong guess. The pretraining process is simply millions of rounds of this game, played across a dataset so vast it contains a large slice of everything humans have ever written. One more analogy: think of the model's weights after pretraining as a very sophisticated compression of the internet. The model has squeezed the statistical patterns of all that text into its parameters — and when you query it, you are decompressing a relevant slice of that knowledge.

Historical Development

Origins · 1 minhow this was discovered
History
2003: Yoshua Bengio et al. publish A Neural Probabilistic Language Model, introducing learned word embeddings and demonstrating that neural networks can model language better than traditional n-gram methods.
2013: Tomas Mikolov et al. release Word2Vec at Google, showing that simple self-supervised objectives on text produce word embeddings that encode semantic relationships (the famous "king − man + woman ≈ queen" result).
2017: Vaswani et al. publish Attention Is All You Need, introducing the Transformer architecture — replacing recurrent networks with self-attention, making training dramatically faster and enabling much larger models.
2018 (Jan): Google releases ELMo (Embeddings from Language Models), the first widely-adopted system to use pretrained contextual representations from a bidirectional language model, showing large gains across many NLP benchmarks.
2018 (Jun): OpenAI releases GPT (Improving Language Understanding by Generative Pre-Training), establishing the pretrain-on-raw-text, then-finetune paradigm for causal language models.
2018 (Oct): Google releases BERT (Bidirectional Encoder Representations from Transformers), using masked language modeling on 3.3 billion words and achieving state-of-the-art on 11 NLP tasks simultaneously.
2020: OpenAI releases GPT-3 with 175 billion parameters — the first model to demonstrate striking few-shot and zero-shot task performance, proving that pretraining scale alone could produce qualitatively new capabilities without any task-specific finetuning.
2022: Hoffmann et al. (DeepMind) publish the Chinchilla paper (Training Compute-Optimal Language Models), showing that most large models were undertrained — optimal pretraining requires roughly 20 tokens of data per parameter, reshaping how the field allocates compute budgets.
2023–2024: A wave of open-weight pretrained models — Llama, Mistral, Falcon, Gemma — make large-scale pretraining artifacts publicly available, democratizing research on post-pretraining alignment and finetuning.

Why It Matters

Real-World Impact
GPT-4 (OpenAI)Pretrained on an estimated ~13 trillion tokens spanning web text, books, and code; pretraining accounts for the overwhelming majority of the model's capability, with RLHF adding behavioral alignment on top. Its coding, reasoning, and multilingual abilities all trace directly to pretraining data diversity.
GitHub CopilotPowered by the Codex model, pretrained specifically on hundreds of billions of tokens of public GitHub code in addition to natural language. The pretraining on code is what gives Copilot the ability to complete functions, suggest tests, and explain bugs — tasks no one explicitly trained it to do.
Google Translate (Neural Era)Google's neural translation models shifted to Transformer-based architectures pretrained on multilingual data. Pretraining on multiple languages simultaneously allows the model to leverage cross-lingual transfer — knowledge from high-resource languages (English) bleeds into low-resource ones.
BioMedLM / BioGPTDomain-specific pretrained models trained on PubMed abstracts and biomedical literature. Pretraining on domain text instead of general web data dramatically improves performance on clinical NLP tasks like medical question answering and biomedical named-entity recognition.
AlphaCode (DeepMind)A language model pretrained on a curated dataset of 715 GB of code across 12 programming languages, achieving competitive performance in programming contests — demonstrating that the pretraining objective (predict next token of code) is sufficient to learn algorithmic problem-solving.
Llama 3 (Meta)Pretrained on over 15 trillion tokens — roughly 10× more data than Llama 1 — illustrating the Chinchilla principle: given a fixed compute budget, training a smaller model on more tokens often outperforms training a larger model on fewer tokens.
Claude (Anthropic)Pretraining provides the base knowledge and language understanding; Anthropic's Constitutional AI and RLHF are applied after pretraining. This separation makes clear that pretraining and alignment are distinct, sequential stages — you cannot skip pretraining and expect alignment techniques alone to produce a capable model.

Prerequisites

What This Leads To

See Also

Transformers and Self-Attention · Byte Pair Encoding · Scaling Laws for Language Models · Instruction Tuning and Supervised Finetuning · Emergent Capabilities in Large Language Models

Before this