Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 10 min read How-to mediumAdept

Visualize and understand GPU memory in PyTorch

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).

Assumes: PyTorch training loopsbackpropagation basicsAdam optimizer

Related posts

  • Learn the Hugging Face Kernel Hub in 5 Minutes

    Hugging Face mediumAdept

    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. The library matches the binary to the installed Python, PyTorch and CUDA versions, which avoids source builds that for FlashAttention can need about 96 GB of RAM and hours of compilation. The walkthrough checks a fast GELU kernel against PyTorch and swaps a Triton LlamaRMSNorm into a simple model with the `use_kernel_forward_from_hub` decorator. It then benchmarks the kernel against a plain PyTorch RMSNorm using CUDA events.

  • GPU Memory Snapshots: Supercharging sub-second startup

    Modal mediumAdept

    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. Restoring skips expensive warm-up such as torch.compile, CUDA graph capture and moving weights from CPU to GPU, which removes the earlier split between pre- and post-restore setup methods. Reported cold starts drop by up to 10x, for example Parakeet from about 20s to 2s, vLLM with Qwen2.5-0.5B from 45s to 5s, and a compiled ViT from 8.5s to 2.25s. The feature is in alpha and is enabled with an experimental flag.

  • Trading off compute for memory with activation checkpointing

    Jane Street Tech Blog hardMaster

    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. Before training, a planning pass on fake tensors captures every graph, its min-cut save set and the execution order. Peak memory is simulated from tensor liveness across stacked forward and backward graphs, and recompute time is estimated from FLOP formulas. On single-graph models it beat PyTorch's memory-budget knapsack and op-level SAC policies for most budgets, avoided recompute-induced peak spikes, and still lacks support for dynamic shapes, data-dependent ops and RNG ops.

  • Profiling in PyTorch (Part 1): A Beginner's Guide to torch.profiler

    Hugging Face mediumAdept

    torch.profiler produces two artifacts: a statistical table showing where time goes and a Chrome/Perfetto trace showing when CPU and GPU events happen. A small 64x64 bf16 matmul+add is overhead-bound, with GPU time under 1% of CPU time, while 4096x4096 matrices shift the work to compute-bound GEMM kernels. The trace walkthrough explains a cold-start dead window of about 228 µs before aten::matmul dispatch, a CPU-GPU lane offset, gaps caused by profiler Activity Buffer Requests, and the dispatch chain from Python down to CUDA kernels. The practical advice is to warm up, use profiler schedules, and read Self versus total times carefully before turning to torch.compile.

  • GaLore: Advancing Large Model Training on Consumer-grade Hardware

    Hugging Face hardMaster

    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. Periodic subspace switching preserves full-parameter learning, and combining the projection with 8-bit quantized optimizer states (via bitsandbytes) saves further memory. It is available in Hugging Face Transformers 4.39+ through optimizers like galore_adamw, galore_adamw_8bit and galore_adafactor with optim_target_modules, plus layer-wise variants that apply updates per layer via PyTorch post-accumulation hooks.

The quest for software engineering knowledge.

Look:
Layout: