Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
FREEDOM251

Freedom251 › Blog › AI training

Why AI Model Training Runs Out of GPU Memory—and How to Fix It

By Freedom251 Team · · 12 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A CUDA out-of-memory (OOM) error means that one allocation pushed the training run’s peak GPU demand beyond the VRAM available at that moment. The allocation may be a model tensor, a saved activation, an optimizer update, or a short-lived temporary buffer—not necessarily the model weights alone.

Find the peak first: record the model, per-device batch, sequence length, precision and optimizer; compare PyTorch’s allocated and reserved memory with device-level usage; check other processes and tensors retained by your training loop. Then reduce per-device pressure or change how activations are stored before buying a GPU with more VRAM.

As an Amazon Associate I earn from qualifying purchases.

What a GPU OOM error actually means

PyTorch describes the failure as the CUDA runtime being unable to satisfy an allocation. In training, the important number is the highest simultaneous allocation, not the average memory shown between steps. A forward pass may fit, then backward propagation or an optimizer update requests a temporary block and crosses the device limit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The PyTorch FAQ phrases the common failure as “cuda runtime error(2): out of memory.” It also notes that computations involving variables that require gradients retain autograd history, so a value accidentally kept in a long-lived list or metric can make usage grow from iteration to iteration.

#1 Best Overall
Sale
ASUS Dual Radeon RX 9060 XT 16GB GDDR6 Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1a, 2.5-slot design, Axial-tech Fans, 0dB Technology
  • Axial-tech fans now feature a smaller fan hub that facilitates longer blades and a barrier ring that increases downward air pressure
  • 2.5-slot design allows for greater build compatibility while maintaining cooling performance
  • 0dB technology lets you enjoy light gaming in relative silence
  • Dual BIOS switch lets you toggle between Quiet and Performance BIOS profiles
  • Dual ball fan bearings last up to twice as long as sleeve bearing designs

The memory categories that compete for VRAM

Category What it contains When it creates pressure
Model weights Parameters used by the forward pass Present throughout training; a model that barely fits for inference may not fit for training.
Gradients Parameter gradients produced during backward Held until they are cleared or replaced by the next update.
Optimizer state Extra tensors maintained by the optimizer for each parameter Allocated in addition to weights and gradients, often becoming visible when the optimizer is created or the first step runs.
Saved activations Intermediate forward results required to compute gradients Usually the largest variable component for large batches or long sequences; retained until the corresponding backward operations finish.
Temporary tensors Short-lived workspaces and outputs created by individual operations Cause a peak when an operation needs a large buffer while other tensors are still live.
Framework and device allocations Allocator blocks and memory used outside PyTorch Can make device-level usage differ from the amount PyTorch reports as live tensor storage.

A useful lower-bound illustration in the PyTorch FAQ says that you need “at least twice the size of the weights” when storing weights and gradients. That is not a complete training estimate: optimizer state, activations, temporary tensors and other allocations can add substantially more.

Why batch size and sequence length cause sudden peaks

Batch size increases the number of examples whose intermediate results must remain available for backward. Reducing the per-device batch therefore directly reduces activation volume, although it also reduces the amount of work completed by each device step.

Sequence length has a second effect in attention-based models. The Hugging Face GPU memory guide explains that implementations which materialize attention score matrices can require space that grows with the square of sequence length. Doubling the sequence length can therefore increase that part of the peak by roughly four times, subject to the model architecture and implementation. Long context can be the reason a batch that worked at one length fails at another.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

One documented example gives the scale without turning it into a hardware rule: Hugging Face’s Transformers v5.12.0 guide estimates roughly 85 GB for training a 4-billion-parameter model in mixed precision with batch size 16. The estimate is tied to that model, sequence and optimizer assumptions; it is not a universal requirement for every 4-billion-parameter model.

Diagnose the peak before changing settings

Run the checks below in order. The aim is to distinguish a genuinely oversized live workload from allocator caching, another process, or a training-loop leak.

1. Record the exact workload that fails

  1. Write down the model architecture and parameter count.
  2. Record the per-device batch size, gradient-accumulation count and sequence length (or image/audio dimensions).
  3. Record the dtype or mixed-precision mode, optimizer and whether checkpointing or offloading is enabled.
  4. Note the stage of failure: model construction, first forward pass, backward pass, optimizer step or a later iteration.

