评价此页

torch.cuda.memory.memory_stats#

torch.cuda.memory.memory_stats(device=None)[source]#

返回给定设备的 CUDA 内存分配器统计信息的字典。

此函数的返回值是一个统计字典,其中每个统计项都是一个非负整数。

核心统计信息

  • "allocated.{all,large_pool,small_pool}.{current,peak,allocated,freed}":内存分配器收到的分配请求数量。

  • "allocated_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}":已分配的内存量。

  • "segment.{all,large_pool,small_pool}.{current,peak,allocated,freed}":从cudaMalloc()保留的段的数量。

  • "reserved_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}":已保留的内存量。

  • "active.{all,large_pool,small_pool}.{current,peak,allocated,freed}":活动内存块的数量。

  • "active_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}":活动内存量。

  • "inactive_split.{all,large_pool,small_pool}.{current,peak,allocated,freed}":非活动、不可释放内存块的数量。

  • "inactive_split_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}":非活动、不可释放内存量。

对于这些核心统计信息,值按以下方式细分。

内存池类型

  • all:所有内存池的组合统计信息。

  • large_pool:大内存分配池的统计信息(截至 2019 年 10 月,分配大小 >= 1MB)。

  • small_pool:小内存分配池的统计信息(截至 2019 年 10 月,分配大小 < 1MB)。

指标类型

  • current:此指标的当前值。

  • peak:此指标的最大值。

  • allocated:此指标的历史总增长量。

  • freed:此指标的历史总减少量。

除了核心统计信息外,我们还提供了一些简单的事件计数器

  • "num_alloc_retries":导致缓存刷新和重试的失败cudaMalloc调用次数。

  • "num_ooms":抛出的内存不足错误次数。

  • "num_sync_all_streams"synchronize_and_free_events调用次数。

  • "num_device_alloc":CUDA 分配调用的数量。这包括 `cuMemMap` 和 `cudaMalloc`。

  • "num_device_free":CUDA 释放调用的数量。这包括 `cuMemUnmap` 和 `cudaFree`。

可以通过环境变量配置缓存分配器,使其不分割大于定义大小的块(请参阅 Cuda Semantics 文档的内存管理部分)。这有助于避免内存碎片,但可能会带来性能损失。其他用于协助调优和评估影响的输出

  • "max_split_size":大于此大小的块将不会被拆分。

  • "oversize_allocations.{current,peak,allocated,freed}":内存分配器收到的超大分配请求数量。

  • "oversize_segments.{current,peak,allocated,freed}":从cudaMalloc()保留的超大段的数量。

缓存分配器可以通过环境变量进行配置,通过四舍五入内存分配来减少碎片。有时四舍五入带来的开销可能高于其帮助减少的碎片。以下统计数据可用于检查四舍五入是否增加了过多的开销。

  • "requested_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}":客户端代码请求的内存,将其与allocated_bytes进行比较,以检查分配四舍五入是否增加了过多的开销。

参数

device (torch.deviceint, 可选) – 选择的设备。如果 deviceNone(默认值),则返回当前设备(由 current_device() 指定)的统计信息。

返回类型

dict[str, Any]

注意

有关 GPU 内存管理的更多详细信息,请参阅 内存管理

注意

使用 backend:cudaMallocAsync 时,某些统计数据无意义,并且始终报告为零。