AnyLearn
All lessons
AIintermediate

LLM Pretraining: Data, Loss, and What Actually Happens

A deep dive into how large language models learn from raw text: the next-token prediction objective, cross-entropy loss, the messy reality of web data curation (Common Crawl, dedup, quality filters), and the lineage from The Pile to FineWeb.

Not signed in β€” your progress and quiz score won't be saved.
Lesson progress1 / 12

The One Objective That Rules Them All

Every major language model β€” GPT, Llama, Mistral, Gemini β€” is pretrained on a single objective: predict the next token. That's it. Given a sequence of tokens x1,x2,…,xtβˆ’1x_1, x_2, \ldots, x_{t-1}, the model outputs a probability distribution over the vocabulary for xtx_t. Training minimizes how wrong those predictions are across billions of such positions.

This deceptively simple objective forces the model to internalize grammar, facts, reasoning patterns, and world knowledge β€” because all of that information is latent in which token comes next. You cannot predict "The capital of France is ___" without knowing geography. You cannot predict the closing brace of a function without understanding syntax. The objective is humble; its consequences are not.

Formally, given a corpus of NN tokens, the training loss is: L=βˆ’1Nβˆ‘t=1Nlog⁑P(xt∣x1,…,xtβˆ’1)\mathcal{L} = -\frac{1}{N} \sum_{t=1}^{N} \log P(x_t \mid x_1, \ldots, x_{t-1}) This is the average negative log-likelihood over all token positions β€” also called cross-entropy loss.

Full lesson text

All 12 steps on one page β€” for reading, reference, and search.

Show

1. The One Objective That Rules Them All

Every major language model β€” GPT, Llama, Mistral, Gemini β€” is pretrained on a single objective: predict the next token. That's it. Given a sequence of tokens x1,x2,…,xtβˆ’1x_1, x_2, \ldots, x_{t-1}, the model outputs a probability distribution over the vocabulary for xtx_t. Training minimizes how wrong those predictions are across billions of such positions.

This deceptively simple objective forces the model to internalize grammar, facts, reasoning patterns, and world knowledge β€” because all of that information is latent in which token comes next. You cannot predict "The capital of France is ___" without knowing geography. You cannot predict the closing brace of a function without understanding syntax. The objective is humble; its consequences are not.

Formally, given a corpus of NN tokens, the training loss is: L=βˆ’1Nβˆ‘t=1Nlog⁑P(xt∣x1,…,xtβˆ’1)\mathcal{L} = -\frac{1}{N} \sum_{t=1}^{N} \log P(x_t \mid x_1, \ldots, x_{t-1}) This is the average negative log-likelihood over all token positions β€” also called cross-entropy loss.

2. Cross-Entropy Loss, Unpacked

Cross-entropy measures the surprise of seeing the true token under the model's predicted distribution. If the model assigns probability 1.0 to the correct token, βˆ’log⁑(1.0)=0-\log(1.0) = 0 β€” no surprise. If it assigns 0.01, βˆ’log⁑(0.01)β‰ˆ4.6-\log(0.01) \approx 4.6 nats β€” very surprised.

In practice, the model outputs a logit vector of size ∣V∣|V| (vocabulary size, typically 32k–128k), which is passed through a softmax: P(xt=k)=ezkβˆ‘jezjP(x_t = k) = \frac{e^{z_k}}{\sum_j e^{z_j}} The loss for that position is βˆ’log⁑P(xt=trueΒ token)-\log P(x_t = \text{true token}).

A useful reference point: a random model over a 50k-token vocabulary gives log⁑(50000)β‰ˆ10.8\log(50000) \approx 10.8 nats. A well-trained model reaches ~2.0–2.5 nats on held-out English text. Perplexity is just eLe^{\mathcal{L}} β€” a 2.0 nat loss means perplexity β‰ˆ 7.4, meaning the model is roughly as uncertain as choosing uniformly among 7 tokens.

Heads up: Loss comparisons across models are only valid when tokenizers are identical. A model with a larger vocabulary will mechanically show lower per-token loss, even with the same underlying capability.

3. From Raw Text to Gradient Update

