Skip to content
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
97 changes: 79 additions & 18 deletions gpustack_runtime/detector/nvidia.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def detect_info(self) -> Devices | None:
return None

ret: Devices = []
unqueryable_error: pynvml.NVMLError | None = None

try:
pynvml.nvmlInit()
Expand Down Expand Up @@ -128,19 +129,36 @@ def detect_info(self) -> Devices | None:

dev_count = pynvml.nvmlDeviceGetCount()
for dev_idx in range(dev_count):
dev = pynvml.nvmlDeviceGetHandleByIndex(dev_idx)
# Every other query of a card below degrades on its own; these
# do not. A card that cannot answer them, for example one that
# has fallen off the bus, is skipped instead of failing the
# detection of every other card.
try:
dev = pynvml.nvmlDeviceGetHandleByIndex(dev_idx)
dev_cc_t = pynvml.nvmlDeviceGetCudaComputeCapability(dev)
# Unversioned on purpose, unlike the memory query above: pynvml
# exposes no v2 PCI accessor to prefer or fall back to --
# `nvmlDeviceGetPciInfo` is its alias of `nvmlDeviceGetPciInfo_v3`,
# a superset of the v2 structure the operator's `GetPciInfoV`
# prefers, and `nvmlPciInfo_v2_t` is declared but unused. The
# operator never reads the structure's string either, its
# `GetBusId()` formatting the BDF from domain/bus/device.
dev_pci_info = pynvml.nvmlDeviceGetPciInfo(dev)
dev_name = pynvml.nvmlDeviceGetName(dev)
dev_uuid = pynvml.nvmlDeviceGetUUID(dev)
except pynvml.NVMLError as e:
if not _is_unqueryable_device_error(e):
raise
debug_log_warning(
logger,
"Failed to query device %d, skipping it: %s",
dev_idx,
e,
)
unqueryable_error = e
continue

dev_cc_t = pynvml.nvmlDeviceGetCudaComputeCapability(dev)
dev_cc = ".".join(map(str, dev_cc_t))

# Unversioned on purpose, unlike the memory query above: pynvml
# exposes no v2 PCI accessor to prefer or fall back to --
# `nvmlDeviceGetPciInfo` is its alias of `nvmlDeviceGetPciInfo_v3`,
# a superset of the v2 structure the operator's `GetPciInfoV`
# prefers, and `nvmlPciInfo_v2_t` is declared but unused. The
# operator never reads the structure's string either, its
# `GetBusId()` formatting the BDF from domain/bus/device.
dev_pci_info = pynvml.nvmlDeviceGetPciInfo(dev)
dev_bdf = str(dev_pci_info.busIdLegacy).lower()

dev_numa = get_numa_node_by_bdf(dev_bdf)
Expand Down Expand Up @@ -190,10 +208,6 @@ def detect_info(self) -> Devices | None:
# in this inventory. A MIG-enabled card is marked ``mig``
# in the appendix instead.

dev_name = pynvml.nvmlDeviceGetName(dev)

dev_uuid = pynvml.nvmlDeviceGetUUID(dev)

dev_cores = None
with contextlib.suppress(pynvml.NVMLError):
dev_cores = pynvml.nvmlDeviceGetNumGpuCores(dev)
Expand Down Expand Up @@ -277,6 +291,11 @@ def detect_info(self) -> Devices | None:
debug_log_exception(logger, "Failed to process devices fetching")
raise

if not ret and unqueryable_error is not None:
# No card could be queried: fail as before, rather than report a
# host without cards.
raise unqueryable_error

return ret

def detect_usage(self, devices: Devices | None = None) -> Devices | None:
Expand Down Expand Up @@ -310,15 +329,29 @@ def detect_usage(self, devices: Devices | None = None) -> Devices | None:
# metrics list is joined by device identity, never by index, as an index
# is not stable across a re-detection.
usages: Devices = []
unqueryable_error: pynvml.NVMLError | None = None

try:
pynvml.nvmlInit()

dev_count = pynvml.nvmlDeviceGetCount()
for dev_idx in range(dev_count):
dev = pynvml.nvmlDeviceGetHandleByIndex(dev_idx)

