Pretraining

Wiki chapters and blog posts about Pretraining.

Wiki

Long Context: Position Extrapolation, Training Recipes and Cost

Extending a context window from 4K to 128K takes three fixes: RoPE extensions (PI, NTK-aware scaling, YaRN) for unseen positions, staged continued training on long text, and a budget for attention compute and KV cache. This page covers all three with formulas, code and two interactive tools.

Advanced 40 min read

Information Theory Refresher: Entropy, Cross-Entropy and KL Divergence

Entropy measures how surprising a distribution is on average, cross-entropy is the cost of coding data from p with a code built for q, and KL divergence is the excess over the optimum. A language model's training loss is cross-entropy; perplexity is its exponential.

Intermediate 11 min read

From Language Modeling to Next-Token Prediction

Language modeling estimates the probability of a text and uses the chain rule to split it into a product of "predict the next token given the prefix"; training uses cross-entropy, evaluation uses perplexity, and the causal mask hides future tokens.

Beginner 13 min read

Pretraining Data Engineering: Filtering, Deduplication and Mixing

A pretraining corpus is filtered, deduplicated and mixed step by step, and every step throws data away. This page shows what FineWeb, Gopher, DCLM and Llama 3 drop and why, then runs MinHash near-duplicate removal on a demo corpus: how many words may change before a copy stops counting.

Intermediate 52 min read

Distributed Training: Data, Tensor and Pipeline Parallelism

Data parallelism splits the batch, tensor parallelism the matrices inside a layer, pipeline parallelism the stack of layers; ZeRO and FSDP shard optimizer state, gradients and parameters. Runnable checks verify gradient averaging, ring all-reduce traffic, the Megatron split and the bubble formula.

Advanced 42 min read

Scaling Laws: How to Split Compute Between Parameters and Data

From Kaplan to Chinchilla: why loss falls as a power law in parameters, data and compute, how to split a compute budget between parameters and tokens, and why today's models are trained on far more data than the compute-optimal amount.

Intermediate 20 min read

The LLM Training Pipeline: From Data to Deployment

A map of the pipeline from data preparation, pretraining, SFT and preference alignment to evaluation and release, with the public data scale of each stage, plus a 0.86M-parameter byte-level GPT that trains on a laptop CPU to test data mixture, resuming and leakage.

Intermediate 34 min read

GPUs and the Roofline Model: Why Big Models Are Bound by Memory Bandwidth

A GPU is limited by two walls: how many operations it can do per second (compute) and how many bytes it can move per second (memory bandwidth). The roofline model uses arithmetic intensity to tell which wall an operator hits first. This page derives it and measures it on a CPU.

Intermediate 41 min read

GPU kernels: from tiled matrix multiplication to fusion

GPU kernel tuning combines thread-level memory access, on-chip reuse, and output stores. Derive the logical traffic of tiled matrix multiplication with edge masks, check it with a CPU experiment, and trace practical limits from staging buffers to register spills.

Intermediate 9 min read

Numerical Precision: Three Ways FP16, BF16 and FP8 Training Goes Wrong

Floating-point formats split a few bits between exponent (range) and mantissa (precision). Low-precision training fails in three ways: overflow, underflow (small gradients become 0) and swamping (small terms vanish in sums). Real Qwen2.5-0.5B data shows how large each is and what fixes it.

Intermediate 31 min read

Blog posts

mHC: Keeping a Wider Residual Stream Stable With Doubly Stochastic Mixing

mHC (Manifold-Constrained Hyper-Connections) is DeepSeek's fix for hyper-connections: Sinkhorn-Knopp turns the residual mixing matrix into a doubly stochastic one, keeping multi-layer gain bounded, for a reported 6.7% extra training time. This post reviews it and checks the math in code.

Paper notes arXiv 2025

GR00T N1: A Data Pyramid and a Dual-System Model for Generalist Humanoid Robots

GR00T N1 (NVIDIA, 2025) pairs an Eagle-2 vision-language model with a flow-matching DiT action head and pretrains on a data pyramid of real robots, simulation, neural videos and human video. This post recomputes its parameters, the Eq. (1) sign, the data tallies and the results.

Paper notes arXiv 2025

DeepSeek-V3 Technical Report: MoE, MLA, FP8 and Low-Cost Training

DeepSeek-V3 is a 671B-parameter MoE model that activates 37B per token. It adds auxiliary-loss-free balancing and multi-token prediction to MLA and DeepSeekMoE, and trains with FP8 and DualPipe on 2048 H800 GPUs in 2.788M GPU hours. This post covers methods, numbers and limits.

Paper notes arXiv 2024

Chameleon: Early-fusion token modeling for images and text

Chameleon trains one Transformer from scratch over discrete image and text tokens. Its 7B and 34B models handle interleaved multimodal input and output instead of attaching vision to a frozen LLM.

Paper notes arXiv 2024

Phi-3: Small models and the textbook-quality data strategy

Phi-3-mini trains 3.8B parameters on 3.3T tokens using heavily filtered and synthetic textbook-quality data. It demonstrates local deployment potential while acknowledging capacity limits in factual knowledge.

Paper notes arXiv 2024

LLaMA: Train Smaller Models for Longer, on Public Data Only

LLaMA is a family of 7B to 65B foundation models from Meta (2023), trained on public data only. The paper reports LLaMA-13B beating GPT-3 175B on most benchmarks. This post covers the inference-budget argument, data, architecture, training cost and evidence, and which claims need a discount.

Paper notes arXiv 2023

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness

FlashAttention computes exact attention block by block in the GPU's on-chip SRAM, so the N×N score matrix never goes to GPU memory. HBM accesses fall from Θ(Nd+N²) to Θ(N²d²/M); BERT trains about 15% faster, GPT-2 3.0 to 3.5 times faster than HuggingFace. A breakdown of the paper.

Paper notes NeurIPS 2022

Scaling Laws for Neural Language Models

OpenAI's 2020 paper trained Transformers of 768 to 1.5 billion parameters on WebText2 and found that loss falls as a power law in parameters, data and compute, almost regardless of shape. At a fixed budget, grow the model first (size ∝ compute^0.73). A breakdown that checks the numbers.

Paper notes arXiv 2020

RoBERTa: A robustly optimized BERT recipe

RoBERTa keeps BERT’s architecture but uses more data, larger batches, longer training, dynamic masking, byte-level BPE and no NSP, showing the original model was undertrained.

Paper notes arXiv 2019

AdamW: Decoupling Weight Decay from Adam's Adaptive Update

L2 regularization and weight decay are equivalent in SGD but not in Adam: the L2 gradient is rescaled by the second-moment estimate, so weights with large gradients are decayed less. AdamW applies the decay directly to the weights. We verify the propositions in code and check PyTorch's convention.

Paper notes ICLR 2019 2017

Topics