Files
DeepInfer/tests/test_hardware_device.py
T
shunyadandClaude Opus 5.5 05cd50f21a fix(hardware): resolve MIG entries and ambiguous device order in compute_capability
compute_capability() only understood digit indices and GPU- UUIDs, so the first
CUDA_VISIBLE_DEVICES entry being a MIG instance made is_sm_major_10/12 return
False: SM120 then imported flash_mla/deep_select (not built there) and SM100
lost its GPU-type-dependent paths. Unresolvable entries also silently read as
"not this architecture".

- Resolve "MIG-<uuid>" (via NVML's MIG lookups) and the legacy
  "MIG-GPU-<uuid>/<gi>/<ci>" form to the capability of the physical GPU.
- Raise LookupError when NVML is present but the entry names no GPU, instead
  of returning None.
- Digit entries are NVML (PCI bus) indices, but CUDA orders devices fastest
  first unless CUDA_DEVICE_ORDER=PCI_BUS_ID. On hosts whose GPUs differ in
  capability that is ambiguous, so ask for CUDA_DEVICE_ORDER=PCI_BUS_ID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 22:22:14 -08:00

144 lines
5.6 KiB
Python

# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
from collections.abc import Generator
import pytest
from deep_cluster.hardware import device, nvidia
@pytest.fixture
def capability(monkeypatch: pytest.MonkeyPatch) -> Generator[list[tuple[int, int] | None]]:
"""Pretend to be an NVIDIA host whose first visible GPU reports the capability stored in the returned cell."""
cell: list[tuple[int, int] | None] = [None]
monkeypatch.setattr(device, "accelerator_kind", lambda **_: "nvidia")
monkeypatch.setattr(nvidia, "compute_capability", lambda **_: cell[0])
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0")
caches = (device.is_sm_major_10, device.is_sm_major_12, device.visible_device_env_name)
for fn in caches:
fn.cache_clear()
yield cell
for fn in caches:
fn.cache_clear()
@pytest.mark.parametrize(
("cap", "sm100", "sm120"),
[((10, 0), True, False), ((10, 3), True, False), ((12, 0), False, True), ((9, 0), False, False), (None, False, False)],
)
def test_sm_major_detection(capability: list[tuple[int, int] | None], cap: tuple[int, int] | None, sm100: bool, sm120: bool) -> None:
capability[0] = cap
assert device.is_sm_major_10() is sm100
assert device.is_sm_major_12() is sm120
def test_bf16_tflops_limit_known_for_sm120(capability: list[tuple[int, int] | None]) -> None:
capability[0] = (12, 0)
assert device.get_bf16_tflops_limit() > 0
def test_bf16_tflops_limit_rejects_unknown_gpu(capability: list[tuple[int, int] | None]) -> None:
capability[0] = (9, 0)
with pytest.raises(RuntimeError, match="unknown GPU type"):
device.get_bf16_tflops_limit()
@pytest.mark.parametrize(("visible", "probed"), [("3,1", ["3"]), ("GPU-1234abcd,0", ["GPU-1234abcd"]), ("", [])])
def test_sm_major_probes_first_visible_device(
capability: list[tuple[int, int] | None], monkeypatch: pytest.MonkeyPatch, visible: str, probed: list[str]
) -> None:
seen: list[int | str] = []
def fake_capability(*, index: int | str = 0) -> tuple[int, int]:
seen.append(index)
return (12, 0)
monkeypatch.setattr(nvidia, "compute_capability", fake_capability)
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", visible)
assert device.is_sm_major_12() is bool(probed)
assert seen == probed
class _FakeNvml:
"""Just enough of libnvidia-ml for nvidia._resolve_handle / compute_capability: GPUs by index with a UUID and a
capability, plus MIG instances (by UUID) that live on one of them."""
def __init__(self, gpus: list[tuple[str, tuple[int, int]]], migs: dict[str, int] | None = None) -> None:
self.gpus = gpus
self.migs = migs or {}
def nvmlDeviceGetCount_v2(self, count: object) -> int:
count._obj.value = len(self.gpus)
return 0
def nvmlDeviceGetHandleByIndex_v2(self, index: int, handle: object) -> int:
if index >= len(self.gpus):
return 1
handle._obj.value = 1000 + index
return 0
def nvmlDeviceGetUUID(self, handle: object, buffer: object, _length: int) -> int:
buffer.value = self.gpus[handle.value - 1000][0].encode()
return 0
def nvmlDeviceGetHandleByUUID(self, uuid: bytes, handle: object) -> int:
if uuid.decode() not in self.migs:
return 1
handle._obj.value = 2000 + self.migs[uuid.decode()]
return 0
def nvmlDeviceGetDeviceHandleFromMigDeviceHandle(self, mig: object, parent: object) -> int:
parent._obj.value = 1000 + (mig.value - 2000)
return 0
def nvmlDeviceGetCudaComputeCapability(self, handle: object, major: object, minor: object) -> int:
major._obj.value, minor._obj.value = self.gpus[handle.value - 1000][1]
return 0
@pytest.fixture
def fake_nvml(monkeypatch: pytest.MonkeyPatch) -> Generator[list[_FakeNvml]]:
cell: list[_FakeNvml] = []
monkeypatch.setattr(nvidia, "_nvml", lambda: cell[0])
monkeypatch.delenv("CUDA_DEVICE_ORDER", raising=False)
nvidia.compute_capability.cache_clear()
yield cell
nvidia.compute_capability.cache_clear()
_SM120_HOST = [("GPU-aaaa1111", (12, 0)), ("GPU-bbbb2222", (12, 0))]
@pytest.mark.parametrize(
("entry", "expected"),
[
("1", (12, 0)),
("GPU-bbbb", (12, 0)),
("MIG-0f0f0f0f-1111-2222-3333-444444444444", (12, 0)),
("MIG-GPU-aaaa1111/1/0", (12, 0)),
],
)
def test_compute_capability_resolves_visible_device_entries(fake_nvml: list[_FakeNvml], entry: str, expected: tuple[int, int]) -> None:
fake_nvml.append(_FakeNvml(_SM120_HOST, migs={"MIG-0f0f0f0f-1111-2222-3333-444444444444": 1}))
assert nvidia.compute_capability(index=entry) == expected
@pytest.mark.parametrize("entry", ["2", "GPU-cccc", "MIG-unknown", "bogus"])
def test_compute_capability_rejects_unknown_entries(fake_nvml: list[_FakeNvml], entry: str) -> None:
fake_nvml.append(_FakeNvml(_SM120_HOST))
with pytest.raises(LookupError, match="matches no NVIDIA GPU"):
nvidia.compute_capability(index=entry)
def test_compute_capability_digit_needs_pci_order_on_mixed_hosts(fake_nvml: list[_FakeNvml], monkeypatch: pytest.MonkeyPatch) -> None:
fake_nvml.append(_FakeNvml([("GPU-aaaa1111", (10, 0)), ("GPU-bbbb2222", (12, 0))]))
with pytest.raises(LookupError, match="CUDA_DEVICE_ORDER=PCI_BUS_ID"):
nvidia.compute_capability(index="1")
assert nvidia.compute_capability(index="GPU-bbbb") == (12, 0)
nvidia.compute_capability.cache_clear()
monkeypatch.setenv("CUDA_DEVICE_ORDER", "PCI_BUS_ID")
assert nvidia.compute_capability(index="1") == (12, 0)