← Infrastructure AI Observability
Infrastructure

CUDA Out-of-Memory Diagnostics

Provides granular, block-level visibility into GPU VRAM allocation, reservation, and fragmentation states.

Source: mortalapps.com
TL;DR
  • Provides granular, block-level visibility into GPU VRAM allocation, reservation, and fragmentation states.
  • The core purpose is isolating the exact tensor or state mechanism causing a fatal RuntimeError: CUDA out of memory.
  • The primary optimization idea centers on minimizing memory fragmentation and optimizing intermediate activation storage dynamically.
  • The most important engineering insight is distinguishing between memory genuinely allocated to tensors versus memory merely reserved by the caching allocator due to internal fragmentation.

Why This Matters

CUDA Out-of-Memory (OOM) errors are the single most frequent halting condition in large-scale AI training. Models scale to hundreds of billions of parameters, testing the absolute physical limits of VRAM clusters. Understanding complex memory allocation patterns enables engineers to implement activation checkpointing, ZeRO optimizations, or tensor parallelism precisely where needed, turning an impossible training run into a highly stable production pipeline.

Core Intuition

VRAM is not cleanly or synchronously managed like CPU RAM. To strictly avoid the massive overhead of calling cudaMalloc constantly during tight training loops, PyTorch implements a Caching Allocator. It requests large pools of memory from the OS (Reserved Memory) and doles out smaller blocks to tensors (Allocated Memory). OOMs overwhelmingly occur not because total allocations exceed GPU capacity, but because the Caching Allocator becomes heavily fragmented and cannot find a contiguous block large enough to satisfy a new request, despite sufficient total "free" reserved space.

Technical Deep Dive

PyTorch tracks VRAM via complex block state mechanics. The torch.cuda.memory_snapshot() API successfully dumps the entire internal state of the allocator.

Block State DesignationTechnical Meaning
Diagnostic Implicationactive_allocated
Currently backing an active Python/C++ Tensor.Memory is in active use and absolutely cannot be freed.
inactivePreviously allocated, now freed, but held in PyTorch's reserved cache.
Available for PyTorch, but leads to fragmentation if blocks are too small to reuse.segment_type
Categorization into small versus large memory pools.Determines how aggressively the allocator handles block splitting.

Key Takeaways

PyTorch manages VRAM via a caching allocator; OS-level tools like nvidia-smi cannot see internal fragmentation states.
Reserved memory is held by PyTorch; Allocated memory is actively utilized by tensors.
Fragmentation causes OOMs even when total available VRAM theoretically exceeds the allocation request.
Always use memory_snapshot to visually reconstruct the exact block state at the precise time of a crash.
Optimizer states and un-detached loss logs represent the most common invisible VRAM consumers.