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
Studio: stop the SWA config probe from adding a phantom base model and refetching every load (#10607)
Chaitanya Laxman095e66476cda
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex 5755366e2fa..0f4bc5cc686 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -2522,7 +2522,7 @@ _BOOTSTRAP_SWA_DEFAULTS: dict[str, int] = {}# Process-wide cache backed by JSON on disk. Values are int period or-# list[bool] mask. Lazy-loaded.+# list[bool] mask; `__missed_repos__` lists casefolded repos with no SWA field. Lazy-loaded._SWA_CACHE: Optional[dict] = None_SWA_CACHE_LOCK = threading.Lock()@@ -2704,25 +2704,56 @@ def _swa_entry_from_layer_types(lt) -> Optional[object]:return None+_SWA_CONFIRMED_MISS = False+_SWA_MISSED_REPOS_KEY = "__missed_repos__"+++def _swa_missed_repos(cache: dict) -> set:+ missed = cache.get(_SWA_MISSED_REPOS_KEY)+ if not isinstance(missed, list):+ return set()+ return {item.casefold() for item in missed if isinstance(item, str)}+++def _remember_swa_repo_miss(cache: dict, repo_id: str) -> None:+ folded = repo_id.casefold()+ with _SWA_CACHE_LOCK:+ missed = _swa_missed_repos(cache)+ if folded in missed:+ return+ missed.add(folded)+ cache[_SWA_MISSED_REPOS_KEY] = sorted(missed)+ _save_swa_cache(cache)++def _fetch_swa_entry_from_hf(repo_id: str) -> Optional[object]:try:- from huggingface_hub import hf_hub_download+ from huggingface_hub import hf_hub_download, try_to_load_from_cachefrom utils.hf_cache_settings import active_hf_hub_cachefrom utils.hf_probe import hf_file_definitely_absent- # Avoid caching the expected 404 for GGUF repos without config.json.- if hf_file_definitely_absent(repo_id, "config.json"):- return None- cfg_path = call_hub_with_anonymous_retry(- hf_hub_download,- None,- repo_id,- "config.json",- repo_type = "model",- cache_dir = active_hf_hub_cache(),- )- with open(cfg_path, encoding = "utf-8-sig") as f:- cfg = json.load(f)+ cached = try_to_load_from_cache(repo_id, "config.json", cache_dir = active_hf_hub_cache())+ if isinstance(cached, str):+ with open(cached, encoding = "utf-8-sig") as f:+ cfg = json.load(f)+ else:+ # Only a confirmed 404 is a miss; timeouts, 429 and gated stay None so a later load retries.+ if hf_file_definitely_absent(repo_id, "config.json"):+ return _SWA_CONFIRMED_MISS+ # Throwaway cache: a config-only repo in the user's cache lists as a phantom model (#10047).+ with tempfile.TemporaryDirectory(+ prefix = "unsloth-swa-", ignore_cleanup_errors = True+ ) as tmp:+ cfg_path = call_hub_with_anonymous_retry(+ hf_hub_download,+ None,+ repo_id,+ "config.json",+ repo_type = "model",+ cache_dir = tmp,+ )+ with open(cfg_path, encoding = "utf-8-sig") as f:+ cfg = json.load(f)except Exception:return None@@ -2730,7 +2761,8 @@ def _fetch_swa_entry_from_hf(repo_id: str) -> Optional[object]:period = src.get("sliding_window_pattern")if isinstance(period, int) and period > 0:return period- return _swa_entry_from_layer_types(src.get("layer_types"))+ entry = _swa_entry_from_layer_types(src.get("layer_types"))+ return entry if entry is not None else _SWA_CONFIRMED_MISSdef _arch_aliases(arch: str) -> tuple:@@ -2842,12 +2874,21 @@ def _resolve_swa_pattern(_persist(entry)return _entry_to_mask(entry)- # Tier 3: live HF fetch (result persistently cached)+ # Tier 3: live HF fetch. Misses are keyed per repo, not arch: a new GGUF may name a new source.if allow_network:+ seen = set()+ missed = _swa_missed_repos(cache)for repo_id in source_repo_candidates:if not repo_id:continue+ folded = repo_id.casefold()+ if folded in seen or folded in missed:+ continue+ seen.add(folded)entry = _fetch_swa_entry_from_hf(repo_id)+ if entry is _SWA_CONFIRMED_MISS:+ _remember_swa_repo_miss(cache, folded)+ continueif entry is not None:_persist(entry)return _entry_to_mask(entry)diff --git a/studio/backend/tests/test_kv_cache_estimation.py b/studio/backend/tests/test_kv_cache_estimation.pyindex 53553624edc..42738e65c1c 100644--- a/studio/backend/tests/test_kv_cache_estimation.py+++ b/studio/backend/tests/test_kv_cache_estimation.py@@ -613,6 +613,117 @@ class TestDynamicSwaResolver:assert b._sliding_window_pattern is Noneassert not (tmp_path / "swa_cache.json").exists()+ def test_remembered_miss_skips_hf_refetch(self, monkeypatch, tmp_path):+ self._isolate_cache(monkeypatch, tmp_path)+ calls = []+ monkeypatch.setattr(+ lc,+ "_fetch_swa_entry_from_hf",+ lambda repo_id: calls.append(repo_id) or lc._SWA_CONFIRMED_MISS,+ )+ monkeypatch.setattr(lc, "_resolve_swa_entry_from_transformers", lambda arch: None)+ general = {"general.source.huggingface.repository": "vendor/does-not-exist"}+ first = _backend_from_gguf("newmodel", _SWA_FIELDS, general = general)+ assert first._sliding_window_pattern is None+ assert calls == ["vendor/does-not-exist"]+ with open(tmp_path / "swa_cache.json") as f:+ assert json.load(f) == {"__missed_repos__": ["vendor/does-not-exist"]}+ monkeypatch.setattr(lc, "_SWA_CACHE", None)+ second = _backend_from_gguf("newmodel", _SWA_FIELDS, general = general)+ assert second._sliding_window_pattern is None+ assert calls == ["vendor/does-not-exist"]++ def test_transient_hf_error_is_retried(self, monkeypatch, tmp_path):+ self._isolate_cache(monkeypatch, tmp_path)+ calls = []+ monkeypatch.setattr(+ lc, "_fetch_swa_entry_from_hf", lambda repo_id: calls.append(repo_id) or None+ )+ monkeypatch.setattr(lc, "_resolve_swa_entry_from_transformers", lambda arch: None)+ general = {"general.source.huggingface.repository": "vendor/newmodel-base"}+ first = _backend_from_gguf("newmodel", _SWA_FIELDS, general = general)+ assert first._sliding_window_pattern is None+ assert not (tmp_path / "swa_cache.json").exists()+ monkeypatch.setattr(lc, "_SWA_CACHE", None)+ monkeypatch.setattr(+ lc, "_fetch_swa_entry_from_hf", lambda repo_id: calls.append(repo_id) or 4+ )+ second = _backend_from_gguf("newmodel", _SWA_FIELDS, general = general)+ assert second._sliding_window_pattern == [(i + 1) % 4 != 0 for i in range(12)]+ assert calls == ["vendor/newmodel-base", "vendor/newmodel-base"]++ def test_repo_miss_does_not_block_another_candidate(self, monkeypatch, tmp_path):+ self._isolate_cache(monkeypatch, tmp_path)+ calls = []++ def fake_fetch(repo_id):+ calls.append(repo_id)+ return 4 if repo_id == "vendor/newmodel-base" else lc._SWA_CONFIRMED_MISS++ monkeypatch.setattr(lc, "_fetch_swa_entry_from_hf", fake_fetch)+ monkeypatch.setattr(lc, "_resolve_swa_entry_from_transformers", lambda arch: None)+ b = _backend_from_gguf(+ "newmodel",+ _SWA_FIELDS,+ general = {+ "general.source.huggingface.repository": "quanter/newmodel-GGUF",+ "general.base_model.0.repo_url": "https://huggingface.co/vendor/newmodel-base",+ },+ )+ assert b._sliding_window_pattern == [(i + 1) % 4 != 0 for i in range(12)]+ assert calls == ["quanter/newmodel-GGUF", "vendor/newmodel-base"]++ def test_casefold_duplicate_repos_are_fetched_once(self, monkeypatch, tmp_path):+ self._isolate_cache(monkeypatch, tmp_path)+ calls = []+ monkeypatch.setattr(+ lc, "_fetch_swa_entry_from_hf", lambda repo_id: calls.append(repo_id) or None+ )+ monkeypatch.setattr(lc, "_resolve_swa_entry_from_transformers", lambda arch: None)+ b = _backend_from_gguf(+ "newmodel",+ _SWA_FIELDS,+ general = {+ "general.source.huggingface.repository": "DeepSeek-AI/Flash",+ "general.organization": "deepseek-ai",+ "general.basename": "Flash",+ },+ )+ assert b._sliding_window_pattern is None+ assert calls == ["DeepSeek-AI/Flash"]++ def test_hf_fetch_leaves_no_repo_in_the_model_cache(self, monkeypatch, tmp_path):+ import huggingface_hub+ import utils.hf_probe++ active = tmp_path / "hub"+ active.mkdir()+ monkeypatch.setattr("utils.hf_cache_settings.active_hf_hub_cache", lambda: str(active))+ monkeypatch.setattr(utils.hf_probe, "hf_file_definitely_absent", lambda *a, **k: False)++ def fake_download(repo_id, filename, *, cache_dir, **kwargs):+ path = Path(cache_dir) / f"models--{repo_id.replace('/', '--')}" / filename+ path.parent.mkdir(parents = True)+ path.write_text(json.dumps({"sliding_window": 128}))+ return str(path)++ monkeypatch.setattr(huggingface_hub, "hf_hub_download", fake_download)+ assert (+ lc._fetch_swa_entry_from_hf("deepseek-ai/DeepSeek-V4-Flash") is lc._SWA_CONFIRMED_MISS+ )+ assert list(active.iterdir()) == []++ def test_hf_fetch_reads_an_already_cached_config_offline(self, monkeypatch, tmp_path):+ import huggingface_hub++ cfg = tmp_path / "config.json"+ cfg.write_text(json.dumps({"sliding_window_pattern": 6}))+ monkeypatch.setattr(huggingface_hub, "try_to_load_from_cache", lambda *a, **k: str(cfg))+ monkeypatch.setattr(+ huggingface_hub, "hf_hub_download", lambda *a, **k: pytest.fail("network fetch")+ )+ assert lc._fetch_swa_entry_from_hf("org/model") == 6+class TestTransformersIntrospection:"""Tier 2.5: default-init the matching Config; on failure, parse via inspect."""