06 / 16

Fine-tuning & Adaptation

Changing the weights: when it pays, and which knob to turn.

0/13 studied
01Prompt → RAG → fine-tune: the decision

Climb only as high as you must: prompt, few-shot, RAG for missing knowledge, fine-tuning for behavior, format or cost. RAG adds facts; fine-tuning changes form. Pretraining is almost never the answer.

Core◇ Diagram
02Supervised fine-tuning (SFT)

Keep training a pretrained model on (prompt, ideal response) pairs so it imitates them. It teaches format, style and task behavior; a few thousand curated examples usually beat a million noisy ones.

Core◇ Diagram</> Code
03LoRA & QLoRA

Freeze the model and train a low-rank update per weight matrix: usually under 1% of parameters, saved as a megabyte-sized adapter. QLoRA stores the frozen base in 4-bit, so big models fine-tune on one GPU.

Core◇ Diagram</> Code
04RLHF: reinforcement learning from human feedback

Humans pick the better of two answers, a reward model learns those preferences, and PPO pushes the model toward high reward while a KL penalty keeps it near the original. It turned GPT-3 into InstructGPT.

Advanced◇ Diagram</> Code
05DPO & preference optimization

Tune a model directly on (prompt, chosen, rejected) pairs with a simple classification-style loss: the RLHF objective without a reward model or an RL loop, so it is cheaper and far more stable than PPO.

Advanced◇ Diagram</> Code
06RL with verifiable rewards (GRPO)

Reinforcement learning where a program, not a person, scores each answer (tests pass, result matches). GRPO makes it cheap: sample a group of answers per prompt and reinforce those that beat the group average.

Advanced◇ Diagram</> Code
07Knowledge distillation

Train a small student to imitate a large teacher, via its probabilities or its generated text, keeping most of the quality on your task at a fraction of the cost and latency.

Core◇ Diagram</> Code
08Synthetic data

Use LLMs to generate training or eval data, then filter, dedup and verify it. It's cheap coverage for rare cases, but it inherits the generator's blind spots, so humans still check eval labels.

Core◇ Diagram</> Code
09Training data quality & curation

Quality beats quantity: remove exact and near duplicates, filter junk with heuristics and classifiers, decontaminate against evals, scrub PII and balance the mix. A smaller clean set routinely beats a bigger dirty one.

Advanced◇ Diagram</> Code
10Catastrophic forgetting

Fine-tuning on narrow data overwrites what the model already knew, degrading general skills, instruction following and even safety. Mitigate with LoRA, general-data replay, lower learning rates and fewer epochs, and gate on general evals.

Advanced◇ Diagram</> Code
11Continued pretraining

Keep training a base model with the plain next-token objective on billions of unlabeled domain tokens so it absorbs a new domain or language; powerful but expensive, and for most enterprises RAG plus SFT wins.

Staff+◇ Diagram</> Code
12Fine-tuning embeddings & rerankers

Train your retriever on domain (query, relevant passage) pairs with a contrastive loss, using LLM-generated queries and mined hard negatives. A tuned embedder means re-indexing everything; a tuned reranker doesn't.

Advanced◇ Diagram</> Code
13Distributed training: DP, TP, PP, FSDP

Split training across GPUs by data (replicate model, split batch), by state (FSDP/ZeRO shard weights, gradients, optimizer), inside layers (tensor) or across layers (pipeline). Mixed-precision Adam needs ~16 bytes per parameter.

Advanced◇ Diagram</> Code