These dimensions determine which memory category is likely responsible. A first-step failure points toward weights, optimizer state or a large initial workspace; a failure after many successful steps points toward retained tensors or a gradually rising peak.

Rank #2
GIGABYTE GeForce RTX 5070 Ti Gaming OC 16G Graphics Card, 16GB 256-bit GDDR7, PCIe 5.0, WINDFORCE Cooling System, GV-N507TGAMING OC-16GD Video Card
  • Powered by the NVIDIA Blackwell architecture and DLSS 4
  • Powered by GeForce RTX 5070 Ti
  • Integrated with 16GB GDDR7 256bit memory interface
  • PCIe 5.0
  • WINDFORCE cooling system

2. Compare allocated, reserved and peak PyTorch memory

PyTorch’s caching allocator keeps freed blocks so they can be reused. Consequently, torch.cuda.memory_reserved() can be much larger than torch.cuda.memory_allocated(), and both can differ from the total shown by nvidia-smi. Capture the values around the failing stage:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import torch

device = torch.cuda.current_device()
print('allocated:', torch.cuda.memory_allocated(device) / 1024**3, 'GiB')
print('reserved: ', torch.cuda.memory_reserved(device) / 1024**3, 'GiB')
print('peak allocated:', torch.cuda.max_memory_allocated(device) / 1024**3, 'GiB')
print('peak reserved: ', torch.cuda.max_memory_reserved(device) / 1024**3, 'GiB')
print(torch.cuda.memory_summary(device=device, abbreviated=True))

Reset peak counters immediately before a controlled test if you need a per-step measurement, then inspect them immediately after the forward, backward and optimizer stages. The PyTorch CUDA memory documentation also describes recording allocation history and viewing a snapshot to see which allocations were live around an OOM event. Use that history when a summary is not enough to identify the operation or tensor lifetime.

3. Compare framework accounting with device-level usage

Run nvidia-smi while the job is active and compare its process list and total used memory with PyTorch’s allocated and reserved values. A gap can have several explanations: cached blocks, CUDA allocations made by a library outside PyTorch, or another process on the same device. Device-level numbers show what the GPU cannot currently give to your job; PyTorch numbers help explain which part belongs to its tensor allocator.

4. Check for other processes and lingering subprocesses

Inspect the process table in nvidia-smi. A crashed notebook cell, worker, or Python subprocess can keep a context alive after the main script appears to have stopped. The PyTorch FAQ specifically warns that memory remaining after Python exits often indicates a live Python subprocess. Stop only the process you own, then rerun the measurement; do not terminate an unrelated user’s job on a shared machine.

5. Look for tensors that retain autograd history

A common leak is appending a loss or model output that still requires gradients to a history list, or adding such values to a running metric. Each object can keep its entire computation graph reachable, so memory rises over iterations even though the model and batch are unchanged. For values used only for logging, detach them and, when a host-side number is sufficient, convert after detaching:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
history.append(loss.detach().cpu())
metric_total += loss.detach().item()

Do not detach a tensor that you still need for the current backward pass. The goal is to detach only the copy retained for bookkeeping, as described in the PyTorch FAQ.

Rank #3
GIGABYTE GeForce RTX 5060 WINDFORCE OC 8G Graphics Card, Cooling System, 8GB 128-bit GDDR7, PCIe 5.0, Manufactured by NVIDIA, DisplayPort & HDMI - Video Output Interface, GV-N5060WF2OC-8GD Video Card
  • Powered by the NVIDIA Blackwell architecture and DLSS 4
  • Powered by GeForce RTX 5060
  • Integrated with 8GB GDDR7 128bit memory interface
  • PCIe 5.0
  • WINDFORCE cooling system

6. Identify the operation that creates the peak

Place measurements around forward, backward and optimizer calls, and test a single batch with the same dimensions as the failing run. If forward fits but backward fails, saved activations are a primary suspect. If the optimizer step fails, optimizer state or an update workspace may be the peak. If all stages fit in isolation but a later iteration fails, inspect retained references and data-loader or worker behavior before changing the model.

Fixes that reduce memory without replacing the GPU

Reduce the per-device batch size

Lowering the batch used by each GPU is usually the fastest way to reduce saved-activation demand. Change only the per-device value first, rerun the same dimensions and observe the new peak. A smaller physical batch changes optimization noise and the number of examples processed per step, so record the change when comparing training runs.

Shorten the sequence length when the task allows it

Trim padding, use a shorter maximum length, or split long documents into windows when the application can tolerate less context per example. This reduces ordinary activation storage and can sharply reduce materialized attention-score storage because of its quadratic dependence on sequence length. Verify that truncation does not remove information required by the task.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recover effective batch size with gradient accumulation

Gradient accumulation divides the desired batch into several smaller device batches and delays the optimizer update until their gradients have been combined. If a target effective batch contains N examples and each device step holds n, configure approximately N/n accumulation steps (adjust for the number of devices and any remainder). The Hugging Face gradient accumulation guide describes this mini-batch pattern.

Accumulation lowers activation pressure because only one smaller mini-batch is active at a time, but it does not remove weights, gradients or optimizer state. It therefore cannot make a model whose parameters and optimizer already exceed VRAM fit. It also increases the number of forward and backward passes between updates, so wall-clock throughput and numerical details can change.

Enable gradient checkpointing

Normally, the forward pass saves many intermediate activations for backward. Checkpointing saves selected boundaries, discards other intermediates and recomputes them during backward. That lowers activation memory while increasing computation and step time. Enable it only on a model implementation that supports it; in the Transformers ecosystem, this is commonly exposed through the model’s gradient-checkpointing setting.

Rank #4
Sale
GIGABYTE Radeon RX 9070 XT Gaming OC 16G Graphics Card, PCIe 5.0, 16GB GDDR6, GV-R9070XTGAMING OC-16GD Video Card
  • Powered by Radeon RX 9070 XT
  • WINDFORCE Cooling System
  • Hawk Fan
  • Server-grade Thermal Conductive Gel
  • RGB Lighting

The Hugging Face gradient-checkpointing documentation also describes offloading checkpointed activations to pinned host memory. Offloading exchanges GPU capacity for host RAM and transfers over the host-device link. It is a way to make a run fit, not a speed optimization; monitor transfer overhead and available host memory.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Evaluate mixed precision instead of assuming it saves memory

Using a lower-precision dtype can reduce storage for some weights, activations and temporary tensors and may improve throughput on supported hardware. The Hugging Face guide discusses fp16, bf16 and tf32, but the result depends on the framework’s implementation and which higher-precision copies are retained.

For example, an autocast region can be tested as follows, provided the selected dtype is supported by the GPU and model:

with torch.autocast(device_type='cuda', dtype=torch.bfloat16):
    output = model(batch)
    loss = loss_fn(output, target)

Measure allocated and reserved peaks before and after the change. Mixed precision is not a universal percentage reduction: fp16 may coexist with fp32 master weights or optimizer state, and some operations remain in higher precision for stability. Keep loss scaling and convergence checks appropriate to the dtype you choose.

Remove retained autograd history from metrics and logs

Audit lists, dictionaries, callbacks and progress-bar values that survive an iteration. Store detached CPU values for charts and running averages. Clear references to large outputs after they are no longer needed. This fix preserves the model, batch and sequence settings; it simply stops bookkeeping from keeping old graphs alive.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand what the caching allocator can and cannot do

torch.cuda.empty_cache() releases unused blocks held by PyTorch’s caching allocator so another GPU application can use them. It does not release memory occupied by live tensors and does not increase the memory available to those tensors. Calling it repeatedly inside a training loop is therefore not a solution to a live-tensor OOM and can add allocation overhead. Use it when a completed job or phase should return unused cached blocks to other applications, then confirm the effect with both PyTorch statistics and nvidia-smi.

Best Value
ASUS PRIME Radeon RX 9070 XT 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-slot, Axial-tech Fans, Dual-ball Fan Bearings, dual BIOS, ASUS GPU Guard
  • Axial-tech fans now feature a smaller fan hub that facilitates longer blades and a barrier ring that increases downward air pressure
  • Phase-change GPU thermal pad helps ensure optimal heat transfer, lowering GPU temperatures for enhanced performance and reliability
  • 2.5-slot design allows for greater build compatibility while maintaining cooling performance
  • Dual-ball fan bearings last up to twice as long as standard conventional sleeve bearings designs
  • 0dB technology lets you enjoy light gaming in relative silence
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing among the remedies

Remedy Main pressure reduced Effect on speed Preserves desired effective batch or sequence? Additional requirement
Smaller per-device batch Saved activations Less work per device step; more steps may be needed Preserves sequence length, but not the physical batch Configuration change
Shorter sequence Activations and, where materialized, attention scores Often less computation per example Preserves batch, but changes available context Task must tolerate truncation or windowing
Gradient accumulation Per-step activations More forward/backward passes per optimizer update Preserves a larger effective batch Training-loop or trainer configuration change
Gradient checkpointing Saved activations Slower backward because of recomputation Usually preserves batch and sequence settings Model support; optional host RAM for offloading
Activation offloading GPU-resident checkpointed activations Host-device transfer overhead Can preserve batch and sequence settings Pinned host memory and sufficient transfer capacity
Mixed precision Some tensor and workspace storage May improve speed on supported hardware; may not lower memory if fp32 copies remain Potentially, but must be measured Supported dtype and numerical validation
Detach bookkeeping values Accidentally retained graphs No intentional model slowdown Yes Code audit
empty_cache() Unused cached blocks only Can add allocator overhead if overused Does not change live-tensor capacity Useful mainly for sharing memory after a phase
GPU with more VRAM Total capacity pressure Depends on the new device; purchase alone does not fix leaks Can preserve the intended workload Hardware, driver and compatibility check

How to interpret common failure patterns

OOM on the first forward pass

Start with model weights, dtype, device placement and the initial batch or sequence dimensions. Check whether the model was accidentally copied to the GPU more than once. If the parameters plus required gradients and optimizer state already exceed capacity, checkpointing will not solve the fundamental limit because it mainly targets activations.

OOM only during backward

Inspect saved activations and temporary backward workspaces. Reduce the per-device batch or sequence length, then test checkpointing. If checkpointing makes the run fit but much slower, decide whether accumulation or a shorter context gives a better tradeoff.

OOM at the optimizer step

Measure memory immediately before and during the first optimizer update. Optimizer state may be allocated lazily at that point, producing a peak not visible during forward. A smaller batch will not remove this state; reducing retained tensors, changing the optimizer configuration where appropriate, using a lower-memory precision strategy that you have validated, or moving to a larger-memory device may be necessary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Memory rises after every iteration

Look for graph-connected losses, outputs or hidden states stored in Python containers, callbacks that retain batches, and worker processes that outlive a run. Detach logging values, release references after use and repeat a fixed single-batch test. A steadily rising allocated value is different from a stable allocated value paired with a high reserved value.

nvidia-smi is high but PyTorch allocated memory is low

High reserved memory can be harmless cache, while a large unexplained gap can indicate allocations outside PyTorch or another process. Use allocator statistics and the allocation-history snapshot, then inspect the device process list. Do not infer that all memory shown by nvidia-smi is occupied by live model tensors.

The script works alone but fails in a notebook or service

Check for a previous kernel, child process or crashed worker that still owns the CUDA context. End the stale process or restart the environment, then verify that only the intended job remains before measuring the training peak again.

When a larger-memory GPU is justified

Consider a GPU with more VRAM only after measurements show that the correctly configured workload’s live peak remains above available capacity. The sequence should be: remove retained autograd history, account for other processes and allocator behavior, reduce batch or sequence pressure, and evaluate accumulation, checkpointing, offloading and mixed precision. If the remaining requirement is still above the device limit—especially because weights plus optimizer state are the dominant cost—additional capacity is a reasonable hardware decision.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the capacity from the measured peak plus operational headroom, not from parameter count alone. The 85-GB example in the Hugging Face v5.12.0 guide demonstrates why architecture, sequence length, optimizer and batch assumptions must accompany any number. A larger device cannot correct a memory leak, and it does not guarantee faster training if the workload is limited by computation or host-device transfers.

A repeatable OOM workflow

  1. Reproduce with a fixed batch and record model, per-device batch, sequence length, dtype, optimizer and failing stage.
  2. Capture allocated, reserved and peak PyTorch memory around forward, backward and optimizer calls.
  3. Compare those values with nvidia-smi; identify external allocations and other processes.
  4. Inspect allocation history if the peak or a large accounting gap is unclear.
  5. Audit metrics, logs and lists for tensors that still carry autograd history; detach bookkeeping values.
  6. Reduce per-device batch or sequence length and retest.
  7. If effective batch size matters, add gradient accumulation rather than restoring the physical batch.
  8. If activations dominate, enable checkpointing and consider host offloading when transfer costs are acceptable.
  9. Test mixed precision with the actual dtype and hardware, measuring memory rather than assuming a saving.
  10. Only then decide whether the measured live peak requires a GPU with more VRAM.

This order separates a peak-capacity problem from allocator visibility, process contamination and accidental tensor retention, so each change addresses the cause rather than merely hiding the symptom.

Quick Recap

SaleBestseller No. 1
ASUS Dual Radeon RX 9060 XT 16GB GDDR6 Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1a, 2.5-slot design, Axial-tech Fans, 0dB Technology
ASUS Dual Radeon RX 9060 XT 16GB GDDR6 Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1a, 2.5-slot design, Axial-tech Fans, 0dB Technology
0dB technology lets you enjoy light gaming in relative silence; Dual BIOS switch lets you toggle between Quiet and Performance BIOS profiles
$529.00
Bestseller No. 2
GIGABYTE GeForce RTX 5070 Ti Gaming OC 16G Graphics Card, 16GB 256-bit GDDR7, PCIe 5.0, WINDFORCE Cooling System, GV-N507TGAMING OC-16GD Video Card
GIGABYTE GeForce RTX 5070 Ti Gaming OC 16G Graphics Card, 16GB 256-bit GDDR7, PCIe 5.0, WINDFORCE Cooling System, GV-N507TGAMING OC-16GD Video Card
Powered by the NVIDIA Blackwell architecture and DLSS 4; Powered by GeForce RTX 5070 Ti; Integrated with 16GB GDDR7 256bit memory interface
$1,249.99
Bestseller No. 3
GIGABYTE GeForce RTX 5060 WINDFORCE OC 8G Graphics Card, Cooling System, 8GB 128-bit GDDR7, PCIe 5.0, Manufactured by NVIDIA, DisplayPort & HDMI - Video Output Interface, GV-N5060WF2OC-8GD Video Card
GIGABYTE GeForce RTX 5060 WINDFORCE OC 8G Graphics Card, Cooling System, 8GB 128-bit GDDR7, PCIe 5.0, Manufactured by NVIDIA, DisplayPort & HDMI - Video Output Interface, GV-N5060WF2OC-8GD Video Card
Powered by the NVIDIA Blackwell architecture and DLSS 4; Powered by GeForce RTX 5060; Integrated with 8GB GDDR7 128bit memory interface
$495.99
SaleBestseller No. 4
GIGABYTE Radeon RX 9070 XT Gaming OC 16G Graphics Card, PCIe 5.0, 16GB GDDR6, GV-R9070XTGAMING OC-16GD Video Card
GIGABYTE Radeon RX 9070 XT Gaming OC 16G Graphics Card, PCIe 5.0, 16GB GDDR6, GV-R9070XTGAMING OC-16GD Video Card
Powered by Radeon RX 9070 XT; WINDFORCE Cooling System; Hawk Fan; Server-grade Thermal Conductive Gel
$814.50
Bestseller No. 5
ASUS PRIME Radeon RX 9070 XT 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-slot, Axial-tech Fans, Dual-ball Fan Bearings, dual BIOS, ASUS GPU Guard
ASUS PRIME Radeon RX 9070 XT 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-slot, Axial-tech Fans, Dual-ball Fan Bearings, dual BIOS, ASUS GPU Guard
0dB technology lets you enjoy light gaming in relative silence; Dual BIOS switch lets you toggle between Quiet and Performance BIOS profiles
$829.99

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

AI trainingCUDADeep LearningGPU memoryPyTorch

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.