flowchart TD
  A["Raw Text Corpus"] --> B["Tokenizer (BPE/SentencePiece)"]
  B --> C["Token IDs: [x1, x2, ..., xT]"]
  C --> D["Transformer Forward Pass"]
  D --> E["Logits: shape [T, vocab_size]"]
  E --> F["Softmax -> Probabilities"]
  F --> G["Cross-Entropy vs True Tokens"]
  G --> H["Scalar Loss"]
  H --> I["Backprop + AdamW Update"]
  I --> D

4. Where the Data Comes From: Common Crawl

Common Crawl is a non-profit that has been crawling the web since 2008. Each monthly snapshot (a "crawl") contains ~3–4 billion pages in WARC format β€” compressed raw HTTP responses including HTML, headers, and metadata. The total archive exceeds a petabyte.

For LLM pretraining, you typically start from the WET files (plain text extracted from HTML) or re-extract text yourself using tools like trafilatura or resiliparse. Common Crawl is the upstream source for nearly every major open-weights pretraining dataset: The Pile, RedPajama, Dolma, RefinedWeb, FineWeb, and more.

The catch: raw Common Crawl is ~50% garbage. Boilerplate navigation menus, ad-injected pages, machine-translated spam, duplicate content, and toxic material are all in there. Naively training on raw WET files produces a noticeably worse model than training on the same volume of curated text. The curation pipeline is where the real engineering lives.

5. Deduplication: More Important Than It Looks

Duplicate documents inflate the apparent dataset size, cause the model to memorize specific sequences, and waste compute on redundant gradient signal. Two main strategies:

  • Exact dedup: hash each document (MD5 / SHA-256), drop duplicates. Cheap and catches copy-pastes.
  • Fuzzy dedup: MinHash + Locality-Sensitive Hashing (LSH). Builds a signature for each document from multiple hash functions, then buckets near-duplicates together. Catches paraphrased or slightly-edited copies. The standard tooling is datasketch or the C++ pipeline from the BigScience workshop.

The Pile team found ~30% of CommonCrawl is near-duplicate. FineWeb deduplication at the URL level + MinHash removed roughly half the data before quality filtering even ran. Lee et al. (2022, "Deduplicating Training Data Makes Language Models Better") showed that dedup improves downstream performance and significantly reduces verbatim memorization.

Gotcha: Dedup across train/test splits is critical. If your evaluation benchmark text appears verbatim in training data, benchmark scores are meaningless.

6. Quality Filtering: Heuristics and Classifiers

After dedup, a typical pipeline applies several filter passes:

