CUDA out of memory: diagnose loading, context and training failures

Trace a CUDA memory failure to loading, prompt processing, generation or training, then inspect allocator state and change one budget at a time.

By benchr Editorial Team · · · Changelog

A CUDA out-of-memory error tells you an allocation failed. The useful next question is when it failed: loading weights, processing the prompt, generating later tokens or training. Those failures point to different fixes. Save the error and configuration before changing the model or allocator.

Locate the failure before changing settings

Weight loading

What to investigate: The complete checkpoint, precision, device map, temporary loading buffers and other GPU processes.

Controlled first change: Stop your unused processes or try a supported smaller checkpoint or quantization. Reducing the prompt cannot make oversized resident weights fit.

Prompt processing

What to investigate: Input length, image processing, simultaneous requests and temporary attention or graph buffers.

Controlled first change: Repeat one short text-only request at a fixed low concurrency, then increase one input dimension at a time.

Later generation

What to investigate: Growing cache, retained request state and overlapping sessions.

Controlled first change: Bound generated tokens and concurrent sessions. Check whether completed requests release their state.

Training

What to investigate: Activations, gradients, optimizer state, sequence lengths and batch configuration.

Controlled first change: Reduce the microbatch or sequence length and investigate the framework's checkpointing or adapter-training options.

This is a diagnostic sequence, not a guaranteed mapping from each error to one cause. A framework may reserve cache during startup, so a loading-time failure can include the planned request capacity. Record the exact failing allocation and runtime logs rather than assuming it was the weights.

Distinguish live tensors from reserved memory

PyTorch's allocator documentation distinguishes tensor allocations from memory reserved for reuse. A process can retain reserved memory after temporary tensors are gone. A high device-memory reading alone does not establish a leak or fragmentation.

For a PyTorch CUDA process you control, the following diagnostic uses the documented memory-summary API. It inspects that process's allocator; it does not enumerate every allocation made by other libraries or other processes. The snippet is syntax-checked here, but has not been executed on a GPU for this article.

"""Documented PyTorch CUDA diagnostic; no GPU execution claimed."""
import torch

if not torch.cuda.is_available():
    raise SystemExit("This diagnostic requires an available PyTorch CUDA device.")

print(torch.cuda.memory_summary(device=torch.cuda.current_device(), abbreviated=True))

Capture measurements at the same points in repeated runs: after loading, after a representative prompt and after request cleanup. Compare allocated memory, reserved memory and the peak under the same configuration. If retained state grows on every request, inspect references to outputs, caches and unfinished tasks. Reducing a model until it no longer crashes can conceal the retention problem.

Change the memory budget with a reproducible request

Keep the prompt, output cap, concurrency, precision and runtime version in the case record. In vLLM, the memory guide identifies context length, maximum simultaneous sequences, multimodal limits and CUDA graphs as allocation controls. These controls have different effects; changing them together prevents you from learning which budget was exceeded.

After a successful minimal request, restore the real workload in stages. Include the longest legitimate input and the concurrent workload the service must accept. Report queueing and rejected requests as well as successful output. A configuration that fits one short request is not a capacity result for production.

Use allocator tuning only when the evidence supports it

torch.cuda.empty_cache() releases unused cached allocations. It does not free live tensors or make a too-large model smaller. Calling it repeatedly is not a substitute for fixing retained objects or a capacity shortfall.

The current PyTorch documentation names PYTORCH_ALLOC_CONF; PYTORCH_CUDA_ALLOC_CONF remains a compatibility alias. Its experimental expandable_segments option addresses certain changing-allocation patterns. Check the installed version and backend before trying allocator options, retain the original settings and compare the same failing workload. A larger free-memory figure after tuning does not by itself establish improved end-to-end behavior.

Preserve the information needed to reproduce the error

Keep the model revision, quantization artifact, runtime and driver versions, device memory, full error, tokenized input size, output cap and concurrency. Redact sensitive prompts and credentials before sharing logs. If the problem persists with a small supported fixture, use the runtime's issue template with the minimal reproduction and the configuration that failed.

If the workload cannot fit after supported adjustments, choose a smaller model, a different deployment or more capacity. The deployment-options guide explains those choices, and the training guide separates adapter training from inference. benchr has not measured a GPU-fit or throughput result for this article.

Changelog

  • 2026-10-16 — Rebuilt the error diagnosis around failure stages, live versus reserved allocations, bounded workload tests and documented allocator limits. Removed unsupported hardware-fit and vendor-price claims.
  • June 19, 2026 — Originally published.

Sources and documentation review

Documentation review dates are recorded per source. No provider inference or independent benchmark replication was performed for this revision.

  1. PyTorch 2.14 allocator semantics — read
  2. PyTorch allocator summary API — read
  3. vLLM memory configuration — read