Navigation
🌐 Distributed GPU Systems· 12 min read

Memory Budget Verification

Calculating per-GPU memory for a given parallelism configuration — weights, optimizer states, activations, and KV cache with exact formulas.

memorybudgetverificationactivationsoptimizercheckpoint

After choosing (TP, PP, DP), you must verify that the per-GPU memory actually fits. This chapter provides the exact formulas and a systematic method to add up every memory component.


1. The Complete Training Memory Ledger

Training memory has two parts: static (independent of batch size) and dynamic (scales with batch/sequence).

Static Memory: 16Ψ per Parameter

Memory Ledger

With BF16 mixed precision + AdamW:

ComponentPrecisionBytes/param7B model70B model
Parameters (working copy)BF16214 GB140 GB
GradientsBF16214 GB140 GB
FP32 master weightsFP32428 GB280 GB
Adam 1st moment (mm)FP32428 GB280 GB
Adam 2nd moment (vv)FP32428 GB280 GB
Static total16112 GB1,120 GB

Why 16 and not 18? Some implementations keep an additional FP32 gradient accumulator (extra 4Ψ → 20Ψ). The canonical BF16 mixed-precision setup uses 16Ψ (BF16 gradients, FP32 optimizer states). Our Envelope Estimation chapter uses 18Ψ for a more conservative estimate. Both are valid — just be consistent.

How Parallelism Reduces Static Memory

StrategyParameters per GPUOptimizer per GPUGradients per GPU
No parallelismΨΨΨ
TP=TΨ/TΨ/TΨ/T
PP=PΨ/P (layers assigned)Ψ/PΨ/P
FSDP (ZeRO-3)Ψ/D (gathered on demand)Ψ/DΨ/D
TP=T + PP=P + FSDP(D)Ψ/(T×P×D)Ψ/(T×P×D)Ψ/(T×P×D)

FSDP is the most memory-efficient for static memory because it shards everything across all DP ranks, gathering parameters on-the-fly during forward/backward.


2. Dynamic Memory: Activations

Activations are the intermediate tensors saved during forward pass for use in backward. This is the “variable cost” that depends on batch size and sequence length.

Per-Layer Activation Memory (Transformer)

For one Transformer layer with hidden dimension hh, sequence length ss, batch size bb, attention heads aa:

ComponentShapeBytes (BF16)
Input to attention (Q, K, V)3×b×s×h3 \times b \times s \times h6bsh6bsh
Attention scoresb×a×s×sb \times a \times s \times s2bas22bas^2
Attention outputb×s×hb \times s \times h2bsh2bsh
FFN intermediateb×s×4hb \times s \times 4h (or b×s×dffb \times s \times d_{ff})8bsh8bsh
LayerNorm inputs (×2)2×b×s×h2 \times b \times s \times h4bsh4bsh
Dropout masks(various)~2bsh2bsh
Per-layer total≈22bsh+2bas2\approx 22bsh + 2bas^2

For LL layers:

Mactivations=L×(22bsh+2bas2)M_{\text{activations}} = L \times (22bsh + 2bas^2)

Worked Example: 70B Model

Architecture: h=8192h=8192, L=80L=80, a=64a=64 (GQA with 8 KV heads), s=4096s=4096, b=1b=1 (micro-batch):

Per-layer (linear terms)=(14×8192+6×28672)×4096×1=1.2 GBPer-layer (attention)=3×64×40962=3.2 GBTotal (80 layers)=80×4.5=357 GB (!!)\begin{aligned} \text{Per-layer (linear terms)} &= (14 \times 8192 + 6 \times 28672) \times 4096 \times 1 = 1.2 \text{ GB} \\ \text{Per-layer (attention)} &= 3 \times 64 \times 4096^2 = 3.2 \text{ GB} \\ \text{Total (80 layers)} &= 80 \times 4.5 = 357 \text{ GB (!!)} \end{aligned}

357 GB of activations for a single micro-batch! This is clearly impossible without mitigation.

Activation Checkpointing

Instead of saving all activations, recompute them during backward:

StrategyMemoryCompute overhead
No checkpointingL×(22bsh+2bas2)L \times (22bsh + 2bas^2)0%
Selective checkpointingSave only cheap tensors, recompute attention~30% memory reduction, ~5% compute
Full checkpointingSave only layer inputs, recompute everything~L×2bshL \times 2bsh (just inputs)

