Where the Memory Goes.
Sixteen bytes per parameter before a single activation is stored — and the activations can be larger still.
Mixed-precision training with Adam keeps, for every parameter, 2 bytes of BF16 weights, 2 of BF16 gradients, 4 of FP32 master weights and 8 for Adam’s two FP32 moments: 16 bytes in all (plate XI.6). On top come the activations saved during the forward pass for use in the backward pass — for each layer and token, about 34 d bytes plus the attention scores if they are materialised (Korthikanti and colleagues, 2023). Activations scale with the tokens in flight, not with the parameters.
Add it up for a published model and the problem of this volume appears. The workshop totals the memory to train each Llama 3 size on one device, for the chosen number of tokens per micro-batch, with or without FlashAttention’s avoidance of the score matrix: even the smallest needs over 100 GB, against 80 GB on an H100.
Below: left, the memory to train the selected model on a single device, split into weights, gradients, master weights, optimiser moments and activations. Right, how the activations grow with tokens per micro-batch.
- Count bytes per parameter in mixed-precision Adam training.
- Estimate activation memory per token and layer.
- Total the memory to train a published model.
- Compare it with an accelerator’s capacity.
Sixteen bytes and then some
Llama 3 8B has 8.03 billion parameters, so its weights, gradients, master copy and Adam moments take 128.5 GB before a single activation is stored. With 8 192 tokens per micro-batch and FlashAttention, the activations add 36.5 GB, for 165.0 GB in all — 2.1 H100s’ worth. Without FlashAttention the attention-score term dominates: 508.6 GB. For the 405-billion-parameter model the states alone are 6.5 TB, about 88 H100s’ worth with activations.
Nothing fits on one device, and the two parts behave differently. The 16 bytes per parameter are fixed by the model and can only be divided among devices — by sharding them (plate XIV.5) or by splitting the model itself (plates XIV.6–XIV.7). Activations scale with the tokens each device processes at once and can also be traded for compute by recomputing them (plate XIV.2) or split along the sequence (plate XIV.8). Every parallel layout is a way of dividing these terms until each device’s share fits.
Terms
- Model states
- Weights, gradients, master weights and optimiser moments: 16 bytes per parameter here.
- Activations
- Intermediate results saved in the forward pass for the backward pass.
- Micro-batch
- The tokens one device processes in one forward and backward pass.
- Memory budget
- The accounting of everything a training step must hold at once.
Sources
- Rajbhandari et al., “ZeRO”, SC 2020 (16 bytes per parameter for mixed-precision Adam).
- Korthikanti et al., “Reducing Activation Recomputation in Large Transformer Models”, MLSys 2023 (activation memory per layer).
- Llama 3 configurations, arXiv:2407.21783, Table 3.