评价此页

torch.accelerator.memory.memory_stats#

torch.accelerator.memory.memory_stats(device_index=None, /)[源代码]#

返回给定设备索引的加速器设备内存分配器统计信息的字典。

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

核心统计信息

  • "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}": 从设备内存分配中预留的段数量。

  • "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: 大型分配池的统计信息(截至 2025 年 6 月,用于大小 >= 1MB 的分配)。

  • small_pool: 小型分配池的统计信息(截至 2025 年 6 月,用于大小 < 1MB 的分配)。

指标类型

  • current:此指标的当前值。

  • peak:此指标的最大值。

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

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

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

  • "num_alloc_retries": 导致缓存刷新并重试的设备内存分配调用失败次数。

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

  • "num_sync_all_streams": synchronize_and_free_events 调用次数。

  • "num_device_alloc": 设备内存分配调用次数。

  • "num_device_free": 设备内存释放调用次数。

参数

device_index (torch.device, str, int, optional) – 要定位的设备索引。如果未给出,则默认使用 torch.accelerator.current_device_index()。如果提供了 torch.device 或 str,则其类型必须与当前的 accelerator 设备类型匹配。

返回类型

OrderedDict[str, Any]