Why do Python NVML discovery helpers use nvmlInit rather than nvmlInit_v2?

I am trying to understand an NVML initialization choice in PyTorch after a GPU fault on a shared multi-GPU server.

Technical summary

In this source revision, _raw_device_count_nvml() and _raw_device_uuid_nvml() call nvmlInit() through ctypes.CDLL.

NVIDIA’s documentation describes legacy initialization as initializing all GPUs, whereas nvmlInit_v2() defers initialization until device-handle acquisition. The C header aliases nvmlInit to nvmlInit_v2, but my understanding is that this macro does not apply to a ctypes symbol lookup.

On our server, using PyTorch 2.11.0+cu128 and NVIDIA driver 570.172.08, separate tests of both initialization entry points succeeded and each reported eight devices. We have not established that this API choice caused our incident.

Is use of the legacy symbol intentional, perhaps for compatibility or discovery semantics? Are there reasons these helpers should avoid nvmlInit_v2()?

I am asking for clarification rather than reporting a confirmed PyTorch bug.

From the aicost.tools team, not a PyTorch maintainer, so take this as a reading of the NVML docs rather than an official answer. You’re right that the header macro doesn’t help a ctypes lookup, so nvmlInit resolves to the legacy v1 export the driver keeps for old binaries. The practical difference on a shared box is that v1 initializes every GPU up front, while v2 is documented to keep working when another GPU is unstable or in a bad state, so after a fault the legacy call is the one more likely to fail discovery as a whole. My guess is it’s simply historical, and a small PR that tries nvmlInit_v2 first and falls back to nvmlInit when the symbol is missing would probably be the fastest way to get a maintainer’s view.

Thanks for taking a look and sharing your interpretation. I’ve opened an improvement/design issue to ask the maintainers about the intended behavior: [issue URL].

Both initialization entry points passed our tests, so the connection to our original incident remains unconfirmed. I’ll follow up on GitHub about whether any change is warranted.