diff --git a/gpustack_runtime/detector/__types__.py b/gpustack_runtime/detector/__types__.py index e642f9b..9e32dc0 100644 --- a/gpustack_runtime/detector/__types__.py +++ b/gpustack_runtime/detector/__types__.py @@ -480,6 +480,7 @@ def index_mig_devices( devices: Devices, mig_devices: dict[int, list[dict]], slots: int, + reserved: int = 0, ) -> None: """ Number the given cards' MIG devices in place. @@ -503,9 +504,13 @@ def index_mig_devices( found at, which the block offset is added to. slots: The number of MIG devices a card can host, i.e. the block size. + reserved: + The number of card indexes taken even if no detected card reports + them, e.g. by a card the detector skipped, so that skipping the + card with the largest index does not renumber the MIG devices. """ - base = max((dev.index for dev in devices), default=-1) + 1 + base = max(max((dev.index for dev in devices), default=-1) + 1, reserved) for dev_idx, migs in mig_devices.items(): for mig in migs: mig["index"] += base + dev_idx * slots diff --git a/gpustack_runtime/detector/nvidia.py b/gpustack_runtime/detector/nvidia.py index 9ab4ed2..e6b6328 100644 --- a/gpustack_runtime/detector/nvidia.py +++ b/gpustack_runtime/detector/nvidia.py @@ -283,7 +283,15 @@ def detect_info(self) -> Devices | None: ), ) - index_mig_devices(ret, devs_mig_devices, devs_mig_slots) + # A skipped card's enumeration index stays taken. Minor numbers are + # not the enumeration range, so they get no reservation. + reserved = 0 + if ( + unqueryable_error + and not envs.GPUSTACK_RUNTIME_DETECT_PHYSICAL_INDEX_PRIORITY + ): + reserved = dev_count + index_mig_devices(ret, devs_mig_devices, devs_mig_slots, reserved=reserved) except pynvml.NVMLError: debug_log_exception(logger, "Failed to fetch devices") raise diff --git a/tests/gpustack_runtime/detector/test_nvidia.py b/tests/gpustack_runtime/detector/test_nvidia.py index 07208a6..5d5e519 100644 --- a/tests/gpustack_runtime/detector/test_nvidia.py +++ b/tests/gpustack_runtime/detector/test_nvidia.py @@ -688,6 +688,56 @@ def test_detect_info_fails_when_no_card_can_be_queried(fake_nvml): NVIDIADetector().detect_info() +def test_detect_info_keeps_mig_indexes_when_the_last_card_cannot_be_queried( + fake_nvml, +): + # MIG indexes are numbered above the cards' indexes. Skipping the card with + # the largest index must not renumber the MIG devices of the other cards. + def mig_indexes(lost: bool) -> list[int]: + error = {"query_error_code": _FakeNVML.NVML_ERROR_GPU_IS_LOST} if lost else {} + fake_nvml( + [ + _mig_card(), + _FakeDevice(uuid="GPU-1"), + _FakeDevice(uuid="GPU-2", **error), + ], + ) + devices = NVIDIADetector().detect_info() + return [mig["index"] for mig in devices[0].appendix["mig_devices"]] + + assert mig_indexes(lost=False) == [3, 4] + assert mig_indexes(lost=True) == [3, 4] + + +def test_detect_info_keeps_physical_mig_indexes_when_a_card_cannot_be_queried( + fake_nvml, + monkeypatch, +): + # With minor numbers as indexes, the card count is not the index namespace: + # a card without a readable minor number is already left out. + monkeypatch.setattr( + nvidia.envs, + "GPUSTACK_RUNTIME_DETECT_PHYSICAL_INDEX_PRIORITY", + True, + raising=False, + ) + + def mig_indexes(lost: bool) -> list[int]: + error = {"query_error_code": _FakeNVML.NVML_ERROR_GPU_IS_LOST} if lost else {} + fake_nvml( + [ + _FakeDevice(uuid="GPU-A", minor_number=0, **error), + _mig_card(minor_number=1), + _FakeDevice(uuid="GPU-C", minor_number=None), + ], + ) + devices = NVIDIADetector().detect_info() + card = next(dev for dev in devices if dev.index == 1) + return [mig["index"] for mig in card.appendix["mig_devices"]] + + assert mig_indexes(lost=True) == mig_indexes(lost=False) + + @pytest.mark.usefixtures("health_check") def test_detect_info_reports_the_memory_health(fake_nvml): fake_nvml([_FakeDevice(ecc_errors=1)])