Neural Mastery

Training Pipeline

Going from a randomly-initialized Transformer to something like ChatGPT is a multi-stage pipeline, not a single training run.

Think of it like educating a person from scratch. Pretraining is like reading an enormous amount of everything ever written — you come out knowing a huge amount, able to complete sentences fluently, but with no sense of how to actually be helpful to someone asking you something. Supervised fine-tuning is like an apprenticeship: watching a skilled person handle real requests and imitating the shape of a good answer. RLHF/DPO/GRPO are like getting ongoing feedback — "that answer was better than that one" — and adjusting your instincts based on which responses people actually preferred, not just copying examples anymore. Everything after that (PEFT, distillation, quantization) is about making an already-trained model cheaper or faster to specialize and run, without repeating the expensive parts.

Everything below makes each of those stages exact — the real objectives, the real formulas, and why each one exists.

Pretraining

The model is trained on massive amounts of text with a simple self-supervised objective: predict the next token, given everything before it. Loss is cross-entropy between the predicted token distribution and the actual next token (see Probability & Statistics). This single objective, at sufficient scale, produces a model with broad world knowledge, grammar, reasoning patterns, and style — but one that isn't yet reliably helpful or instructable; it just continues text plausibly.

The
cat
sat
on
the
mat
predicting token after "The cat" — actual next token: sat
sat
0.55
ran
0.18
the
0.02
purred
0.25
loss = -log(0.55) = 0.60
L=logP(xtx<t)\mathcal{L} = -\log P(x_t \mid x_{<t})
Click a token -- the model's predicted distribution over candidate next-tokens at that position, and the cross-entropy loss (-log p) for whichever token actually came next. Sharper, more confident correct predictions mean lower loss.

The Data Pipeline

Pretraining data is not "download the internet and go" — it's a multi-stage pipeline, and the quality of each stage directly shows up in the final model:

Crawl → Clean → Deduplicate → Filter → Tokenize → Pack → Pretraining
  • Crawl: raw sources — Common Crawl (a public web crawl), curated sources (Wikipedia, books, code repositories), and increasingly licensed/proprietary data.
  • Clean: strip HTML/boilerplate, fix encoding issues, remove obviously broken or non-natural-language text — raw web crawl is dominated by navigation menus, ads, and garbage that provides no useful training signal.
  • Deduplicate: remove exact and near-duplicate documents — web crawl data is heavily duplicated (the same article mirrored/scraped repeatedly), and training on duplicates wastes compute and can cause the model to memorize specific duplicated passages verbatim rather than generalizing, a real, measured effect in the literature.
  • Filter: remove low-quality, toxic, or otherwise undesirable content — via heuristic rules (document length, symbol-to-word ratio) and learned quality classifiers (a smaller model trained to distinguish "high-quality" text, e.g. Wikipedia-like, from low-quality web text).
  • Contamination checking: specifically verify that benchmark evaluation sets (the ones the model will later be scored on) haven't leaked into the training data — a real, easy-to-introduce-by-accident problem, since web-scale crawls can and do contain copies of published benchmark questions and answers, and training on them invalidates the resulting evaluation scores the same way data leakage invalidates a classical ML evaluation.
  • Tokenize and pack: convert cleaned text into token IDs (see Foundation Model Internals — Tokenization), then pack many documents back-to-back into fixed-length training sequences (with a separator token between documents) so every training batch is fully utilized — leaving sequences padded to a fixed length instead would waste significant compute on padding tokens that carry no signal.
100%
78%
45%
30%
29%
29%
Crawl
Clean
Deduplicate
Filter
Check contamination
Tokenize & pack
Illustrative relative volumes -- click any stage for what it does.
Remove exact + near-duplicate documents -- training on duplicates wastes compute and causes verbatim memorization.

Data Mixtures and Curriculum

