Memory Required for Gradients and Optimizer States
Weight memory equals the parameter count multiplied by the bytes per parameter; a 13B model at 2 bytes per parameter needs 26 GB for its weights.
Why Memory Estimates Come First
Before you run a large model, you need an estimate of how much memory it will occupy. Large language models have billions of parameters, and each parameter has to be held in memory while the model works. A good estimate tells you whether a model will run at all, and whether you need more hardware or a smaller model.
The good news is that the estimate is simple arithmetic built from two numbers: how many parameters the model has, and how many bytes each parameter takes to store. This article builds that arithmetic step by step, first for inference (running the model) and then for training (updating its parameters).
What do you think happens?
A 13B-parameter model stores each parameter in 2 bytes. How many gigabytes do its weights need?
Reveal answer
26 GB
Multiply the parameter count by the bytes per parameter: 13 billion × 2 bytes = 26 billion bytes, which is 26 GB.
Weight Memory from Parameters and Bytes
A model's weights are stored as a long list of numbers, one entry per parameter. The storage cost of the weights is therefore the number of parameters multiplied by the number of bytes used to store each one. The result is usually expressed in gigabytes, which is why the parameter count is written with a B for billions.
In this formula, N is the number of parameters and b is the number of bytes each parameter occupies. The choice of b matters a great deal. Halving b halves the memory needed for the weights, so the same model can have very different footprints depending on how its values are stored.
Weight memory for a 13B model
A model has 13B parameters, and each parameter takes 2 bytes. What is the weight memory?
- 1Identify N and b
N = 13B parameters and b = 2 bytes per parameter.
- 2Multiply
Weights = 13B × 2 bytes = 26B bytes.
- 3Express in gigabytes
26B bytes is 26 GB.
The weights need 26 GB.
The formula is linear in the parameter count, so doubling the model roughly doubles its weight memory. Consider a 70B-parameter model with 2 bytes per parameter. Its weights alone need 70B × 2 bytes = 140 GB, which is far more than the 26 GB needed by the 13B model. Memory quickly becomes a bottleneck when operating large models.
| Bytes per parameter (b) | Weight memory for 13B parameters | How it is computed |
|---|---|---|
| 2 bytes | 26 GB | 13B × 2 |
| 1 byte | 13 GB | 13B × 1 |
| 4 bytes | 52 GB | 13B × 4 |
The same 13B parameter count gives different weight memory depending on the bytes per parameter. The 1-byte and 4-byte rows are generated illustrations of the same formula.
From Weights to the Inference Total
Inference only needs the weights and their overhead. To estimate the total inference memory, you take the weight memory and multiply it by an overhead factor. The source uses an overhead factor of 1.2 in its example.
Inference total for the 13B model
Using the 26 GB weight memory of the 13B model, what is the total inference memory with an overhead factor of 1.2?
- 1Start from the weights
The weight memory is 26 GB.
- 2Apply the overhead factor
26 GB × 1.2 = 31.2 GB.
The total inference memory estimate is 31.2 GB.
Training Adds Gradient and Optimizer Values
Training is different from inference. When you update the parameters, the system must also keep information about each trainable parameter, in addition to the weights. The source uses the Adam optimizer as its example. For each trainable parameter, three values are stored for its gradient and optimizer states.
If each of those three values takes 2 bytes, then each trainable parameter needs 3 × 2 = 6 bytes of additional storage on top of its weight. Written as a formula, the gradient and optimizer storage depends on three quantities: the number of trainable parameters, the number of values stored per parameter, and the bytes per value.
Here N_{trainable} is the number of trainable parameters, k is the number of values stored per parameter for gradients and optimizer states (three in the Adam example), and b is the number of bytes per value. This term is separate from the weight memory calculated earlier, so training memory is the weight memory plus this gradient and optimizer term.
Training storage when all 13B parameters train
A 13B model trains all of its parameters with Adam, storing 3 values per parameter at 2 bytes per value. How much memory do gradients and optimizer states need?
- 1Identify the quantities
N_{trainable} = 13B, k = 3, and b = 2 bytes.
- 2Multiply
13B × 3 × 2 bytes = 78B bytes.
- 3Express in gigabytes
78B bytes is 78 GB.
Gradient and optimizer states need 78 GB, on top of the 26 GB of weights.
Freezing Parameters Lowers the Training Term
The number of trainable parameters is the lever that controls gradient and optimizer memory. The same per-value rule applies whatever the trainable count is. If only 1B of the parameters are trainable, the calculation gives 1B × 3 × 2 bytes = 6 GB for gradients and optimizer states. That is a large reduction from the 78 GB needed when the full 13B model trains.
The source states this effect qualitatively: reducing trainable parameters from 13B to 1B significantly reduces the memory needed for gradients and optimizer states. The calculation above gives the size of that reduction.
Mistakes Beginners Make with the Estimate
Common mistake Treating the training total as only the weights
What it looks likeEstimating a 13B model's training memory as 26 GB because that is its weight memory.
Why it failsTraining also stores gradient and optimizer values for each trainable parameter, which for the Adam example is 78 GB when all 13B parameters train.
Do this insteadAdd the gradient and optimizer term to the weight memory when estimating training.
Common mistake Thinking freezing parameters removes the weights
What it looks likeAssuming that with only 1B trainable parameters the 13B model needs only 6 GB.
Why it failsThe model still holds all 13B parameters, so the weights still occupy their full storage.
Do this insteadKeep the full weight memory and reduce only the gradient and optimizer term.
Common mistake Forgetting the overhead factor for inference
What it looks likeReporting 26 GB as the inference total for the 13B model.
Why it failsThe overhead factor of 1.2 is applied to the weight memory to get the total inference estimate.
Do this insteadMultiply the weight memory by 1.2 to get 31.2 GB.
Common mistake Mixing up the bytes-per-value and bytes-per-parameter terms
Why it failsThe gradient and optimizer term multiplies by k values per parameter and b bytes per value, which is not the same as the single bytes-per-parameter value used for weights.
Do this insteadApply the weight formula to the weights and the training formula, with k and b, to the gradient and optimizer term.
Practice the Estimate
A 7B-parameter model stores each parameter in 2 bytes. First find its weight memory. Then apply the overhead factor of 1.2 to find its total inference memory. Finally, if all 7B parameters train with Adam (3 values per parameter at 2 bytes each), find the gradient and optimizer memory.
Hints
- Weight memory = parameters × bytes per parameter, so start with 7B × 2 bytes.
- Multiply the weight memory by 1.2 for the inference total.
- The gradient and optimizer term is trainable parameters × 3 × 2 bytes.
Key Takeaways
- Weight memory equals the parameter count multiplied by the bytes per parameter; a 13B model at 2 bytes per parameter needs 26 GB for its weights.
- Total inference memory is the weight memory multiplied by the overhead factor of 1.2, giving 31.2 GB for the 13B example.
- Training adds gradient and optimizer storage: with three values per trainable parameter at 2 bytes each in the Adam example, 13B trainable parameters need 78 GB.
- Reducing the number of trainable parameters lowers gradient and optimizer memory, but the weights still occupy their full storage.