Concepts / Model Memory Footprint During Inference

Model Memory Footprint During Inference

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.

  • Programming

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, then extends it to training.

What do you think happens?

A 13B-parameter model stores each parameter in 2 bytes. How many gigabytes do its weights need?

Reveal answer
Answer:

26 GB

13 billion parameters multiplied by 2 bytes per parameter gives 26 billion bytes, which is 26 GB. The next section shows exactly where this comes from.

Weight Memory from Parameter Count

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, and the parameter count is written with a B for billions.

Rule

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.

Worked example

Weight memory for a 13B model at 2 bytes per parameter

A model has 13B parameters, and each parameter is stored in 2 bytes. What is the weight memory?

  1. 1
    Identify N

    N = 13B = 13 billion parameters.

  2. 2
    Identify b

    b = 2 bytes per parameter.

  3. 3
    Multiply

    $13 \times 10^{9} \times 2 = 26 \times 10^{9}$ bytes.

  4. 4
    Convert to gigabytes

    $26 \times 10^{9}$ bytes is 26 GB.

Result

The weights need 26 GB.

2 bytes per parameter26 GB4 bytes per parameter52 GB
The same 13B parameters take different amounts of memory depending on how many bytes each parameter uses. Doubling the bytes per parameter doubles the weight memory.

The 4-byte row above is a generated variation on the source's 2-byte example, calculated with the same formula: 13 × 4 = 52 GB.

From Weights to Total Inference Memory

Weight memory is not the whole story for inference. The source applies an overhead factor of 1.2 on top of the weights. Total inference memory is the weight memory multiplied by that factor.

Rule

Worked example

Total inference memory for the 13B model

Using the 26 GB weight memory for the 13B model, what is the total inference memory with the overhead factor applied?

  1. 1
    Start from the weights

    Weights = 26 GB, from the previous calculation.

  2. 2
    Apply the overhead factor

    $26 \times 1.2 = 31.2$ GB.

Result

The total inference memory is 31.2 GB.

Storage Per Trainable Parameter

Inference only needs the weights and their overhead. Training is different. When you update the parameters, the system must also keep information about each trainable parameter. The source uses the Adam optimizer as its example, and each trainable parameter has three values stored for its gradient and optimizer states.

parameter weight2 bytes, already in weightmemorygradient andoptimizer value 12 bytes, added in traininggradient andoptimizer value 22 bytes, added in traininggradient andoptimizer value 32 bytes, added in training
For one trainable parameter, the weight is already counted in the weight memory. Training adds three more stored values for gradient and optimizer states, each taking 2 bytes in the source example.

Each trainable parameter therefore needs 3 × 2 = 6 bytes of additional storage on top of the weights. The general form uses k for the number of values stored per parameter and b for the bytes per value.

Rule

In this formula, 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 covers only gradient and optimizer storage. It is separate from the weight memory calculated earlier.

Worked example

Gradient and optimizer memory for the full 13B model

All 13B parameters are trainable, with k = 3 values per parameter and 2 bytes per value. What is the gradient and optimizer storage?

  1. 1
    Identify the values

    N_{trainable} = 13B, k = 3, b = 2 bytes.

  2. 2
    Multiply

    $13 \times 10^{9} \times 3 \times 2 = 78 \times 10^{9}$ bytes.

  3. 3
    Convert

    78 × 109 bytes is 78 GB.

Result

Training the full 13B model adds 78 GB of gradient and optimizer storage, on top of the 26 GB of weights.

Trainable Parameters as the Lever

The number of trainable parameters is the lever that controls gradient and optimizer memory. The per-value rule stays the same; only N_{trainable} changes. If only 1B of the parameters are trainable, the same rule 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.

weights26 GBweights26 GBgradient andoptimizer78 GBgradient andoptimizer6 GB
Reducing trainable parameters from 13B to 1B shrinks the gradient and optimizer storage from 78 GB to 6 GB. The weight memory of 26 GB stays the same in both cases.

Notice that the weight memory does not disappear when fewer parameters train. The model still has all of its parameters, so the weights still need their full 26 GB of storage. Freezing parameters only removes the gradient and optimizer storage tied to the frozen values.

Mistakes Beginners Make With Memory Estimates

  • Common mistake

    Using the parameter count alone as the memory figure

    What it looks like

    Treating a 13B model as needing 13 GB.

    Why it fails

    Memory is parameter count multiplied by bytes per parameter. Without the bytes per parameter, the result has no unit of memory.

    Do this instead

    Multiply by b. At 2 bytes per parameter, 13B parameters need 26 GB.

  • Common mistake

    Forgetting the overhead factor for inference

    What it looks like

    Reporting 26 GB as the total inference memory for the 13B model.

    Why it fails

    The source applies an overhead factor of 1.2 on top of the weight memory for total inference memory.

    Do this instead

    Multiply the weight memory by 1.2 to get 31.2 GB.

  • Common mistake

    Adding training storage to inference estimates

    What it looks like

    Including the 78 GB of gradient and optimizer storage when estimating how much memory inference needs.

    Why it fails

    Inference only needs the weights and their overhead. Gradient and optimizer storage belongs to training.

    Do this instead

    Use the weight-based total for inference, and add the training term only when estimating training.

  • Common mistake

    Assuming fewer trainable parameters shrink the weights

    What it looks like

    Estimating a 1B-trainable model as 6 GB total.

    Why it fails

    Trainable count only controls the gradient and optimizer term. All parameters still need storage.

    Do this instead

    Keep the full weight memory, such as 26 GB for 13B, and reduce only the 78 GB training term to 6 GB.

Practice: Estimating a Different Model

MEDIUM

A model has 7B parameters, each stored in 2 bytes. First calculate its weight memory, then its total inference memory with the overhead factor of 1.2. Finally, if 1B of its parameters are trainable with k = 3 and 2 bytes per value, calculate the gradient and optimizer storage. Do not add the training storage to the inference total.

Hints
  • Weight memory is 7 × 2 = 14 GB.
  • Multiply the weight memory by 1.2 for total inference memory.
  • Use N_{trainable} × k × b, with N_{trainable} = 1B, k = 3 and b = 2 bytes.
  • The training term is 6 GB; the weights are still 14 GB.

Putting the Estimates Together

  • Weight memory equals the parameter count multiplied by bytes per parameter; 13B at 2 bytes needs 26 GB.
  • Total inference memory is weight memory multiplied by the overhead factor of 1.2, giving 31.2 GB for the 13B example.
  • Training adds three stored values per trainable parameter in the Adam example, so 13B trainable parameters at 2 bytes per value need 78 GB.
  • Reducing trainable parameters lowers only the gradient and optimizer term; the weights keep their full storage.

Key Takeaways

  • Weight memory = parameter count × bytes per parameter; 13B at 2 bytes per parameter needs 26 GB.
  • Total inference memory = weight memory × 1.2 overhead factor, giving 31.2 GB for the 13B example.
  • Training adds gradient and optimizer storage of trainable parameters × 3 values × bytes per value, giving 78 GB for 13B at 2 bytes.
  • Reducing trainable parameters from 13B to 1B cuts that training term to 6 GB, but the weights remain at full size.

Related concepts

More concepts starting with M