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: let the GPUs picker set device order, not just membership (#11041)

Leo Borcherdingd709671b5fd2
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index cfe11c45455..e3e78039e41 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -7868,7 +7868,7 @@ class LlamaCppBackend:
# without it re-sending the same selection would reload every time.
return requested in ((self._gpu_ids or None), (self._requested_gpu_ids or None))
- requested = sorted(int(x) for x in gpu_ids) if gpu_ids else None
+ requested = [int(x) for x in gpu_ids] if gpu_ids else None
raw = self._requested_gpu_ids or None
effective = self._gpu_ids or None
return requested == raw or requested == effective
@@ -7883,7 +7883,7 @@ class LlamaCppBackend:
if self._is_diffusion:
self._requested_gpu_ids = [sorted(int(x) for x in gpu_ids)[0]] if gpu_ids else None
else:
- self._requested_gpu_ids = sorted(int(x) for x in gpu_ids) if gpu_ids else None
+ self._requested_gpu_ids = [int(x) for x in gpu_ids] if gpu_ids else None
if self._last_load_intent is not None:
self._last_load_intent = replace(
self._last_load_intent,
@@ -22751,7 +22751,7 @@ class LlamaCppBackend:
self._tensor_split = None
self._auto_tensor_split = None
self._auto_tensor_split_emitted = None
- self._requested_gpu_ids = sorted(gpu_ids) if gpu_ids else None
+ self._requested_gpu_ids = [int(i) for i in gpu_ids] if gpu_ids else None
self._gpu_ids = list(self._requested_gpu_ids) if self._requested_gpu_ids else None
# Manual offload skips the TP planner but still emits --split-mode
# tensor at launch; drop it when fewer than 2 GPUs are in use --
@@ -25333,8 +25333,30 @@ class LlamaCppBackend:
# GPU picker: when no narrower subset was chosen (manual, or
# a failed/file-size selection), pin the whole picked set so the
# model can't spill onto an unpicked GPU.
- if gpu_ids and gpu_indices is None:
- gpu_indices = sorted(gpu_ids)
+ if gpu_ids:
+ _picked_order = [int(i) for i in gpu_ids]
+ if gpu_indices is None:
+ gpu_indices = _picked_order
+ else:
+ # A fit narrowed the pool. Only the survivors remain, but their
+ # relative order is still the one the user dragged them into.
+ _kept = {int(i) for i in gpu_indices}
+ _before_reorder = [int(i) for i in gpu_indices]
+ _reordered = [i for i in _picked_order if i in _kept] + [
+ int(i) for i in gpu_indices if int(i) not in set(_picked_order)
+ ]
+ # _plan_tensor_parallel returned the shares positional over the
+ # ascending gpu_indices, so reordering the devices without moving
+ # the shares hands the roomier card's share to the smaller one --
+ # exactly when a weighted split was needed. Veto rather than
+ # half-apply, as _repoint_emitted_tensor_split does for the mask.
+ if tp_tensor_split and len(tp_tensor_split) == len(_before_reorder):
+ _share_by_id = dict(zip(_before_reorder, tp_tensor_split))
+ if all(i in _share_by_id for i in _reordered):
+ tp_tensor_split = [_share_by_id[i] for i in _reordered]
+ gpu_indices = _reordered
+ elif not tp_tensor_split:
+ gpu_indices = _reordered
# Auto Vulkan fit prefers discrete GPUs and keeps that pool pinned.
elif (
is_vulkan_backend
@@ -25355,7 +25377,11 @@ class LlamaCppBackend:
# pick, so repeating it keeps matching.
if gpu_ids and not _paravirtual_cpu_forced:
effective_pin = gpu_indices if gpu_indices is not None else gpu_ids
- self._gpu_ids = sorted(int(idx) for idx in effective_pin)
+ # Survivor order, not sorted: the block above put gpu_indices in the
+ # order the user picked, and _runtime_matches_intent compares this
+ # value positionally. Sorting here reports an effective pin that a
+ # client round-tripping it would match against the opposite order.
+ self._gpu_ids = [int(idx) for idx in effective_pin]
else:
self._gpu_ids = None
@@ -26497,9 +26523,7 @@ class LlamaCppBackend:
# pins below, but recording it would misreport an explicit pin and
# make dedupe miss the loaded server; mirrors the CUDA/ROCm branch.
self._gpu_ids = (
- sorted(int(x) for x in _vulkan_pin_ids)
- if (gpu_ids and _vulkan_pin_ids)
- else None
+ [int(x) for x in _vulkan_pin_ids] if (gpu_ids and _vulkan_pin_ids) else None
)
elif gpu_ids:
# Physical pin: the fit-selected subset when the fit ran, else the raw
@@ -26510,8 +26534,12 @@ class LlamaCppBackend:
if gpu_indices is not None
else [int(x) for x in gpu_ids]
)
+ # Survivor order, not sorted. This is the authoritative record and it
+ # overwrites the one above, so sorting here put the order back and
+ # /status reported a pin a client could round-trip into a match
+ # against the opposite order.
self._gpu_ids = (
- sorted(int(x) for x in _effective_pin_ids) if _effective_pin_ids else None
+ [int(x) for x in _effective_pin_ids] if _effective_pin_ids else None
)
else:
self._gpu_ids = None
@@ -26519,7 +26547,7 @@ class LlamaCppBackend:
# Also record the RAW requested pin (before the fit narrowed it). Load
# dedupe compares this so a [0, 1] narrowed to [0] and re-sent as [0, 1]
# still matches, while /status keeps echoing the effective pin (#7239).
- self._requested_gpu_ids = sorted(int(x) for x in gpu_ids) if gpu_ids else None
+ self._requested_gpu_ids = [int(x) for x in gpu_ids] if gpu_ids else None
if is_vulkan_backend and _vulkan_pin_ids is not None:
cmd += LlamaCppBackend._vulkan_pin_args(_vulkan_pin_ids)
@@ -27457,7 +27485,11 @@ class LlamaCppBackend:
# and gpu_indices is ascending at every producer. Keep a mask that
# deliberately reorders the same cards.
_pin_ids = list(gpu_indices)
- _inherited_order = self._inherited_child_gpu_order(_pin_ids)
+ # A dragged picker order is the more explicit and more recent of
+ # the two, so it wins over whatever the environment was masked to.
+ _inherited_order = (
+ None if gpu_ids else self._inherited_child_gpu_order(_pin_ids)
+ )
if _inherited_order is not None:
# A user --tensor-split is positional over the order they
# expected, so reordering under it re-weights the wrong cards.
@@ -27475,6 +27507,18 @@ class LlamaCppBackend:
_pin_ids,
)
_pin_ids = _inherited_order
+ # The emitted fingerprint was recorded from the pre-rewrite
+ # value, so /status would pair each share with the wrong
+ # device and the repeat check would compare against a split
+ # the child never ran.
+ if "--tensor-split" in cmd and gpu_memory_mode != "manual":
+ _rewritten = cmd[cmd.index("--tensor-split") + 1]
+ try:
+ self._auto_tensor_split_emitted = self._auto_split_fingerprint(
+ [float(x) for x in str(_rewritten).split(",")]
+ )
+ except (TypeError, ValueError):
+ self._auto_tensor_split_emitted = None
# Mask on AMD at the ROCr/HSA layer: HIP-only masking still
# enumerates every agent first, which segfaults on a deselected
# unsupported GPU (e.g. gfx1036 iGPU under a gfx103X prebuilt).
diff --git a/studio/backend/tests/test_empty_gpu_probe_reason.py b/studio/backend/tests/test_empty_gpu_probe_reason.py
index ad234d0c357..b72fb605178 100644
--- a/studio/backend/tests/test_empty_gpu_probe_reason.py
+++ b/studio/backend/tests/test_empty_gpu_probe_reason.py
@@ -276,7 +276,9 @@ class TestItDoesNotAssertTheLaunchOutcome:
src = inspect.getsource(mod.LlamaCppBackend.load_model)
warn = src.find("could not enumerate any GPU")
- restore = src.find("gpu_indices = sorted(gpu_ids)")
+ # The pick is restored in the picker's own order now, not sorted, so the
+ # anchor is the assignment rather than the sort that used to spell it.
+ restore = src.find("gpu_indices = _picked_order")
assert warn != -1 and restore != -1
assert warn < restore, "if the pin moved above the warning, re-derive this guard"
diff --git a/studio/backend/tests/test_gpu_memory_mode.py b/studio/backend/tests/test_gpu_memory_mode.py
index 2f1da841ef8..a9a01f976a8 100644
--- a/studio/backend/tests/test_gpu_memory_mode.py
+++ b/studio/backend/tests/test_gpu_memory_mode.py
@@ -622,15 +622,18 @@ def _target_state_gpu_ids(backend, gpu_ids):
)
-def test_gpu_ids_reload_detection_is_order_insensitive():
+def test_gpu_ids_reload_detection_is_order_sensitive():
backend = _loaded_backend("auto")
backend._gpu_ids = [0, 1]
# A real non-narrowed load records the raw request too; the non-diffusion
- # dedupe now compares that raw pin (#7239). Set it to match the effective pin
- # (no narrowing) so this exercises the order-insensitive comparison.
+ # dedupe compares that raw pin (#7239). Set it to match the effective pin
+ # (no narrowing) so this exercises the order comparison alone.
backend._requested_gpu_ids = [0, 1]
- # Same set, different order -> no reload.
- assert _target_state_gpu_ids(backend, [1, 0]) is True
+ # The picker's order IS the device order, so the same set dragged into a
+ # different order is a different placement and has to reload.
+ assert _target_state_gpu_ids(backend, [1, 0]) is False
+ # Same set, same order -> no reload.
+ assert _target_state_gpu_ids(backend, [0, 1]) is True
# Different set -> reload.
assert _target_state_gpu_ids(backend, [0]) is False
# Dropping the pick (auto) -> reload.
@@ -647,7 +650,7 @@ def test_gpu_ids_reload_detection_accepts_raw_and_effective_pin():
)
# The original request still matches after the fitter narrows it.
- assert _target_state_gpu_ids(backend, [1, 0]) is True
+ assert _target_state_gpu_ids(backend, [0, 1]) is True
assert backend.requested_gpu_ids == [0, 1]
# The status response echoes the effective pin, which must also round-trip.
# Treat the incoming subset as the latest intent so status and a future
diff --git a/studio/backend/tests/test_gpu_order_preserved.py b/studio/backend/tests/test_gpu_order_preserved.py
index b91ccb0ebbf..53b30695f91 100644
--- a/studio/backend/tests/test_gpu_order_preserved.py
+++ b/studio/backend/tests/test_gpu_order_preserved.py
@@ -53,12 +53,19 @@ def test_reordered_parent_mask_reaches_the_child(monkeypatch, tmp_path):
assert result["env"]["CUDA_VISIBLE_DEVICES"] == "1,0"
-def test_reordered_parent_mask_survives_a_gpu_pick(monkeypatch, tmp_path):
- """The picker owns the SET; the mask still owns the order within it."""
+def test_an_explicit_pick_outranks_the_inherited_mask(monkeypatch, tmp_path):
+ """Both name an order. The picker is the more explicit and more recent one."""
backend, result = _run(monkeypatch, tmp_path, mask = "1,0", gpu_ids = [0, 1])
- assert result["env"]["CUDA_VISIBLE_DEVICES"] == "1,0"
+ assert result["env"]["CUDA_VISIBLE_DEVICES"] == "0,1"
# The ordinal -> physical map the buffer parser reads has to match what we emitted.
+ assert backend._child_gpu_physical_ids == (0, 1)
+
+
+def test_the_picked_order_is_the_child_order(monkeypatch, tmp_path):
+ backend, result = _run(monkeypatch, tmp_path, mask = None, gpu_ids = [1, 0])
+ assert result["env"]["CUDA_VISIBLE_DEVICES"] == "1,0"
assert backend._child_gpu_physical_ids == (1, 0)
+ assert backend.requested_gpu_ids == [1, 0]
def test_ascending_parent_mask_is_left_alone(monkeypatch, tmp_path):
@@ -132,7 +139,9 @@ def test_a_split_scrubbed_from_the_child_does_not_veto(monkeypatch, tmp_path):
backend, gguf = _backend(tmp_path, vulkan = False, memory = _TWO_GPUS)
backend._get_gguf_size_bytes = lambda _path: 14 * 1024**3
backend._select_gpus = lambda *args, **kwargs: ([0, 1], False)
- result = _launch(backend, gguf, n_ctx = 4096, gpu_ids = [0, 1], tensor_parallel = True)
+ # No explicit pick: here the picker outranks the inherited mask, so only an
+ # unpicked load reaches the mask reorder this cell is about.
+ result = _launch(backend, gguf, n_ctx = 4096, tensor_parallel = True)
assert result["env"].get("LLAMA_ARG_TENSOR_SPLIT") is None
assert result["env"]["CUDA_VISIBLE_DEVICES"] == "1,0"
@@ -160,6 +169,43 @@ def test_an_explicit_pick_always_owns_device_flags(monkeypatch, tmp_path):
assert own(None) is False
+def test_the_authoritative_effective_pin_keeps_the_picked_order(monkeypatch, tmp_path):
+ """/status serves _gpu_ids, and two blocks assign it: the later one wins.
+
+ Sorting in either put the order back, so a client round-tripping the effective
+ value matched the stored pin and skipped a reload the child needed.
+ """
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0,1")
+ monkeypatch.setenv("CUDA_DEVICE_ORDER", "PCI_BUS_ID")
+ backend, gguf = _backend(tmp_path, vulkan = False, memory = _TWO_GPUS)
+ backend._select_gpus = lambda *args, **kwargs: ([1, 0], False)
+ _launch(backend, gguf, n_ctx = 4096, gpu_ids = [1, 0])
+ assert backend._gpu_ids == [1, 0], f"the effective pin was re-sorted: {backend._gpu_ids}"
+
+
+def test_the_reported_split_follows_the_reorder(monkeypatch, tmp_path):
+ """/status serves the recorded emitted split, which was captured before the
+ reorder rewrote it, so each share was paired with the wrong visible device."""
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "1,0")
+ monkeypatch.setenv("CUDA_DEVICE_ORDER", "PCI_BUS_ID")
+ backend, gguf = _backend(tmp_path, vulkan = False, memory = _TWO_GPUS)
+ backend._get_gguf_size_bytes = lambda _path: 14 * 1024**3
+ backend._select_gpus = lambda *args, **kwargs: ([0, 1], False)
+ # No explicit pick: in this branch an explicit pick outranks the inherited
+ # mask, so only an unpicked load reaches the mask reorder at all.
+ result = _launch(backend, gguf, n_ctx = 4096, tensor_parallel = True)
+ cmd = result["cmd"]
+ assert result["env"]["CUDA_VISIBLE_DEVICES"] == "1,0"
+ in_argv = [float(x) for x in cmd[cmd.index("--tensor-split") + 1].split(",")]
+ reported = backend.tensor_split
+ assert reported is not None, "no split reported at all; this would be vacuous"
+ assert [round(float(x), 3) for x in reported] == [
+ round(v / sum(in_argv), 3) for v in in_argv
+ ] or list(
+ reported
+ ) == in_argv, f"reported {reported} does not match the argv the child ran: {in_argv}"
+
+
def test_a_full_set_pick_strips_a_device_flag(monkeypatch, tmp_path):
"""Both spellings, including the env twin, which needs no user argv at all."""
monkeypatch.setenv("LLAMA_ARG_DEVICE", "CUDA0")
@@ -174,8 +220,12 @@ def test_a_full_set_pick_strips_a_device_flag(monkeypatch, tmp_path):
assert result["env"].get("LLAMA_ARG_DEVICE") is None
-def test_the_mask_is_still_how_a_full_pick_orders_its_cards(monkeypatch, tmp_path):
- """The ordering route the withdrawn pass-through was meant to preserve."""
- backend, result = _run(monkeypatch, tmp_path, mask = "1,0", gpu_ids = [0, 1])
+def test_the_picker_is_how_a_full_pick_orders_its_cards(monkeypatch, tmp_path):
+ """The ordering route the withdrawn pass-through was meant to preserve.
+
+ In this branch the picker carries the order and outranks the inherited mask,
+ so the pick is what the child enumerates by.
+ """
+ backend, result = _run(monkeypatch, tmp_path, mask = "0,1", gpu_ids = [1, 0])
assert result["env"]["CUDA_VISIBLE_DEVICES"] == "1,0"
assert backend._child_gpu_physical_ids == (1, 0)
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
index 7c3b332bd94..fa334f89357 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
@@ -210,13 +210,14 @@ async function readServerWideReloadHints(): Promise<boolean> {
return serverWideReloadRequired({ modelMemory, vramBudget });
}
-/** Placement is a set: the backend narrows and reorders it at fit time. */
+/** Placement is an ordered list: position decides which card the model is given
+ * first, so the same set in a different order is a different placement. */
function sameGpuSelection(
left: readonly number[] | null | undefined,
right: readonly number[] | null | undefined,
): boolean {
- const a = [...(left ?? [])].sort((x, y) => x - y);
- const b = [...(right ?? [])].sort((x, y) => x - y);
+ const a = left ?? [];
+ const b = right ?? [];
return a.length === b.length && a.every((id, index) => id === b[index]);
}
diff --git a/studio/frontend/src/features/chat/lib/resident-config-match.ts b/studio/frontend/src/features/chat/lib/resident-config-match.ts
index 88582e19c80..81898ea1cce 100644
--- a/studio/frontend/src/features/chat/lib/resident-config-match.ts
+++ b/studio/frontend/src/features/chat/lib/resident-config-match.ts
@@ -60,13 +60,13 @@ function sameList(
return a.length === b.length && a.every((item, index) => item === b[index]);
}
-/** Placement is a set, not an order: the backend narrows and reorders it at fit time. */
-function sameGpuSet(
+/** Placement is an ordered list: position decides which card the model is given first,
+ * so the same set in a different order is a different placement and must reload. */
+function sameGpuPlacement(
left: readonly number[] | null | undefined,
right: readonly number[] | null | undefined,
): boolean {
- const sort = (ids: readonly number[]) => [...ids].sort((a, b) => a - b);
- return sameList(sort(left ?? []), sort(right ?? []));
+ return sameList(left ?? [], right ?? []);
}
/** What a config field resolves to when left unset. These four are not per-model, so omitting them
@@ -446,13 +446,13 @@ const SETTING_CHECKS: SettingCheck[] = [
s.is_diffusion === true && reconciled?.length
? [Math.min(...reconciled)]
: reconciled;
- if (sameGpuSet(pick, s.requested_gpu_ids)) {
+ if (sameGpuPlacement(pick, s.requested_gpu_ids)) {
return true;
}
// Either pool, as matches_gpu_ids accepts either: fitting may narrow the request to the smallest
// subset that holds the model. Guarded on a non-empty echo, since an absent one is no
// placement rather than Automatic.
- return Boolean(s.gpu_ids?.length) && sameGpuSet(pick, s.gpu_ids);
+ return Boolean(s.gpu_ids?.length) && sameGpuPlacement(pick, s.gpu_ids);
},
},
{
diff --git a/studio/frontend/src/features/model-picker/components/model-config-page.tsx b/studio/frontend/src/features/model-picker/components/model-config-page.tsx
index 30740557fde..a4d50320b39 100644
--- a/studio/frontend/src/features/model-picker/components/model-config-page.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-config-page.tsx
@@ -765,18 +765,38 @@ function GpuMemorySettings({
const showGpuPicker = (gpuContext.ids?.length ?? 0) > 1;
const isGpuChecked = (index: number) =>
selectedGpuIds === null || selectedGpuIds.includes(index);
- const toggleGpu = (index: number) => {
- const all = gpuContext.ids ?? [];
- const current = selectedGpuIds ?? all;
- const next = current.includes(index)
- ? current.filter((i) => i !== index)
- : [...current, index].sort((a, b) => a - b);
+ // The list order IS the device order the backend pins, so a re-checked GPU goes
+ // to the end rather than back to its numeric slot.
+ const orderedGpuIds = selectedGpuIds ?? gpuContext.ids ?? [];
+ const commitGpuIds = (next: number[]) => {
if (next.length === 0) return; // keep at least one GPU selected
update({
selectedGpuIds: next,
selectedGpuIndexKind: gpuIndexKind,
});
};
+ const toggleGpu = (index: number) => {
+ commitGpuIds(
+ orderedGpuIds.includes(index)
+ ? orderedGpuIds.filter((i) => i !== index)
+ : [...orderedGpuIds, index],
+ );
+ };
+ // Selected devices in the order they will be given to the model, then the rest.
+ const orderedPinnableDevices = [
+ ...orderedGpuIds
+ .map((id) => pinnableDevices.find((d) => d.index === id))
+ .filter((d): d is SystemGpuDevice => d !== undefined),
+ ...pinnableDevices.filter((d) => !orderedGpuIds.includes(d.index)),
+ ];
+ const moveGpu = (index: number, delta: -1 | 1) => {
+ const from = orderedGpuIds.indexOf(index);
+ const to = from + delta;
+ if (from < 0 || to < 0 || to >= orderedGpuIds.length) return;
+ const next = [...orderedGpuIds];
+ [next[from], next[to]] = [next[to], next[from]];
+ commitGpuIds(next);
+ };
return (
<>
<div className={isDiffusion ? "hidden" : ROW_CLASS}>
@@ -876,12 +896,14 @@ function GpuMemorySettings({
<span className={LABEL_CLASS}>GPUs</span>
<InfoHint>
By default, Unsloth chooses GPUs automatically. Editing this list
- makes the checked GPUs the explicit candidate pool. At least one
- GPU must stay selected.
+ makes the checked GPUs the explicit candidate pool.
+ {!isDiffusion &&
+ " Their order here is the order they are given to the model, so the first one takes the prompt."}{" "}
+ At least one GPU must stay selected.
</InfoHint>
</div>
<div className="flex flex-col gap-2">
- {pinnableDevices.map((d) => (
+ {orderedPinnableDevices.map((d, position) => (
<div
key={d.index}
className="flex items-center justify-between gap-3"
@@ -892,6 +914,31 @@ function GpuMemorySettings({
? ` · ${Math.round(d.memoryTotalGb)} GiB`
: ""}
</span>
+ {/* Not for diffusion: that runner drives one device and matches_gpu_ids
+ reduces the request to its lowest id, so the arrows would move a row
+ without moving the model, under help text promising the opposite. */}
+ {isGpuChecked(d.index) && !singleGpuInUse && !isDiffusion && (
+ <div className="flex shrink-0 items-center gap-0.5">
+ <button
+ type="button"
+ className="rounded px-1 text-ui-12 text-nav-fg/60 hover:text-nav-fg disabled:opacity-30"
+ aria-label={`Move GPU ${d.index} earlier`}
+ disabled={position === 0}
+ onClick={() => moveGpu(d.index, -1)}
+ >
+ ↑
+ </button>
+ <button
+ type="button"
+ className="rounded px-1 text-ui-12 text-nav-fg/60 hover:text-nav-fg disabled:opacity-30"
+ aria-label={`Move GPU ${d.index} later`}
+ disabled={position >= orderedGpuIds.length - 1}
+ onClick={() => moveGpu(d.index, 1)}
+ >
+ ↓
+ </button>
+ </div>
+ )}
<Switch
className="panel-switch shrink-0"
checked={isGpuChecked(d.index)}
diff --git a/studio/frontend/src/features/model-picker/model-config/config-signature.ts b/studio/frontend/src/features/model-picker/model-config/config-signature.ts
index d9e69fe358a..08940d24800 100644
--- a/studio/frontend/src/features/model-picker/model-config/config-signature.ts
+++ b/studio/frontend/src/features/model-picker/model-config/config-signature.ts
@@ -15,7 +15,9 @@ export function gpuFieldsSignature(config: PerModelConfig): string {
config.selectedGpuIds == null
? "automatic"
: [
- [...config.selectedGpuIds].sort((a, b) => a - b).join(","),
+ // Order-preserving: the list order is the device order, so sorting here
+ // made a reorder read as no change and left Apply disabled on it.
+ config.selectedGpuIds.join(","),
config.selectedGpuIndexKind === undefined
? "physical"
: (config.selectedGpuIndexKind ?? "deferred"),
diff --git a/studio/frontend/tests/gpu-order.test.ts b/studio/frontend/tests/gpu-order.test.ts
new file mode 100644
index 00000000000..1539c5e66c6
--- /dev/null
+++ b/studio/frontend/tests/gpu-order.test.ts
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// The GPUs list is an ordered list, not a set: position decides which card the
+// model is given first. These cover the reordering the picker performs on it.
+
+import assert from "node:assert/strict";
+import test from "node:test";
+import {
+ reconcileGpuSelection,
+ sameGpuSelection,
+} from "../src/hooks/gpu-selection.ts";
+
+import { readSrc } from "./helpers/kit.ts";
+
+/** The picker's toggle, as model-config-page drives it. */
+function toggle(current: number[], index: number): number[] {
+ const next = current.includes(index)
+ ? current.filter((i) => i !== index)
+ : [...current, index];
+ return next.length === 0 ? current : next;
+}
+
+/** The picker's move-earlier / move-later, as model-config-page drives it. */
+function move(current: number[], index: number, delta: -1 | 1): number[] {
+ const from = current.indexOf(index);
+ const to = from + delta;
+ if (from < 0 || to < 0 || to >= current.length) return current;
+ const next = [...current];
+ [next[from], next[to]] = [next[to], next[from]];
+ return next;
+}
+
+test("moving a GPU earlier puts it in front of the model", () => {
+ assert.deepEqual(move([0, 1], 1, -1), [1, 0]);
+ assert.deepEqual(move([0, 1, 2], 2, -1), [0, 2, 1]);
+});
+
+test("the ends do not wrap", () => {
+ assert.deepEqual(move([0, 1], 0, -1), [0, 1]);
+ assert.deepEqual(move([0, 1], 1, 1), [0, 1]);
+});
+
+test("a GPU switched back on goes last, not back to its numeric slot", () => {
+ // Re-inserting by index would silently undo a reorder the user just made.
+ const afterDrop = toggle([1, 0], 1);
+ assert.deepEqual(afterDrop, [0]);
+ assert.deepEqual(toggle(afterDrop, 1), [0, 1]);
+});
+
+test("the last GPU cannot be switched off", () => {
+ assert.deepEqual(toggle([0], 0), [0]);
+});
+
+test("the same set in a different order is a different placement", () => {
+ const physical = (ids: number[] | null) =>
+ ({ ids, indexKind: "physical" }) as const;
+ assert.equal(sameGpuSelection(physical([0, 1]), physical([1, 0])), false);
+ assert.equal(sameGpuSelection(physical([0, 1]), physical([0, 1])), true);
+});
+
+test("dropping an unpinnable GPU keeps the order of the rest", () => {
+ assert.deepEqual(
+ reconcileGpuSelection([2, 0, 1], "physical", "physical", [0, 1]).ids,
+ [0, 1],
+ );
+ assert.deepEqual(
+ reconcileGpuSelection([1, 0], "physical", "physical", [0, 1]).ids,
+ [1, 0],
+ );
+});
+
+// Diffusion drives ONE device and matches_gpu_ids reduces the request to its lowest
+// id, so an ordering control there moves a row without moving the model. The arrows
+// and the sentence promising the first card takes the prompt are both withheld.
+test("the ordering controls and their promise are withheld for diffusion", () => {
+ const src = readSrc("features/model-picker/components/model-config-page.tsx");
+ const arrows = src.slice(src.indexOf("Move GPU ${d.index} earlier") - 900);
+ assert.match(
+ arrows.slice(0, 900),
+ /!singleGpuInUse && !isDiffusion/,
+ "the arrows must not render for a diffusion model",
+ );
+ assert.match(
+ src,
+ /!isDiffusion &&\s*" Their order here is the order they are given to the model/,
+ "the help text must not promise ordering for a diffusion model",
+ );
+});
+
+// The arrows move the list, but Apply is gated on the config differing from the
+// loaded baseline. A sorting signature made a reorder read as no change, so the
+// control moved the GPUs and could never apply them.
+test("a reorder is a config change, so Apply stays reachable", async () => {
+ const { gpuFieldsSignature } = await import(
+ "../src/features/model-picker/model-config/config-signature.ts"
+ );
+ const base = { selectedGpuIds: [0, 1], selectedGpuIndexKind: "physical" } as never;
+ const swapped = { selectedGpuIds: [1, 0], selectedGpuIndexKind: "physical" } as never;
+ assert.notEqual(
+ gpuFieldsSignature(base),
+ gpuFieldsSignature(swapped),
+ "a reordered pick must not share a signature with the loaded baseline",
+ );
+ const same = { selectedGpuIds: [0, 1], selectedGpuIndexKind: "physical" } as never;
+ assert.equal(gpuFieldsSignature(base), gpuFieldsSignature(same));
+});
diff --git a/studio/frontend/tests/model-config-instance-key.test.ts b/studio/frontend/tests/model-config-instance-key.test.ts
index 0b744a4aa0d..06428a738dd 100644
--- a/studio/frontend/tests/model-config-instance-key.test.ts
+++ b/studio/frontend/tests/model-config-instance-key.test.ts
@@ -123,11 +123,16 @@ test("every mirrored setting moves the instance key", () => {
for (const changed of changes) {
assert.notEqual(modelConfigInstanceKey(MODEL, VARIANT, changed), base);
}
- // The GPU pick is a set, not an order.
- assert.equal(
+ // The GPU pick is an order, not a set: the list order is the device order, so a
+ // reorder is a different config and has to move the key like any other edit.
+ assert.notEqual(
modelConfigInstanceKey(MODEL, VARIANT, { ...LIVE, selectedGpuIds: [1, 0] }),
base,
);
+ assert.equal(
+ modelConfigInstanceKey(MODEL, VARIANT, { ...LIVE, selectedGpuIds: [...LIVE.selectedGpuIds!] }),
+ base,
+ );
});
test("the model and its quant still key the editor", () => {
diff --git a/studio/frontend/tests/resident-config-match-accelerator-matrix.test.ts b/studio/frontend/tests/resident-config-match-accelerator-matrix.test.ts
index 7fe07c3ff1a..709db1d8db6 100644
--- a/studio/frontend/tests/resident-config-match-accelerator-matrix.test.ts
+++ b/studio/frontend/tests/resident-config-match-accelerator-matrix.test.ts
@@ -324,13 +324,21 @@ for (const [accelerator, base] of Object.entries(ACCELERATORS)) {
}
}
-test("placement compares as a set on a multi-GPU host, not as an order", () => {
- // The backend narrows and reorders the pool at fit time, so only membership counts.
+test("placement compares as an order on a multi-GPU host, not as a set", () => {
+ // The picker's order is the order the backend pins, so the same cards in a
+ // different order are a different placement and the runner has to restart.
assert.equal(
residentRuntimeMatchesConfig(
{ ...ACCELERATORS["amd-rocm"], requested_gpu_ids: [0, 1] },
{ ...BLANK, selectedGpuIds: [1, 0] },
),
+ false,
+ );
+ assert.equal(
+ residentRuntimeMatchesConfig(
+ { ...ACCELERATORS["amd-rocm"], requested_gpu_ids: [0, 1] },
+ { ...BLANK, selectedGpuIds: [0, 1] },
+ ),
true,
);
assert.equal(
diff --git a/studio/frontend/tests/resident-config-match.test.ts b/studio/frontend/tests/resident-config-match.test.ts
index e4611c0db1c..cb48ff7c193 100644
--- a/studio/frontend/tests/resident-config-match.test.ts
+++ b/studio/frontend/tests/resident-config-match.test.ts
@@ -219,7 +219,8 @@ const FIELDS: {
{
name: "GPU placement",
config: { selectedGpuIds: [0, 2] },
- same: { requested_gpu_ids: [2, 0] },
+ // Same ORDER, not merely the same cards: the reordered pair is a reload now.
+ same: { requested_gpu_ids: [0, 2] },
differs: { requested_gpu_ids: [0, 1] },
},
];
@@ -245,12 +246,21 @@ for (const field of FIELDS) {
}
/** Ordering is the backend's to choose: it narrows and reorders placement at fit time. */
-test("GPU placement compares as a set, not as an order", () => {
+test("GPU placement compares as an order, not as a set", () => {
+ // The picker hands the list to the backend in order and position decides which
+ // card takes the prompt, so a reorder is a different placement and must reload.
assert.equal(
matches(
{ requested_gpu_ids: [3, 1, 0] },
{ ...BLANK, selectedGpuIds: [0, 1, 3] },
),
+ false,
+ );
+ assert.equal(
+ matches(
+ { requested_gpu_ids: [3, 1, 0] },
+ { ...BLANK, selectedGpuIds: [3, 1, 0] },
+ ),
true,
);
});