A Guide to Pre-training Large Language Models
The Library Metaphor: What Pre-training Actually Does
Pre-training is the phase where an LLM learns "Language" and "World Knowledge" by reading petabytes of text.
π― System Scenario & Requirements
System Goal
Pre-training is the phase where an LLM learns "Language" and "World Knowledge" by reading petabytes of text.
Actors & Components
- Application or API tier
- Core service under design
- Operator or SRE observer
Functional Requirements
- TLDR: Pre-training is the phase where an LLM learns "Language" and "World Knowledge" by reading petabytes of text.
- It uses Self-Supervised Learning to predict the next word in a sentence.
- This creates the "Base Model" which is later fine-tuned.
Constraints & Assumptions
- Training a frontier-scale LLM (GPT-4, LLaMA 3 70B) requires: - Compute: thousands of H100 GPUs running for months - Cost: $50Mβ$100M+ per frontier run - Energy: significant carbon footprint The key trade-offs: - Data scβ¦
- Most practitioners work with open base models (LLaMA, Mistral, Qwen) and apply LoRA fine-tuning.
β‘ Execution Stages & Decision Trace
Stage 1 β Deep Dive: Pre-Training Fundamentals
Before an LLM can write poetry, translate languages, or explain code, it needs to understand the basic patterns of human text.
Pre-training is the process of building that foundational understanding from scratch β no hand-labeled data required.
Deep Dive
Scenario context
Pre-training is the phase where an LLM learns "Language" and "World Knowledge" by reading petabytes of text. It uses Self-Supervised Learning to predict the next word in a sentence. This creates the "Base Model" which is later fine-tuned.
Imagine teaching a child to read.
- Pre-training: You lock the child in a library for ten years. They read every book β grammar, history, math, code, recipes. They aβ¦
This walkthrough focuses on The Library Metaphor: What Pre-training Actually Does.
Before making a design choice, clarify:
- Imagine teaching a child to read.
- Pre-training: You lock the child in a library for ten years.
- They read every book β grammar, history, math, code, recipes.
Concepts in play: deep-learning-foundations, api-gateway, vector-embeddings-and-similarity, database-sharding
Stage 1 β Deep Dive: Pre-Training Fundamentals
- Before an LLM can write poetry, translate languages, or explain code, it needs to understand the basic patterns of human text.
- Pre-training is the process of building that foundational understanding from scratch β no hand-labeled data required.
- Self-supervised learning is the key insight that makes this scale.
- Instead of asking humans to annotate millions of examples, the model creates its own training signal: given the words so far, predict the next one.
Observable outcome: Instead of asking humans to annotate millions of examples, the model creates its own training signal: given the words so far, predict the next one.
Stage 2 β The Pre-Training Workflow: From Raw Data to Base Model
- Pre-training follows a structured pipeline.
- Each stage is essential β skipping or rushing any one of them measurably degrades the final model quality.
- What each stage does: - Dedup + filter: Remove near-duplicate web pages and low-quality HTML.
- Training on repeated text causes the model to memorize rather than generalize.
graph TD
A[Raw Text Sources Web Books Code Papers] --> B[Deduplication & Quality Filtering]
B --> C[Tokenization BPE / SentencePiece]
C --> D[Packed Sequences Fill Context Windows]
D --> E[GPU / TPU Cluster Forward Pass + Backpropagation]
E --> F{Checkpoint?}
F -->|Every N steps| G[Save Checkpoint to Storage]
F -->|Continue training| E
G --> H[Base Model]
Observable outcome: Training on repeated text causes the model to memorize rather than generalize.
Stage 3 β Next-Token Prediction: The Self-Supervised Training Signal
- The entire pre-training game is one question repeated billions of times: "Given the text so far, what is the most likely next token?" This is self-supervised because the labels are already in the data β you just mask thβ¦
- The Loss Function: Cross-Entropy $$L = -\sum {t} \log P(x t \mid x {<t})$$ - $x t$: the correct next token at step $t$ - $x {<t}$: all preceding tokens - $P(x t \mid x {<t})$: the model's probability for the correct tokβ¦
- Minimizing $L$ over trillions of tokens forces the model to learn grammar, facts, and reasoning patterns.
- π Next-Token Prediction Training Loop This sequence diagram traces one complete training step from raw text to updated model weights.
Input: "The capital of France is"
Target: "Paris"
sequenceDiagram
participant D as Data Loader
participant T as Tokenizer
participant M as Transformer
participant L as Loss Function
participant O as Optimizer
D->>T: Raw text sequence
T->>M: Token IDs [x1, x2, ..., xN]
M->>M: Forward pass (attention layers)
M->>L: Predicted logits for each position
L->>L: Cross-entropy vs true next token
L->>O: Backpropagate gradients
O->>M: Update all weights
M->>D: Ready for next batch
Observable outcome: π Next-Token Prediction Training Loop This sequence diagram traces one complete training step from raw text to updated model weights.
Stage 4 β The Data Pipeline: From the Web to a Training Run
- This pipeline shows the journey from raw internet crawl data to packed training batches ready for a GPU cluster.
- Near-duplicate removal and quality filtering happen early because noisy or repeated text degrades model quality far more than simply adding more volume.
- The final packing step is critical for GPU efficiency: sequences are concatenated to fill each context window completely, ensuring no compute cycles are wasted on padding tokens.
- Training data typically includes Common Crawl (web text), Books3, GitHub code, arXiv papers, and Wikipedia.
flowchart LR
A[Common Crawl Books3 / GitHub / arXiv] --> B[Deduplication]
B --> C[Quality Filtering]
C --> D[Tokenization]
D --> E[Packed Sequences 2K128K tokens]
E --> F[Training Shards on Object Storage]
F --> G[GPU Cluster]
Observable outcome: Training data typically includes Common Crawl (web text), Books3, GitHub code, arXiv papers, and Wikipedia.
Stage 5 β Deep Dive: Inside the Training Loop: Loss, Gradients, and Checkpoints
- The training loop looks like this in pseudocode: In practice, training runs on thousands of GPUs or TPUs for weeks to months, using advanced parallelism strategies (data parallelism, tensor parallelism, pipeline paralleβ¦
- π¬ Internals Pre-training minimizes cross-entropy loss over next-token prediction: L = -Ξ£ log P(t i t 1,...,t {i-1}).
- The training corpus is tokenized and packed into fixed-length sequences (e.g., 2048 or 4096 tokens); documents are concatenated with separator tokens to maximize GPU utilization.
- Learning rate follows a warmup-then-cosine-decay schedule β warmup prevents early instability; cosine decay improves final perplexity by 2β5%.
for batch in training_data:
tokens = tokenize(batch)
logits = model(tokens[:-1]) # predict all positions
loss = cross_entropy(logits, tokens[1:]) # compare to true next token
loss.backward() # compute gradients
optimizer.step() # update weights
optimizer.zero_grad()
if step % checkpoint_interval == 0:
save_checkpoint(model)
Observable outcome: Learning rate follows a warmup-then-cosine-decay schedule β warmup prevents early instability; cosine decay improves final perplexity by 2β5%.
Stage 6 β Real-World Applications: Where Pre-trained Models Power the World
- Pre-trained language models are the invisible backbone of dozens of products in use today.
- The same foundational technique β next-token prediction on massive corpora β powers everything from consumer chat apps to scientific research tools.
- Beyond chat assistants, pre-trained models drive progress across many fields: - Search engines: Google and Bing use LLMs to improve query understanding and surface direct answers within results.
- Legal and finance: Domain-specific models read and summarize contracts, regulatory filings, and earnings calls in seconds.
Observable outcome: - Legal and finance: Domain-specific models read and summarize contracts, regulatory filings, and earnings calls in seconds.
End-to-end outcome
Imagine teaching a child to read.
Alternatives considered
Decision Guide: What a Base Model Can and Cannot Do
A base model will happily continue any text you give it β including harmful content.
Failure and degraded modes
Trade-offs & Failure Modes: Cost, Carbon, and Scaling
- Detection: Training a frontier-scale LLM (GPT-4, LLaMA 3 70B) requires: - Compute: thousands of H100 GPUs running for months - Cost: $50Mβ$100M+ per frontier run - Energy: significant carbon footprint The key tβ¦
- User or operator impact: Training a frontier-scale LLM (GPT-4, LLaMA 3 70B) requires: - Compute: thousands of H100 GPUs running for months - Cost: $50Mβ$100M+ per frontier run - Energy: significant carbon footprint The key tβ¦
