
What happened
A measurement routine for PyTorch training reports allocated memory (torch.cuda.memory_allocated()), reserved memory (torch.cuda.memory_reserved()) and peak memory, and is called right after forward, backward and optimizer.step.
Why it matters
If allocated memory climbs with every update, the likely cause is retained tensors or the computation graph; if only reserved memory grows and then levels off, it is probably caching or variation in allocation size.
Ask the AI about this article. Questions and answers are published on this page
This is a counterpoint to a common reflex: seeing a big GPU memory number and immediately reducing the batch size. Its argument is that PyTorch's allocator holds memory for reuse, so the reserved figure can be large without any leak. To separate the two, the author proposes a small report function that prints allocated, reserved and peak GiB, built on torch.cuda.memory_allocated(), torch.cuda.memory_reserved() and torch.cuda.max_memory_allocated().
The placement of those measurements matters. The suggested practice is to call the report right after forward, right after backward and right after optimizer.step, and to compare the first update with steady state because optimizer state may be created on the first step. Observations map to next steps: allocated memory rising on every update points to held tensor references or a retained computation graph; reserved memory rising and then stabilizing points to caching or allocation-size variation; a peak at backward points to activations, gradients or activation checkpointing; a jump on the first step points to optimizer state. It also shows loss_history.append(loss.detach().item()) for keeping only loss values, while cautioning that item may involve GPU synchronization, so logging frequency should be considered.
The author closes with a practical caveat: PyTorch's counters do not cover Windows shared GPU memory, CUDA contexts or allocations by external libraries, so OS-side dedicated and shared memory should also be watched. The code is presented as a measurement example, not as results from a specific environment.
Pick your industry and the AI tools you use, and get news related to your work every day.
By signing up you agree to the Terms of Service and Privacy Policy.
Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →
A team lead whose LLM features hit production problems — output quality swings, costs at 3x budget, hallucinat…

EDN reports on Project Solas, a project that connects AI coding with chip design resources

At Microsoft Build 2026, Azure Databricks introduced Lakebase branching with GitHub Copilot agent mode in Publ…

CodingDINO, a 17-author vision model that represents images and videos as executable SVG, HTML, ThreeJS, Blend…

Mend.io's 2026 State of AI in AppSec report, surveying 500 security practitioners at large organizations, foun…

Banco Guayaquil product engineering leader Franz Zambrano said the bank wants to build an AI developer platfor…