A ai coolAId
Chapter 2 of 7

Training, Fine-Tuning & Adaptation

How weights actually change (backprop), when to use RAG vs. SFT vs. LoRA vs. distillation, and how to match compute and data to your real problem size.

Mental Model

Pretraining gives a model general capability. Adapting it to a real product means choosing the cheapest lever that actually solves your bottleneck: Prompting → RAG → SFT / LoRA → Distillation → RL.

1. How Learning Happens: Backpropagation & Gradient Descent

Every neural network—from a 500-parameter linear classifier to a trillion-parameter LLM—learns via the same loop (Backpropagation & Gradient Descent):

  1. Forward pass: Run input through the network and compute a scalar loss \(\mathcal{L}\) measuring how wrong the prediction was.
  2. Backward pass: Apply the chain rule backwards from \(\mathcal{L}\) to compute the gradient \(\nabla_\theta \mathcal{L}\) for every weight.
  3. Optimizer step: Nudge each weight slightly downhill (usually with AdamW): \[\theta_{t+1} = \theta_t - \eta \cdot \text{AdamW}(\nabla_\theta \mathcal{L})\]

2. The Adaptation Ladder: RAG vs. SFT vs. LoRA vs. Distillation

Engineers frequently reach for fine-tuning when they actually need retrieval—or vice versa. Match the tool to the failure mode:

Technique What It Changes Reach For It When...
RAG Prompt context at query time (weights frozen) Facts change often, need citations, or depend on user permissions (docs, tickets, codebase).
SFT Model weights via curated (input, output) pairs You need consistent tone, domain reasoning patterns, or strict multi-step tool-use formatting.
LoRA Tiny low-rank adapter matrices (0.1%–2% of weights) You want SFT on a single GPU or need to serve many customer-specific adapters from one base model.
Knowledge Distillation Trains a small student model on a large teacher's outputs Your frontier prompt works great, but latency or API cost is 10x too high for production volume.

Rule of Thumb: Knowledge vs. Behavior

  • Missing fresh facts? Use RAG (non-parametric memory).
  • Wrong style, format, or decision boundary? Use SFT / LoRA (parametric behavior).
  • Too slow or expensive at scale? Distill a strong teacher's verified outputs into a small 1B–8B student.

3. Match Compute & Data to the Actual Problem Size

One of the easiest traps in AI engineering is over-provisioning hardware or assuming every problem needs millions of rows and a GPU cluster:

  • CPU vs. GPU: GPUs shine on large parallel matrix multiplications (big batches, wide neural nets, image/text tensors). A tiny 20-input, 30-step Python simulator or small tabular model is CPU-bound—shipping tiny tensors across PCIe to a GPU is often slower than running on CPU.
  • Data vs. Parameters: Data requirements scale with effective parameter count, not vibes. A smooth seasonal demand curve \(y(d) \approx w_0 + w_1 \sin(2\pi d/365) + w_2 \cos(2\pi d/365)\) only has 3–5 parameters, so 3–5 years of daily observations already pins it down. By contrast, learning 2,000 independent categorical weights from noisy episodes requires orders of magnitude more data—or stronger inductive bias.

Engineering Takeaway: Before scaling to a bigger model or larger cluster, check whether you are bottlenecked on data quality, environment speed, or problem formulation.