cuda
oom_error
ai_generated
partial
torch.cuda.OutOfMemoryError: CUDA out of memory
ID: cuda/torch-cuda-oom-new
85%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2 | active | — | — | — |
Root Cause
New PyTorch 2.x OOM error type. GPU memory exhausted during training or inference.
genericWorkarounds
-
95% success Reduce batch size — most direct fix for OOM
# Halve the batch size: batch_size = 16 # was 32 # Or use gradient accumulation to maintain effective batch size: accumulation_steps = 2 for i, batch in enumerate(dataloader): loss = model(batch) / accumulation_steps loss.backward() if (i + 1) % accumulation_steps == 0: optimizer.step() optimizer.zero_grad() -
88% success Use gradient checkpointing to trade compute for memory
model.gradient_checkpointing_enable()
-
90% success Use mixed precision training (fp16/bf16) to halve memory
scaler = torch.amp.GradScaler(); with torch.autocast('cuda'): -
82% success Use DeepSpeed ZeRO or FSDP for multi-GPU memory distribution
Use DeepSpeed ZeRO or FSDP for multi-GPU memory distribution
Dead Ends
Common approaches that don't work:
-
Add torch.cuda.empty_cache() in training loop
75% fail
Only frees cached memory, not memory held by tensors — negligible effect
-
Set PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb
65% fail
Only helps with fragmentation, not actual OOM
Error Chain
Frequently confused with: