Concepts / Estimating Activation Memory in Training

Estimating Activation Memory in Training

Activation memory can be much larger than the memory used by model weights, so the weights are not always the main memory cost.

  • Programming

The Weights Are Not Always the Biggest Cost

When people first estimate how much memory a model needs, the model's weights usually receive most of the attention. The weights are the learned parameters, and it is natural to assume they are the largest consumer of memory. For large models, that assumption is often wrong. During training, a second group of values, called activations, can take up a surprising amount of memory, and in some cases far more than the weights themselves.

This article looks at why that happens and at one of the main techniques used to reduce it. Keep one question in mind as you read: if activations are stored so that gradients can be computed later, what is one way to shrink the memory they occupy without changing the model itself?

Weights Versus Stored Activations

The weights have a fixed footprint set by the network's architecture. Once the model is defined, the number of parameters does not change from one batch to the next. Activations behave differently. They are the intermediate values produced as data moves forward through the network, and when they are kept so that the gradient computation can use them later, they accumulate. The stored activations depend on the work the network does on each batch as it moves forward, which is why their combined size can grow to dwarf the memory taken by the weights.

Weightsfixed by architectureActivations, layer 1stored for backwardActivations, layer 2stored for backwardActivations, layer 3stored for backwardActivations, layer 4stored for backward
What does the memory layout look like for weights versus stored activations, and why does the activation portion grow larger than the weight portion?

The diagram shows the two groups side by side. The weights form one block with a fixed size. The activations form a growing stack, one entry for each layer whose output is kept. Adding more layers, or processing larger batches, adds more entries to the activation stack while the weight block stays the same.

Measured Activation Memory at Growing Model Scale

Published measurements show the effect at real scale. A chart compares the memory needed for activations with the memory needed for the weights across several Megatron models of different sizes. The numbers come from work by Korthikanti and colleagues (2022). The main lesson is visible across the scales: as models grow, the activation side of the comparison can become the larger share.

Tracing One Activation Through Checkpointing

If stored activations are the problem, one direct response is to stop storing all of them. Instead of keeping activations around for reuse, the training process recomputes them when they are needed. This technique is called gradient checkpointing, and it is also known as activation recomputation. The two names refer to the same idea, and you will see both in the literature.

Follow one activation through its life under this technique. It is produced during the forward pass. It is not kept in memory for the whole run. When the gradient step needs it, it is produced again. The diagram below shows the order of these events. It is a simplified view of the sequence, not a description of how any particular framework schedules its work.

Forward passproduces activationsActivation memoryholds only what is keptBackward passneeds activations forgradients1. activation produced, not kept2. activation required again3. activation recomputed for gradient step
During the forward and backward passes, which activations are kept in memory, which are discarded, and when are the discarded ones recomputed?

Trading Memory for Training Time

The core move is a swap of storage for computation. Memory is saved because activations are not kept between the forward pass and the point where the gradient calculation needs them. Work is added because those activations must be produced again at that later point. The technique reduces memory requirements, but it increases the time needed for training because of the recomputation. Neither side of this exchange is free.

Peak memoryhigherPeak memorylowerExtra computationnone addedExtra computationrecomputation
How does storing fewer activations change peak memory on one side and the amount of extra computation on the other?
QuestionStore all activationsGradient checkpointing
What is kept for the backward pass?Every activationOnly some activations
What happens to discarded activations?Not applicable, none are discardedRecomputed when needed
Effect on memoryActivation memory can dominateReduced memory requirements
Effect on training timeNo recomputation costIncreased time from recomputation
When it tends to fit bestMemory already fits and time is tightMemory is the constraint that blocks training

Two strategies for handling activations during training

Which strategy to use depends on which constraint is tighter. When memory is the constraint that prevents a model from being trained at all, the extra time may be a reasonable price. When time is the tighter constraint and memory already fits, storing activations as usual may be the better choice. The source names both effects and does not claim that one always beats the other.

A Four-Layer Network Worked Through

Worked example

Counting activations held at the start of the gradient calculation

A small hypothetical network has four layers, and each layer produces one activation during the forward pass. How many activations must sit in memory when the gradient calculation begins under two strategies: storing all activations, and checkpointing that keeps only the activations at layers 2 and 4?

  1. 1
    Step 1: Store all activations

    Every layer's output is kept for reuse. Four activations, one per layer, are in memory when the gradient calculation begins.

  2. 2
    Step 2: Checkpoint with two kept activations

    Only the activations at layers 2 and 4 are kept. The activations at layers 1 and 3 are discarded during the forward pass.

  3. 3
    Step 3: Recompute when the gradient step needs them

    When the gradient calculation reaches the discarded activations, they are produced again. This is the extra work that buys the memory saving.

  4. 4
    Step 4: Compare the two strategies

    Storing all keeps four activations in memory. Checkpointing keeps two, at the cost of recomputing the two discarded ones. The memory saving and the added time are both real.

Result

Storing all activations holds 4 in memory at the start of the gradient calculation. The checkpointing scheme holds 2, and it recomputes the 2 discarded activations later. Real systems choose their own schemes, so these counts are for illustration.

Mistakes When Estimating Training Memory

  • Common mistake

    Assuming the weights are always the largest memory cost

    Why it fails

    Activation memory can be much larger than the memory used by the weights, especially in large models.

    Do this instead

    Estimate the activation side as well as the weights before deciding what the memory budget looks like.

  • Common mistake

    Treating activation memory as fixed like the weights

    Why it fails

    Weights have a fixed footprint set by the architecture, while stored activations depend on the work done on each batch as data moves forward.

    Do this instead

    Recognize that activation memory grows with the amount of work per batch, and reason about it separately from the parameter count.

  • Common mistake

    Assuming checkpointing is free

    Why it fails

    Gradient checkpointing reduces memory requirements but increases training time because of the extra recomputation.

    Do this instead

    Describe checkpointing as a deliberate trade of memory for time, and weigh both sides before using it.

  • Common mistake

    Using checkpointing when memory already fits

    Why it fails

    When memory is not the blocking constraint, the added recomputation time may be a cost with no benefit.

    Do this instead

    Use checkpointing when memory is the constraint preventing training, and store activations as usual when time is tighter and memory already fits.

Predicting the Memory Strategy

MEDIUM

A team is training a large model. Their memory estimate shows that the model weights fit comfortably on their hardware, but the stored activations do not fit. Should they consider gradient checkpointing, and what will it cost them? Explain your answer in terms of memory and training time.

Hints
  • Ask which component is the constraint: the weights fit, but the activations do not.
  • Checkpointing reduces memory by recomputing activations instead of storing them.
  • Recomputation adds work, so training time increases.

Key Takeaways on Activation Memory

Activation memory is one of the main costs of training, and it can exceed the memory used by the weights in large models. Gradient checkpointing, also called activation recomputation, reduces that cost by recomputing activations when they are needed rather than storing them. The reduction in memory is paid for with additional training time, so the choice is a deliberate trade.

At a glance

  • Activation memory can be much larger than the memory used by model weights, so weights are not always the main memory cost in large models.
  • Weights have a fixed footprint set by the architecture, while stored activations accumulate with the work done on each batch as data moves forward.
  • Gradient checkpointing (activation recomputation) avoids storing activations for reuse and recomputes them when needed.
  • Checkpointing reduces memory requirements but increases training time because of the extra recomputation, so it is a deliberate trade of memory for time.
  • Choose checkpointing when memory is the constraint that blocks training, and store activations as usual when time is tighter and memory already fits.

Related concepts

More concepts starting with E