Skip to content
mentoor

Topic 1 of 6 · 5 min read

Memory budget of a training run

Goal: Estimate the GPU memory for full fine-tuning versus LoRA of a stated model size and choose a GPU that fits.

Training memory is four bills, not one: weights, gradients, optimizer state and activations, and only the first is what you pay for at inference.

Check yourself · 1 of 3

Which part of training memory exists during full fine-tuning but not during plain inference?

Choose one answer

Why it matters

A 7-billion-parameter model fits on a 24 GB card for chat, and then fails with an out-of-memory error the moment you start full fine-tuning. People blame the framework. The cause is arithmetic you can do in advance, before you rent a GPU for an hour and watch it crash.

The four bills

Let N be the parameter count.

  1. Weights. In 16-bit (bf16 or fp16) each parameter takes 2 bytes, so 2N bytes.
  2. Gradients. One value per trained parameter, usually 2 bytes each: another 2N.
  3. Optimizer state. Adam keeps two running averages per parameter, plus a 32-bit master copy of the weights when you train in mixed precision. The ZeRO paper counts this as 12 bytes per parameter, which makes 16 bytes per parameter for the first three bills together.
  4. Activations. The intermediate results saved during the forward pass so the backward pass can use them. They grow with batch size, sequence length and layer count, not with parameter count alone.

How to estimate

For full fine-tuning with Adam in mixed precision, take 16 bytes times N before activations. A 7B model is about 7 x 16 = 112 GB. That is more than any single 80 GB card, whatever the batch size. Activations then add anywhere from a few GB to tens of GB.

For LoRA, the base weights stay frozen in 2 bytes each (14 GB for 7B), and only the small adapter matrices get gradients and optimizer state. The adapters are often well under 1 percent of the parameters, so the 14 GB of weights dominate and the total can land near 20 GB with moderate batches. Quantizing the frozen base to 4 bits (QLoRA) cuts the weight bill further.

Always leave headroom of 10 to 20 percent for the CUDA context, fragmentation and spikes.

Before and after

Weak plan: "A 13B model is 26 GB in bf16, so a 40 GB card is enough to fine-tune it."

Fixed plan: "Full fine-tuning is about 13 x 16 = 208 GB before activations, so it needs sharding across several GPUs. LoRA needs 26 GB of frozen weights plus adapters and activations, so a 40 GB card works with a small batch."

What goes wrong

  • Sizing from the checkpoint file. The file size is the weight bill only.
  • Forgetting activations. The estimate fits, then a longer sequence length triggers the crash.
  • Trusting the first step. Memory peaks can appear at the first optimizer step, when Adam allocates its states, so a run that survives the forward pass can still fail.

Examples

Will 7B fit on one 80 GB card?

Setup Weights Grads + Adam Verdict
Full fine-tune, bf16 + Adam 14 GB about 98 GB Does not fit, even before activations
LoRA, bf16 base 14 GB under 2 GB Fits with room for activations
QLoRA, 4-bit base about 4 GB under 2 GB Fits on a 24 GB card

The crash at step 1

A run prints a loss for the first forward and backward pass, then dies at optimizer.step() with CUDA out of memory. Adam allocates its two state tensors on the first step. The forward pass was never the problem. The fix is to budget the optimizer state up front, or move to LoRA or sharded optimizer state.

Key points

  • Training pays for weights, gradients, optimizer state and activations; inference pays mostly for weights.
  • Full fine-tuning with Adam in mixed precision costs about 16 bytes per parameter before activations.
  • LoRA freezes the base model, so gradients and optimizer state exist only for the small adapters.
  • Activation memory scales with batch size and sequence length, so it is the part you can tune.
  • Check the estimate against a real first optimizer step and leave 10 to 20 percent headroom.

Common mistakes

  • Sizing the GPU from the model's file size, which counts only the weights.
  • Estimating with an inference calculator and skipping gradients and optimizer state.
  • Ignoring that a longer sequence length raises activation memory and can break a run that fit yesterday.
  • Planning to the last gigabyte with no headroom for fragmentation and the CUDA context.

Check yourself · 2 of 3

Roughly how much memory does full fine-tuning a 3B-parameter model with Adam in mixed precision need before activations, at 16 bytes per parameter?

Choose one answer

Check yourself · 3 of 3

Why does LoRA fit on a smaller GPU than full fine-tuning of the same model?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic