-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_pynvml.py
More file actions
422 lines (357 loc) · 13.1 KB
/
test_pynvml.py
File metadata and controls
422 lines (357 loc) · 13.1 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env python3
"""
Test suite for pynvml-equivalent APIs in pymtml.py
Tests the NVML wrapper functions that provide compatibility with nvidia-ml-py (pynvml).
Run with: python test_pynvml.py
"""
import sys
import traceback
# Import pymtml as if it were pynvml
import pymtml as pynvml
def print_section(title):
print(f"\n{'='*60}")
print(f" {title}")
print(f"{'='*60}")
def print_result(name, value, indent=2):
prefix = " " * indent
print(f"{prefix}{name}: {value}")
def test_api(name, func):
"""Run a test function and catch any errors"""
try:
result = func()
print_result(name, result)
return True, result
except pynvml.NVMLError as e:
print_result(name, f"[NVMLError: {e}]")
return False, None
except Exception as e:
print_result(name, f"[Error: {e}]")
return False, None
class PynvmlTestSuite:
def __init__(self):
self.passed = 0
self.failed = 0
def test_library_apis(self):
print_section("Library APIs")
test_api("nvmlSystemGetDriverVersion", pynvml.nvmlSystemGetDriverVersion)
test_api("nvmlDeviceGetCount", pynvml.nvmlDeviceGetCount)
test_api(
"nvmlSystemGetCudaDriverVersion", pynvml.nvmlSystemGetCudaDriverVersion
)
def test_device_handle_apis(self):
print_section("Device Handle APIs")
# Get handle by index
success, handle = test_api(
"nvmlDeviceGetHandleByIndex(0)",
lambda: pynvml.nvmlDeviceGetHandleByIndex(0),
)
if not success:
return None
# Get UUID for testing GetHandleByUuid
_, uuid = test_api("nvmlDeviceGetUUID", lambda: pynvml.nvmlDeviceGetUUID(handle))
if uuid:
test_api(
"nvmlDeviceGetHandleByUuid",
lambda: pynvml.nvmlDeviceGetHandleByUuid(uuid),
)
# Get PCI info for testing GetHandleByPciBusId
_, pci_info = test_api(
"nvmlDeviceGetPciInfo", lambda: pynvml.nvmlDeviceGetPciInfo(handle)
)
if pci_info and hasattr(pci_info, "sbdf"):
test_api(
"nvmlDeviceGetHandleByPciBusId",
lambda: pynvml.nvmlDeviceGetHandleByPciBusId(pci_info.sbdf),
)
return handle
def test_device_info_apis(self, device):
print_section("Device Info APIs")
test_api("nvmlDeviceGetIndex", lambda: pynvml.nvmlDeviceGetIndex(device))
test_api("nvmlDeviceGetName", lambda: pynvml.nvmlDeviceGetName(device))
test_api("nvmlDeviceGetUUID", lambda: pynvml.nvmlDeviceGetUUID(device))
test_api("nvmlDeviceGetSerial", lambda: pynvml.nvmlDeviceGetSerial(device))
test_api("nvmlDeviceGetBrand", lambda: pynvml.nvmlDeviceGetBrand(device))
test_api(
"nvmlDeviceGetVbiosVersion", lambda: pynvml.nvmlDeviceGetVbiosVersion(device)
)
test_api(
"nvmlDeviceGetMinorNumber", lambda: pynvml.nvmlDeviceGetMinorNumber(device)
)
test_api(
"nvmlDeviceGetNumGpuCores", lambda: pynvml.nvmlDeviceGetNumGpuCores(device)
)
def test_memory_apis(self, device):
print_section("Memory APIs")
test_api(
"nvmlDeviceGetMemoryInfo", lambda: pynvml.nvmlDeviceGetMemoryInfo(device)
)
test_api(
"nvmlDeviceGetBAR1MemoryInfo",
lambda: pynvml.nvmlDeviceGetBAR1MemoryInfo(device),
)
test_api(
"nvmlDeviceGetMemoryBusWidth",
lambda: pynvml.nvmlDeviceGetMemoryBusWidth(device),
)
def test_utilization_apis(self, device):
print_section("Utilization APIs")
test_api(
"nvmlDeviceGetUtilizationRates",
lambda: pynvml.nvmlDeviceGetUtilizationRates(device),
)
test_api(
"nvmlDeviceGetEncoderUtilization",
lambda: pynvml.nvmlDeviceGetEncoderUtilization(device),
)
test_api(
"nvmlDeviceGetDecoderUtilization",
lambda: pynvml.nvmlDeviceGetDecoderUtilization(device),
)
def test_clock_apis(self, device):
print_section("Clock APIs")
test_api(
"nvmlDeviceGetClockInfo (GRAPHICS)",
lambda: pynvml.nvmlDeviceGetClockInfo(device, pynvml.NVML_CLOCK_GRAPHICS),
)
test_api(
"nvmlDeviceGetClockInfo (SM)",
lambda: pynvml.nvmlDeviceGetClockInfo(device, pynvml.NVML_CLOCK_SM),
)
test_api(
"nvmlDeviceGetClockInfo (MEM)",
lambda: pynvml.nvmlDeviceGetClockInfo(device, pynvml.NVML_CLOCK_MEM),
)
test_api(
"nvmlDeviceGetClockInfo (VIDEO)",
lambda: pynvml.nvmlDeviceGetClockInfo(device, pynvml.NVML_CLOCK_VIDEO),
)
test_api(
"nvmlDeviceGetMaxClockInfo (GRAPHICS)",
lambda: pynvml.nvmlDeviceGetMaxClockInfo(
device, pynvml.NVML_CLOCK_GRAPHICS
),
)
test_api(
"nvmlDeviceGetMaxClockInfo (SM)",
lambda: pynvml.nvmlDeviceGetMaxClockInfo(device, pynvml.NVML_CLOCK_SM),
)
test_api(
"nvmlDeviceGetMaxClockInfo (MEM)",
lambda: pynvml.nvmlDeviceGetMaxClockInfo(device, pynvml.NVML_CLOCK_MEM),
)
def test_temperature_power_apis(self, device):
print_section("Temperature & Power APIs")
test_api(
"nvmlDeviceGetTemperature (GPU)",
lambda: pynvml.nvmlDeviceGetTemperature(
device, pynvml.NVML_TEMPERATURE_GPU
),
)
test_api(
"nvmlDeviceGetPowerUsage", lambda: pynvml.nvmlDeviceGetPowerUsage(device)
)
test_api(
"nvmlDeviceGetPowerManagementLimit",
lambda: pynvml.nvmlDeviceGetPowerManagementLimit(device),
)
def test_fan_apis(self, device):
print_section("Fan APIs")
test_api("nvmlDeviceGetFanSpeed", lambda: pynvml.nvmlDeviceGetFanSpeed(device))
test_api(
"nvmlDeviceGetFanSpeed_v2 (fan 0)",
lambda: pynvml.nvmlDeviceGetFanSpeed_v2(device, 0),
)
def test_pci_apis(self, device):
print_section("PCI APIs")
test_api("nvmlDeviceGetPciInfo", lambda: pynvml.nvmlDeviceGetPciInfo(device))
# Verify busId is populated (should be filled from sbdf if empty)
pci_info = pynvml.nvmlDeviceGetPciInfo(device)
if pci_info.busId and pci_info.busId[0].isalnum():
print_result("nvmlDeviceGetPciInfo busId populated", pci_info.busId)
else:
print_result("nvmlDeviceGetPciInfo busId populated", "[FAIL: busId is empty or invalid]")
test_api(
"nvmlDeviceGetPcieThroughput (TX)",
lambda: pynvml.nvmlDeviceGetPcieThroughput(
device, pynvml.NVML_PCIE_UTIL_TX_BYTES
),
)
test_api(
"nvmlDeviceGetPcieThroughput (RX)",
lambda: pynvml.nvmlDeviceGetPcieThroughput(
device, pynvml.NVML_PCIE_UTIL_RX_BYTES
),
)
def test_compute_apis(self, device):
print_section("Compute APIs")
test_api(
"nvmlDeviceGetComputeMode", lambda: pynvml.nvmlDeviceGetComputeMode(device)
)
test_api(
"nvmlDeviceGetCudaComputeCapability",
lambda: pynvml.nvmlDeviceGetCudaComputeCapability(device),
)
test_api(
"nvmlDeviceGetComputeRunningProcesses",
lambda: pynvml.nvmlDeviceGetComputeRunningProcesses(device),
)
test_api(
"nvmlDeviceGetGraphicsRunningProcesses",
lambda: pynvml.nvmlDeviceGetGraphicsRunningProcesses(device),
)
def test_ecc_apis(self, device):
print_section("ECC APIs")
test_api("nvmlDeviceGetEccMode", lambda: pynvml.nvmlDeviceGetEccMode(device))
test_api(
"nvmlDeviceGetCurrentEccMode",
lambda: pynvml.nvmlDeviceGetCurrentEccMode(device),
)
test_api(
"nvmlDeviceGetPendingEccMode",
lambda: pynvml.nvmlDeviceGetPendingEccMode(device),
)
test_api(
"nvmlDeviceGetTotalEccErrors (Corrected/Volatile)",
lambda: pynvml.nvmlDeviceGetTotalEccErrors(
device,
pynvml.NVML_MEMORY_ERROR_TYPE_CORRECTED,
pynvml.NVML_VOLATILE_ECC,
),
)
test_api(
"nvmlDeviceGetRetiredPagesPendingStatus",
lambda: pynvml.nvmlDeviceGetRetiredPagesPendingStatus(device),
)
def test_mode_apis(self, device):
print_section("Mode APIs")
test_api(
"nvmlDeviceGetDisplayMode", lambda: pynvml.nvmlDeviceGetDisplayMode(device)
)
test_api(
"nvmlDeviceGetDisplayActive",
lambda: pynvml.nvmlDeviceGetDisplayActive(device),
)
test_api(
"nvmlDeviceGetPersistenceMode",
lambda: pynvml.nvmlDeviceGetPersistenceMode(device),
)
test_api(
"nvmlDeviceGetPerformanceState",
lambda: pynvml.nvmlDeviceGetPerformanceState(device),
)
test_api(
"nvmlDeviceGetCurrentDriverModel",
lambda: pynvml.nvmlDeviceGetCurrentDriverModel(device),
)
def test_mig_apis(self, device):
print_section("MIG APIs")
test_api(
"nvmlDeviceIsMigDeviceHandle",
lambda: pynvml.nvmlDeviceIsMigDeviceHandle(device),
)
test_api("nvmlDeviceGetMigMode", lambda: pynvml.nvmlDeviceGetMigMode(device))
test_api(
"nvmlDeviceGetMaxMigDeviceCount",
lambda: pynvml.nvmlDeviceGetMaxMigDeviceCount(device),
)
def test_topology_apis(self, devices):
if len(devices) < 2:
print_section("Topology APIs (Skipped - need 2+ devices)")
return
print_section("Topology APIs")
dev1, dev2 = devices[0], devices[1]
test_api(
"nvmlDeviceGetP2PStatus",
lambda: pynvml.nvmlDeviceGetP2PStatus(
dev1, dev2, pynvml.NVML_P2P_CAPS_INDEX_READ
),
)
test_api(
"nvmlDeviceGetTopologyCommonAncestor",
lambda: pynvml.nvmlDeviceGetTopologyCommonAncestor(dev1, dev2),
)
def test_nvlink_apis(self, device):
print_section("NVLink APIs")
test_api(
"nvmlDeviceGetNvLinkState (link 0)",
lambda: pynvml.nvmlDeviceGetNvLinkState(device, 0),
)
test_api(
"nvmlDeviceGetNvLinkCapability (link 0)",
lambda: pynvml.nvmlDeviceGetNvLinkCapability(device, 0, 0),
)
test_api(
"nvmlDeviceGetNvLinkRemotePciInfo (link 0)",
lambda: pynvml.nvmlDeviceGetNvLinkRemotePciInfo(device, 0),
)
def test_reinit_cycle(self):
"""Test that init/shutdown/init cycle works correctly"""
print_section("Reinit Cycle Test")
try:
pynvml.nvmlShutdown()
print_result("First shutdown", "OK")
pynvml.nvmlInit()
print_result("Second init", "OK")
count = pynvml.nvmlDeviceGetCount()
print_result("Device count after reinit", count)
return True
except Exception as e:
print_result("Reinit cycle", f"[Error: {e}]")
return False
def run_all_tests(self):
print("\n" + "=" * 60)
print(" pynvml-equivalent APIs Test Suite")
print("=" * 60)
# Test library APIs
self.test_library_apis()
# Get device handles
device = self.test_device_handle_apis()
if device is None:
print("\nNo device available, skipping device tests")
return
# Collect all devices for topology tests
device_count = pynvml.nvmlDeviceGetCount()
devices = [pynvml.nvmlDeviceGetHandleByIndex(i) for i in range(device_count)]
# Run device tests
self.test_device_info_apis(device)
self.test_memory_apis(device)
self.test_utilization_apis(device)
self.test_clock_apis(device)
self.test_temperature_power_apis(device)
self.test_fan_apis(device)
self.test_pci_apis(device)
self.test_compute_apis(device)
self.test_ecc_apis(device)
self.test_mode_apis(device)
self.test_mig_apis(device)
self.test_nvlink_apis(device)
self.test_topology_apis(devices)
# Test reinit cycle
self.test_reinit_cycle()
print_section("Test Complete")
def main():
try:
# Initialize library
print("Initializing NVML (via pymtml)...")
pynvml.nvmlInit()
# Run tests
suite = PynvmlTestSuite()
suite.run_all_tests()
except pynvml.NVMLError as e:
print(f"NVML Error: {e}")
traceback.print_exc()
return 1
except Exception as e:
print(f"Error: {e}")
traceback.print_exc()
return 1
finally:
# Shutdown
try:
pynvml.nvmlShutdown()
print("\nNVML shutdown complete.")
except Exception:
pass
return 0
if __name__ == "__main__":
sys.exit(main())