/errors/pytorch/cuda-out-of-memory
torch.OutOfMemoryError: CUDA out of memory. Tried to allocate …
The commands on this page have not been executed by our verification runner yet.updated 2026-10-20
- tool
- PyTorch
- exit code
- 1
- versions
- PyTorch 2.x
- os
- linux, windows
torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 2.00 GiB means the next allocation does not fit in the free memory of the GPU. The message also reports the GPU's total capacity, how much memory PyTorch has allocated and how much it has reserved but not used. Make each step need less memory, or stop holding memory you no longer use.
Read the numbers in the message
- allocated by PyTorch: memory used by live tensors.
- reserved by PyTorch but unallocated: cached blocks PyTorch keeps for reuse. A large value here points to fragmentation.
- If the GPU's free memory is small but PyTorch's own usage is low, another process holds the memory; check with
nvidia-smi.
Fixes, from most to least effective
# 1. smaller batches, same effective batch size with gradient accumulation
accum = 4
for i, (x, y) in enumerate(loader): # loader with batch_size / accum
with torch.autocast("cuda", dtype=torch.bfloat16): # 2. mixed precision
loss = model(x.cuda()).loss / accum
loss.backward()
if (i + 1) % accum == 0:
optimizer.step(); optimizer.zero_grad(set_to_none=True)
running += loss.item() # 3. keep a number, not the graph
# 4. evaluation without autograd buffers
with torch.no_grad():
preds = model(x_val.cuda())Fragmentation
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:TrueExpandable segments let the caching allocator grow existing blocks instead of needing one large contiguous block. torch.cuda.memory_summary() prints a detailed breakdown when you need to see where memory goes.
Sources
Related pages
errors · numpyValueError: operands could not be broadcast together with shapesNumPy raises operands could not be broadcast together when array shapes are incompatible. Compare shapes from the right: each pair must be equal or 1.compat · numpyNumPy versions and supported Python versions (2.0 to 2.5)Python versions supported by each NumPy release: 2.0 runs on 3.9-3.12, 2.1 and 2.2 on 3.10-3.13, 2.4 on 3.11-3.14 and 2.5 on 3.12-3.14, per the release notes.glossary · airflowWhat is a DAG in Apache Airflow?A DAG, or directed acyclic graph, is how Airflow defines a workflow: tasks as nodes, dependencies as edges with no cycles, and a schedule that creates runs.