[BUG] DeviceContext misses healthy GPUs with legacy NVML handle lookup
Bug description
Actual behavior
I hit this while running Mojo GPU tests on an eight-card RTX 4090D machine. One card returns NVML error 999, but the card selected for the test is healthy. CUDA sees that selected card and reports the same PCI location as nvmlDeviceGetHandleByIndex_v2. Creating a DeviceContext still fails.
Unhandled exception caught during execution: At max/mojo/max/gpu/host/device_context.mojo:4010:17: NVML: no device at the PCI location CUDA reports for device 0
I narrowed it down to a difference between the legacy and v2 NVML handle lookups. nvmlDeviceGetCount_v2 returns 8. With that index range, the legacy nvmlDeviceGetHandleByIndex succeeds only for index 0 and returns 999 for indices 1–7. nvmlDeviceGetHandleByIndex_v2 succeeds for 0 and 2–7, including the selected card at index 6. Only index 1 fails with v2.
A process-local probe confirms that the runtime requests the legacy handle symbol through dlsym. I also found nvmlDeviceGetCount_v2 among the lookup names in its runtime libraries.
Expected behavior
DeviceContext() should find the healthy CUDA-selected card through the corresponding v2 NVML enumeration, even when another card reports an error.
Steps to reproduce
This small program reproduces it during device creation.
from max.gpu.host import DeviceContext
def main() raises:
var context = DeviceContext()
print(context.name())
I built it with Mojo 1.1.0, targeting haswell for the host and sm_89 for the GPU, then ran it with the healthy card selected by UUID through CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER=PCI_BUS_ID. It exits with the error above. Setting MODULAR_DEVICE_CONTEXT_DISABLE_HEALTHCHECK=true gives the same failure.
mojo build --target-cpu haswell --target-accelerator sm_89 repro.mojo -o repro
healthy_gpu_uuid="$(nvidia-smi -i 6 --query-gpu=uuid --format=csv,noheader)"
CUDA_VISIBLE_DEVICES="$healthy_gpu_uuid" CUDA_DEVICE_ORDER=PCI_BUS_ID ./repro
The following Python check shows the enumeration difference on the same machine.
import ctypes as c
nvml = c.CDLL("libnvidia-ml.so.1")
assert nvml.nvmlInit_v2() == 0
count = c.c_uint()
assert nvml.nvmlDeviceGetCount_v2(c.byref(count)) == 0
for index in range(count.value):
results = []
for name in ("nvmlDeviceGetHandleByIndex", "nvmlDeviceGetHandleByIndex_v2"):
handle = c.c_void_p()
status = getattr(nvml, name)(c.c_uint(index), c.byref(handle))
results.append(status)
print(index, *results)
nvml.nvmlShutdown()
0 0 0
1 999 999
2 999 0
3 999 0
4 999 0
5 999 0
6 999 0
7 999 0
To test that connection, I used a process-local LD_PRELOAD library that redirects the exact dlsym request for nvmlDeviceGetHandleByIndex to nvmlDeviceGetHandleByIndex_v2 on the original NVML library handle. It calls the original dlsym for other symbols. With that redirect, the minimal program prints NVIDIA GeForce RTX 4090 D and exits successfully. The same redirect also changes our GPU test suite from 3 passing / 35 failing at DeviceContext creation to 38 passing / 0 failing, with the default healthcheck enabled.
NVIDIA documents GetHandleByIndex_v2 as the companion to GetCount_v2 and notes that the legacy and v2 index mappings can differ. That seems consistent with what I am seeing here. The earlier NVML discussion in #6896 also helped me trace the PCI-matching path. NVML device queries · Related discussion
System information
- Mojo 1.1.0 (
8189361e), max-core 26.6.0 - Ubuntu 24.04.5 LTS, x86_64, glibc 2.39
- AMD EPYC 7K62
- Eight NVIDIA GeForce RTX 4090D cards, driver 580.82.09
- Healthy test card at PCI
0000:C1:00.0, selected as CUDA device 0 - Full physical GPUs