Resources/Model training/What happens during training

Best practice

Will the model fit? Memory for training

Training needs room for the model's weights, its gradients and the optimizer's state, plus working memory. A simple rule of thumb, about 2 GB per billion parameters for LoRA and 12 GB for full fine-tuning, tells you before you start whether a model will fit your machine.

Chapter 64 min read4 references

Why training needs more than the download

Running a model needs little more than its weights: a 7B model in 16-bit is about 14 GB. Training needs more, because every weight that learns also needs a gradient (which way to move it) and optimizer state (Adam's running averages that set how far). On top come the activations from the forward pass, which grow with batch size and example length. Which of these exist for which weights is what separates full fine-tuning from LoRA.

The rule of thumb

A rule of thumb for training memory
LoRA   ≈ 2 GB per billion parameters  + ~3 GB
         frozen 16-bit weights            adapter, activations, overhead

full   ≈ 12 GB per billion parameters + ~4 GB
         16-bit weight          2 bytes
         16-bit gradient        2 bytes
         Adam moment 1 (fp32)   4 bytes
         Adam moment 2 (fp32)   4 bytes
                              ─────────
                               12 bytes per weight
         (+ 4 bytes for a 32-bit master copy = 16)
  • LoRA keeps the base frozen in 16-bit: 2 bytes per weight, so 2 GB per billion parameters. The adapter is tiny, and the ~3 GB covers it, its optimizer state, activations at moderate lengths and the framework itself.
  • Full fine-tuning trains every weight: a 16-bit weight and gradient plus Adam's two 32-bit moment estimates, 12 bytes per weight in all.
  • QLoRA stores the frozen base in 4 bits, which brings memory down to roughly a third of LoRA's: an 8B model needs about 9 GB instead of 19.

The full fine-tuning page counts 16 bytes per weight, not 12. The difference is the 32-bit master copy of the weights that the classic mixed-precision recipe keeps alongside the 16-bit ones. This rule of thumb assumes training without that extra copy; a setup that keeps one needs about 16 GB per billion parameters. Both are estimates before long examples: activations come on top and grow with max length and batch size.

Full fine-tuning16-bit + Adam≈ 112 GBLoRA16-bit base, frozen≈ 15 GBQLoRA4-bit base, frozen≈ 4–5 GBAdam + fp32 copyfrozen weightstrainable weightsgradientsoptimizer state
Rough GPU memory to fine-tune a 7-billion-parameter model, before activations. Full fine-tuning with Adam needs about 16 bytes for every weight: the weights, their gradients, a full-precision master copy and two optimizer statistics. LoRA keeps the base frozen, so gradients and optimizer state exist only for the small adapter (here about 40 million weights).
Model sizeLoRAFull fine-tuning
0.5B~4 GB~10 GB
1.5B~6 GB~22 GB
3B~9 GB~40 GB
7B~17 GB~88 GB
14B~31 GB~172 GB

Activations are the part the rule can only approximate. They grow with the number of tokens held at once, batch size times example length, so doubling the max length roughly doubles them, and attention can add more on top for very long examples. The fixed overhead in the rule assumes moderate lengths. If your examples are long, leave extra headroom rather than planning to the last gigabyte.

How much memory you really have

On laptops with unified memory, such as Apple silicon Macs, the GPU shares the main memory, so a 36 GB laptop can give a model more room than a typical gaming GPU. But the operating system and your other apps need their share, so plan on roughly three-quarters of it being free for training. On a PC with a separate graphics card, the limit is the GPU's own memory, not the computer's RAM.

36 GB laptop27 GB usableOS, apps1.5B · LoRA≈ 6 GB · fits7B · LoRA≈ 17 GB · fits14B · LoRA≈ 31 GB · too big32B · QLoRA≈ 22–26 GB · fits1.5B · full≈ 22 GB · fits3B · full≈ 40 GB · too big
What fits on a 36 GB laptop with shared memory, by the rule of thumb on this page. About 27 GB is left for training once the operating system and your other apps take their share. Illustrative estimates: long examples and large batches need more.
What fits on a 36 GB laptop (illustrative)
  1. Usable memory: after the operating system and apps, about 27 GB.
  2. 1.5B model: LoRA 2 × 1.5 + 3 ≈ 6 GB, full fine-tuning 12 × 1.5 + 4 ≈ 22 GB. Both fit.
  3. LoRA, larger models: a 7B model needs about 17 GB and fits; a 14B model needs over 30 GB and doesn't. In practice LoRA fits models up to about 10B.
  4. Full fine-tuning, larger models: a 3B model needs about 40 GB, so about 1.5B is the ceiling.
  5. QLoRA: a 27–32B model needs about 22–26 GB, which fits.
The 1.5B model, both ways
1.5B · LoRA    2 × 1.5 + 3   ≈  6 GB
1.5B · full   12 × 1.5 + 4   ≈ 22 GB

On a 16 GB NVIDIA GPU, the same rule says an 8B model needs about 19 GB with LoRA, too much, but about 9 GB with QLoRA, which fits with room for longer examples.

If a run runs out of memory

  1. Lower the max length. Memory grows with example length, so this gives the most back. Check first that your typical examples still fit.
  2. Lower the batch size. Fewer examples per step means fewer activations held at once.
  3. Switch to QLoRA. The 4-bit base takes about a quarter of the memory of a 16-bit one.
  4. Use gradient checkpointing. It trades about 20–30% speed for much less activation memory, and many recipes already turn it on.
  5. Choose a smaller model, or use LoRA instead of full fine-tuning.

Common mistakes

  • Judging by download size. Full fine-tuning needs six to eight times the 16-bit weights, plus activations.
  • Counting all of a shared-memory laptop's RAM as available. The operating system and other apps need their share.
  • Counting a PC's RAM instead of its graphics card's memory.
  • Raising max length or batch size after a run fit, and expecting the next run to fit too.
  • Cutting max length below the length of your typical examples, so the model never sees the end of the replies it should learn.

References and further reading

External sources open in a new tab.

  1. 01ZeRO: Memory Optimizations Toward Training Trillion Parameter ModelsRajbhandari et al., 2019 · arXiv · arxiv.orgThe standard accounting of weights, gradients and optimizer state per parameter.
  2. 02GPU memory usageHugging Face Transformers docs · huggingface.co
  3. 03Model memory estimatorHugging Face Accelerate docs · huggingface.coA tool to estimate a specific Hugging Face model's memory for running and training.
  4. 04Unified MemoryMLX docs · ml-explore.github.ioHow Apple silicon's CPU and GPU share one pool of memory.

Turn weeks of expert work into minutes

See how Scalata fits your team, your data, and your controls.