Commit cf16667
Replace nvgpu with nvidia-ml-py (#2160)
Resolves #2159
* As `nvgpu` is no longer maintained and uses `pynvml`, which directly
tells the user to use `nvidia-ml-py` instead at import
> The pynvml package is deprecated. Please install nvidia-ml-py instead.
If you did not install pynvml directly, please report this to the
maintainers of the package that installed pynvml for you."
drop `nvgpu` and replace its `nvgpu.gpu_info()` call with a single
function using `nvidia-ml-py` (which uses the `pynvml` namespace).
* Place a lower bound on `nvidia-ml-py` of `12.535.77`, which was the
first release to support `nvmlMemory_v2` which properly accounts for
system-reserved memory.
* Remove all mentions of `nvgpu` in other areas of the codebase and
replace them with `nvidia-ml-py`, except for `publications/` as this is
historical information.
- Do not add `nvidia-ml-py` to `dependabot.yml` as pinning this tightly
[will cause installation
issues](https://iscinumpy.dev/post/bound-version-constraints/),
especially with NVIDIA libraries.
---
Example:
```console
$ nvidia-smi --version
NVIDIA-SMI version : 590.48.01
NVML version : 590.48
DRIVER version : 590.48.01
CUDA Version : 13.1
```
* **On `main`** (2471d55)
```console
$ uv venv main
$ . main/bin/activate
$ uv pip install .
$ python
Python 3.13.7 (main, Sep 18 2025, 19:47:49) [Clang 20.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import desc
>>> desc.set_device("gpu")
/tmp/DESC/main/lib/python3.13/site-packages/nvgpu/__init__.py:8: SyntaxWarning: invalid escape sequence '\('
gpu_infos = [re.match('GPU ([0-9]+): (.+?) \(UUID: ([^)]+)\)', gpu) for gpu in gpus]
>>> import os
>>> os.environ["CUDA_VISIBLE_DEVICES"]
'0'
>>>
```
```console
$ python -c 'import nvgpu; print(nvgpu.gpu_info())'
[{'index': '0', 'type': 'NVIDIA GeForce RTX 4060 Laptop GPU', 'uuid': 'GPU-7fef9454-d8d1-86cf-c4b3-e2fd5e35e862', 'mem_used': 8, 'mem_total': 8188, 'mem_used_percent': 0.09770395701025891}]
```
* **This PR**
```console
$ uv venv
$ . .venv/bin/activate
$ uv pip install .
$ python
Python 3.13.7 (main, Sep 18 2025, 19:47:49) [Clang 20.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import desc
>>> desc.set_device("gpu")
>>> import os
>>> os.environ["CUDA_VISIBLE_DEVICES"]
'0'
>>>
```
As I made it a guarded import I can't directly import it from `desc`,
but is the same code
```python
# _implementation.py
from pynvml import (
nvmlDeviceGetCount,
nvmlDeviceGetHandleByIndex,
nvmlDeviceGetMemoryInfo,
nvmlDeviceGetName,
nvmlDeviceGetUUID,
nvmlInit,
nvmlMemory_v2,
nvmlShutdown,
)
def _gpu_info():
"""Equivalent to nvgpu.gpu_info() using nvidia-ml-py."""
nvmlInit()
try:
info = []
for device_idx in range(nvmlDeviceGetCount()):
handle = nvmlDeviceGetHandleByIndex(device_idx)
mem = nvmlDeviceGetMemoryInfo(handle, version=nvmlMemory_v2)
_bytes_to_mib = 1024 * 1024
mem_used = mem.used // _bytes_to_mib
mem_total = mem.total // _bytes_to_mib
info.append(
{
"index": str(device_idx),
"type": nvmlDeviceGetName(handle),
"uuid": nvmlDeviceGetUUID(handle),
"mem_used": mem_used,
"mem_total": mem_total,
"mem_used_percent": 100.0 * mem_used / mem_total,
}
)
return info
finally:
nvmlShutdown()
if __name__ == "__main__":
print(_gpu_info())
```
so
```console
$ python ./_implementation.py
[{'index': '0', 'type': 'NVIDIA GeForce RTX 4060 Laptop GPU', 'uuid': 'GPU-7fef9454-d8d1-86cf-c4b3-e2fd5e35e862', 'mem_used': 7, 'mem_total': 8188, 'mem_used_percent': 0.08549096238397655}]
```
So the memory consumption is effectively the same (good), and an
unmaintained dependency can be replaced with a maintained one.
---------
Co-authored-by: Daniel Dudt <33005725+ddudt@users.noreply.github.com>1 parent 3314e9a commit cf16667
4 files changed
Lines changed: 41 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | 32 | | |
34 | 33 | | |
35 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 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 | + | |
94 | 130 | | |
95 | 131 | | |
96 | | - | |
| 132 | + | |
97 | 133 | | |
98 | 134 | | |
99 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
0 commit comments