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: Make `unsloth start --reasoning on/off` take effect (#11718)

Nilay0b5b4442a5ca
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index cab56b097b8..36574443cc9 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -4459,6 +4459,20 @@ def _swa_full_from_args_or_env(
return value in _LLAMA_ARG_TRUE_VALUES
+def _reasoning_from_env(env: Mapping[str, str]) -> Optional[bool]:
+ value = env.get("LLAMA_ARG_REASONING")
+ if value in _LLAMA_ARG_TRUE_VALUES:
+ return True
+ if value in _LLAMA_ARG_FALSE_VALUES:
+ return False
+ return None
+
+
+def _reasoning_effort_from_env(env: Mapping[str, str]) -> Optional[str]:
+ value = env.get("LLAMA_ARG_REASONING_EFFORT")
+ return None if value == "default" else value
+
+
def _env_asks_for_the_native_context(env: Optional[Mapping[str, str]] = None) -> bool:
"""Whether an inherited LLAMA_ARG_CTX_SIZE is 0.
@@ -6387,9 +6401,6 @@ def _build_launch_reasoning_args(
tools/server/server-common.cpp then overrides from the merged kwarg anyway. If
llama.cpp stops writing that kwarg, or stops letting it override, this becomes a
behaviour change rather than a substitution.
-
- An exported LLAMA_ARG_REASONING is still overridden by the command line, exactly as
- the kwargs channel overrides it on main; honouring it is #8521.
"""
remaining = dict(reasoning_kwargs)
args: list[str] = []
@@ -7856,7 +7867,11 @@ class LlamaCppBackend:
def reasoning_default(self) -> bool:
return self._reasoning_default
- def _reasoning_kwargs(self, enable_thinking: bool) -> dict:
+ def _reasoning_kwargs(
+ self,
+ enable_thinking: bool,
+ effort: Optional[str] = None,
+ ) -> dict:
if self._reasoning_style == "enable_thinking_effort":
# GLM-5.2-style: enable_thinking is the on/off gate; when on, leave
# the template's default effort (max) in place.
@@ -7864,7 +7879,7 @@ class LlamaCppBackend:
if self._reasoning_style == "reasoning_effort":
return _coerce_reasoning_effort(
getattr(self, "_architecture", None),
- {"reasoning_effort": "high" if enable_thinking else "low"},
+ {"reasoning_effort": effort or ("high" if enable_thinking else "low")},
)
return {"enable_thinking": enable_thinking}
@@ -27096,8 +27111,17 @@ class LlamaCppBackend:
# <= 9, not < 9: 9B is the top of the Small tier, so it is off too.
if size_b <= 9:
thinking_default = False
- self._reasoning_default = thinking_default
- reasoning_kw = self._reasoning_kwargs(thinking_default)
+ # argv beats the env `unsloth start` pins these through, so repeat it.
+ _env_reasoning = _reasoning_from_env(env)
+ if _env_reasoning is not None:
+ thinking_default = _env_reasoning
+ reasoning_kw = self._reasoning_kwargs(
+ thinking_default, _reasoning_effort_from_env(env)
+ )
+ # An effort ladder thinks at every level but "none" (Inkling: 0), low included.
+ self._reasoning_default = reasoning_kw.get(
+ "enable_thinking", reasoning_kw.get("reasoning_effort") not in ("none", 0)
+ )
# preserve_thinking is independent of the thinking gate.
# Qwen3.8 defaults it on; Qwen3.6, Gemma 4, and every other
# supporting family keep the existing off default. The
diff --git a/studio/backend/tests/test_launch_reasoning_flag.py b/studio/backend/tests/test_launch_reasoning_flag.py
index 923b6befa70..6b0e251b21d 100644
--- a/studio/backend/tests/test_launch_reasoning_flag.py
+++ b/studio/backend/tests/test_launch_reasoning_flag.py
@@ -249,8 +249,18 @@ def _fake_torch(*, rocm: bool, device_count: int) -> types.ModuleType:
return torch
-def _argv_for(tmp_path, monkeypatch, *, os_label, vendor, modern, template):
- """The argv the real ``load_model`` builds on one host, for one vintage."""
+def _launched(
+ tmp_path,
+ monkeypatch,
+ *,
+ os_label,
+ vendor,
+ modern,
+ template,
+ model_identifier = "test",
+ architecture = None,
+):
+ """The backend and argv the real ``load_model`` builds on one host, for one vintage."""
platform, is_wsl = OSES[os_label]
monkeypatch.setattr(llama_cpp.sys, "platform", platform, raising = False)
monkeypatch.setattr(llama_cpp, "_is_wsl", lambda: is_wsl, raising = False)
@@ -261,6 +271,7 @@ def _argv_for(tmp_path, monkeypatch, *, os_label, vendor, modern, template):
)
memory = [] if vendor == "cpu" else [(0, 24_000, 24_000)]
backend, gguf = _backend(tmp_path, vulkan = False, memory = memory)
+ backend._architecture = architecture
monkeypatch.setattr(
LlamaCppBackend,
"probe_server_capabilities",
@@ -269,10 +280,21 @@ def _argv_for(tmp_path, monkeypatch, *, os_label, vendor, modern, template):
captured = _launch(
backend,
gguf,
+ model_identifier = model_identifier,
n_ctx = 4096,
chat_template_override = template,
)
- return captured["cmd"]
+ return backend, captured["cmd"]
+
+
+def _argv_for(tmp_path, monkeypatch, **kwargs):
+ return _launched(tmp_path, monkeypatch, **kwargs)[1]
+
+
[email protected](autouse = True)
+def _no_inherited_reasoning(monkeypatch):
+ monkeypatch.delenv("LLAMA_ARG_REASONING", raising = False)
+ monkeypatch.delenv("LLAMA_ARG_REASONING_EFFORT", raising = False)
def _reasoning_slice(cmd: list[str]) -> list[str]:
@@ -350,6 +372,129 @@ class TestTheRealLaunchOnEveryHost:
assert "reasoning_effort" in _reasoning_slice(cmd)[1]
+class TestAnInheritedReasoningModeIsHonoured:
+ """`unsloth start --reasoning` and `--reasoning-effort` reach Studio only as env.
+
+ The launch always emits its own default, and llama.cpp lets argv beat the env, so
+ the env value has to become that default for the argv and the backend to agree.
+ """
+
+ @pytest.mark.parametrize("modern", [True, False], ids = ["flag", "kwargs"])
+ @pytest.mark.parametrize(
+ "model_identifier,value,expected",
+ [
+ ("unsloth/Qwen3.6-35B-A3B-GGUF", "off", False),
+ ("unsloth/Qwen3.6-35B-A3B-GGUF", "disabled", False),
+ ("unsloth/Qwen3.5-9B-GGUF", "on", True),
+ ("unsloth/Qwen3.5-9B-GGUF", "1", True),
+ ("test", "false", False),
+ ],
+ )
+ def test_on_or_off_overrides_the_default(
+ self, tmp_path, monkeypatch, modern, model_identifier, value, expected
+ ):
+ monkeypatch.setenv("LLAMA_ARG_REASONING", value)
+ backend, cmd = _launched(
+ tmp_path,
+ monkeypatch,
+ os_label = "linux",
+ vendor = "nvidia",
+ modern = modern,
+ template = THINKING_TEMPLATE,
+ model_identifier = model_identifier,
+ )
+ if modern:
+ assert _reasoning_slice(cmd)[:2] == ["--reasoning", "on" if expected else "off"]
+ else:
+ kwargs = json.loads(_reasoning_slice(cmd)[1])
+ assert kwargs["enable_thinking"] is expected
+ assert backend.reasoning_default is expected
+
+ @pytest.mark.parametrize("modern", [True, False], ids = ["flag", "kwargs"])
+ @pytest.mark.parametrize("value", [None, "auto", "-1"])
+ @pytest.mark.parametrize(
+ "model_identifier,expected",
+ [("unsloth/Qwen3.6-35B-A3B-GGUF", True), ("unsloth/Qwen3.5-9B-GGUF", False)],
+ )
+ def test_auto_or_unset_keeps_the_model_default(
+ self, tmp_path, monkeypatch, modern, value, model_identifier, expected
+ ):
+ if value is not None:
+ monkeypatch.setenv("LLAMA_ARG_REASONING", value)
+ backend, cmd = _launched(
+ tmp_path,
+ monkeypatch,
+ os_label = "linux",
+ vendor = "nvidia",
+ modern = modern,
+ template = THINKING_TEMPLATE,
+ model_identifier = model_identifier,
+ )
+ thinking = "true" if expected else "false"
+ if modern:
+ assert _reasoning_slice(cmd) == [
+ "--reasoning",
+ "on" if expected else "off",
+ "--chat-template-kwargs",
+ '{"preserve_thinking": false}',
+ ]
+ else:
+ assert _reasoning_slice(cmd) == [
+ "--chat-template-kwargs",
+ f'{{"enable_thinking": {thinking}, "preserve_thinking": false}}',
+ ]
+ assert backend.reasoning_default is expected
+
+ @pytest.mark.parametrize(
+ "reasoning,effort_env,effort,thinks",
+ [
+ ("on", None, "high", True),
+ ("off", None, "low", True),
+ ("off", "default", "low", True),
+ ("auto", "medium", "medium", True),
+ ("off", "medium", "medium", True),
+ ("on", "none", "none", False),
+ ],
+ )
+ def test_an_effort_ladder_takes_the_pinned_effort(
+ self, tmp_path, monkeypatch, reasoning, effort_env, effort, thinks
+ ):
+ """Off maps to low like the chat toggle, which still thinks; a pinned effort wins."""
+ monkeypatch.setenv("LLAMA_ARG_REASONING", reasoning)
+ if effort_env is not None:
+ monkeypatch.setenv("LLAMA_ARG_REASONING_EFFORT", effort_env)
+ backend, cmd = _launched(
+ tmp_path,
+ monkeypatch,
+ os_label = "linux",
+ vendor = "nvidia",
+ modern = True,
+ template = EFFORT_TEMPLATE,
+ )
+ assert "--reasoning" not in cmd
+ assert json.loads(_reasoning_slice(cmd)[1]) == {"reasoning_effort": effort}
+ assert backend.reasoning_default is thinks
+
+ @pytest.mark.parametrize(
+ "effort_env,effort,thinks", [("medium", 0.7, True), ("none", 0.0, False)]
+ )
+ def test_an_inkling_ladder_coerces_the_pinned_effort(
+ self, tmp_path, monkeypatch, effort_env, effort, thinks
+ ):
+ monkeypatch.setenv("LLAMA_ARG_REASONING_EFFORT", effort_env)
+ backend, cmd = _launched(
+ tmp_path,
+ monkeypatch,
+ os_label = "linux",
+ vendor = "nvidia",
+ modern = True,
+ template = EFFORT_TEMPLATE,
+ architecture = "inkling",
+ )
+ assert json.loads(_reasoning_slice(cmd)[1]) == {"reasoning_effort": effort}
+ assert backend.reasoning_default is thinks
+
+
class TestInheritedExtrasTreatBothSpellingsAlike:
"""Applying a chat template override recomputes the reasoning default.
diff --git a/studio/backend/tests/test_llama_cpp_placement.py b/studio/backend/tests/test_llama_cpp_placement.py
index 35c39951d5b..ef367fc7854 100644
--- a/studio/backend/tests/test_llama_cpp_placement.py
+++ b/studio/backend/tests/test_llama_cpp_placement.py
@@ -145,7 +145,12 @@ def _backend_non_vulkan(
return _backend(*args, vulkan = vulkan, **kwargs)
-def _launch(backend, gguf, **load_kwargs):
+def _launch(
+ backend,
+ gguf,
+ model_identifier = "test",
+ **load_kwargs,
+):
captured = {}
def fake_popen(cmd, **kwargs):
@@ -170,7 +175,7 @@ def _launch(backend, gguf, **load_kwargs):
assert backend.load_model(
GgufLoadIntent(
gguf_path = str(gguf),
- model_identifier = "test",
+ model_identifier = model_identifier,
**load_kwargs,
)
)