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: place automatic native audio loads on one GPU instead of refusing a pooled pick (#16022)

oobabooga05aa79771c96
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 604677769e2..44223c4dacd 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -15971,6 +15971,8 @@ async def _preflight_native_audio_placement(
load_in_4bit = False,
max_seq_length = request.max_seq_length,
required_override_gb = required_gb,
+ # Past one card, fallback_all lets the branch below weigh every card after the handoff.
+ max_gpus = 1,
)
post_handoff_free_gb = None
if required_gb is None:
@@ -15990,7 +15992,11 @@ async def _preflight_native_audio_placement(
if not fitting:
refusal = ValueError(
"No single GPU has enough free memory for this native audio model. "
- "Select a GPU with enough free memory and try again."
+ + (
+ "Free GPU memory and try again."
+ if audio_type == "minimax_music3"
+ else "Free GPU memory, or load it into CPU RAM, and try again."
+ )
)
if audio_type in AUDIO_CPP_AUDIO_TYPES and audio_cpp_model is not None:
from core.inference.audio_cpp_failures import (
diff --git a/studio/backend/tests/test_chat_load_during_training.py b/studio/backend/tests/test_chat_load_during_training.py
index 1133c39e17f..6b613a55930 100644
--- a/studio/backend/tests/test_chat_load_during_training.py
+++ b/studio/backend/tests/test_chat_load_during_training.py
@@ -1073,6 +1073,79 @@ class TestNativeAudioPlacementContracts(unittest.TestCase):
selected = [0, 1],
)
+ def _automatic_preflight(
+ self,
+ devices,
+ *,
+ required_gb,
+ availability = None,
+ ):
+ """The real automatic selector over ``devices`` ((index, total, used) tuples)."""
+ config = SimpleNamespace(identifier = "test/native-audio", audio_type = "higgs_tts2")
+ request = SimpleNamespace(gpu_ids = None, hf_token = None, max_seq_length = 2048)
+ placement = self.route._LoadPlacement(None, None, False, False)
+ with (
+ patch("utils.hardware.get_device", return_value = DeviceType.CUDA),
+ patch("utils.hardware.hardware.get_device", return_value = DeviceType.CUDA),
+ patch.object(_hw_module, "IS_ROCM", False),
+ patch(
+ "utils.hardware.hardware.get_visible_gpu_utilization",
+ return_value = {"devices": _devices(*devices)},
+ ),
+ patch(
+ "utils.hardware.hardware.get_parent_visible_gpu_ids",
+ return_value = [index for index, _total, _used in devices],
+ ),
+ patch("utils.hardware.hardware._with_mig_instance_memory", side_effect = lambda d: d),
+ # Skip the host torch probe: it would be the first torch import, made from a worker thread.
+ patch("utils.hardware.hardware.rocm_gpu_ids_without_torch_kernels", return_value = set()),
+ patch(
+ "utils.hardware.hardware.estimate_required_model_memory_gb",
+ return_value = (required_gb, {"required_gb": required_gb}),
+ ),
+ patch(
+ "core.inference.native_audio.native_audio_security_targets",
+ return_value = [config.identifier],
+ ),
+ patch.object(
+ self.route,
+ "_native_audio_post_handoff_free_gb",
+ return_value = availability,
+ ),
+ ):
+ return asyncio.run(
+ self.route._preflight_native_audio_placement(config, request, placement)
+ )
+
+ def test_automatic_selection_never_pools_cards(self):
+ # GPU 0 holds the chat model: 4 GB free now, 24 GB once the handoff evicts it.
+ availability = self.route._NativeAudioAvailability({0: 24.0, 1: 6.0}, {0: 20.0})
+ placement = self._automatic_preflight(
+ ((0, 24, 20), (1, 8, 2)), required_gb = 9.0, availability = availability
+ )
+ self.assertEqual(placement.resolved_gpu_ids, [0])
+ self.assertEqual(placement.native_post_handoff_free_gb, {0: 24.0})
+
+ def test_automatic_selection_weighs_a_card_outside_the_pooled_prefix(self):
+ availability = self.route._NativeAudioAvailability({0: 22.0, 1: 6.0, 2: 5.0}, {0: 20.0})
+ placement = self._automatic_preflight(
+ ((0, 24, 22), (1, 8, 2), (2, 8, 3)), required_gb = 9.0, availability = availability
+ )
+ self.assertEqual(placement.resolved_gpu_ids, [0])
+
+ def test_automatic_selection_without_a_fitting_card_is_a_placement_refusal(self):
+ with self.assertRaises(HTTPException) as raised:
+ self._automatic_preflight(((0, 8, 2), (1, 8, 2)), required_gb = 9.0)
+ detail = str(raised.exception.detail)
+ self.assertIn("No single GPU has enough free memory", detail)
+ self.assertIn("CPU RAM", detail)
+ self.assertNotIn("Select a", detail)
+
+ def test_automatic_selection_keeps_the_freest_card_that_fits_now(self):
+ placement = self._automatic_preflight(((0, 24, 20), (1, 24, 2)), required_gb = 9.0)
+ self.assertEqual(placement.resolved_gpu_ids, [1])
+ self.assertEqual(placement.native_required_gb, 9.0)
+
def test_live_training_recheck_cannot_switch_off_selected_gpu(self):
with (
patch("utils.hardware.get_device", return_value = DeviceType.CUDA),
diff --git a/studio/backend/tests/test_gpu_selection.py b/studio/backend/tests/test_gpu_selection.py
index 3631f98de08..f86cc7afdb7 100644
--- a/studio/backend/tests/test_gpu_selection.py
+++ b/studio/backend/tests/test_gpu_selection.py
@@ -927,6 +927,36 @@ class TestGpuAutoSelection(_GpuCacheResetMixin, unittest.TestCase):
# 10 + 10*0.85 = 18.5GB
self.assertAlmostEqual(metadata["usable_gb"], 18.5, places = 3)
+ def test_auto_select_gpu_ids_max_gpus_stops_pooling(self):
+ fake_devices = {
+ "devices": [
+ {"index": 0, "vram_total_gb": 16.0, "vram_used_gb": 4.0},
+ {"index": 1, "vram_total_gb": 16.0, "vram_used_gb": 6.0},
+ {"index": 2, "vram_total_gb": 16.0, "vram_used_gb": 7.0},
+ ]
+ }
+
+ with (
+ patch("utils.hardware.hardware.get_device", return_value = DeviceType.CUDA),
+ patch(
+ "utils.hardware.hardware.get_visible_gpu_utilization",
+ return_value = fake_devices,
+ ),
+ ):
+ pooled, _ = auto_select_gpu_ids("unsloth/test", required_override_gb = 14.0)
+ capped, metadata = auto_select_gpu_ids(
+ "unsloth/test", required_override_gb = 14.0, max_gpus = 1
+ )
+ fits, fits_metadata = auto_select_gpu_ids(
+ "unsloth/test", required_override_gb = 11.0, max_gpus = 1
+ )
+
+ self.assertEqual(pooled, [0, 1])
+ self.assertEqual(capped, [0, 1, 2])
+ self.assertEqual(metadata["selection_mode"], "fallback_all")
+ self.assertAlmostEqual(metadata["usable_gb"], 12.0, places = 3)
+ self.assertEqual((fits, fits_metadata["selection_mode"]), ([0], "auto"))
+
def test_prepare_gpu_selection_preserves_explicit_ids_without_auto_selection(self):
with (
patch("utils.hardware.hardware.get_device", return_value = DeviceType.CUDA),
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index 44ccaaad2a8..7f5e9af9294 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -6587,7 +6587,9 @@ def auto_select_gpu_ids(
gradient_checkpointing: str = "unsloth",
optimizer: str = "adamw_8bit",
required_override_gb: Optional[float] = None,
+ max_gpus: Optional[int] = None,
) -> tuple[Optional[list[int]], Dict[str, Any]]:
+ """``max_gpus`` caps how many cards may be pooled; past it the result is ``fallback_all``."""
metadata: Dict[str, Any] = {"selection_mode": "auto"}
# Auto-selection needs per-device free-VRAM telemetry, available on CUDA (nvidia-smi) and XPU (torch.xpu) but not MLX/CPU, which inherit parent visibility.
@@ -6726,7 +6728,7 @@ def auto_select_gpu_ids(
if discrete is not None:
ranked = [discrete] + [c for c in ranked if c is not discrete]
- for candidate in ranked:
+ for candidate in ranked[:max_gpus]:
selected.append(candidate["index"])
if len(selected) == 1:
usable_gb = candidate["free_gb"]
@@ -6766,13 +6768,14 @@ def auto_select_gpu_ids(
metadata["selection_mode"] = "fallback_all"
if ranked:
fallback_usable = ranked[0]["free_gb"] + sum(
- c["free_gb"] * multi_gpu_overhead for c in ranked[1:]
+ c["free_gb"] * multi_gpu_overhead for c in ranked[1:max_gpus]
)
else:
fallback_usable = 0.0
metadata["usable_gb"] = round(fallback_usable, 3)
metadata["selected_gpu_ids"] = fallback_all
- logger.warning(
+ # A capped caller weighs these cards itself.
+ (logger.warning if max_gpus is None else logger.debug)(
"Falling back to all visible GPUs; model may not fit: model=%s "
"selected=%s usable_gb=%s required_gb=%s multi_gpu_overhead=%s",
model_name,
@@ -6798,6 +6801,7 @@ def prepare_gpu_selection(
gradient_checkpointing: str = "unsloth",
optimizer: str = "adamw_8bit",
required_override_gb: Optional[float] = None,
+ max_gpus: Optional[int] = None,
) -> tuple[Optional[list[int]], Dict[str, Any]]:
"""Resolve which physical GPUs to use for a model load. Explicit (gpu_ids=[5, 6, 7]) uses exactly those, sharded via device_map="balanced" even if fewer would do, validated against the parent-visible set; auto (None or []) lets auto_select_gpu_ids estimate VRAM and pick the minimum, preferring the most free memory. The result is passed to get_device_map() and to apply_gpu_ids() in the worker, which narrows CUDA_VISIBLE_DEVICES before torch init."""
if gpu_ids and get_device() not in (DeviceType.CUDA, DeviceType.XPU):
@@ -6827,6 +6831,7 @@ def prepare_gpu_selection(
gradient_checkpointing = gradient_checkpointing,
optimizer = optimizer,
required_override_gb = required_override_gb,
+ max_gpus = max_gpus,
)
return selected_gpu_ids, metadata