dev_uuid = pynvml.nvmlDeviceGetUUID(dev)
# As in detect_info: a card that cannot be queried is skipped,
# so the other cards keep their usage.
try:
dev = pynvml.nvmlDeviceGetHandleByIndex(dev_idx)
dev_uuid = pynvml.nvmlDeviceGetUUID(dev)
except pynvml.NVMLError as e:
if not _is_unqueryable_device_error(e):
raise
debug_log_warning(
logger,
"Failed to query device %d usage, skipping it: %s",
dev_idx,
e,
)
unqueryable_error = e
continue
Comment thread
skandtandon marked this conversation as resolved.
Resolved
Comment thread
thxCode marked this conversation as resolved.
Resolved

dev_cores_util = _get_sm_util_from_gpm_metrics(dev)
if dev_cores_util is None:
Expand Down Expand Up @@ -391,6 +424,10 @@ def detect_usage(self, devices: Devices | None = None) -> Devices | None:
debug_log_exception(logger, "Failed to process devices usage fetching")
raise

if not usages and unqueryable_error is not None:
# As in detect_info: no card could be queried, so fail as before.
raise unqueryable_error

return merge_devices_usage(devices, usages)

def get_topology(self, devices: Devices | None = None) -> Topology | None:
Expand Down Expand Up @@ -491,6 +528,30 @@ def get_topology(self, devices: Devices | None = None) -> Topology | None:
return ret


def _is_unqueryable_device_error(e: pynvml.NVMLError) -> bool:
"""
Whether an NVML error means the card itself cannot be queried: it has
fallen off the bus (NVML_ERROR_GPU_IS_LOST, often reported as
NVML_ERROR_UNKNOWN), needs a reset, lacks power, or has an interrupt
problem. Other errors concern the driver or the call, not one card.

Args:
e:
The NVML error to classify.

Returns:
True if the error is specific to the card being queried.

"""
return getattr(e, "value", None) in (
pynvml.NVML_ERROR_GPU_IS_LOST,
pynvml.NVML_ERROR_UNKNOWN,
pynvml.NVML_ERROR_RESET_REQUIRED,
pynvml.NVML_ERROR_INSUFFICIENT_POWER,
pynvml.NVML_ERROR_IRQ_ISSUE,
)


def _get_memory_info(
dev: pynvml.c_nvmlDevice_t,
) -> tuple[int, int]:
Expand Down
110 changes: 109 additions & 1 deletion tests/gpustack_runtime/detector/test_nvidia.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ class _FakeDevice:
verdict that names the aggregate counter cannot be satisfied by the
volatile one.
"""
query_error_code: int | None = None
"""
The error code the card's identity query raises, as a card that has fallen
off the bus does, or None for a card that answers.
"""
query_error_at: str = "handle"
"""
The identity query that raises query_error_code: "handle" or "name".
"""
ecc_counter_error_code: int | None = None
"""
The error code the ECC counter query raises, or None for a readable one.
Expand Down Expand Up @@ -142,6 +151,15 @@ class _FakeDevice:
"""


def _raise_query_error(device: _FakeDevice, query: str):
"""
Raise the card's query error, if it has one for this query.
"""
if device.query_error_code is not None and device.query_error_at == query:
msg = f"device query failed at {query}"
raise _NVMLError(msg, device.query_error_code)


class _FakeNVML:
"""
A pynvml stand-in exposing only what the NVIDIA detector touches, recording
Expand All @@ -151,6 +169,11 @@ class _FakeNVML:
NVMLError = _NVMLError
NVML_SUCCESS = 0
NVML_ERROR_NOT_SUPPORTED = 3
NVML_ERROR_NO_PERMISSION = 4
NVML_ERROR_INSUFFICIENT_POWER = 8
NVML_ERROR_IRQ_ISSUE = 11
NVML_ERROR_GPU_IS_LOST = 15
NVML_ERROR_RESET_REQUIRED = 16
NVML_ERROR_UNKNOWN = 999
NVML_VALUE_TYPE_DOUBLE = 0
NVML_VALUE_TYPE_UNSIGNED_INT = 1
Expand Down Expand Up @@ -235,7 +258,9 @@ def nvmlDeviceGetCount(self): # noqa: N802

