Training Optimization: Memory Efficiency

By Ludovico Bessi, Senior ML Engineer at Google ยท Updated

Gradient checkpointing and mixed precision: fitting more model and more sequence into the same GPU.

Where Training Memory Goes

For each step, a GPU must hold:

Component Mixed-precision Adam, per parameter
BF16/FP16 weights 2 bytes
BF16/FP16 gradients 2 bytes
FP32 master weights 4 bytes
Adam first moment (m) 4 bytes
Adam second moment (v) 4 bytes
Total 16 bytes

Plus activations: the intermediate results of the forward pass, kept so the backward pass can compute gradients. Activations scale with batch size ร— sequence length ร— hidden size ร— number of layers, and for long sequences they are often the largest item of all.

Two classic techniques attack these: mixed precision shrinks the bytes per number, and gradient checkpointing avoids storing most activations.

Mixed Precision Training

Mixed precision training (Micikevicius et al., 2018) runs most computation in 16-bit floating point while keeping the numerically sensitive parts in 32-bit.

How it works

  1. Keep an FP32 master copy of the weights.
  2. Each step, cast weights to 16-bit and run the forward and backward passes in 16-bit. Tensor Cores run 16-bit matmuls many times faster than FP32.
  3. Apply the gradients to the FP32 master weights. Small updates that would be rounded away in 16-bit survive in FP32.

FP16 vs. BF16

  • FP16 has 5 exponent bits: good precision but a narrow range. Small gradients underflow to zero, so FP16 training needs loss scaling โ€” multiply the loss by a large factor before backpropagation, then divide the gradients back down. Dynamic loss scaling raises the factor until overflow happens, then backs off.
  • BF16 has 8 exponent bits, the same range as FP32, with less precision. Gradients rarely underflow, so loss scaling is unnecessary. BF16 is the default for LLM training on A100 and newer GPUs.

FP8

Hopper and newer GPUs support FP8 Tensor Core math (E4M3 for forward, often E5M2 for gradients), roughly doubling throughput again. FP8 training needs per-tensor or per-block scaling factors and careful handling of outliers, and is used in some recent large training runs.

What mixed precision saves

Activations and the matmul inputs are stored in 16-bit, halving activation memory and speeding up compute. It does not shrink optimizer state โ€” that is why the table above still totals 16 bytes per parameter. Sharding (ZeRO/FSDP, see Distributed Training) and 8-bit optimizers address that part.

8-bit optimizers

Adam's two moment estimates take 8 of the 16 bytes. 8-bit optimizers (Dettmers et al., 2021) store them in 8 bits using block-wise quantization: the tensor is split into small blocks, each with its own scale, so an outlier only hurts its own block. A dynamic (non-linear) quantization map keeps precision for both small and large values. This cuts optimizer memory by about 75% with little effect on final quality, and is widely used for fine-tuning through the bitsandbytes library.

Gradient Checkpointing

Also called activation checkpointing or rematerialization (Chen et al., 2016).

The idea

Instead of storing every activation from the forward pass, store only some of them โ€” the checkpoints, typically the input to each transformer block. During the backward pass, when a block's activations are needed, rerun that block's forward pass from its checkpoint to recreate them.

The trade-off

  • Memory: activation memory drops from proportional to the number of layers to roughly proportional to the number of checkpoints. Checkpointing every โˆšn layers gives O(โˆšn) activation memory.
  • Compute: each checkpointed segment runs forward twice, adding roughly one extra forward pass โ€” about 33% more compute per step (forward is about one-third of a forward+backward step).

Selective checkpointing

Not all activations are equally expensive. Attention score matrices are large but cheap to recompute; FFN outputs are smaller but expensive. Selective activation recomputation (as in Megatron-LM) recomputes only the cheap-to-recompute, memory-heavy parts, recovering most of the memory savings for a few percent of extra compute. FlashAttention already behaves this way inside attention: it recomputes the attention matrix in the backward pass instead of storing it (see Attention Optimizations).

Putting It Together

A typical memory-efficient training setup for a large model:

  1. BF16 mixed precision for compute and activations.
  2. FlashAttention to remove O(nยฒ) attention memory.
  3. Selective or full activation checkpointing to fit long sequences and large micro-batches.
  4. ZeRO/FSDP to shard optimizer state, gradients and weights.
  5. 8-bit optimizers or LoRA when fine-tuning on limited hardware.

Each technique trades something โ€” compute, communication or a little precision โ€” for memory, and the memory is then spent on larger batches or longer sequences, which usually improves throughput overall.

Connections to Inference

  • Inference has no gradients or optimizer state, so the 16-bytes-per-parameter problem disappears. Weights alone set the floor.
  • The precision story continues: models trained in BF16 are routinely served in FP8, INT8 or INT4. See Memory Optimizations.
  • The recompute-instead-of-store idea reappears in inference, for example recomputing evicted KV cache entries from the prompt instead of keeping them in memory.

Key Takeaways

  1. Mixed-precision Adam needs about 16 bytes per parameter, plus activations that grow with batch and sequence length.
  2. Mixed precision runs math in 16-bit with FP32 master weights; BF16 avoids the loss scaling FP16 needs.
  3. 8-bit optimizers use block-wise quantization to cut optimizer memory by about 75%.
  4. Gradient checkpointing stores a few activations and recomputes the rest, trading about one extra forward pass for large memory savings.
  5. Selective recomputation gets most of the savings at a fraction of the compute cost.