With full activation checkpointing for the 70B example:

Mactivations≈80×67 MB=5.4 GBM_{\text{activations}} \approx 80 \times 67\text{ MB} = 5.4 \text{ GB}

That’s 357 GB → 5.4 GB — a 66× reduction at the cost of 33% extra forward compute.

How Parallelism Reduces Activation Memory

StrategyEffect on activations
TP=TReduces some tensors by T (attention, FFN), but not all (LayerNorm is replicated)
PP=POnly stores activations for assigned layers: L/PL/P layers
SP (Sequence Parallel)Reduces ss by the SP degree for LayerNorm/Dropout regions
CP (Context Parallel)Reduces ss by CP degree for attention computation
Gradient accumulationDoesn’t reduce peak activation memory (still processes full micro-batch)

3. The Complete Memory Budget Formula

Mper-GPU=16ΨTP×PP×ZeRO-stage⏟static+LPP×f(bμ,s,h,a,ckpt)⏟activations+Mbuffers⏟miscM_{\text{per-GPU}} = \underbrace{\frac{16\Psi}{\text{TP} \times \text{PP} \times \text{ZeRO-stage}}}_{\text{static}} + \underbrace{\frac{L}{\text{PP}} \times f(b_\mu, s, h, a, \text{ckpt})}_{\text{activations}} + \underbrace{M_{\text{buffers}}}_{\text{misc}}

Where:

  • bμb_\mu = micro-batch size (not global batch)
  • ZeRO-stage: 1 (optimizer only) shards by D, 2 adds gradients, 3 adds params
  • MbuffersM_{\text{buffers}} ≈ 1-4 GB for communication buffers, CUDA context, fragmentation

4. Memory Verification Checklist

For a given configuration, fill in this table:

ComponentFormulaValueNotes
Parameters2Ψ/(TP×PP)2\Psi / (\text{TP} \times \text{PP})__ GBBF16 working copy
Gradients2Ψ/(TP×PP)2\Psi / (\text{TP} \times \text{PP})__ GBSharded further with FSDP
Optimizer12Ψ/(TP×PP×D)12\Psi / (\text{TP} \times \text{PP} \times D)__ GBAlways sharded across DP
Activationssee formula__ GBWith/without checkpointing
Comm buffers~2 GB2 GBNCCL buffers
CUDA context~1 GB1 GBPer-GPU fixed overhead
Totalsum__ GBMust be < 80 GB

Worked Example: 70B, TP=8, PP=1, DP=32

ComponentCalculationValue
Parameters140 GB / 817.5 GB
Gradients140 GB / 817.5 GB
Optimizer840 GB / (8 × 32)3.28 GB
Activations (full ckpt, b_μ=1)5.4 GB / 8 (rough TP reduction)~0.7 GB
Buffers + CUDA3 GB
Total42.6 GB ✅

Plenty of headroom on 80 GB. Can increase micro-batch size to 2-4.

Worked Example: 405B, TP=8, PP=4, DP=8

ComponentCalculationValue
Parameters810 GB / (8 × 4)25.3 GB
Gradients810 GB / (8 × 4)25.3 GB
Optimizer4,860 GB / (8 × 4 × 8)18.98 GB
Activations (full ckpt, b_μ=1)~2 GB (126/4 layers, TP=8)~2 GB
Buffers + CUDA3 GB
Total74.6 GB ⚠️ tight

Tight fit — micro-batch size must stay at 1, or use gradient accumulation.


5. Common Pitfalls

PitfallWhat happensFix
Forgetting optimizer states“70B = 140 GB, TP=8 = 17.5 GB, plenty of room!”Include 12Ψ optimizer overhead
Ignoring activationsOOM at forward pass, not at parameter loadingAdd activation checkpointing
Wrong micro-batch sizeCalculated with bμ=1b_\mu=1 but training uses bμ=4b_\mu=4Verify with actual micro-batch
FragmentationTheoretical fits, practical OOMLeave 10-15% headroom
PP stage imbalanceFirst/last stages have embedding layersAccount for embedding table size

Self-Check

  • Calculate the 16Ψ memory breakdown for a 13B model
  • Explain why optimizer states are 12 out of 16 bytes per parameter
  • Calculate activation memory for one Transformer layer with and without checkpointing
  • Fill in the memory verification table for 70B on TP=8, DP=8
  • Identify when a configuration won’t fit and what to adjust

References