Optimizer States Memory Requirements
Training memory is the sum of model weights, activations, gradients, and optimizer states.
Why Training Costs More Than Holding a Model
Keeping a trained model in memory and training that same model are different jobs. Training has to keep track of more information while it runs, and that extra information is what makes training expensive. This article focuses on one part of that extra cost: the memory used by gradients and optimizer states, which grows with the size of the model.
What do you think happens?
If a model has billions of trainable parameters and you switch to an optimizer that keeps more bookkeeping for each parameter, what happens to the memory cost of the gradient and optimizer portion?
Reveal answer
It goes up, because each parameter needs more stored values.
The optimizer changes how many values are stored per parameter. The model and the bytes per value stay the same, so a larger count of stored values per parameter means a larger memory cost. The rest of this article turns that intuition into a calculation.
Four Pieces of Training Memory
The total memory needed for training is built from four pieces: the model's weights, its activations, its gradients, and its optimizer states. The weights and activations are parts you have already seen. The gradients and optimizer states are the new pieces during training, and their size is tied to how many trainable parameters the model has.
Training memory is the sum of model weights, activations, gradients, and optimizer states.
Read the diagram as a sum rather than a chain. Each of the four pieces is a separate contribution to the same total. The gradient and optimizer-state pieces are the ones this article calculates, because their size depends directly on the number of trainable parameters.
What Each Parameter Carries During the Backward Pass
During the backward pass, every trainable parameter needs one value for its gradient. On top of that, it may need zero, one, or two extra values for optimizer states, depending on which optimizer is in use. The number of values per parameter is called V, and for the optimizers discussed here V ranges from 1 to 3.
The count of values per parameter is the key quantity. It does not describe the model's architecture or its weights. It describes the bookkeeping that the training process must hold for each parameter while it runs.
Calculating the Gradient and Optimizer Slice
The gradient and optimizer slice of training memory is computed with a single expression. In it, M is the memory for gradients and optimizer states, P is the number of trainable parameters, V is the number of values stored per parameter, and B is the number of bytes used for each value.
Each factor plays a separate role. The parameter count $P$ sets how many things need storage. The values per parameter $V$ set how many numbers each thing needs. The bytes per value $B$ set how much space each number takes. Multiplying the three gives the size of the slice.
Adam on a 13 billion parameter model
A model has 13 billion trainable parameters. It is trained with Adam, which stores three values per parameter in total (the gradient plus two optimizer state values). Each value uses 2 bytes. How much memory is needed for gradients and optimizer states?
- 1Identify P
P = 13 billion, or 13 × 109 trainable parameters.
- 2Identify V
Adam keeps the gradient plus two state values, so V = 3.
- 3Identify B
Each value uses 2 bytes, so B = 2.
- 4Multiply
M = 13 × 109 × 3 × 2 = 78 × 109 bytes, which is 78 GB.
The gradient and optimizer slice for this model is 78 GB. This is only that slice. The full training memory also includes weights and activations.
Comparing SGD, Momentum, and Adam
The optimizer changes only the value V in the expression. Vanilla SGD keeps no state, so its only per-parameter value is the gradient, which gives V = 1. A momentum optimizer keeps one state value per parameter in addition to the gradient, which gives V = 2. An Adam optimizer keeps two state values per parameter in addition to the gradient, which gives V = 3.
| Optimizer | Values per parameter (V) | Memory for 13B parameters at 2 bytes per value |
|---|---|---|
| Vanilla SGD | 1 (gradient only) | 26 GB |
| Momentum | 2 (gradient plus one state value) | 52 GB |
| Adam | 3 (gradient plus two state values) | 78 GB |
Same model and same bytes per value, different optimizer. The 26 GB and 52 GB values are computed from the same formula, with V changed.
Notice that the optimizer's choice changes the result by a factor of three in this comparison, even though the model and the bytes per value stay the same. This is why the same model can need very different amounts of training memory depending on the optimizer.
Scaling Down to One Billion Parameters
Now keep the same optimizer and the same bytes per value, but reduce the trainable parameters to 1 billion. Only the P factor changes, so the calculation becomes 1 billion × 3 × 2 bytes, which comes to 6 GB for gradients and optimizer states.
Because memory here is proportional to the parameter count, the ratio of two results matches the ratio of the two parameter counts. Going from 13 billion to 1 billion parameters reduces this slice from 78 GB to 6 GB, roughly a thirteenfold reduction. Checking this ratio is a quick way to catch arithmetic errors.
Mistakes When Estimating Optimizer Memory
Common mistake Treating the 78 GB result as the total training memory
What it looks likeReporting 78 GB as the amount needed to train the 13 billion parameter model.
Why it failsThe calculation covers only gradients and optimizer states. Weights and activations are separate pieces of the total.
Do this insteadReport the slice as gradient and optimizer memory, then add weights and activations to reach the total.
Common mistake Assuming every optimizer stores the same number of values
What it looks likeUsing V = 3 for vanilla SGD because Adam uses three.
Why it failsVanilla SGD keeps only the gradient, so V = 1. The optimizer determines V.
Do this insteadLook up V for the specific optimizer before multiplying.
Common mistake Forgetting that the optimizer changes only V
What it looks likeExpecting a different model size when only the optimizer is swapped.
Why it failsSwitching optimizers changes V, while P and B can stay the same.
Do this insteadHold P and B fixed and change only V to see the effect of the optimizer.
Common mistake Mixing up the factors in the product
What it looks likeUsing bytes per value where the number of values per parameter belongs.
Why it failsEach factor has a separate role: P counts parameters, V counts values per parameter, and B measures bytes per value.
Do this insteadName each factor before multiplying, and check that the result scales the way each factor should.
Estimating a Different Training Setup
Momentum on a 7 billion parameter model
A model has 7 billion trainable parameters and is trained with momentum. Each value uses 2 bytes. Find the gradient and optimizer memory.
- 1Identify P
P = 7 billion.
- 2Identify V
Momentum keeps the gradient plus one state value, so V = 2.
- 3Identify B
B = 2 bytes.
- 4Multiply
M = 7 × 109 × 2 × 2 = 28 × 109 bytes, which is 28 GB.
The gradient and optimizer slice is 28 GB. Adding weights and activations would give the full training memory.
A model has 4 billion trainable parameters, uses 2 bytes per value, and is trained with Adam. Calculate the gradient and optimizer memory. Then predict how the result would change if the same model used vanilla SGD, and check your prediction with the formula.
Hints
- Adam uses V = 3 in this article's comparison.
- Vanilla SGD uses V = 1, so compare the two values of V while holding P and B fixed.
- The ratio of the two results should equal the ratio of the two V values.
Key Takeaways
- Training memory is the sum of model weights, activations, gradients, and optimizer states, and the gradient and optimizer portion is calculated separately as M = P × V × B.
- The number of trainable parameters P and the bytes per value B are shared across optimizers, while the number of values per parameter V depends on the optimizer: 1 for vanilla SGD, 2 for momentum, and 3 for Adam.
- Because the slice is proportional to P, scaling a model from 13 billion to 1 billion trainable parameters cuts the gradient and optimizer memory from 78 GB to 6 GB, a roughly thirteenfold reduction.
- The gradient and optimizer slice is only part of the full training memory, so weights and activations must be added to get the total.