AI Hints
Home›Fine-Tuning LLMs Tutorial›Memory and Data Types
Fine-Tuning LLMs · Lesson 21 of 120

Training Memory

Why training needs more.

⏱ 2 min read▶ Video at 33:32🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

Running a model only needs its weights. But training needs much more. To understand why fine-tuning big models is hard, and why LoRA helps, we need to see where the memory goes.

  • Running: just the weights
  • Training: much more
  • Where does it go?

Where training memory goes

During full training, every parameter needs four things. The weight itself. A gradient, which says how to change the weight. And two extra numbers for the Adam optimizer, which tracks the recent direction, and size, of the changes. Add them up, with the usual formats, and it's about sixteen bytes per parameter. Plus some more for the activations.

ItemBytes per parameter
Weight2 to 4
Gradient2 to 4
Adam: 2 states8
Total (rule of thumb)≈ 16
Activationsextra, grows with batch size

A memory calculator

Let's turn the rule into a tiny calculator. For each model, we work out the memory to use it, at two bytes per parameter. And the memory for full fine-tuning, at sixteen bytes per parameter. Run it. Our small model is easy. But an eight billion parameter model needs one hundred and twenty eight gigabytes to fully fine-tune. That's more than any single normal GPU has. And seventy billion needs over a terabyte.

app.pypython
def gb(n): return f"{n / 1e9:,.1f} GB"

for name, params in [("SmolLM2-135M", 135e6), ("Qwen2.5-0.5B", 0.5e9),
                     ("Llama-8B", 8e9), ("Llama-70B", 70e9)]:
    use = params * 2           # bf16 weights: 2 bytes each
    full = params * 16         # weights + gradients + Adam states
    print(f"{name:13} use: {gb(use):>9}   full fine-tune: {gb(full):>9}")
Output
SmolLM2-135M  use:    0.3 GB   full fine-tune:    2.2 GB
Qwen2.5-0.5B  use:    1.0 GB   full fine-tune:    8.0 GB
Llama-8B      use:   16.0 GB   full fine-tune:  128.0 GB
Llama-70B     use:  140.0 GB   full fine-tune: 1,120.0 GB

The way out

So how does anyone fine-tune big models? The trick is to train only a tiny fraction of the parameters. Then gradients, and Adam states, are only needed for that tiny part. That's the idea behind PEFT, and LoRA. It can cut training memory by more than ten times.

keyvalue
Problem16 bytes for every parameter
Tricktrain only a tiny part
NamePEFT, e.g. LoRA
Savingoften more than 10×

Summary

Let's recap. Training needs weights, gradients, and optimizer states, about sixteen bytes per parameter. Full fine-tuning of big models needs huge memory. And LoRA avoids that by training only a small part. Next, how training actually works, starting with the loss.

Key takeaways
  • ≈16 bytes per parameter
  • Big models: huge memory
  • LoRA trains a small part
Next up: Loss — measuring mistakes