---
title: "Visualize and understand GPU memory in PyTorch"
description: "PyTorch's memory snapshot recorder (torch.cuda.memory._record_memory_history plus the memory_viz web tool) makes it possible to see exactly where GPU memory goes during training: persistent parameters, activations that grow in the forward pass and shrink in backward, gradients, optimizer state and transient optimizer intermediates."
url: "https://swe.quest/articles/visualize-and-understand-gpu-memory-in-pytorch-69d1cac2293ba761"
canonical: "https://huggingface.co/blog/train_memory"
source: "Hugging Face"
published: "2024-12-24"
difficulty: "medium"
kind: "How-to"
reading_minutes: 10
tags: ["model-training","python","memory-management","hardware","llms"]
---

# Visualize and understand GPU memory in PyTorch

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2024-12-24, 10 min read, How-to. Original: [https://huggingface.co/blog/train\_memory](https://huggingface.co/blog/train_memory)

## Summary

PyTorch's memory snapshot recorder (torch.cuda.memory.\_record\_memory\_history plus the memory\_viz web tool) makes it possible to see exactly where GPU memory goes during training: persistent parameters, activations that grow in the forward pass and shrink in backward, gradients, optimizer state and transient optimizer intermediates. Profiling a Qwen2.5-1.5B training loop shows the peak can come from either the forward pass or the optimizer step depending on batch size. The resulting estimate is total memory = parameters + optimizer state (2×N×P for AdamW) + the larger of activations or gradients plus optimizer intermediates. Activations per token can be measured with forward hooks or approximated by a linear fit against parameter count (A ≈ 4.69e-4×N + 1.85e6).

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Hardware](https://swe.quest/tags/hardware.md) (Infrastructure)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- PyTorch training loops ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- backpropagation basics
- Adam optimizer

## Keywords

PyTorch, CUDA, AdamW, Qwen2.5-1.5B, Hugging Face Transformers, TRL

## Related posts

- [Learn the Hugging Face Kernel Hub in 5 Minutes](https://swe.quest/articles/learn-the-hugging-face-kernel-hub-in-5-minutes-049114b1e85677a7.md): Hugging Face · 2025-06-12 · Medium · 13 min read · Inference & serving, Performance engineering, Python, Hardware
  Hugging Face's Kernel Hub and \`kernels\` library let PyTorch code download pre-compiled, optimized GPU kernels such as FlashAttention, activations, RMSNorm and quantization kernels with a single \`get\_kernel\` call.
- [GPU Memory Snapshots: Supercharging sub-second startup](https://swe.quest/articles/gpu-memory-snapshots-supercharging-sub-second-startup-14c07f765f85f384.md): Modal · 2025-07-30 · Medium · 6 min read · Serverless, Inference & serving, Hardware, Performance engineering
  Modal's GPU memory snapshots extend its gVisor-based checkpoint/restore to capture GPU state, using NVIDIA's new CUDA checkpoint API (driver branches 570/575) to lock CUDA processes, copy device memory, kernels, streams, contexts and memory mappings to host memory, then snapshot CPU and GPU state together.
- [Trading off compute for memory with activation checkpointing](https://swe.quest/articles/trading-off-compute-for-memory-with-activation-checkpointing-1f2b8d13685e738d.md): Jane Street Tech Blog · 2026-09-30 · Hard · 9 min read · Model training, Memory management, Compilers, Performance engineering, Python
  A whole-graph activation checkpointing planner for torch.compile takes an absolute GB memory budget and greedily drops min-cut saves with the cheapest recompute cost per byte of peak memory freed.
- [Profiling in PyTorch (Part 1): A Beginner's Guide to torch.profiler](https://swe.quest/articles/profiling-in-pytorch-part-1-a-beginner-s-guide-to-torch-profiler-de0c791495fa22c4.md): Hugging Face · 2026-05-29 · Medium · 27 min read · Performance engineering, Machine learning, Python, Debugging
  torch.profiler produces two artifacts: a statistical table showing where time goes and a Chrome/Perfetto trace showing when CPU and GPU events happen.
- [GaLore: Advancing Large Model Training on Consumer-grade Hardware](https://swe.quest/articles/galore-advancing-large-model-training-on-consumer-grade-hardware-1747480276ec0047.md): Hugging Face · 2024-03-20 · Hard · 7 min read · Model training, LLMs, Python
  GaLore cuts LLM training memory by projecting gradients into a low-rank subspace before the optimizer sees them, reporting over 82.5% reduction in optimizer-state memory and enabling 7B Llama-style models to train on a single RTX 4090.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
