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: hold the Auto-layers context exemption only while --fit runs (#8825)

Daniel Han27326c4d8590
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 45ceec4a412..eb281cda223 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -6202,7 +6202,8 @@ class LlamaCppBackend:
same 4096 the cap falls back to without a KV estimate.
Exemptions: no Metal budget (0 off Apple Silicon, so this is inert
- there); ``auto_fit``, which omits -c so --fit sizes it; and
+ there); ``auto_fit``, which omits -c so --fit sizes it, and which the
+ caller passes only while --fit will actually run; and
``caller_owns_budget``, a manual mode with a fixed layer count where the
user owns memory management. That last is read off the REQUEST, because
the paravirtual CPU pin rewrites every placement to manual/0 first and
@@ -15413,7 +15414,12 @@ class LlamaCppBackend:
)
_metal_floor = self._metal_zero_ctx_floor(
effective_ctx,
- auto_fit,
+ # Auto-layers earns its exemption by leaving the context to --fit,
+ # so it only holds while --fit actually runs. Extras land after
+ # Studio's own "--fit on" and win, and a "--fit off" there would
+ # otherwise leave a command with no -c and no fitter, which is
+ # llama.cpp's native context.
+ auto_fit and fit_is_effectively_on(extra_args),
_caller_owns_budget,
self._context_length,
max_available_ctx,
diff --git a/studio/backend/tests/test_metal_never_starts_at_native_context.py b/studio/backend/tests/test_metal_never_starts_at_native_context.py
index cbae668ef20..e08f7179611 100644
--- a/studio/backend/tests/test_metal_never_starts_at_native_context.py
+++ b/studio/backend/tests/test_metal_never_starts_at_native_context.py
@@ -332,6 +332,54 @@ class TestAutoLayers:
assert _ctx_values(cmd)[-1] == "0"
+class TestAutoLayersWithTheFitterTurnedOff:
+ """The Auto-layers exemption is only as good as the fitter it defers to.
+
+ Extras land after Studio's own "--fit on" and win, so a pass-through
+ "--fit off" leaves a command carrying no -c and no fitter, which is
+ llama.cpp's native context and the over-commit this branch prevents.
+ """
+
+ AUTO_LAYERS = {"gpu_memory_mode": "manual", "gpu_layers": -1}
+
+ def test_the_floor_applies_once_fitting_is_off(self, tmp_path, monkeypatch):
+ cmd, _ = _launch(tmp_path, monkeypatch, extra_args = ["--fit", "off"], **self.AUTO_LAYERS)
+ assert _ctx_values(cmd) == ["4096"]
+
+ def test_a_zero_override_alongside_it_is_still_dropped(self, tmp_path, monkeypatch):
+ cmd, _ = _launch(
+ tmp_path,
+ monkeypatch,
+ extra_args = ["--fit", "off", "-c", "0"],
+ **self.AUTO_LAYERS,
+ )
+ assert _ctx_values(cmd) == ["4096"]
+
+ def test_an_explicit_fit_on_keeps_the_exemption(self, tmp_path, monkeypatch):
+ cmd, _ = _launch(tmp_path, monkeypatch, extra_args = ["--fit", "on"], **self.AUTO_LAYERS)
+ assert _ctx_values(cmd) == []
+
+ def test_off_metal_nothing_is_touched(self, tmp_path, monkeypatch):
+ cmd, _ = _launch(
+ tmp_path,
+ monkeypatch,
+ extra_args = ["--fit", "off"],
+ metal = False,
+ **self.AUTO_LAYERS,
+ )
+ assert _ctx_values(cmd) == []
+
+ def test_a_caller_owned_budget_is_left_alone(self, tmp_path, monkeypatch):
+ cmd, _ = _launch(
+ tmp_path,
+ monkeypatch,
+ extra_args = ["--fit", "off"],
+ gpu_memory_mode = "manual",
+ gpu_layers = 20,
+ )
+ assert _ctx_values(cmd) == ["0"]
+
+
class TestAnInheritedContextEnvironment:
"""LLAMA_ARG_CTX_SIZE runs -c's own handler, and env parses before argv.