Models & Sampling

How a model is made, how it picks each token, and how to tame the randomness.

Reference · Chapter 2

Training pipeline

StageDataResult
Pre-trainingWeb-scale text, self-supervisedA text completer; ~98% of compute (InstructGPT)
Supervised finetuning (SFT)(prompt, response) demonstrationsConverses instead of just completing
Preference finetuning(prompt, winner, loser) comparisons → reward model (RLHF), or DPO / RLAIFResponses people prefer

SFT + preference finetuning = post-training. Memory napkin math: parameters × 2 bytes (16-bit) ≥ inference memory, so 7B → 14 GB.

Sampling pipeline

logits ÷ temperature → softmax → top-k → top-p → sample

NeedSettings
Consistent output (grading, classification, extraction)Temperature 0 (greedy), fixed seed, cache answers
Creative output (taglines, brainstorming)Temperature ≈ 0.7 as a starting point, top-p 0.9–0.95
Smaller, cheaper softmaxTop-k (50–500)
Context-sized candidate poolTop-p (nucleus)
Cap latency and costMax tokens or stop tokens, with enough headroom for structured output
Higher quality, more moneyTest-time compute: best-of-N (cost ≈ N×)

Worked example: logits [1, 2]. At T = 1, B = 73%. At T = 0.5, B = 88%. As T → 0, B → 100%.

Probabilistic failure modes

ProblemCauseMitigation
Inconsistency (same input)Sampling randomness; hardwareFix sampling variables and seed; cache. Still not guaranteed.
Inconsistency (slightly different input)Model sensitivityCareful prompts (Ch 5), memory (Ch 6)
HallucinationSelf-delusion (treats its own output as fact; snowballs) · knowledge mismatch with SFT labelersConcise answers, “say I don't know,” grounding with context (Ch 5–6), detection (Ch 4)

← Lesson 2Book Map

Related ideas