Skip to content

Commit

Permalink
pynvml compatibility
Browse files Browse the repository at this point in the history
Seems like some version pynvml switched from returning bytes to str (I
can pin down the exact version if needed). This updates our test and
type annotations to accomodate either.
  • Loading branch information
TomAugspurger committed Jan 9, 2025
1 parent 94222c0 commit d04fdf8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
9 changes: 5 additions & 4 deletions distributed/diagnostics/nvml.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class NVMLState(IntEnum):


class CudaDeviceInfo(NamedTuple):
uuid: bytes | None = None
# Older versions of pynvml returned bytes, newer versions return str.
uuid: str | bytes | None = None
device_index: int | None = None
mig_index: int | None = None

Expand Down Expand Up @@ -278,13 +279,13 @@ def get_device_index_and_uuid(device):
Examples
--------
>>> get_device_index_and_uuid(0) # doctest: +SKIP
{'device-index': 0, 'uuid': b'GPU-e1006a74-5836-264f-5c26-53d19d212dfe'}
{'device-index': 0, 'uuid': 'GPU-e1006a74-5836-264f-5c26-53d19d212dfe'}
>>> get_device_index_and_uuid('GPU-e1006a74-5836-264f-5c26-53d19d212dfe') # doctest: +SKIP
{'device-index': 0, 'uuid': b'GPU-e1006a74-5836-264f-5c26-53d19d212dfe'}
{'device-index': 0, 'uuid': 'GPU-e1006a74-5836-264f-5c26-53d19d212dfe'}
>>> get_device_index_and_uuid('MIG-7feb6df5-eccf-5faa-ab00-9a441867e237') # doctest: +SKIP
{'device-index': 0, 'uuid': b'MIG-7feb6df5-eccf-5faa-ab00-9a441867e237'}
{'device-index': 0, 'uuid': 'MIG-7feb6df5-eccf-5faa-ab00-9a441867e237'}
"""
init_once()
try:
Expand Down
7 changes: 4 additions & 3 deletions distributed/diagnostics/tests/test_nvml.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
pynvml = pytest.importorskip("pynvml")

import dask
from dask.utils import ensure_unicode

from distributed.diagnostics import nvml
from distributed.utils_test import gen_cluster
Expand Down Expand Up @@ -66,7 +67,7 @@ def run_has_cuda_context(queue):
assert (
ctx.has_context
and ctx.device_info.device_index == 0
and isinstance(ctx.device_info.uuid, bytes)
and isinstance(ctx.device_info.uuid, str)
)

queue.put(None)
Expand Down Expand Up @@ -127,7 +128,7 @@ def test_visible_devices_uuid():
assert info.uuid

with mock.patch.dict(
os.environ, {"CUDA_VISIBLE_DEVICES": info.uuid.decode("utf-8")}
os.environ, {"CUDA_VISIBLE_DEVICES": ensure_unicode(info.uuid)}
):
h = nvml._pynvml_handles()
h_expected = pynvml.nvmlDeviceGetHandleByIndex(0)
Expand All @@ -147,7 +148,7 @@ def test_visible_devices_uuid_2(index):
assert info.uuid

with mock.patch.dict(
os.environ, {"CUDA_VISIBLE_DEVICES": info.uuid.decode("utf-8")}
os.environ, {"CUDA_VISIBLE_DEVICES": ensure_unicode(info.uuid)}
):
h = nvml._pynvml_handles()
h_expected = pynvml.nvmlDeviceGetHandleByIndex(index)
Expand Down

0 comments on commit d04fdf8

Please sign in to comment.