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.

Unsloth Studio (AMD): pick a discrete GPU that fits over a larger APU in automatic GPU selection (#13196)

Daniel Han7d71eee3e926
diff --git a/studio/backend/tests/test_rocm_apu_discrete_first.py b/studio/backend/tests/test_rocm_apu_discrete_first.py
new file mode 100644
index 00000000000..a182d122827
--- /dev/null
+++ b/studio/backend/tests/test_rocm_apu_discrete_first.py
@@ -0,0 +1,79 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Automatic GPU selection on a ROCm host with an APU beside a discrete card: the APU's free
+reading is the shared host pool, so it must not outrank a discrete card that fits alone."""
+
+from unittest.mock import patch
+
+import utils.hardware.hardware as hw
+from utils.hardware import DeviceType, auto_select_gpu_ids
+
+
+def _auto_select(
+ devices,
+ unified,
+ required_gb,
+ *,
+ rocm = True,
+):
+ inventory = [
+ {"index": d["index"], "_rocm_known_unified": d["index"] in unified} for d in devices
+ ]
+ with (
+ patch.object(hw, "get_device", return_value = DeviceType.CUDA),
+ patch.object(hw, "IS_ROCM", rocm),
+ patch.object(
+ hw,
+ "estimate_required_model_memory_gb",
+ return_value = (required_gb, {"required_gb": required_gb, "model_size_source": "config"}),
+ ),
+ patch.object(
+ hw,
+ "_get_parent_visible_gpu_spec",
+ return_value = {
+ "raw": None,
+ "numeric_ids": [d["index"] for d in devices],
+ "supports_explicit_gpu_ids": True,
+ },
+ ),
+ patch.object(hw, "rocm_gpu_ids_without_torch_kernels", return_value = set()),
+ patch.object(hw, "get_parent_visible_gpu_ids", return_value = [d["index"] for d in devices]),
+ patch.object(hw, "get_visible_gpu_utilization", return_value = {"devices": devices}),
+ patch.object(hw, "_torch_get_device_inventory", return_value = inventory) as inv,
+ ):
+ selected, metadata = auto_select_gpu_ids("unsloth/test")
+ return selected, metadata, inv
+
+
+APU_ROW = {"index": 0, "vram_total_gb": 96.0, "vram_used_gb": 8.0} # GTT pool
+DGPU_ROW = {"index": 1, "vram_total_gb": 24.0, "vram_used_gb": 1.0}
+
+
+def test_auto_select_picks_the_discrete_card_that_fits():
+ selected, metadata, _ = _auto_select([APU_ROW, DGPU_ROW], {0}, 16.0)
+ assert selected == [1]
+ assert metadata["selection_mode"] == "auto"
+
+
+def test_auto_select_keeps_the_apu_when_no_discrete_card_fits():
+ selected, _, _ = _auto_select([APU_ROW, DGPU_ROW], {0}, 40.0)
+ assert selected == [0]
+
+
+def test_auto_select_unchanged_without_an_apu():
+ other = {"index": 0, "vram_total_gb": 48.0, "vram_used_gb": 0.0}
+ selected, _, _ = _auto_select([other, DGPU_ROW], set(), 16.0)
+ assert selected == [0]
+
+
+def test_auto_select_unchanged_off_rocm():
+ selected, _, inv = _auto_select([APU_ROW, DGPU_ROW], {0}, 16.0, rocm = False)
+ assert selected == [0]
+ inv.assert_not_called()
+
+
+def test_auto_select_single_gpu_skips_the_inventory():
+ selected, _, inv = _auto_select([APU_ROW], {0}, 16.0)
+ assert selected == [0]
+ inv.assert_not_called()
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index ce42da1d60d..30c188ca1fd 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -5812,6 +5812,21 @@ def auto_select_gpu_ids(
# Per-GPU check: activations do not shard, so each GPU needs its weight shard plus the full activation cost.
vram_breakdown = estimate_metadata.get("vram_breakdown", {})
+ # A ROCm APU's free reading is the shared host pool: a discrete card that fits alone goes first.
+ if IS_ROCM and len(ranked) > 1:
+ unified = {
+ td["index"]
+ for td in _torch_get_device_inventory(get_parent_visible_gpu_ids())
+ if td.get("_rocm_known_unified")
+ }
+ if unified and ranked[0]["index"] in unified:
+ discrete = next(
+ (c for c in ranked if c["index"] not in unified and c["free_gb"] >= required_gb),
+ None,
+ )
+ if discrete is not None:
+ ranked = [discrete] + [c for c in ranked if c is not discrete]
+
for candidate in ranked:
selected.append(candidate["index"])
if len(selected) == 1: