Course outline

Deep Learning / Representation and acceptance evidence / Lesson 21

Activation Checkpointing and Recomputation

Understand the memory and computation tradeoff of recomputing activations during backpropagation.

About 3 min readContent reviewed
On this page

Lesson goals

  • Explain the role of saved activations in backpropagation.
  • Compare checkpointed and ordinary gradients in a controlled lab.
  • Identify state and randomness risks during recomputation.

Trade saved activations for computation

Backpropagation may need intermediate values from the forward pass. Activation checkpointing retains selected inputs and recomputes parts of the forward computation during backward, reducing saved activation memory at the cost of additional computation. Actual savings and runtime depend on the model, inputs, device, and implementation; measure them instead of assuming a fixed improvement.

This technique is distinct from saving a training checkpoint for later resumption. A resumable training checkpoint must also preserve the model and other workflow state. Define which problem is being addressed before selecting a mechanism.

Run a small checkpointed block

In a local lab with a compatible PyTorch installation, the following example applies a deterministic block through activation checkpointing and computes an input gradient. It requires the external library and is illustrative, so the public reader does not execute it. Consult the installed version’s documentation for supported arguments and behavior.

python Checkpoint a deterministic block
import torch
from torch.utils.checkpoint import checkpoint

def block(x):
    return torch.sin(x) * x

x = torch.tensor([0.5, 1.0], requires_grad=True)
y = checkpoint(block, x, use_reentrant=False)
y.sum().backward()
print(x.grad)

The checkpointed computation is recomputed as needed during backward, and the gradient is stored on x.

Compare against ordinary execution

Use identical inputs and model state to compare ordinary and checkpointed gradients within a justified tolerance. The second lab example uses a simple deterministic function; it checks a small case rather than proving equivalence for a full model. Repeat with representative shapes and compare task behavior before adopting the optimization.

python Compare two gradients
import torch
from torch.utils.checkpoint import checkpoint

def block(x):
    return torch.sin(x) * x

x1 = torch.tensor([0.5, 1.0], requires_grad=True)
x2 = x1.detach().clone().requires_grad_()
block(x1).sum().backward()
checkpoint(block, x2, use_reentrant=False).sum().backward()
print(torch.allclose(x1.grad, x2.grad))

The example compares gradients from the same deterministic function with and without checkpointing.

Inspect randomness and side effects

A recomputed function may execute more than once. Mutable state, external side effects, or changes in execution context can make recomputation differ from the original forward pass. Random operations also require careful handling of random state. Follow the chosen implementation’s documented behavior and test the actual block, including any stateful layers.

Measure and accept

Profile peak memory and step time using the same model, batch, device, and warm-up conditions. Compare output, gradients, and a relevant validation metric with the baseline; record tolerances and configuration. Keep a rollback path and avoid claiming exact reproducibility across hardware or library versions unless it has been established for that environment.

Common errors and fixes

A checkpointed block produces different gradients.

Why it happens
Recomputation sees changed state, randomness, or execution context.
How to fix it
Isolate the block and compare forward and gradient behavior under controlled conditions.
Prevent it next time
Avoid unintended side effects and test the real block.

Training becomes slower without enough memory benefit.

Why it happens
Recomputation overhead outweighs savings for the chosen block.
How to fix it
Profile alternative checkpoint boundaries and compare the baseline.
Prevent it next time
Measure peak memory and step time before rollout.

A model cannot resume after an interruption.

Why it happens
Activation checkpointing was mistaken for a saved training checkpoint.
How to fix it
Save and restore the required training state separately.
Prevent it next time
Document the different purposes of the two mechanisms.

2 practice exercises

1. Why can activation checkpointing reduce memory while increasing step time?

Show worked solution

It saves fewer intermediate activations and recomputes part of the forward work during backward. The actual tradeoff depends on the model and implementation and must be measured.

2. A checkpointed function increments a counter on each call. Why is that risky?

Show worked solution

The function may run again during backward, so the counter can change more than once and affect behavior. Remove the side effect or redesign the block, then compare outputs and gradients under controlled conditions.

Key takeaways

  • Activation checkpointing trades recomputation for saved activations.
  • Compare gradients and task behavior on representative inputs.
  • State, randomness, and side effects matter during recomputation.

Official references