Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pynvml compatibility #8981

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading