openxlab / gpu.py
ACCA225's picture
Update gpu.py
c455131 verified
raw
history blame
986 Bytes
import os
import time
import wandb
import nvidia_smi
import os
import time
import threading
import wandb
os.system("pip install nvidia-ml-py3")
# WandB登录
os.system('wandb login 6cdd066d56acd416b1c1680133e37589511d81f7')
nvidia_smi.nvmlInit()
# 初始化WandB项目
wandb.init(project="GPU-使用率-温度检测")
def monitor_gpu():
while True:
try:
# 获取 GPU 温度
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0) # 0 表示第一个 GPU
gpu_temperature = nvidia_smi.nvmlDeviceGetTemperature(handle, nvidia_smi.NVML_TEMPERATURE_GPU)
# 获取 GPU 使用率
utilization = nvidia_smi.nvmlDeviceGetUtilizationRates(handle)
gpu_usage = utilization.gpu
# 使用 WandB 记录 GPU 温度和使用率
wandb.log({"GPU 温度": gpu_temperature, "GPU 使用率": gpu_usage})
except Exception as e:
print(f"Error: {e}")
time.sleep(60)
monitor_gpu()