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 crashing on a lowercase boolean in PYTORCH_ALLOC_CONF (#12976)

oobabooga1b81d95368a3
diff --git a/studio/backend/main.py b/studio/backend/main.py
index dcec3718896..84546d4f3ad 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -42,6 +42,16 @@ try:
except Exception:
pass
+# normalize allocator booleans before torch parses them; spawned workers inherit the environment.
+from utils.allocator_conf import normalize_allocator_conf as _normalize_allocator_conf
+
+for _name, _old, _new in _normalize_allocator_conf():
+ print(
+ f"Unsloth: {_name}={_old!r} has noncanonical boolean casing, which PyTorch rejects; "
+ f"using {_new!r}.",
+ file = sys.stderr,
+ )
+
# Windows terminals default to the active system code page. Reconfigure stdout/stderr
# before the startup banner so non-ASCII output cannot crash the backend process.
if sys.platform == "win32":
diff --git a/studio/backend/tests/test_allocator_conf.py b/studio/backend/tests/test_allocator_conf.py
new file mode 100644
index 00000000000..9664dc367cf
--- /dev/null
+++ b/studio/backend/tests/test_allocator_conf.py
@@ -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
+
+"""covers early PyTorch allocator-config normalization."""
+
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+from utils.allocator_conf import ALLOCATOR_CONF_ENV_VARS, normalize_allocator_conf
+
+
+_BACKEND_DIR = Path(__file__).resolve().parent.parent
+
+
[email protected]("name", ALLOCATOR_CONF_ENV_VARS)
+ "raw, expected",
+ [
+ ("expandable_segments:false", "expandable_segments:False"),
+ ("expandable_segments:true", "expandable_segments:True"),
+ ("expandable_segments:FALSE", "expandable_segments:False"),
+ ("expandable_segments : tRuE", "expandable_segments : True"),
+ (
+ "max_split_size_mb:128,expandable_segments:false,pinned_use_cuda_host_register:true",
+ "max_split_size_mb:128,expandable_segments:False,pinned_use_cuda_host_register:True",
+ ),
+ (
+ "expandable_segments:false,roundup_power2_divisions:[32:256,64:128,>:32]",
+ "expandable_segments:False,roundup_power2_divisions:[32:256,64:128,>:32]",
+ ),
+ ],
+)
+def test_noncanonical_booleans_are_capitalized(name, raw, expected):
+ env = {name: raw}
+
+ assert normalize_allocator_conf(env) == [(name, raw, expected)]
+ assert env == {name: expected}
+
+
+ "raw",
+ [
+ "expandable_segments:True",
+ "expandable_segments:False,max_split_size_mb:128",
+ "roundup_power2_divisions:[32:256,64:128,256:64,>:32]",
+ "backend:cudaMallocAsync",
+ "garbage_collection_threshold:0.6",
+ # non-case errors remain for PyTorch to report.
+ "expandable_segments:1",
+ "expandable_segments:falsey",
+ "",
+ ],
+)
+def test_values_without_a_noncanonical_boolean_are_untouched(raw):
+ env = {"PYTORCH_ALLOC_CONF": raw}
+
+ assert normalize_allocator_conf(env) == []
+ assert env == {"PYTORCH_ALLOC_CONF": raw}
+
+
+def test_unrelated_variables_are_untouched():
+ env = {"PYTORCH_MPS_HIGH_WATERMARK_RATIO": "false", "UNSLOTH_FLAG": "expandable_segments:false"}
+ snapshot = dict(env)
+
+ assert normalize_allocator_conf(env) == []
+ assert env == snapshot
+
+
+def test_uses_os_environ_and_is_idempotent(monkeypatch):
+ for name in ALLOCATOR_CONF_ENV_VARS:
+ monkeypatch.delenv(name, raising = False)
+ monkeypatch.setenv("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:false")
+
+ assert normalize_allocator_conf() == [
+ ("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:false", "expandable_segments:False")
+ ]
+ assert normalize_allocator_conf() == []
+
+
+# `import main` must normalize before CUDA without importing torch so workers inherit the fixed value.
+def test_import_main_normalizes_the_allocator_config():
+ env = {k: v for k, v in os.environ.items() if k not in ALLOCATOR_CONF_ENV_VARS}
+ env["PYTORCH_ALLOC_CONF"] = "expandable_segments:false"
+ env["UNSLOTH_STUDIO_DISABLE_TORCH_WARM"] = "1"
+ proc = subprocess.run(
+ [
+ sys.executable,
+ "-c",
+ "import os, sys, main; "
+ "print('ALLOC', os.environ['PYTORCH_ALLOC_CONF'], 'torch' in sys.modules)",
+ ],
+ cwd = str(_BACKEND_DIR),
+ env = env,
+ capture_output = True,
+ text = True,
+ timeout = 900,
+ )
+ assert proc.returncode == 0, proc.stderr[-4000:]
+ assert "ALLOC expandable_segments:False False" in proc.stdout
+ assert (
+ "PYTORCH_ALLOC_CONF='expandable_segments:false' has noncanonical boolean casing, "
+ "which PyTorch rejects; using 'expandable_segments:False'."
+ ) in proc.stderr
diff --git a/studio/backend/tests/test_utils.py b/studio/backend/tests/test_utils.py
index 732716225e0..173a27a05b0 100644
--- a/studio/backend/tests/test_utils.py
+++ b/studio/backend/tests/test_utils.py
@@ -93,8 +93,6 @@ class TestGetDevice:
@needs_torch
def test_detect_survives_device0_probe_failure(self, capsys):
- # is_available() True but the device-0 name probe raises: startup must
- # still resolve CUDA rather than crash.
with (
patch("utils.hardware.hardware._has_torch", return_value = True),
patch("torch.cuda.is_available", return_value = True),
@@ -104,6 +102,27 @@ class TestGetDevice:
assert _reset_and_detect() == DeviceType.CUDA
assert "<unavailable>" in capsys.readouterr().out
+ @needs_torch
+ def test_device0_probe_failure_is_logged_with_allocator_config(self, monkeypatch):
+ # failed CUDA initialization can crash later, so log its cause at error rather than debug
+ monkeypatch.delenv("PYTORCH_CUDA_ALLOC_CONF", raising = False)
+ monkeypatch.delenv("PYTORCH_HIP_ALLOC_CONF", raising = False)
+ monkeypatch.setenv("PYTORCH_ALLOC_CONF", "expandable_segments:maybe")
+ error = ValueError(
+ "Expected 'True' or 'False' at index 2 in ConfigTokenizer but got 'maybe'"
+ )
+ with (
+ patch("utils.hardware.hardware._has_torch", return_value = True),
+ patch("torch.cuda.is_available", return_value = True),
+ patch("torch.cuda.device_count", return_value = 1),
+ patch("torch.cuda.get_device_properties", side_effect = error),
+ patch.object(_hw_module, "logger") as logger,
+ ):
+ assert _reset_and_detect() == DeviceType.CUDA
+ logged = [c.args for c in logger.error.call_args_list if error in c.args]
+ assert len(logged) == 1
+ assert {"PYTORCH_ALLOC_CONF": "expandable_segments:maybe"} in logged[0]
+
@needs_mlx
def test_returns_mlx_when_on_apple_silicon_with_mlx(self):
with (
diff --git a/studio/backend/utils/allocator_conf.py b/studio/backend/utils/allocator_conf.py
new file mode 100644
index 00000000000..97cb921ad40
--- /dev/null
+++ b/studio/backend/utils/allocator_conf.py
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""early PyTorch allocator-config normalization for Unsloth processes."""
+
+import os
+import re
+from typing import List, MutableMapping, Optional, Tuple
+
+
+ALLOCATOR_CONF_ENV_VARS = (
+ "PYTORCH_ALLOC_CONF",
+ "PYTORCH_CUDA_ALLOC_CONF",
+ "PYTORCH_HIP_ALLOC_CONF",
+)
+
+# bracketed numeric lists such as roundup_power2_divisions:[32:256,64:128] cannot match.
+_BOOL_OPTION = re.compile(
+ r"(?P<head>(?:^|,)\s*[A-Za-z_][A-Za-z0-9_]*\s*:\s*)(?P<value>true|false)(?=\s*(?:,|$))",
+ re.IGNORECASE,
+)
+
+
+def _canonical_bool(match: "re.Match[str]") -> str:
+ return match.group("head") + match.group("value").capitalize()
+
+
+def normalize_allocator_conf(
+ env: Optional[MutableMapping[str, str]] = None,
+) -> List[Tuple[str, str, str]]:
+ """normalize before CUDA init to avoid PyTorch parser failures; returns (name, old, new)."""
+ environ = os.environ if env is None else env
+ changed = []
+ for name in ALLOCATOR_CONF_ENV_VARS:
+ value = environ.get(name)
+ if not value:
+ continue
+ normalized = _BOOL_OPTION.sub(_canonical_bool, value)
+ if normalized != value:
+ environ[name] = normalized
+ changed.append((name, value, normalized))
+ return changed
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index dad36aa8a25..a50d184a805 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -1481,17 +1481,27 @@ def _detect_hardware_locked() -> DeviceType:
print(f"Hardware detected: XPU -- {device_name} ({reason})")
return DEVICE
- # Reuse the guarded answer: a raising second is_available() would skip the XPU branch below.
+ # reuse guarded result because another is_available() exception would skip XPU fallback
if not cuda_unavailable:
DEVICE = DeviceType.CUDA
CHAT_ONLY = False
try:
device_name = torch.cuda.get_device_properties(0).name
except Exception as e:
- logger.debug("CUDA device 0 property probe failed: %s", e)
+ # failed CUDA init can make later PyTorch calls segfault, so log the cause
+ from utils.allocator_conf import ALLOCATOR_CONF_ENV_VARS
+
+ allocator_conf = {
+ name: os.environ[name] for name in ALLOCATOR_CONF_ENV_VARS if name in os.environ
+ }
+ logger.error(
+ "CUDA device 0 property probe failed: %r (allocator config: %s)",
+ e,
+ allocator_conf or "unset",
+ )
device_name = "<unavailable>"
- # Distinguish ROCm from CUDA for display only (DeviceType stays CUDA). AMD SDK wheels do not set torch.version.hip, so fall back to __version__.
+ # AMD SDK wheels omit torch.version.hip; ROCm label uses __version__; DEVICE stays CUDA
_hip_ver = getattr(torch.version, "hip", None)
if _hip_ver is not None or "rocm" in torch.__version__.lower():
IS_ROCM = True