Not all data sources are weighted equally — pretraining corpora are deliberately mixed from multiple sources (web text, code, books, academic papers, multilingual text) in specific proportions, tuned because the mixture measurably affects downstream capability (e.g. including code data measurably improves a model's general reasoning ability, not just its coding ability). Some pretraining runs also use a data curriculum — deliberately changing the mixture over the course of training (e.g. upweighting higher-quality data in later stages) rather than sampling from a single fixed distribution throughout.

Scaling Laws and the Chinchilla Result

Scaling laws are empirical relationships describing how model loss improves as you scale up model size, dataset size, and compute — power-law relationships fit from many training runs at different scales, precise enough to extrapolate and plan a training run's expected final loss before running it at full scale.

The Chinchilla paper's specific, influential finding: for a fixed compute budget, earlier practice (exemplified by GPT-3-era models) trained models that were too large relative to the amount of data they saw — compute-optimal training instead scales model size and training tokens together, roughly in a fixed ratio (Chinchilla's finding was approximately 20 training tokens per parameter). A smaller model trained compute-optimally on more data reliably beats a larger model undertrained on less data, for the same total compute cost — a result that directly reshaped how frontier labs allocate compute between "bigger model" and "more data," and part of why data pipeline quality (above) matters as much as architecture.

Compute-optimal (Chinchilla)
N ≈ 28.9B params
D ≈ 577.4B tokens
ratio D/N ≈ 20
Old (too large, undertrained)
N ≈ 63.5B params
D ≈ 262.4B tokens
ratio D/N ≈ 4.1
Same compute cost, both satisfy C ≈ 6ND — only the split between model size and data differs.
C6NDDoptimal20NC \approx 6ND \qquad D_{\text{optimal}} \approx 20N
At the SAME total compute, an old GPT-3-era split (too-large model, undertrained) lands off the compute-optimal ratio -- Chinchilla's finding is that the smaller, more-data-trained model on the ratio line reliably wins for the same cost.

Compute, Batching, and Precision

  • Compute budget: total training compute is often approximated as C6NDC \approx 6ND (a standard rule of thumb) where NN is parameter count and DD is training tokens — the "6" absorbing the forward and backward pass FLOPs per token per parameter — the quantity scaling laws are fit against.
  • Batch size and gradient accumulation, mixed precision, checkpointing: covered in full in GPU/AI Infrastructure & Distributed Training — pretraining is precisely the setting that infrastructure page's parallelism strategies (data/tensor/pipeline parallelism, FSDP, mixed precision) exists for, run continuously for weeks across thousands of GPUs.
  • Pretraining evaluation: tracked primarily via the training loss curve itself (see the animated training-loss visual) and perplexity on a held-out set (literally ecross-entropy losse^{\text{cross-entropy loss}} — how "surprised" the model is by held-out text, lower is better), plus periodic evaluation against standard benchmarks to track emerging capabilities over the course of the run, not just at the end.

Supervised Fine-Tuning (SFT)

The pretrained model is fine-tuned on a smaller, curated dataset of (instruction, ideal response) pairs, written or curated by humans. This teaches the model the format and behavior of being a helpful assistant — following instructions, answering directly instead of just continuing text, refusing certain requests.

Model stage
prompt: "How do I reverse a list in Python?"
Use list.reverse() to reverse in place, or reversed(list) / list[::-1] to get a reversed copy without mutating the original.
follows the instruction
SFT model: fine-tuned on (instruction, ideal response) pairs -- it now recognizes the prompt as an instruction to follow and responds directly, in the format of a helpful assistant.

RLHF — Reinforcement Learning from Human Feedback

SFT alone isn't enough to capture nuanced preferences ("this response is better, not just acceptable"). RLHF adds:

  1. Collect human rankings of multiple model outputs for the same prompt.
  2. Train a reward model to predict which output humans would prefer.
  3. Fine-tune the LLM with reinforcement learning (typically PPO — Proximal Policy Optimization) to maximize the reward model's score, while a KL-divergence penalty (see Probability & Statistics) keeps the fine-tuned model from drifting too far from the original SFT model — preventing it from degenerating into responses that game the reward model without actually being good.

Reward hacking: when the model finds a way to score highly on the reward model without genuinely satisfying human intent (e.g. being unnecessarily verbose because the reward model correlates length with thoroughness). A core failure mode RLHF practitioners actively guard against.

Sample outputsHuman rankingTrain reward modelPPO fine-tuneKL penalty
Click a stage. The KL penalty (amber) isn't a separate pipeline step -- it's applied throughout the PPO fine-tune step.
A separate model is trained to predict which output humans would prefer -- turning rankings into a scorable reward signal.

Direct Preference Optimization (DPO)

A simpler alternative to RLHF: instead of training a separate reward model and running full RL, DPO reformulates the preference-learning objective so it can be optimized directly on preference pairs (chosen vs. rejected response) with a supervised-learning-style loss. Removes the complexity and instability of RL while achieving comparable results — widely adopted because it's simpler to implement and tune.

RLHF
DPO
preference pairsdirect supervised-style losson (chosen, rejected)aligned LLM(no reward model, no RL loop)
Same input, 2 stages to the aligned model.
DPO: the same preference pairs feed a single supervised-style loss directly -- no reward model, no RL loop. Simpler and more stable to tune, at some loss of RL's flexibility.

Group Relative Policy Optimization (GRPO)

A more recent RL approach (notably used in DeepSeek's models) that removes the need for a separate value/critic network by instead comparing a group of sampled outputs for the same prompt against each other's average reward — reducing training cost and complexity, particularly effective for reasoning-focused fine-tuning.

PPO
GRPO
sample 1
r=0.82
sample 2
r=0.61
sample 3
r=0.35
sample 4
r=0.74
sample 5
r=0.49
group average reward (baseline) = 0.602
sample 3 advantage = 0.35 − 0.60 = -0.252
GRPO: sample a group of outputs for the SAME prompt, use the group's own average reward (0.60) as the baseline -- no critic network needed. Hover a sample to see its advantage.

Other Preference-Optimization Objectives

DPO and GRPO are the two most widely deployed, but the same "learn directly from preference/reward signal without a full separate RL loop" family has several other named variants worth recognizing:

  • IPO (Identity Preference Optimization): addresses a specific theoretical weakness in DPO's loss — DPO can overfit when preference pairs are close to deterministic (nearly always preferring one response), pushing the model toward extreme, overconfident probability ratios. IPO adds a regularization term that keeps the objective well-behaved even in that regime, without needing DPO's KL-penalty coefficient to be tuned as carefully.
  • KTO (Kahneman-Tversky Optimization): named for the behavioral-economics loss-aversion research it's inspired by — unlike DPO, KTO doesn't need paired preference data (chosen vs. rejected for the same prompt) at all, just independent binary labels ("this output was good" / "this output was bad") — a real practical advantage, since unpaired binary feedback is often far cheaper to collect at scale than carefully constructed preference pairs.
  • ORPO (Odds Ratio Preference Optimization): folds preference optimization directly into the SFT stage itself — a single combined loss (standard SFT cross-entropy plus an odds-ratio-based preference term) trains instruction-following and preference alignment simultaneously, removing the separate SFT-then-DPO two-stage pipeline entirely.
  • RLOO (REINFORCE Leave-One-Out): a simplified policy-gradient RL approach (using the classic REINFORCE algorithm rather than PPO) that, like GRPO, avoids training a separate critic/value network — instead using other samples in the same batch as a variance-reducing baseline ("leave one out" of the batch to estimate the baseline for each sample) — computationally cheaper than PPO-based RLHF while still being a genuine RL method, not a supervised reformulation like DPO/IPO/KTO/ORPO.

The practical pattern across all of these: the field has been steadily moving away from full RLHF's separate-reward-model-plus-PPO complexity toward objectives that get similar alignment quality more cheaply and stably — DPO-family methods differ mainly in what data format they need (paired vs. unpaired) and what theoretical failure mode of plain DPO they patch, while GRPO/RLOO stay genuinely RL-based but drop PPO's critic network specifically.

Method
built on
DPO
data format needed
unpaired binary labels
These differ mainly in data format required and which specific failure mode of the baseline they patch.
Doesn't need paired data at all -- just independent binary labels (good/bad), inspired by loss-aversion research.

Parameter-Efficient Fine-Tuning (PEFT)

Full fine-tuning updates every parameter — expensive and requires storing a full copy of the model per fine-tuned variant. PEFT methods update far fewer parameters:

  • LoRA (Low-Rank Adaptation): freezes the original weights and injects small trainable low-rank matrices alongside them (see Linear Algebra — this works because weight updates during fine-tuning empirically have low effective rank). Dramatically cuts trainable parameters and memory.
Wfrozend×d+A×Br×d, d×r — trainable
65,536 trainable params (0.391% of full fine-tuning)
W=W+BABRd×r, ARr×dW' = W + BA \qquad B \in \mathbb{R}^{d\times r},\ A \in \mathbb{R}^{r\times d}
At rank r=8, LoRA trains 65,536 parameters for this 4096×4096 weight matrix vs. 16,777,216 for full fine-tuning -- 0.391% of the original, because weight UPDATES during fine-tuning empirically have low effective rank.
  • QLoRA: combines LoRA with quantizing the frozen base model to 4-bit precision, enabling fine-tuning of very large models on a single consumer GPU.
  • DoRA (Weight-Decomposed Low-Rank Adaptation): decomposes each frozen weight matrix into a magnitude component and a direction component, applying LoRA's low-rank update only to the direction while letting magnitude adapt separately and freely — closes a measurable quality gap between LoRA and full fine-tuning by giving the model an extra, cheap degree of freedom (magnitude) that plain LoRA's low-rank constraint doesn't allow, at a small additional parameter cost over standard LoRA.
  • Adapters: small trainable modules (typically a down-projection, a nonlinearity, and an up-projection) inserted between frozen Transformer layers, rather than alongside a weight matrix like LoRA — one of the original PEFT approaches, predating LoRA.
  • IA3 (Infused Adapter by Inhibiting and Amplifying Inner Activations): instead of adding new weight matrices at all, learns a small set of per-channel rescaling vectors that multiply existing activations (in attention and the MLP) elementwise — dramatically fewer trainable parameters than even LoRA, at some capacity cost, and notably doesn't add any extra inference latency once merged since it's a pure elementwise rescale.
  • Prefix tuning: prepends a small number of trainable virtual tokens' worth of key/value vectors to every attention layer (not actual text tokens, learned continuous vectors) — the frozen model attends to these learned prefixes alongside the real input, steering behavior without touching any of the model's own weights at all.
  • Prompt tuning: a simplification of prefix tuning — instead of learned KV vectors injected at every layer, learns a small number of continuous "soft prompt" embeddings prepended only at the input layer. Fewer trainable parameters than prefix tuning, generally somewhat less expressive, but simpler to implement and still effective at larger model scales.
  • Quantization-aware fine-tuning: distinct from QLoRA's "quantize the frozen base, train full-precision LoRA adapters on top" — this instead simulates quantization effects during the fine-tuning forward/backward pass itself (see LLM Inference Optimization — QAT), producing a model that's fine-tuned to already be robust to the precision loss it'll actually run at in production, rather than being fine-tuned in full precision and only quantized afterward.
Full fine-tune
100%
Adapters
3.5%
Prefix tuning
2%
LoRA
0.5%
DoRA
0.6%
Prompt tuning
0.1%
IA3
0.03%
Log-scale bars (illustrative percentages) -- click a method for its mechanism.
Frozen weights + small trainable low-rank matrices alongside them.

PEFT frameworks like Unsloth make this training step itself faster and more memory-efficient — but training and serving are different jobs. Once fine-tuning produces adapter weights, they still need to be handed off to a real inference engine (vLLM, llama.cpp, SGLang) to actually serve traffic — see LLM Hosting & Serving Patterns for that handoff, and why Unsloth itself isn't what you deploy.

Knowledge Distillation

Train a smaller "student" model to mimic a larger "teacher" model's output distribution (not just the hard labels, but the full soft probability distribution, which carries more information about the teacher's "reasoning"). Produces smaller, faster models that retain much of the teacher's capability — used heavily to create deployable small models from expensive frontier models.

Target
cat
0.62
dog
0.24
fox
0.09
car
0.02
sky
0.03
Same image, same true label ("cat") — the soft distribution carries strictly more information.
Soft labels (teacher's full distribution): also encodes that "dog" is far more plausible than "car" -- this relative-similarity signal is what the student actually learns from, beyond just the right answer.

Quantization

Reduces the numerical precision used to store model weights — from 32-bit or 16-bit floats down to 8-bit or 4-bit integers (int8, int4; GPTQ and AWQ are popular quantization algorithms). Cuts memory footprint and can speed up inference substantially, at a small, often negligible, cost to output quality — essential for running large models on limited hardware.

Precision
7.0 GB
weight memory (7B params)
256
representable values per weight
value grid at this precision (showing 16 of many)
A 7B-parameter model at 8-bit precision needs ≈7.0 GB just to store weights -- 256 distinct representable values per weight. Lower precision means a coarser value grid (quantization error), at real memory and speed savings.

Next: Prompt Engineering — getting the most out of a model without touching its weights at all.

Last updated Sep 5, 2026Edit this pageReport an issue
← Previous
Foundation Model & Transformer Internals
Next →
Prompt Engineering