• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _torch_cuda_memory:
2
3Understanding CUDA Memory Usage
4===============================
5To debug CUDA memory use, PyTorch provides a way to generate memory snapshots that record the state of allocated CUDA memory
6at any point in time, and optionally record the history of allocation events that led up to that snapshot.
7
8The generated snapshots can then be drag and dropped onto the interactiver viewer hosted at `pytorch.org/memory_viz <https://pytorch.org/memory_viz>`_ which
9can be used to explore the snapshot.
10
11Generating a Snapshot
12=====================
13The common pattern for recording a snapshot is to enable memory history, run the code to be observed, and then save a file with a pickled snapshot:
14
15.. code-block:: python
16
17   # enable memory history, which will
18   # add tracebacks and event history to snapshots
19   torch.cuda.memory._record_memory_history()
20
21   run_your_code()
22   torch.cuda.memory._dump_snapshot("my_snapshot.pickle")
23
24Using the visualizer
25====================
26
27Open `pytorch.org/memory_viz <https://pytorch.org/memory_viz>`_ and drag/drop the pickled snapshot file into the visualizer.
28The visualizer is a javascript application that runs locally on your computer. It does not upload any snapshot data.
29
30
31Active Memory Timeline
32----------------------
33
34The Active Memory Timeline shows all the live tensors over time in the snapshot on a particular GPU. Pan/Zoom over the plot to look at smaller allocations.
35Mouse over allocated blocks to see a stack trace for when that block was allocated, and details like its address. The detail slider can be adjusted to
36render fewer allocations and improve performance when there is a lot of data.
37
38.. image:: _static/img/torch_cuda_memory/active_memory_timeline.png
39
40
41Allocator State History
42-----------------------
43
44The Allocator State History shows individual allocator events in a timeline on the left. Select an event in the timeline to see a visual summary of the
45allocator state at that event. This summary shows each individual segment returned from cudaMalloc and how it is split up into blocks of individual allocations
46or free space. Mouse over segments and blocks to see the stack trace when the memory was allocated. Mouse over events to see the stack trace when the event occurred,
47such as when a tensor was freed. Out of memory errors are reported as OOM events. Looking at the state of memory during an OOM may provide insight into why
48an allocation failed even though reserved memory still exists.
49
50.. image:: _static/img/torch_cuda_memory/allocator_state_history.png
51
52The stack trace information also reports the address at which an allocation occurred.
53The address b7f064c000000_0 refers to the (b)lock at address 7f064c000000 which is the "_0"th time this address was allocated.
54This unique string can be looked up in the Active Memory Timeline and searched
55in the Active State History to examine the memory state when a tensor was allocated or freed.
56
57Snapshot API Reference
58======================
59
60.. currentmodule:: torch.cuda.memory
61.. autofunction:: _record_memory_history
62.. autofunction:: _snapshot
63.. autofunction:: _dump_snapshot
64