Memory Management › Memory Estimation
Language AI HandbookAll chapters Memory Management › Memory EstimationOne section · Sections in this chapter3 / 10 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Estimating total memory before running a training job lets you choose the right GPU configuration, set batch sizes appropriately, and avoid wasting hours waiting for an OOM crash that was predictable from the start. A good estimate also helps you make principled decisions about which memory reduction techniques to apply and in what order.
The Estimation Formula
For a standard mixed-precision training setup with Adam, the total memory breaks down as:
where covers CUDA context, framework overhead, and temporary buffers. The CUDA runtime itself requires roughly 300-500 MB when initialized. PyTorch maintains a memory caching allocator that retains freed memory for reuse; its overhead depends on the number and sizes of allocations. Temporary workspace for cuBLAS and cuDNN operations can require several hundred megabytes more. In practice, typically adds 500 MB to 2 GB depending on the framework version and GPU model.
The activation memory depends heavily on whether gradient checkpointing is used:
where the factor of 2 bytes per element (FP16) is incorporated into the constants 34 and 5.
Worked Example
Consider a GPT-2 style model with the following architecture:
- Vocabulary:
- Hidden dim:
- Layers:
- Heads:
- FF dim:
The parameter count is approximately:
In mixed precision with Adam:
For a batch size of 8 and sequence length 512:
Total estimated memory: roughly 3.7 GB, well within a single 16 GB GPU's budget. Scaling to a 7B model with batch size 16 and sequence length 2048 pushes this into the multi-hundred-gigabyte range, requiring distributed training.
One additional nuance in the estimation is memory fragmentation. GPU memory is allocated and freed in blocks of varying sizes. Over time, freed blocks of one size may not be reusable for allocations of a different size, causing apparent "wasted" memory that shows as free but is not contiguous enough for large allocations. PyTorch's CUDA memory allocator tries to mitigate this through block pooling and splitting, but fragmentation can cause an OOM even when the total free memory nominally exceeds the requested allocation. In practice, you should maintain a 10-20% safety margin in your memory estimates to account for fragmentation.