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 Auto context from reserving a model's whole window on a Mac (#15843)

Nilayf33a2b026f4c
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 659f1996ae1..7d9775677db 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -4023,6 +4023,9 @@ _TP_UNMEASURED_CTX = 4096
# Auto offload context when no GPU subset holds the model; prefers a chat-useful size, kept separate from _FIT_MIN_CTX (search and Apple floor).
_AUTO_OFFLOAD_CTX = 8192
+# Auto context on unified memory; 65536 is the floor unsloth start agents need.
+_APPLE_AUTO_CTX = 65536
+
# Must stay >= fit search floor or the Auto re-check flips --fit off (test pins it). amd-smi vs HIP VRAM totals differing over 10% are different scopes.
_AMD_SMI_TOTAL_TOLERANCE = 0.10
@@ -26414,7 +26417,7 @@ class LlamaCppBackend:
max_available_ctx = min(_metal_floor_ctx, native_ctx_for_cap)
_apple_ctx_unmeasured = True
if not explicit_ctx:
- effective_ctx = max_available_ctx
+ effective_ctx = min(max_available_ctx, _APPLE_AUTO_CTX)
elif _apple_ctx_unmeasured:
# Never publish a ceiling below the launched context.
max_available_ctx = max(max_available_ctx, effective_ctx)
@@ -26530,9 +26533,10 @@ class LlamaCppBackend:
and _vram_fit_ctx
and min(_FIT_MIN_CTX, effective_ctx)
<= _vram_fit_ctx
- < effective_ctx
+ < max_available_ctx
):
- effective_ctx = max_available_ctx = _vram_fit_ctx
+ max_available_ctx = _vram_fit_ctx
+ effective_ctx = min(effective_ctx, _vram_fit_ctx)
# Unmeasured floors still rely on llama.cpp's fitter.
_metal_full_offload_pinned = bool(
_apple_measured_ceiling is not None
diff --git a/studio/backend/tests/test_metal_explicit_context_guard.py b/studio/backend/tests/test_metal_explicit_context_guard.py
index 866899c17aa..f089ddae042 100644
--- a/studio/backend/tests/test_metal_explicit_context_guard.py
+++ b/studio/backend/tests/test_metal_explicit_context_guard.py
@@ -1757,6 +1757,14 @@ class TestAutoGivesUpContextBeforeLayers:
assert _FIT_MIN_CTX <= ctx <= 24576 < budget_ctx
assert backend._max_context_length == ctx
+ def test_the_ceiling_above_the_auto_default_still_trims_to_the_wired_limit(
+ self, tmp_path, monkeypatch
+ ):
+ roomy = dict(self.FITS, budget_bytes = 32 * self.GiB)
+ cap = _launch(tmp_path, monkeypatch, n_ctx = 0, wired_bytes = 12 * self.GiB, **roomy)
+ assert _ctx_values(cap["cmd"])[-1] == "65536"
+ assert 65536 < cap["backend"]._max_context_length < 131072
+
def test_ample_headroom_keeps_the_budget_context(self, tmp_path, monkeypatch):
assert self._ctx(tmp_path, monkeypatch, 20)[0] == self._ctx(tmp_path, monkeypatch, 0)[0]
@@ -1906,3 +1914,20 @@ class TestMetalAlongsideAdmission:
_launch(tmp_path, monkeypatch, budget_bytes = 9 * 1024**3, **args)
assert not backend.is_active
assert _launch(tmp_path, monkeypatch, budget_bytes = 32 * 1024**3, **args)["cmd"]
+
+
+class TestAutoOnAMacWithRoomToSpare:
+ _ROOMY = dict(real_fit = True, budget_bytes = 64 * 1024**3, weights_bytes = 1024**3)
+
+ def test_auto_does_not_reserve_the_whole_native_window(self, tmp_path, monkeypatch):
+ captured = _launch(tmp_path, monkeypatch, n_ctx = 0, **self._ROOMY)
+ assert _ctx_values(captured["cmd"])[-1] == "65536"
+ assert captured["backend"]._max_context_length == NATIVE
+
+ def test_an_explicit_context_above_the_auto_default_still_loads(self, tmp_path, monkeypatch):
+ cmd = _launch(tmp_path, monkeypatch, n_ctx = 131072, **self._ROOMY)["cmd"]
+ assert _ctx_values(cmd)[-1] == "131072"
+
+ def test_a_shorter_native_window_keeps_its_own_length(self, tmp_path, monkeypatch):
+ cmd = _launch(tmp_path, monkeypatch, n_ctx = 0, native = 40960, **self._ROOMY)["cmd"]
+ assert _ctx_values(cmd)[-1] == "40960"