Heuristic filters (fast, rule-based):

  • Minimum token count (drop pages < 100 words)
  • Maximum repetition ratio (flag pages where the same line repeats > 30% of content)
  • Symbol-to-word ratio (drop pages heavy on #@!% β€” SEO spam)
  • Language detection via fastText (keep only target languages)
  • perplexity filter using a small KenLM model trained on Wikipedia β€” drop pages with very high perplexity (gibberish) or suspiciously low perplexity (boilerplate templates)

Classifier filters (slower, higher-signal):

  • A fastText or small BERT-family model trained to classify "high quality" (Wikipedia, books) vs. "low quality" (random crawl). C4 used this; FineWeb uses a custom educational-quality classifier trained on GPT-4 annotations.

The FineWeb-Edu variant (HuggingFace, 2024) filters for educational content specifically and achieves better benchmark scores on knowledge-heavy evals even with fewer tokens β€” a strong argument that quality beats quantity past a certain scale.

7. Dataset Lineage: The Pile β†’ RefinedWeb β†’ FineWeb

The Pile (EleutherAI, 2020) was the first large-scale open pretraining dataset assembled with transparency. It combined 22 diverse data sources β€” Common Crawl (Pile-CC), books (Books3, Gutenberg), code (GitHub), academic papers (PubMed, ArXiv), legal documents (FreeLaw), and more β€” totaling 825 GB. Its multi-source design was intentional: diversity improves downstream generalization. GPT-Neo and GPT-J were trained on it.

RefinedWeb (Mistral/Falcon team, 2023) took a different bet: scale Common Crawl aggressively but filter harder, rather than mixing many sources. Using the MacroData Refinement (MDR) pipeline with strict dedup and heuristics, they extracted ~600B tokens that matched or beat multi-source blends on most benchmarks. Falcon-40B was trained on it.

FineWeb (HuggingFace, 2024) is the current open-source standard. It starts from 95 Common Crawl dumps (~15T tokens before filtering), applies URL dedup, MinHash, heuristic filters, and a custom quality classifier. The 10T-token release and 1.3T FineWeb-Edu subset are both publicly available on the Hub and reproduce Llama-3 level performance when used as the base corpus.

8. Dataset Lineage at a Glance

flowchart LR
  A["Common Crawl"] --> B["The Pile (2020, EleutherAI)"]
  A --> C["C4 (2020, Google)"]
  A --> D["RefinedWeb (2023, Falcon team)"]
  A --> E["FineWeb (2024, HuggingFace)"]
  B --> F["GPT-Neo / GPT-J"]
  C --> G["T5 family"]
  D --> H["Falcon-40B"]
  E --> I["FineWeb-Edu"]
  B --> J["RedPajama / Dolma (2023)"]

9. What a Loss Curve Actually Tells You

A pretraining loss curve plots cross-entropy (nats or bits-per-byte) on the y-axis against training steps or tokens processed on the x-axis. What you actually see:

  • Sharp drop in the first ~1% of training: the model learns basic token co-occurrence statistics and common n-grams almost immediately.
  • Long smooth power-law decay: consistent with the Chinchilla / Hoffmann et al. (2022) scaling laws β€” loss decreases roughly as L∝Nβˆ’0.076\mathcal{L} \propto N^{-0.076} in parameters and Dβˆ’0.095D^{-0.095} in data.
  • Bumps and plateaus: often correspond to learning rate schedule changes, data mixture shifts, or the model "discovering" a new capability (arithmetic, code patterns). Some bumps are artifacts of per-domain difficulty differences as the data pipeline cycles through shards.
  • Train vs. eval divergence: a widening gap between train loss and held-out eval loss signals memorization β€” especially likely if dedup was skipped or the eval set overlaps with training data.

Loss alone doesn't predict downstream task performance. A model at 2.1 nats that trained on diverse domains often beats a 1.9-nat model trained on a narrow corpus.

10. A Minimal Pretraining Loop in PyTorch

Seeing the code makes the math concrete. Here is a stripped-down training step:

import torch
import torch.nn.functional as F

def training_step(model, input_ids, optimizer):
    # input_ids: [batch, seq_len]
    # targets are the same sequence shifted by one position
    inputs = input_ids[:, :-1]   # [batch, seq_len - 1]
    targets = input_ids[:, 1:]   # [batch, seq_len - 1]

    logits = model(inputs)       # [batch, seq_len-1, vocab_size]

    # Flatten for F.cross_entropy
    loss = F.cross_entropy(
        logits.reshape(-1, logits.size(-1)),  # [batch*(seq_len-1), vocab_size]
        targets.reshape(-1),                  # [batch*(seq_len-1)]
    )

    optimizer.zero_grad()
    loss.backward()
    torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)  # gradient clipping
    optimizer.step()
    return loss.item()

Notice: the target at position tt is simply the input at position t+1t+1. There is no separate label generation step β€” the supervision is baked into the sequence itself. F.cross_entropy applies softmax internally, so the model outputs raw logits.

11. Data Mixture and Domain Weighting

Modern pretraining doesn't just dump all filtered text into a single stream. Data is organized by domain β€” web text, code, books, scientific papers, math β€” and each domain is sampled at a specified weight during training.

Weighting matters because domains differ in size and quality. If you sample proportionally to raw token count, code (relatively small) gets drowned out by web text. Upsampling code improves coding benchmarks substantially; upsampling academic text improves reasoning and STEM.

Recent work formulates this as an optimization problem. DoReMi (Xie et al., 2023, Stanford) trains a small proxy model and a reference model simultaneously to find domain weights that minimize worst-case perplexity across domains β€” then uses those weights for the large model. RegMix (2024) and similar methods use regression on small-scale experiments to predict which weights maximize downstream benchmark scores.

The practical takeaway: for a 1T-token run, getting domain weights right is worth as much as adding ~10–20% more tokens from the wrong distribution.

12. Common Gotchas When Reading Pretraining Papers