def nvmlDeviceGetHandleByIndex(self, index): # noqa: N802
self.calls.append("nvmlDeviceGetHandleByIndex")
return self.devices[index]
device = self.devices[index]
_raise_query_error(device, "handle")
return device

# Identity and capability.

Expand All @@ -245,6 +270,7 @@ def nvmlDeviceGetUUID(self, handle): # noqa: N802

def nvmlDeviceGetName(self, handle): # noqa: N802
self.calls.append("nvmlDeviceGetName")
_raise_query_error(handle, "name")
return handle.name

def nvmlDeviceGetCudaComputeCapability(self, handle): # noqa: N802
Expand Down Expand Up @@ -609,6 +635,59 @@ def test_detect_info_physical_index_priority_skips_unreadable_minor_number(
]


@pytest.mark.parametrize(
"error_code, query",
[
(_FakeNVML.NVML_ERROR_GPU_IS_LOST, "handle"),
(_FakeNVML.NVML_ERROR_UNKNOWN, "handle"),
(_FakeNVML.NVML_ERROR_RESET_REQUIRED, "handle"),
(_FakeNVML.NVML_ERROR_INSUFFICIENT_POWER, "handle"),
(_FakeNVML.NVML_ERROR_IRQ_ISSUE, "handle"),
(_FakeNVML.NVML_ERROR_GPU_IS_LOST, "name"),
],
)
def test_detect_info_skips_a_card_that_cannot_be_queried(fake_nvml, error_code, query):
fake_nvml(
[
_FakeDevice(uuid="GPU-0"),
_FakeDevice(
uuid="GPU-1",
query_error_code=error_code,
query_error_at=query,
),
_FakeDevice(uuid="GPU-2"),
],
)

devices = NVIDIADetector().detect_info()

assert [(dev.uuid, dev.index) for dev in devices] == [("GPU-0", 0), ("GPU-2", 2)]


def test_detect_info_fails_on_another_query_error(fake_nvml):
fake_nvml(
[
_FakeDevice(uuid="GPU-0"),
_FakeDevice(
uuid="GPU-1",
query_error_code=_FakeNVML.NVML_ERROR_NO_PERMISSION,
),
],
)

with pytest.raises(_NVMLError):
NVIDIADetector().detect_info()


def test_detect_info_fails_when_no_card_can_be_queried(fake_nvml):
fake_nvml(
[_FakeDevice(query_error_code=_FakeNVML.NVML_ERROR_GPU_IS_LOST)],
)

with pytest.raises(_NVMLError):
NVIDIADetector().detect_info()


@pytest.mark.usefixtures("health_check")
def test_detect_info_reports_the_memory_health(fake_nvml):
fake_nvml([_FakeDevice(ecc_errors=1)])
Expand Down Expand Up @@ -855,6 +934,35 @@ def test_detect_usage_merges_into_the_given_devices(fake_nvml):
assert [device.memory for device in devices] == [23034] * 2


def test_detect_usage_skips_a_card_that_cannot_be_queried(fake_nvml):
fake = fake_nvml(
[
_FakeDevice(uuid="GPU-0"),
_FakeDevice(uuid="GPU-1"),
_FakeDevice(uuid="GPU-2"),
],
)
detector = NVIDIADetector()
devices = detector.detect_info()
# The card falls off the bus between the two queries.
fake.devices[1].query_error_code = _FakeNVML.NVML_ERROR_GPU_IS_LOST

detector.detect_usage(devices)

assert [device.temperature for device in devices] == [47, None, 47]


def test_detect_usage_fails_when_no_card_can_be_queried(fake_nvml):
fake = fake_nvml([_FakeDevice(uuid="GPU-0"), _FakeDevice(uuid="GPU-1")])
detector = NVIDIADetector()
devices = detector.detect_info()
for device in fake.devices:
device.query_error_code = _FakeNVML.NVML_ERROR_GPU_IS_LOST

with pytest.raises(_NVMLError):
detector.detect_usage(devices)


def test_detect_usage_detects_the_information_first(fake_nvml):
fake_nvml([_FakeDevice()])

Expand Down
Loading