Understanding CUDA Memory Usage
record_pinned_host_memory=True
~torch.cuda.memory._record_memory_history
Any memory allocated directly from CUDA APIs in C++ (e.g., cudaMalloc, cuMemCreate) or via third-party
Python bindings such as cuda-python <https://github.com/NVIDIA/cuda-python>_ will not be visible in the
PyTorch memory profiler. NCCL (used for distributed communication on CUDA devices) is a common example of a
library that allocates GPU memory outside of PyTorch’s allocator. See Identifying Non-PyTorch allocations for more info.
Allocator State History
The 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
allocator state at that event. This summary shows each individual segment returned from cudaMalloc and how it is split up into blocks of individual allocations
or 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,
such 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
an allocation failed even though reserved memory still exists.
The stack trace information also reports the address at which an allocation occurred.
The address b7f064c000000_0 refers to the (b)lock at address 7f064c000000 which is the “_0”th time this address was allocated.
This unique string can be looked up in the Active Memory Timeline and searched
in the Active State History to examine the memory state when a tensor was allocated or freed.
record_history
)
{
if
(
alloc_trace
->
size
()
<
alloc_trace_max_entries_
)
{
alloc_trace
->
emplace_back
(
te
);
}
else
{
(
*
alloc_trace
)[
alloc_trace_next
++
]
=
te
;
if
(
alloc_trace_next
==
alloc_trace_max_entries_
)
{
alloc_trace_next
=
0
;
}
}
}
[
List
[
TraceEntry
]]
class
Segment
(
TypedDict
):
address
:
int
total_size
:
int
stream
:
int
segment_type
:
Literal
[
"small"
,
"large"
]
segment_pool_id
:
Tuple
[
int
,
int
]
allocated_size
:
int
active_size
:
int
blocks
:
List
[
Block
]
class
Block
(
TypedDict
):
size
:
int
requested_size
:
int
address
:
int
state
:
Literal
[
"active_allocated"
,
"active_awaiting_free"
,
"inactive"
,
]
frames
:
List
[
Frame
]
class
Frame
(
TypedDict
):
filename
:
str
line
:
int
name
:
str
fx_node_op
:
str
fx_node_name
:
str
fx_original_trace
:
str
class
TraceEntry
(
TypedDict
):
action
:
Literal
[
"alloc"
"free_requested"
,
"free_completed"
,
"segment_alloc"
,
"segment_free"
,
"oom"
,
"snapshot"
,
"annotate"
,
]
addr
:
int
frames
:
List
[
Frame
]
size
:
int
stream
:
int
device_free
:
int
pool_id
:
Tuple
[
int
,
int
]
- Parameters:
device (Device) – Device to capture snapshot for. If None, captures for current device.
augment_with_fx_traces – If True, augment stack trace frames with FX debug information
that maps generated FX code back to original model source code.
This adds fx_node_op, fx_node_name, fx_original_trace, and
fx_node_info fields to Frame objects. Default: False.
- Returns:
The Snapshot dictionary object
augment_with_fx_traces
,
optional