生活随笔
收集整理的這篇文章主要介紹了
python 输出 GPU内存 最大使用率
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 安裝 pip install nvidia-ml-py
from pynvml
import *
def nvidia_info():nvidia_dict
= {"state": True,"nvidia_version": "","nvidia_count": 0,"gpus": []}try:nvmlInit
()nvidia_dict
["nvidia_version"] = nvmlSystemGetDriverVersion
()nvidia_dict
["nvidia_count"] = nvmlDeviceGetCount
()for i
in range(nvidia_dict
["nvidia_count"]):handle
= nvmlDeviceGetHandleByIndex
(i
)memory_info
= nvmlDeviceGetMemoryInfo
(handle
)gpu
= {"gpu_name": nvmlDeviceGetName
(handle
),"total": memory_info
.total
,"free": memory_info
.free
,"used": memory_info
.used
,"temperature": f"{nvmlDeviceGetTemperature(handle, 0)}℃","powerStatus": nvmlDeviceGetPowerState
(handle
)}nvidia_dict
['gpus'].append
(gpu
)except NVMLError
as _
:nvidia_dict
["state"] = Falseexcept Exception
as _
:nvidia_dict
["state"] = Falsefinally:try:nvmlShutdown
()except:passreturn nvidia_dict
def check_gpu_mem_usedRate():max_rate
= 0.0while True:info
= nvidia_info
()used
= info
['gpus'][0]['used']tot
= info
['gpus'][0]['total']print(f"GPU0 used: {used}, tot: {tot}, 使用率:{used/tot}")if used
/tot
> max_rate
:max_rate
= used
/tot
print("GPU0 最大使用率:", max_rate
)
在跑任務時,另外運行腳本調用 check_gpu_mem_usedRate 就可以知道最大的 GPU內存 使用率,線上服務不要用的太滿,最大80%左右為宜,防止極端情況GPU顯存溢出
參考:
python獲取GPU,CPU,硬盤,內存,系統,用戶使用情況信息
【Python管理GPU】pynvml工具的安裝與使用
總結
以上是生活随笔為你收集整理的python 输出 GPU内存 最大使用率的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。