Skip to content

The Training Run, Knob by Knob

The training run is where the dataset becomes an adapter. This chapter runs the lab's, and explains every knob on it as it goes past. By the end you will have a trained adapter and a log full of loss figures; chapter 6 is about what those figures do and do not say.

Setting up

The lab's tools are three Python libraries: PyTorch (the engine that does the arithmetic), Transformers (the library that loads models and runs training), and PEFT (the library that adds LoRA adapters). They are installed into a virtual environment — a private folder of libraries that touches nothing else on the machine:

python3 -m venv lora-venv
lora-venv/bin/pip install torch \
  --index-url https://download.pytorch.org/whl/cpu
lora-venv/bin/pip install transformers peft accelerate

The first install is the CPU build of PyTorch, chosen deliberately: this lab has no graphics card, and the guide wants you to know the run does not secretly need one. On a machine with a suitable graphics card the ordinary install gives you the GPU build instead, and nothing else in this chapter changes. The base model, distilgpt2, downloads itself the first time the script asks for it — about 350 MB, once, from the Hugging Face hub.

The script, in the order it works

The lab's lora-lab/train_lora.py does six things:

  1. Reads dataset.jsonl and formats each example exactly as chapter 4 showed.
  2. Tokenises them — turns the text into the numbers the model works with — with the question part masked.
  3. Loads the base model, untouched, and prints its size: 81,912,576 parameters.
  4. Wraps it in a LoRA adapter with the settings below, and prints how many parameters are actually trainable: 147,456 — under two-tenths of one per cent of the model.
  5. Trains, printing the loss every few steps.
  6. Saves only the adapter, to lora-lab/adapter/.

The knobs

Learning rate — 5×10⁻⁴. The size of each correction step, taken from the loss. Too small and twenty passes teach nothing visible; too big and each step overshoots, wrecking earlier lessons — the loss bounces instead of falling. LoRA tolerates, and usually wants, a higher learning rate than full fine-tuning, because so few parameters are moving. 5×10⁻⁴ is a sane starting point for a small model and a small adapter.

Epochs — 20. One epoch is one full pass over the dataset. The lab's 24 examples at a batch of 4 make 6 steps per epoch, so 20 epochs is 120 steps — a few minutes on the lab's CPU. Epochs are the knob with a cliff at the end of it: each pass teaches more of the habit and then, past a point you cannot see in advance, more of the pages. Chapter 6 and chapter 8 together are how you find out where your dataset's point was.

Batch size — 4. How many examples the model looks at before each correction. Bigger batches make smoother, slower learning and need more memory; smaller ones learn in jerkier steps. With 24 examples the question mostly answers itself; on a real dataset this is a memory question first and a quality question second.

Rank 8, alpha 16. From chapter 2: the adapter's capacity, and how loudly it speaks. With alpha twice the rank, the scaling factor is 2. Neither was tuned for this lab — they are the field's default starting point, stated honestly, and chapter 12 lists that as a limit of the demonstration.

Seed — 42. Training involves shuffles and other coin flips. Fixing the seed makes the run repeatable: same data, same code, same seed, same adapter. Repeatability is what makes the before/after comparison in chapter 8 evidence rather than anecdote, and it is why the comparison there uses greedy generation — the model always picking its favourite next word — so its coin flips are fixed too.

Running it

lora-venv/bin/python lora-lab/train_lora.py

The lab's full run — versions, trainable-parameter count, every logged loss, and the save at the end — is kept in lora-lab/transcripts/t02-training.txt. A healthy run begins with a loss that looks like a stranger guessing (the lab's first logged figure is in chapter 6) and falls, unevenly but unmistakably, as the answers become predictable. If your loss does not fall at all, chapter 13 has the usual suspects, starting with the one this lab checked first: that the adapter is attached to layers that actually exist in the model.