From Estimates to Measurements
Introduction
Project introduction
From Estimates to Measurements
You have written the formulas for the bytes of the model states, the activation bytes of a transformer layer, a checkpoint plan, int8 quantization with absmax scales, and the size of a LoRA fine-tune.
In the notebook you check them against a real PyTorch model. It runs on a CPU, so instead of reading a GPU memory counter you add up the bytes of the actual tensors, covering every parameter, gradient and Adam state, and every tensor autograd saves for the backward pass.
What you will see
Predictions and measurements match to the byte. Activations grow with the batch until they outweigh everything else, checkpointing cuts them by 9 times at the cost of rerunning the forward pass during backward, which lets a four times larger batch fit in the same memory, and bfloat16 halves both weights and activations.
Next, int8 weights lose almost nothing until one value 20 times larger than the rest appears in each layer. The error jumps about 20 times. Giving each row its own scale fixes it, and so does keeping the few outliers in full precision. You finish by training a LoRA adapter and finding the longest sequence a 7B fine-tune can train on a 24 GB GPU.
Where training memory goes
batch 256How the notebook is organized
01
Measure
Predict, then measure model states and saved activations.
02
Cut
Checkpointing, bfloat16, int8, and the outlier that breaks it.
03
Plan
Train a LoRA adapter and fit a 7B fine-tune on a 24 GB GPU.
Measure and shrink a real model
Run the whole investigation in the browser, about 60 minutes.