Cheat sheet · Module 15

Cheat sheet: Fine-tuning

Generative AI · Cheat sheet · Fine-tuning

Open the moduleModule quiz

Decide: retrieval for what the model should know, tuning for how it should behave. A tune must beat the best prompt plus retrieval, on the newest base, on a held-out set. Write the break-even (one-off cost ÷ saving minus hosting minus maintenance per day) and test it against the cheapest alternative, such as prefix caching. The baseline moves; so does the base model.

Arithmetic (state assumptions aloud):

  • Full fine-tuning with Adam in mixed precision: 2 + 2 + 12 = 16 bytes per parameter of model states (ZeRO paper), before activations, logits and overhead. A frozen 16-bit base costs 2 bytes per parameter, a 4-bit base about 0.5; only the adapter pays the 16.
  • Compute is about 6ND (about 2 forward, 4 backward). LoRA skips weight gradients, so roughly 4ND: memory saved, not a hundredfold in compute.
  • LoRA parameters per matrix: r × (d_in + d_out). Per-GPU states with N GPUs: ZeRO-1 4P + 12P/N, ZeRO-2 2P + 14P/N, ZeRO-3 16P/N.
  • Effective batch = sequences per pass × accumulation steps × GPUs.

Data

  • The training set is the specification. Quality and variety first; check whether a learning curve is still rising before buying more.
  • Filter, deduplicate, decontaminate, remove personal data, then split. Split by group or by time when rows are related, not by row. Keep a golden set nobody trains on and report slices.
  • Render with the serving chat template, include the end token, mask the loss to the response, and decode a batch to read what the model sees.
  • Verify synthetic data with something other than the generator; model-generated data can narrow the distribution.

LoRA and QLoRA

  • The update is ΔW = B·A with rank at most r. B starts at zero and A random, so training starts from the base model. The update is scaled by alpha over r.
  • Choose targets before rank. Change one of rank, alpha or learning rate at a time.
  • QLoRA: NF4 base, quantised scaling constants, paged optimisers, 16-bit adapters.
  • Full fine-tuning can learn more on hard domains; LoRA forgets less. Merge for one hot adapter, keep separate for many tenants and one-step rollback.

Training

  • SFT is next-token cross-entropy, usually on response tokens only. Pick checkpoints by a held-out task metric, not validation loss alone.
  • Accumulation and checkpointing save activations, not model states. Sharding shrinks states and costs communication; if the job fits, do not shard.
  • Debug in order: decode labels, overfit a tiny batch, compare train and serve prompts, run the baseline, change one thing.

Alignment

  • RLHF: SFT, a reward model, then RL against the reward minus a KL penalty to a reference. DPO: minus log-sigmoid of beta times the difference of two log-ratios against a frozen reference; higher beta stays closer.
  • IPO bounds the gap, KTO needs single labelled answers, ORPO and SimPO drop the reference. Reported gains are each paper’s own setup, not a ranking.
  • GRPO: advantage = (reward − group mean) ÷ group standard deviation, no critic. Verifiable rewards are a reward source; any scorer can be gamed.

Evaluate and ship

  • Write the gate before training, with separate suites: task (interval and slices), regression, safety in both directions, and format.
  • Compare tuned with base on the same suites; base results do not carry over to the tune.
  • A merge is a new model: test it. An embedding tune forces re-embedding and re-indexing; a reranker tune does not. Check a distilled student on the teacher’s failure slices.
  • Ship with versions kept together (base, data, template, config, evaluation), a canary and a rollback.

A design answer in nine parts

Clarify · baseline · data · method · training plan with a memory and time estimate · evaluation gate · serving and rollback · operations and re-tune triggers · alternatives and fallback. When a constraint changes, re-cost aloud and name the risk you accept.