In this article
Use backend-aware block states and OOM history to distinguish fragmentation from live demand, pending frees and allocations outside PyTorch.
A PyTorch inference worker runs out of memory while reporting substantial reserved storage. That observation suggests several hypotheses: live demand, fragmented inactive blocks, frees waiting for another stream or allocations outside the framework. Use a bounded allocator snapshot to distinguish them before selecting a tuning option from the reserved-minus-allocated difference alone.
Record the allocator backend
Preserve the installed framework and active allocator backend. PyTorch documents backend-specific statistics and tuning controls; native allocator split-block options are not interchangeable with cudaMallocAsync behavior. The hypothesis and evidence must refer to the implementation actually in use. A familiar environment variable can be ignored by that backend while the application appears to accept the configuration.
Capture the failing history
Enable supported memory history before a representative bounded workload, then save the snapshot around the failure. Set a justified history limit: a ring buffer can overwrite earlier allocation events, while an unbounded diagnostic can consume excessive host memory. Record which interval remains available. A snapshot taken only after broad cleanup may no longer show the state that prevented the allocation.
Read segment and block states
For the documented native allocator representation, inspect the OOM event, requested size, segments and individual blocks. Distinguish active_allocated, active_awaiting_free and inactive storage. Waiting for another stream is not the same as freely reusable space. Several inactive pieces also need not form the suitable contiguous block required by the request within that allocator’s constraints.
Connect state with owners
Use recorded allocation traces and event history to identify which live owners or size changes created the problematic state. A raw address can be reused, so preserve the allocation instance rather than joining unrelated lifetimes. Compare a repeated-shape control with the variable-shape path when that difference is the hypothesis. Do not call every cached segment fragmentation simply because it remains reserved.
Respect visibility and test one change
The snapshot primarily sees allocations managed by the PyTorch allocator; direct CUDA and other library allocations can be missing. Compare accounting scopes before explaining unexplained device usage. Apply a justified backend-supported change in an isolated equivalent and repeat the same workload with checked outputs. Acceptance narrows the failure mechanism. It does not make empty_cache a cure for live tensors or turn aggregate free bytes into a guaranteed successful allocation.
Primary references
PyTorch CUDA Memory Snapshots.
PyTorch CUDA Allocator Statistics.
Related reading: Stream-ordered GPU allocation: the lifetime follows execution; Reproducible inference stacks: preserving a GPU pilot’s result.
Earn rental income from GPUs
Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.