Pytorch GPU 有关的一些收集

2022-10-05 12:35:15

torch.cuda.is_available() #检查Pytorch GPU 是否可用
torch.cuda.device_count() #返回gpu的数量
torch.cuda.current_device() #返回当前使用的GPU 的索引
torch.cuda.get_device_name() #返回GPU的名字,从索引0开始
torch.cuda.is_initialized()返回PyTorch的CUDA状态是否已初始化

torch.cuda.empty_cache()
释放未占用的缓存内存,以便这些内存可在其他GPU应用程序中使用并在nvidia-smi中可见。empty_cache()不会增加可用于PyTorch的GPU内存量。但是,有助于减少GPU内存的碎片。

GPU内存查看

torch.cuda.memory_allocated()

将返回当前Tensor占用的GPU内存(以字节为单位),
print(int(torch.cuda.memory_allocated()/1024/1024))

torch.cuda.max_memory_allocated()

将返回自此程序开始以来的Tensor的峰值分配内存

torch.cuda.reset_peak_memory_stats()

 重置用于跟踪给定设备的Tensor占用的最大GPU内存的起点。which resets /all/ peak memory stats(以前版本torch.cuda.reset_max_memory_allocated())

这两个功能可以测量训练循环中每个迭代的峰值分配内存使用量

torch.cuda.reset_max_memory_allocated()
torch.cuda.max_memory_allocated()

	重置跟踪此指标的起点。这两个功能可以测量训练循环中每个迭代的峰值分配内存使用量。

torch.cuda.memory_reserved返回给定设备由缓存分配器管理的当前GPU内存(以字节为单位)
torch.cuda.max_memory_reserved返回给定设备由缓存分配器管理的最大GPU内存

  • 作者:HJC256ZY
  • 原文链接:https://blog.csdn.net/HJC256ZY/article/details/106516894
    更新时间:2022-10-05 12:35:15