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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
fix: context override from JIT API load (#11524)
Jyotirmoy Roydef4d4791252
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex f20fbbd52df..737184b51ab 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -7662,6 +7662,16 @@ def _public_model_identifier(requested: str, resolved: str) -> str:return requested if is_ollama_manifest_ref(requested) else resolved+def _canonical_model_identity(model_id: Optional[str]) -> str:+ """Case-folded *model_id*, reading an HF cache snapshot path as its ``org/name`` repo id.+ Other local paths stay distinct: two files sharing a stem are two models."""+ from core.inference.model_ids import hf_cache_repo_id++ if not model_id:+ return ""+ return (hf_cache_repo_id(model_id) or str(model_id)).lower()++def _as_ollama_manifest_request(request):"""*request* with a materialized Ollama ``.gguf`` link rewritten to the tag that produced it."""from hub.services.models.ollama import ollama_manifest_ref_for_path, ollama_model_ref_files@@ -14786,7 +14796,11 @@ def _resolve_inherited_extra_args(resolved_variant = (config.gguf_variant or "").lower()request_variant = (request.gguf_variant or "").lower()stored_variant = (source[1] or "").lower() if source else ""- same_model = bool(source and source[0] and source[0].lower() == model_identifier.lower())+ # A picker load records the snapshot path, an API auto-switch loads the repo id.+ stored_identity = _canonical_model_identity(source[0]) if source else ""+ same_model = bool(+ stored_identity and stored_identity == _canonical_model_identity(model_identifier)+ )if request.gguf_variant:variant_mismatch = request_variant != stored_variantelse:diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.pyindex aba099e0886..916020adaa3 100644--- a/studio/backend/routes/settings.py+++ b/studio/backend/routes/settings.py@@ -1867,10 +1867,11 @@ def update_openai_auto_switch_override() -> ModelOverridesResponse:from core.inference.llama_server_args import (drop_managed_flags,+ parse_ctx_override,strip_shadowing_flags,validate_extra_args,)- from utils.openai_auto_switch_settings import get_model_override+ from utils.openai_auto_switch_settings import MAX_SEQ_LENGTH_CEILING, get_model_overridetry:if payload.fill_absent_fields and payload.remove is True:@@ -2056,12 +2057,27 @@ def update_openai_auto_switch_override():_kept_reasoning_budget = -1_kept_reasoning_budget_message = ""+ # A -c sent with this save is what its load runs at (llama.cpp takes the last -c); store it as+ # the context or auto-switch strips it as stale (#11511). Carried-over flags and fills keep that rule.+ max_seq_length = payload.max_seq_length+ custom_context_length = payload.custom_context_length+ if payload.llama_extra_args is not None and not payload.fill_absent_fields:+ try:+ explicit_ctx = parse_ctx_override(extra_args)+ except ValueError:+ explicit_ctx = None+ # Past the stored ceiling the field would be dropped, leaving the flag unchecked.+ if explicit_ctx and explicit_ctx <= MAX_SEQ_LENGTH_CEILING:+ if max_seq_length is not None:+ max_seq_length = explicit_ctx+ if custom_context_length is not None:+ custom_context_length = explicit_ctxset_model_override(target_id,llama_extra_args = extra_args,keep_empty_extra_args = keep_empty,- max_seq_length = payload.max_seq_length,- custom_context_length = payload.custom_context_length,+ max_seq_length = max_seq_length,+ custom_context_length = custom_context_length,kv_cache_dtype = payload.kv_cache_dtype,mlx_kv_bits = payload.mlx_kv_bits,speculative_type = payload.speculative_type,diff --git a/studio/backend/tests/test_gguf_reload_inheritance.py b/studio/backend/tests/test_gguf_reload_inheritance.pyindex d9351ff7397..8b4fdd0fa57 100644--- a/studio/backend/tests/test_gguf_reload_inheritance.py+++ b/studio/backend/tests/test_gguf_reload_inheritance.py@@ -548,3 +548,244 @@ def test_an_inherited_env_budget_does_not_force_a_reload():)is True)+++# ── Canonical identity normalization ────────────────────────────+++def test_canonical_model_identity_resolves_snapshot_to_repo():+ from routes.inference import _canonical_model_identity+ snapshot = "/models--unsloth--Qwen3.8-27B-GGUF/snapshots/abc123"+ assert _canonical_model_identity(snapshot) == "unsloth/qwen3.8-27b-gguf"+++def test_canonical_model_identity_folds_repo_id_case():+ from routes.inference import _canonical_model_identity+ assert _canonical_model_identity("unsloth/Qwen3.8-27B-GGUF") == "unsloth/qwen3.8-27b-gguf"+++def test_canonical_model_identity_reads_windows_snapshot_path():+ from routes.inference import _canonical_model_identity+ snapshot = r"C:\hub\models--unsloth--Qwen3.8-27B-GGUF\snapshots\abc123"+ assert _canonical_model_identity(snapshot) == "unsloth/qwen3.8-27b-gguf"+++def test_canonical_model_identity_empty_and_none():+ from routes.inference import _canonical_model_identity+ assert _canonical_model_identity("") == ""+ assert _canonical_model_identity(None) == ""+++def test_canonical_model_identity_keeps_other_local_paths():+ from routes.inference import _canonical_model_identity+ assert _canonical_model_identity("/models/my-model.gguf") == "/models/my-model.gguf"+ # A models-- folder outside the cache layout is not a repo id.+ assert _canonical_model_identity("/models--org--name/x.gguf") == "/models--org--name/x.gguf"+++# ── _resolve_inherited_extra_args: snapshot vs repo id ──────────+++def test_inheritance_works_when_stored_is_snapshot_and_load_is_repo():+ """Storing extras under a snapshot-path identifier and resolving on a JIT+ auto-switch load of the same repo id must still inherit them.+ """+ from routes.inference import _resolve_inherited_extra_args++ class FakeRequest:+ model_path = "unsloth/Qwen3.8-27B-GGUF"+ gguf_variant = None+ llama_extra_args = None+ model_fields_set = set()++ class FakeConfig:+ is_gguf = True+ gguf_variant = "ud-q6_k_l"+ identifier = "unsloth/Qwen3.8-27B-GGUF"++ class FakeBackend:+ extra_args = ["--rope-scaling", "yarn", "-c", "300000"]+ extra_args_source = ("/models--unsloth--Qwen3.8-27B-GGUF/snapshots/abc123", "ud-q6_k_l")++ import routes.inference as _routes++ _get_llama_cpp_backend_orig = _routes.get_llama_cpp_backend+ _routes.get_llama_cpp_backend = lambda: FakeBackend()+ try:+ result = _resolve_inherited_extra_args(+ FakeRequest(), FakeConfig(), "unsloth/Qwen3.8-27B-GGUF", None+ )+ assert result == [+ "--rope-scaling",+ "yarn",+ "-c",+ "300000",+ ], f"Expected inherited extras but got: {result}"+ finally:+ _routes.get_llama_cpp_backend = _get_llama_cpp_backend_orig+++def test_inheritance_works_when_case_differs():+ """A stored snapshot/repo with different casing than the load model_identifier+ still inherits extras for the same Hub GGUF + same variant."""+ from routes.inference import _resolve_inherited_extra_args++ class FakeRequest:+ model_path = "unsloth/Qwen3.8-27B-GGUF"+ gguf_variant = None+ llama_extra_args = None+ model_fields_set = set()++ class FakeConfig:+ is_gguf = True+ gguf_variant = "ud-q6_k_l"+ identifier = "unsloth/Qwen3.8-27B-GGUF"++ class FakeBackend:+ extra_args = ["--rope-scaling", "yarn", "-c", "300000"]+ extra_args_source = ("/models--Unsloth--Qwen3.8-27B-GGUF/snapshots/abc123", "ud-q6_k_l")++ import routes.inference as _routes++ _get_llama_cpp_backend_orig = _routes.get_llama_cpp_backend+ _routes.get_llama_cpp_backend = lambda: FakeBackend()+ try:+ result = _resolve_inherited_extra_args(+ FakeRequest(), FakeConfig(), "unsloth/qwen3.8-27b-gguf", None+ )+ assert result == [+ "--rope-scaling",+ "yarn",+ "-c",+ "300000",+ ], f"Expected inherited extras for case-variant match, got: {result}"+ finally:+ _routes.get_llama_cpp_backend = _get_llama_cpp_backend_orig+++def test_inheritance_refuses_different_repo():+ """Different repo ids must not inherit extras."""+ from routes.inference import _resolve_inherited_extra_args++ class FakeRequest:+ model_path = "different/repo"+ gguf_variant = None+ llama_extra_args = None+ model_fields_set = set()++ class FakeConfig:+ is_gguf = True+ gguf_variant = None+ identifier = "different/repo"++ class FakeBackend:+ extra_args = ["--rope-scaling", "yarn"]+ extra_args_source = ("/models--unsloth--Qwen3.8-27B-GGUF/snapshots/abc123", "ud-q6_k_l")++ import routes.inference as _routes++ _get_llama_cpp_backend_orig = _routes.get_llama_cpp_backend+ _routes.get_llama_cpp_backend = lambda: FakeBackend()+ try:+ result = _resolve_inherited_extra_args(FakeRequest(), FakeConfig(), "different/repo", None)+ assert result == [], f"Expected empty extras for cross-model, got: {result}"+ finally:+ _routes.get_llama_cpp_backend = _get_llama_cpp_backend_orig+++def test_inheritance_refuses_different_variant():+ """Different variants must not inherit extras."""+ from routes.inference import _resolve_inherited_extra_args++ class FakeRequest:+ model_path = "unsloth/Qwen3.8-27B-GGUF"+ gguf_variant = "Q4_K_M"+ llama_extra_args = None+ model_fields_set = set()++ class FakeConfig:+ is_gguf = True+ gguf_variant = "Q4_K_M"+ identifier = "unsloth/Qwen3.8-27B-GGUF"++ class FakeBackend:+ extra_args = ["--rope-scaling", "yarn"]+ extra_args_source = ("/models--unsloth--Qwen3.8-27B-GGUF/snapshots/abc123", "q6_k_l")++ import routes.inference as _routes++ _get_llama_cpp_backend_orig = _routes.get_llama_cpp_backend+ _routes.get_llama_cpp_backend = lambda: FakeBackend()+ try:+ result = _resolve_inherited_extra_args(+ FakeRequest(), FakeConfig(), "unsloth/Qwen3.8-27B-GGUF", None+ )+ assert result == [], f"Expected empty extras for variant mismatch, got: {result}"+ finally:+ _routes.get_llama_cpp_backend = _get_llama_cpp_backend_orig+++def test_explicit_empty_extras_still_clears():+ """An explicit [] still clears extras regardless of identity match."""+ from routes.inference import _resolve_inherited_extra_args++ class FakeRequest:+ model_path = "unsloth/Qwen3.8-27B-GGUF"+ gguf_variant = None+ llama_extra_args = []+ model_fields_set = set()++ class FakeConfig:+ is_gguf = True+ gguf_variant = None+ identifier = "unsloth/Qwen3.8-27B-GGUF"++ class FakeBackend:+ extra_args = ["--rope-scaling", "yarn"]+ extra_args_source = ("unsloth/Qwen3.8-27B-GGUF", "ud-q6_k_l")++ import routes.inference as _routes++ _get_llama_cpp_backend_orig = _routes.get_llama_cpp_backend+ _routes.get_llama_cpp_backend = lambda: FakeBackend()+ try:+ result = _resolve_inherited_extra_args(+ FakeRequest(), FakeConfig(), "unsloth/Qwen3.8-27B-GGUF", None+ )+ assert result is None # early return because llama_extra_args is not None (it's [])+ finally:+ _routes.get_llama_cpp_backend = _get_llama_cpp_backend_orig+++def test_missing_extra_args_source_refuses_without_raising():+ """Stored extras with no recorded source are not inherited, and the refusal log+ must not index into the missing source."""+ from routes.inference import _resolve_inherited_extra_args+ import routes.inference as _routes++ class FakeRequest:+ model_path = "unsloth/Qwen3.8-27B-GGUF"+ gguf_variant = None+ llama_extra_args = None+ model_fields_set = set()++ class FakeConfig:+ is_gguf = True+ gguf_variant = "ud-q6_k_l"+ identifier = "unsloth/Qwen3.8-27B-GGUF"++ original = _routes.get_llama_cpp_backend+ try:+ for source in (None, (None, "ud-q6_k_l"), ("", None)):++ class FakeBackend:+ extra_args = ["--rope-scaling", "yarn"]+ extra_args_source = source++ _routes.get_llama_cpp_backend = lambda: FakeBackend()+ result = _resolve_inherited_extra_args(+ FakeRequest(), FakeConfig(), "unsloth/Qwen3.8-27B-GGUF", None+ )+ assert result == [], (source, result)+ finally:+ _routes.get_llama_cpp_backend = originaldiff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.pyindex 378af06c27e..6e9eaa95c6b 100644--- a/studio/backend/tests/test_openai_auto_switch.py+++ b/studio/backend/tests/test_openai_auto_switch.py@@ -7192,6 +7192,76 @@ def test_a_matching_explicit_ctx_flag_survives_auto_switch(monkeypatch):assert request.llama_extra_args == ["--ctx-size", "100352"]+def test_a_ctx_flag_saved_from_the_picker_reaches_an_api_load(monkeypatch):+ """#11511: the picker saves its slider context beside a typed -c. Its own load runs at the+ -c (llama.cpp takes the last one), so an API auto-switch of the same row must too, instead+ of stripping the flag as a stale shadow of the slider."""+ _mock_override_store(monkeypatch)+ saved = _put(+ "unsloth/B-GGUF:Q4_K_M",+ llama_extra_args = ["-c", "300000", "--rope-scaling", "yarn"],+ custom_context_length = 262144,+ )+ entry = saved.overrides["unsloth/B-GGUF:Q4_K_M"]+ assert entry["custom_context_length"] == 300000+ assert entry["llama_extra_args"] == ["-c", "300000", "--rope-scaling", "yarn"]++ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF")+ )+ _run_hook("unsloth/B-GGUF")+ request = rec.calls[0]+ assert request.max_seq_length == 300000+ assert request.llama_extra_args == ["-c", "300000", "--rope-scaling", "yarn"]+++ "extra_args, fields, expected",+ [+ # llama.cpp's last -c wins, in either spelling.+ (+ ["--ctx-size", "8192", "-c", "65536"],+ {"max_seq_length": 4096, "custom_context_length": 4096},+ {"max_seq_length": 65536, "custom_context_length": 65536},+ ),+ # -c 0 asks llama.cpp for the model's own context: nothing to record.+ (["-c", "0"], {"custom_context_length": 4096}, {"custom_context_length": 4096}),+ # No context field sent: none is invented, the flag stays the only control.+ (["-c", "65536"], {"kv_cache_dtype": "q8_0"}, {}),+ # No -c: the slider value is stored as sent.+ (["--top-k", "40"], {"custom_context_length": 4096}, {"custom_context_length": 4096}),+ # Past the stored ceiling the slider value stays, so the flag is still checked on load.+ (["-c", "99999999"], {"custom_context_length": 4096}, {"custom_context_length": 4096}),+ ],+)+def test_a_saved_ctx_flag_sets_only_the_context_fields_sent(+ monkeypatch, extra_args, fields, expected+):+ _mock_override_store(monkeypatch)+ saved = _put("unsloth/B-GGUF:Q4_K_M", llama_extra_args = extra_args, **fields)+ entry = saved.overrides["unsloth/B-GGUF:Q4_K_M"]+ stored = {+ key: entry[key] for key in ("max_seq_length", "custom_context_length") if key in entry+ }+ assert stored == expected+++def test_a_fill_keeps_the_sent_context_when_it_does_not_store_the_flag(monkeypatch):+ """A fill (the localStorage migration) keeps a stored row's flags, so a -c in its payload is+ not what any load will run with and must not rewrite the context."""+ _mock_override_store(monkeypatch)+ _put("unsloth/B-GGUF:Q4_K_M", llama_extra_args = ["--top-k", "7"])+ saved = _put(+ "unsloth/B-GGUF:Q4_K_M",+ llama_extra_args = ["-c", "65536"],+ custom_context_length = 4096,+ fill_absent_fields = True,+ )+ entry = saved.overrides["unsloth/B-GGUF:Q4_K_M"]+ assert entry["llama_extra_args"] == ["--top-k", "7"]+ assert entry["custom_context_length"] == 4096++@pytest.mark.parametrize("stored_max_seq_length",["100352", "not-a-number", 100352.0, True, [100352], {"v": 100352}],