Why GPU VRAM Fills Up During Deep Learning Training: Runtime Overhead, Model States, and Activations

    This article breaks down GPU memory usage in deep learning training into runtime overhead, model states, and intermediate activations. It explains how to estimate model-state memory for 7B-parameter models under FP16, mixed precision, and Adam; how data parallelism and parameter sharding affect per-GPU memory; and why activation memory grows with batch size and sequence length. Readers can use these rules to identify the main sources of VRAM pressure and judge whether a training configuration can fit on available GPUs.

    1. Three Sources of VRAM Usage

    GPU memory usage during training mainly comes from three parts:

    1. Runtime overhead: Space used by the GPU runtime environment, communication libraries, and the allocator itself.
    2. Model states: Parameters, gradients, and optimizer states, which are directly related to model size.
    3. Intermediate features: Activation values from the forward pass, data that must be saved for backpropagation, and temporary tensors. These depend on input batch size and sequence length.

    The first two parts can be estimated fairly accurately before training. The third part is the most complex, and it is also the main reason VRAM can suddenly run out in the middle of training.

    2. Runtime Overhead

    The GPU runtime environment occupies several hundred MB of VRAM at initialization for contexts, kernel code, and communication buffers. This part is basically fixed after training starts and does not change with batch size.

    In addition, the framework’s memory allocator caches freed memory blocks for reuse. As a result, the usage shown by system monitoring tools is usually larger than the space currently holding tensors. To determine whether VRAM is actually insufficient, use the allocated and peak values reported by the allocator statistics.

    3. Estimating Model-State Memory

    Let the number of model parameters be P. Half precision (FP16) uses 2 bytes per parameter, so a 7B-parameter model needs about 14 GB (decimal) just to store the parameters.

    During full fine-tuning, gradients are the same size as parameters, so gradients take another 2P bytes. The size of optimizer states depends on the algorithm:

    • SGD (without momentum) requires almost no extra state; with momentum, it uses about 1x the gradient size.
    • Adam needs to store first- and second-order moments, about 2x the gradient size, or 4P bytes.

    With pure FP16 plus Adam, model states total about 8P bytes, or about 56 GB for a 7B model. In practice, mixed precision is commonly used: parameters are kept in an FP32 master copy, and optimizer states also use FP32. In this case, each parameter needs about 16 bytes, or about 112 GB for a 7B model. This shows that even with batch size 1, the model states of a large model may exceed single-GPU capacity.

    VRAM usage in multi-GPU training depends on the parallelism method:

    • Data parallelism: Each GPU keeps a full copy of the model states. Per-GPU memory does not decrease as the number of GPUs increases, while total memory increases proportionally with the number of GPUs.
    • Parameter sharding parallelism: Parameters, gradients, and optimizer states are sharded across GPUs. Per-GPU model-state memory is about 1/N of the total (where N is the number of GPUs, excluding temporary gather buffers). The cost is that parameters must be gathered frequently during the forward and backward passes, which increases communication overhead.

    4. How Intermediate Features Scale

    The forward pass generates intermediate results, and some of them are needed when backpropagation computes gradients. These data cannot be released until backpropagation finishes.

    Taking a Transformer layer as an example, academic estimates commonly use the following formula (FP16, without activation recomputation):

    Per-layer activation memory ≈ s · b · h · (34 + 5 · a · s / h) bytes

    Here, s is sequence length, b is batch size, h is hidden dimension, and a is the number of attention heads. The 34 term comes from the inputs and masks for linear layers, normalization, activation functions, and dropout. The 5 · a · s / h term comes from the attention score matrix and its Softmax output.

    For example, with s=2048, b=4, h=4096, and a=32:

    • sbh = 2048 × 4 × 4096 ≈ 3.36 × 10⁷
    • 34 + 5 × 32 × 2048 / 4096 = 114
    • Per-layer activation memory ≈ 3.36 × 10⁷ × 114 ≈ 3.8 GB

    If the model has 32 layers, activations alone require about 122 GB, without even counting model states. This reveals two rules:

    1. Activation memory scales linearly with batch size b.
    2. The attention part is proportional to the square of sequence length s, so it grows fastest as sequences get longer.

    This formula is an order-of-magnitude estimate. The exact coefficients vary with implementation (for example, fused operators and attention implementations), but it is sufficient for judging whether a configuration can fit.