forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.py
More file actions
29 lines (27 loc) · 890 Bytes
/
Copy pathgpu.py
File metadata and controls
29 lines (27 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# GPU stats via pynvml
try:
import pynvml
pynvml.nvmlInit()
NVML_AVAILABLE = True
except ImportError:
NVML_AVAILABLE = False
except Exception:
NVML_AVAILABLE = False
def get_gpu_stats():
if not NVML_AVAILABLE:
return {'error': 'pynvml not available or no NVIDIA GPU detected'}
try:
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
util = pynvml.nvmlDeviceGetUtilizationRates(handle)
mem = pynvml.nvmlDeviceGetMemoryInfo(handle)
temp = pynvml.nvmlDeviceGetTemperature(handle, pynvml.NVML_TEMPERATURE_GPU)
return {
'gpu_util': util.gpu, # percent
'mem_util': util.memory, # percent
'vram_total': mem.total,
'vram_used': mem.used,
'vram_free': mem.free,
'temperature': temp
}
except Exception as e:
return {'error': str(e)}