From 4451c68cf2cd21d97956c578ce6921231d3a47ba Mon Sep 17 00:00:00 2001 From: Skand Tandon <21158496+skandtandon@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:17:35 +0530 Subject: [PATCH 1/4] test(nvidia): MIG indexes when the card with the largest index is skipped MIG devices are numbered in blocks above the largest card index the detected cards report. A skipped card no longer reports one, so a MIG device on another card must keep the index it has when every card answers. Signed-off-by: Skand Tandon <21158496+skandtandon@users.noreply.github.com> --- .../gpustack_runtime/detector/test_nvidia.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/gpustack_runtime/detector/test_nvidia.py b/tests/gpustack_runtime/detector/test_nvidia.py index 07208a6..23b5545 100644 --- a/tests/gpustack_runtime/detector/test_nvidia.py +++ b/tests/gpustack_runtime/detector/test_nvidia.py @@ -688,6 +688,27 @@ 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] + + @pytest.mark.usefixtures("health_check") def test_detect_info_reports_the_memory_health(fake_nvml): fake_nvml([_FakeDevice(ecc_errors=1)]) From de66a1e8efe05faf876aed88d67b3cba8b3cb599 Mon Sep 17 00:00:00 2001 From: Skand Tandon <21158496+skandtandon@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:17:35 +0530 Subject: [PATCH 2/4] fix(nvidia): keep MIG indexes stable when a card is skipped index_mig_devices starts the MIG blocks above the largest index the detected cards report. When the skipped card was the one with the largest index, every other card's MIG devices moved down by one, so an index handed out before now names a different MIG device. Add a reserved count to index_mig_devices and pass the NVML device count when a card was skipped. With every card detected nothing changes. Signed-off-by: Skand Tandon <21158496+skandtandon@users.noreply.github.com> --- gpustack_runtime/detector/__types__.py | 7 ++++++- gpustack_runtime/detector/nvidia.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) 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..eeb056a 100644 --- a/gpustack_runtime/detector/nvidia.py +++ b/gpustack_runtime/detector/nvidia.py @@ -283,7 +283,12 @@ def detect_info(self) -> Devices | None: ), ) - index_mig_devices(ret, devs_mig_devices, devs_mig_slots) + index_mig_devices( + ret, + devs_mig_devices, + devs_mig_slots, + reserved=dev_count if unqueryable_error else 0, + ) except pynvml.NVMLError: debug_log_exception(logger, "Failed to fetch devices") raise From 4090823efda558b809f75723836b74905e074291 Mon Sep 17 00:00:00 2001 From: Skand Tandon <21158496+skandtandon@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:27:39 +0530 Subject: [PATCH 3/4] test(nvidia): physical MIG indexes when a card cannot be queried With GPUSTACK_RUNTIME_DETECT_PHYSICAL_INDEX_PRIORITY the card indexes are minor numbers and a card without a readable one is already left out, so the NVML device count is not the index namespace. A card that cannot be queried must not move another card's MIG indexes. Signed-off-by: Skand Tandon <21158496+skandtandon@users.noreply.github.com> --- .../gpustack_runtime/detector/test_nvidia.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/gpustack_runtime/detector/test_nvidia.py b/tests/gpustack_runtime/detector/test_nvidia.py index 23b5545..5d5e519 100644 --- a/tests/gpustack_runtime/detector/test_nvidia.py +++ b/tests/gpustack_runtime/detector/test_nvidia.py @@ -709,6 +709,35 @@ def mig_indexes(lost: bool) -> list[int]: 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)]) From d6d1813bb6e64aaf3228874da66fe1d70e5c65a0 Mon Sep 17 00:00:00 2001 From: Skand Tandon <21158496+skandtandon@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:27:39 +0530 Subject: [PATCH 4/4] fix(nvidia): reserve skipped indexes only for enumeration indexes Reported in review: with physical index priority, minors 0 and 1 and a third card without a readable minor, losing card 0 made reserved=3 and moved card 1's MIG indexes from [9, 10] to [10, 11]. Reserve the NVML device count only when cards are indexed by enumeration. Signed-off-by: Skand Tandon <21158496+skandtandon@users.noreply.github.com> --- gpustack_runtime/detector/nvidia.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/gpustack_runtime/detector/nvidia.py b/gpustack_runtime/detector/nvidia.py index eeb056a..e6b6328 100644 --- a/gpustack_runtime/detector/nvidia.py +++ b/gpustack_runtime/detector/nvidia.py @@ -283,12 +283,15 @@ def detect_info(self) -> Devices | None: ), ) - index_mig_devices( - ret, - devs_mig_devices, - devs_mig_slots, - reserved=dev_count if unqueryable_error else 0, - ) + # 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