Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: stop rejecting --mmproj-device CUDA1 when gpu_ids are saved (#12505)

Stefan Wanga91ee3789336
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index df2474e520e..2e6fdec3813 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -6343,7 +6343,7 @@ def _widen_pin_ids_for_companion_devices(
pin_ids: list[int],
inherited_ids: Optional[list[int]],
*,
- may_widen: bool = True,
+ allowed_ids: Optional[Iterable[int]] = None,
) -> tuple[list[int], str]:
"""Fit a pinned GPU mask to the companion devices the argv names (#11810).
@@ -6351,12 +6351,14 @@ def _widen_pin_ids_for_companion_devices(
``inherited_ids``, else the physical index). Cards they name are appended after the
pinned ones, the winning flag is renumbered to the child's mask, and a main
``--device`` is added so ``-ngl -1`` does not spread over the extra cards. Tokens
- the parent cannot see are left alone. ``may_widen`` False (explicit gpu_ids) only
- renumbers within the pin. Returns the mask and a log note, empty when unchanged.
+ the parent cannot see are left alone. With ``allowed_ids`` (explicit gpu_ids) the
+ mask only widens onto those cards. Returns the mask and a log note, empty when
+ unchanged.
"""
if not pin_ids:
return list(pin_ids), ""
main_ids = [int(i) for i in pin_ids]
+ allowed = None if allowed_ids is None else {int(i) for i in allowed_ids}
# A main --device past the pinned positions would resolve to a widened companion card.
for token in str(_extra_args_main_device(cmd) or "").split(","):
match = _GPU_DEVICE_TOKEN_RE.match(token.strip())
@@ -6390,7 +6392,7 @@ def _widen_pin_ids_for_companion_devices(
physical = n
elif n < len(inherited_ids):
physical = int(inherited_ids[n])
- if not may_widen and physical not in main_ids:
+ if allowed is not None and physical not in main_ids and physical not in allowed:
physical = None
tokens.append((token, physical))
if any(physical is not None for _, physical in tokens):
@@ -8573,6 +8575,17 @@ class LlamaCppBackend:
effective = self._gpu_ids or None
return requested == raw or requested == effective
+ def _adopt_widened_pin(self, pin_ids: List[int]) -> None:
+ """Record a companion-widened explicit pin as the effective one (#12467).
+
+ The effective pin is recorded before the mask widens. /status echoes it and
+ dedupe adopts a request carrying it, so the narrower pin would come back as
+ the stored intent and reload without the companion card. Only an already
+ recorded pin is replaced: a launch that recorded none (forced CPU) stays None.
+ """
+ if self._gpu_ids is not None:
+ self._gpu_ids = [int(i) for i in pin_ids]
+
def _record_matching_gpu_request(self, gpu_ids: Optional[List[int]]) -> None:
"""Adopt the caller's explicit pool after a full already-loaded match.
@@ -29758,12 +29771,14 @@ class LlamaCppBackend:
cmd,
_pin_ids,
self._resolve_visible_physical_ids(),
- may_widen = not gpu_ids,
+ allowed_ids = gpu_ids or None,
)
if _companion_widen:
_companion_fit_mask = list(_pin_ids)
if not _had_main_device and _extra_args_main_device(cmd) is not None:
_companion_added_device = list(cmd[-2:])
+ if gpu_ids:
+ self._adopt_widened_pin(_pin_ids)
if _companion_widen:
logger.info(
"Companion device flags name GPUs by their unpinned "
@@ -31022,7 +31037,7 @@ class LlamaCppBackend:
cmd,
list(_remaining),
list(_companion_fit_mask),
- may_widen = not gpu_ids,
+ allowed_ids = gpu_ids or None,
)
_companion_added_device = (
list(cmd[-2:])
diff --git a/studio/backend/tests/test_companion_device_widening.py b/studio/backend/tests/test_companion_device_widening.py
index 51e554cbaeb..e75ba525a66 100644
--- a/studio/backend/tests/test_companion_device_widening.py
+++ b/studio/backend/tests/test_companion_device_widening.py
@@ -13,11 +13,11 @@ def _widen(
pin_ids,
cmd,
inherited = None,
- may_widen = True,
+ allowed_ids = None,
):
cmd = list(cmd)
widened, note = llama_cpp._widen_pin_ids_for_companion_devices(
- cmd, pin_ids, inherited, may_widen = may_widen
+ cmd, pin_ids, inherited, allowed_ids = allowed_ids
)
return cmd, widened, note
@@ -141,18 +141,54 @@ def test_load_model_uses_the_argv_and_skips_an_unmappable_mask():
window = src[at - 400 : at + 200]
assert "_visibility_mask_is_unmappable()" in window
assert 'env.get("LLAMA_ARG_DEVICE", "")' in window
- assert "may_widen = not gpu_ids" in src[at : at + 300]
+ assert "allowed_ids = gpu_ids or None" in src[at : at + 300]
-def test_an_explicit_gpu_ids_pin_is_never_widened_onto_another_card():
- cmd, pin, note = _widen([0], ["--mmproj-device", "CUDA1"], may_widen = False)
+def test_a_widened_explicit_pin_is_what_status_and_dedupe_see():
+ # #12467: gpu_ids [0, 1] fitted to [0] then widened for the projector. The effective
+ # pin was recorded before widening, so /status echoed [0]; a client re-sending that
+ # matched, became the stored intent, and the replayed load lost GPU 1 again.
+ import inspect
+
+ src = inspect.getsource(llama_cpp.LlamaCppBackend.load_model)
+ at = src.index("_widen_pin_ids_for_companion_devices(")
+ window = src[at : at + 900]
+ assert "if gpu_ids:" in window
+ assert "self._adopt_widened_pin(_pin_ids)" in window
+
+ backend = llama_cpp.LlamaCppBackend.__new__(llama_cpp.LlamaCppBackend)
+ backend._is_diffusion = False
+ backend._requested_gpu_ids, backend._gpu_ids = [0, 1], [0]
+ backend._adopt_widened_pin([0, 1])
+ assert backend._gpu_ids == [0, 1]
+ assert backend.matches_gpu_ids([0, 1]) is True
+ # Dropping the projector's card is a real placement change, so it reloads.
+ assert backend.matches_gpu_ids([0]) is False
+
+ # A launch that recorded no effective pin (forced CPU) keeps recording none.
+ backend._gpu_ids = None
+ backend._adopt_widened_pin([0, 1])
+ assert backend._gpu_ids is None
+
+
+def test_explicit_gpu_ids_never_widen_onto_an_unselected_card():
+ cmd, pin, note = _widen([0], ["--mmproj-device", "CUDA1"], allowed_ids = [0])
assert pin == [0]
assert cmd == ["--mmproj-device", "CUDA1"]
assert note == ""
+def test_explicit_gpu_ids_widen_onto_a_selected_card_the_fit_left_out():
+ # #12467: gpu_ids [0, 1] saved, the fit chose [0], the projector names GPU 1
+ cmd, pin, note = _widen([0], ["--mmproj-device", "CUDA1"], allowed_ids = [0, 1])
+ assert pin == [0, 1]
+ assert _value(cmd, "--mmproj-device") == "CUDA1"
+ assert cmd[-2:] == ["--device", "CUDA0"]
+ assert note
+
+
def test_an_explicit_pin_still_renumbers_a_companion_on_one_of_its_cards():
- cmd, pin, _ = _widen([2, 3], ["--mmproj-device", "CUDA3"], may_widen = False)
+ cmd, pin, _ = _widen([2, 3], ["--mmproj-device", "CUDA3"], allowed_ids = [2, 3])
assert pin == [2, 3]
assert _value(cmd, "--mmproj-device") == "CUDA1"
assert "--device" not in cmd