A few traps to avoid when evaluating claims about pretraining:

  • Token count vs. parameter count confusion: Chinchilla showed you should train ~20 tokens per parameter for compute-optimal runs. A "7B model trained on 1T tokens" is 3Γ— over-trained for compute but often optimal for inference cost.
  • Perplexity on what?: WikiText-103 perplexity is almost meaningless for a 70B model. Always check the eval set.
  • Data contamination: If a model reports strong MMLU or HumanEval scores, check whether those benchmarks appear in the training data. Many do. HuggingFace's lm-eval-harness now includes contamination detection.
  • "We used open data" β‰  reproducible: The exact version of Common Crawl dumps, filter thresholds, and mixing weights are rarely published in full. FineWeb is a notable exception β€” the full pipeline code is public.
  • Loss reported in different units: nats (log⁑e\log_e) vs. bits (log⁑2\log_2) differ by a factor of log⁑2(e)β‰ˆ1.44\log_2(e) \approx 1.44. Bits-per-character and bits-per-byte add another conversion. Always check the units before comparing numbers across papers.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. What does a cross-entropy loss of 0.0 at a given token position imply?
    • The model assigned probability 1.0 to the correct next token
    • The model's vocabulary is too small to represent the token
    • The gradient update for that position is very large
    • The softmax output was uniform across all vocabulary items
  2. Why does the FineWeb-Edu dataset achieve competitive benchmark performance despite having far fewer tokens than the full FineWeb corpus?
    • It uses a larger tokenizer vocabulary, reducing apparent token count
    • High-quality educational content provides denser learning signal per token than generic web text
    • It was trained with a higher learning rate that compensates for fewer tokens
    • Educational text has shorter sequences, reducing the sequence length penalty
  3. A researcher compares cross-entropy loss between two models: Model A scores 2.1 nats and Model B scores 1.9 nats, but they used different tokenizers. What is the main problem with this comparison?
    • Nats and bits cannot both be used to measure cross-entropy
    • A larger vocabulary mechanically lowers per-token loss even without better language understanding
    • The models must be the same size for loss to be comparable
    • Cross-entropy is only valid when computed on the training set, not a held-out set
  4. In the standard pretraining loop, where do the training labels come from?
    • A separate human annotation pipeline that labels each token's part of speech
    • The next token in the same input sequence, shifted by one position
    • A teacher model that generates soft probability targets via knowledge distillation
    • A curated vocabulary list that maps each token to its expected successor
  5. What did Lee et al. (2022) find when they trained language models on deduplicated versus non-deduplicated data?
    • Deduplication hurt performance because it reduced dataset diversity
    • Deduplication improved downstream performance and reduced verbatim memorization
    • Deduplication had no measurable effect at scales above 100B tokens
    • Deduplication improved memorization but degraded generalization on held-out benchmarks

Related lessons

AI
advanced

Loop engineering: verification, orchestration, and anti-patterns

Make loops trustworthy. Adversarial verification panels, sub-agent orchestration, loop-until-dry for unbounded discovery, loop-until-budget for paid depth, multi-modal sweeps with diverse prompts, eval-driven cap selection, and the five anti-patterns β€” silent caps, infinite plans, drift, premature termination, thrashing β€” that ship to prod more than they should.

9 stepsΒ·~14 min
AI
intermediate

Loop engineering in production: state, errors, and budgets

Once an agent loop runs past a few iterations the failure surface shifts: state bloats, tool calls fail in three distinct ways, retries multiply if you stack them wrong, and unbounded budgets rack up four-figure bills overnight. The state, errors, and budgets you need to make the loop survive contact with reality.

8 stepsΒ·~12 min
AI
beginner

Loop engineering basics: the agent control loop

How LLM agents actually run: the iterative prompt-action-observation loop, the ReAct shape, the smallest tool-calling loop in twelve lines, why you always stack three termination layers, what the model sees on iteration N, and when a single prompt is the better answer.

8 stepsΒ·~12 min
AI
intermediate

LLM Observability with OpenTelemetry: GenAI Semantic Conventions

Master the OTel GenAI semantic conventions β€” gen_ai.* attributes, span structure for prompts/completions/tools, sampling strategies, and cost attribution β€” and understand why standardizing across LangSmith, Phoenix, Datadog, and Grafana matters for production AI systems.

13 stepsΒ·~20 minΒ·audio