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: give a local GGUF one variant identity, so an API load applies its saved settings (#10238)
Long Yixingc55dd94b2036
diff --git a/studio/backend/core/inference/local_model_resolver.py b/studio/backend/core/inference/local_model_resolver.pyindex 6d6b2a8b47b..42a33f69ec8 100644--- a/studio/backend/core/inference/local_model_resolver.py+++ b/studio/backend/core/inference/local_model_resolver.py@@ -32,6 +32,7 @@ class _LocalGgufEntry:load_path: strvariants: tuple[str, ...] # local quant labels; () for a standalone .ggufis_gguf: bool = True # False routes the load to the inference orchestrator+ aliases: tuple[tuple[str, str], ...] = () # see _legacy_variant_aliases_CACHE_TTL_S = 5.0@@ -104,12 +105,52 @@ def _resolve_load_dir(p):return p+def _legacy_variant_aliases(variants) -> tuple[tuple[str, str], ...]:+ """``(legacy label, current label)`` for each id this module used to publish and no longer does.++ /v1/models published the loader's label before the swap to the shared lister, and a client+ pins what it was shown. ``DeepSeek-R1-BF16-Q4_K_M.gguf`` was ``BF16``, is ``Q4_K_M``: quant+ shaped, so _resolve_from_index refuses it and the pin 404s. ``Meta-Llama-3-8B.gguf`` was+ ``8B``, is the whole stem: not quant shaped, so it falls through to the Ollama-tag branch and+ quietly serves the preferred quant instead. The quiet one is why quantless labels alias too.++ Accept-only, so nothing new can pin a legacy spelling and a bare id never reaches here.+ Dropped rather than guessed: a legacy label equal to a current one, and one naming two files.+ Grouped rows give one file per quant, so ``alpha-Q4_K_M.gguf`` beside+ ``zeta-BF16-Q4_K_M.gguf`` keeps the 404. Never raises: _local_gguf_entry answers None on an+ escape, which would drop the whole repo.+ """+ try:+ from utils.models.model_config import _extract_quant_label, _qualified_variant_name++ # .lower() matches how _resolve_from_index folds the request+ current = {str(v.quant).lower() for v in variants if getattr(v, "quant", None)}+ seen: dict[str, Optional[str]] = {}+ for variant in variants:+ quant = getattr(variant, "quant", None)+ filename = getattr(variant, "filename", None)+ if not quant or not filename:+ continue+ legacy = _qualified_variant_name(filename, _extract_quant_label(filename))+ key = str(legacy).lower()+ if not key or key in current:+ continue+ # None = ambiguous: a second file claimed it, so it names neither.+ seen[key] = None if key in seen else str(quant)+ return tuple((legacy, quant) for legacy, quant in seen.items() if quant is not None)+ except Exception:+ return ()++def _local_gguf_entry(loader_id: str, info) -> Optional[_LocalGgufEntry]:"""Build an entry only when GGUF quants are on disk (not Transformers/safetensors), listing only on-disk quants. ``load_path`` is a concrete localpath so /load resolves the variant locally and never fetches a remote one."""from pathlib import Path- from utils.models.model_config import detect_gguf_model, list_local_gguf_variants++ # The lister every stored per-model setting is keyed by, so one file has one identity.+ from hub.utils.gguf import list_local_gguf_variants+ from utils.models.model_config import detect_gguf_modelpath = getattr(info, "path", None)if not isinstance(path, str):@@ -127,7 +168,9 @@ def _local_gguf_entry(loader_id: str, info) -> Optional[_LocalGgufEntry]:return Nonereturn _LocalGgufEntry(loader_id, str(p), ())load_dir = _resolve_load_dir(p)- variants, _ = list_local_gguf_variants(str(load_dir))+ # Asked of the lister, not filtered after: two checkpoints can share a quant, and+ # grouping keeps one, so a dead file would take its readable namesake down with it.+ variants, _ = list_local_gguf_variants(str(load_dir), require_existing_files = True)quants = tuple(v.quant for v in variants if getattr(v, "quant", None))if not quants:return None@@ -146,7 +189,9 @@ def _local_gguf_entry(loader_id: str, info) -> Optional[_LocalGgufEntry]:best = preferred_quant(unqualified or quants)if best and quants[0] != best:quants = (best, *(q for q in quants if q != best))- return _LocalGgufEntry(loader_id, str(load_dir), quants)+ return _LocalGgufEntry(+ loader_id, str(load_dir), quants, aliases = _legacy_variant_aliases(variants)+ )except Exception:return None@@ -753,6 +798,11 @@ def _resolve_from_index(for v in entry.variants:if v.lower() == wanted:return entry.load_path, v, entry.loader_id+ # ahead of both branches below: one refuses a retired spelling, the other answers it+ # with a different quant+ for legacy, current in entry.aliases:+ if legacy == wanted:+ return entry.load_path, current, entry.loader_idfrom core.inference.openai_auto_download import looks_like_quantif looks_like_quant(variant):diff --git a/studio/backend/hub/utils/gguf.py b/studio/backend/hub/utils/gguf.pyindex 0986e154420..9e3c885c985 100644--- a/studio/backend/hub/utils/gguf.py+++ b/studio/backend/hub/utils/gguf.py@@ -1408,8 +1408,19 @@ def _resolve_gguf_dir(path: Path) -> Optional[Path]:return None+def _is_existing_file(path: Path) -> bool:+ """Whether *path* exists: ``os.walk`` names a dangling link like any other file."""+ try:+ return path.is_file()+ except OSError:+ return False++def list_local_gguf_variants(- directory: str, model_root: Optional[str] = None+ directory: str,+ model_root: Optional[str] = None,+ *,+ require_existing_files: bool = False,) -> tuple[list[GgufVariantInfo], bool]:root = _resolve_gguf_dir(Path(directory))if root is None:@@ -1429,12 +1440,19 @@ def list_local_gguf_variants(has_vision = False# Match the cache dir of ANY H3 bundle repo: the aggregation runs over whichever mirror the user# actually downloaded.- root_key = root.as_posix().lower()+ # A whole SEGMENT, not a substring: "models--unsloth--MiniMax-H3-GGUF-mirror" (and -v2, -i1)+ # contains the marker while being an ordinary chat repo, and the denoiser filter then left it+ # with no quants -- which withholds the auto-switch entry, so a downloaded model 404s.+ segments = set(root.as_posix().lower().split("/"))h3_bundle_repo = next(- (r for r in _H3_BUNDLE_REPOS if f"models--{r.replace('/', '--')}" in root_key), None+ (r for r in _H3_BUNDLE_REPOS if f"models--{r.replace('/', '--')}" in segments), None)for file in sorted(iter_gguf_files(root, recursive = True)):+ # Off by default: the Hub lists the dangling link an evicted blob leaves, so a user+ # can see and clean that quant. Only a caller advertising what it loads excludes it.+ if require_existing_files and not _is_existing_file(file):+ continueif h3_bundle_repo and not _is_selectable_repo_gguf(h3_bundle_repo, file.name):continueif is_imatrix_filename(file.name):diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.pyindex 6822a6abacc..f35d0568d82 100644--- a/studio/backend/tests/test_openai_auto_switch.py+++ b/studio/backend/tests/test_openai_auto_switch.py@@ -601,6 +601,174 @@ def test_local_gguf_entry_filters_non_gguf_and_recurses(tmp_path):assert e2 is not None and e2.variants+def test_local_gguf_entry_labels_a_quantless_filename_as_the_picker_does(tmp_path):+ # No quant token in the name, so the picker and every saved setting key this variant by+ # the stem. Labelling it "v2" here left an API load reading a key nothing wrote (#10227).+ from types import SimpleNamespace++ from utils.openai_auto_switch_settings import override_lookup_candidates++ repo = tmp_path / "models--org--KAT-GGUF"+ snapshot = repo / "snapshots" / "abc"+ snapshot.mkdir(parents = True)+ (snapshot / "KAT-Coder-V2.5-Dev-APEX-dynamic-v2.gguf").write_text("x")++ entry = resolver._local_gguf_entry("org/KAT-GGUF", SimpleNamespace(path = str(repo)))+ assert entry is not None+ assert entry.variants == ("KAT-Coder-V2.5-Dev-APEX-dynamic-v2",)+ assert "org/KAT-GGUF:KAT-Coder-V2.5-Dev-APEX-dynamic-v2" in override_lookup_candidates(+ entry.load_path, entry.loader_id, entry.variants[0]+ )+++def test_local_gguf_entry_skips_a_quant_whose_blob_is_gone(tmp_path):+ # An evicted blob leaves a dangling link, and advertising it offers a quant no load can+ # open. The twins repo is the other direction: one quant, two checkpoints, dead one first.+ from types import SimpleNamespace++ evicted = tmp_path / "models--org--evicted" / "snapshots" / "abc"+ evicted.mkdir(parents = True)+ (evicted / "m-Q8_0.gguf").write_text("x")+ twins = tmp_path / "models--org--twins" / "snapshots" / "abc"+ twins.mkdir(parents = True)+ (twins / "z-BF16.gguf").write_text("x")+ try:+ (evicted / "m-Q4_K_M.gguf").symlink_to(tmp_path / "evicted-blob")+ (twins / "a-BF16.gguf").symlink_to(tmp_path / "evicted-blob")+ except (NotImplementedError, OSError):+ pytest.skip("symlinks unavailable (Windows without developer mode)")++ entry = resolver._local_gguf_entry(+ "org/evicted", SimpleNamespace(path = str(evicted.parent.parent))+ )+ assert entry is not None and entry.variants == ("Q8_0",)+ assert resolver._resolve_from_index("org/evicted", {"org/evicted": entry})[1] == "Q8_0"++ twin_entry = resolver._local_gguf_entry(+ "org/twins", SimpleNamespace(path = str(twins.parent.parent))+ )+ assert twin_entry is not None and twin_entry.variants == ("BF16",)+++def _one_gguf_repo(tmp_path, name, filenames):+ """An HF-cache repo dir holding *filenames*, and the entry the index builds for it."""+ from types import SimpleNamespace++ snapshot = tmp_path / f"models--org--{name}" / "snapshots" / "abc"+ snapshot.mkdir(parents = True)+ for index, filename in enumerate(filenames):+ (snapshot / filename).write_bytes(b"\0" * (4096 + index * 100))+ return resolver._local_gguf_entry(+ f"org/{name}", SimpleNamespace(path = str(snapshot.parent.parent))+ )+++def test_a_quant_id_v1_models_used_to_publish_still_resolves(tmp_path):+ # /v1/models published the loader's spelling (first token, BF16) where the shared lister+ # takes the K-quant, and a quant-looking label is refused rather than read as a repo tag,+ # so the swap alone would 404 a client pinned to "<repo>:BF16".+ entry = _one_gguf_repo(tmp_path, "float-then-k", ["DeepSeek-R1-BF16-Q4_K_M.gguf"])+ assert entry is not None+ assert entry.variants == ("Q4_K_M",)+ assert entry.aliases == (("bf16", "Q4_K_M"),)++ index = {"org/float-then-k": entry}+ for requested in ("org/float-then-k:BF16", "org/float-then-k:bf16"):+ resolved = resolver._resolve_from_index(requested, index)+ assert resolved is not None, requested+ # the CURRENT label, so the override lookup reads the key the UI wrote+ assert resolved[1] == "Q4_K_M"+ # a quant genuinely not on disk still misses+ assert resolver._resolve_from_index("org/float-then-k:Q8_0", index) is None+++def test_a_legacy_quant_id_never_shadows_a_quant_that_is_really_there(tmp_path):+ # b-BF16.gguf IS the BF16 file; a-BF16-Q4_K_M.gguf merely used to be called that, so the+ # real one keeps the name and no alias is offered.+ entry = _one_gguf_repo(tmp_path, "collide", ["a-BF16-Q4_K_M.gguf", "b-BF16.gguf"])+ assert entry is not None+ assert set(entry.variants) == {"Q4_K_M", "BF16"}+ assert entry.aliases == ()++ index = {"org/collide": entry}+ assert resolver._resolve_from_index("org/collide:BF16", index)[1] == "BF16"+ assert resolver._resolve_from_index("org/collide:Q4_K_M", index)[1] == "Q4_K_M"+++def test_an_ambiguous_legacy_quant_id_resolves_to_nothing(tmp_path):+ # two files, one old label: it names neither, as the override key folding already does.+ entry = _one_gguf_repo(tmp_path, "ambiguous", ["x-BF16-Q4_K_M.gguf", "y-BF16-Q6_K.gguf"])+ assert entry is not None+ assert set(entry.variants) == {"Q4_K_M", "Q6_K"}+ assert entry.aliases == ()+ assert resolver._resolve_from_index("org/ambiguous:BF16", {"org/ambiguous": entry}) is None+++def test_a_quantless_pin_keeps_its_own_weights_instead_of_quietly_getting_the_quant(tmp_path):+ # This repo published "8B" for the unquantized file and ":8b" loaded it. "8B" does not look+ # like a quant, so the swap does not 404 that pin: it falls through to the Ollama-tag branch+ # and quietly serves the Q4_K_M instead. The quiet failure is why quantless labels alias too.+ entry = _one_gguf_repo(+ tmp_path, "quantless-pin", ["Meta-Llama-3-8B.gguf", "Meta-Llama-3-8B-Q4_K_M.gguf"]+ )+ assert entry is not None+ assert entry.aliases == (("8b", "Meta-Llama-3-8B"),)+ resolved = resolver._resolve_from_index("org/quantless-pin:8b", {"org/quantless-pin": entry})+ assert resolved is not None and resolved[1] == "Meta-Llama-3-8B"+ # the bare id never reaches an alias, so preferred_quant still decides it+ bare = resolver._resolve_from_index("org/quantless-pin", {"org/quantless-pin": entry})+ assert bare[1] == entry.variants[0] == "Q4_K_M"+++def test_a_bundle_repos_mirror_is_not_emptied_by_the_h3_filter(tmp_path):+ # A cache dir name only ever extends a repo id by more of that id, so these contain the+ # bundle marker while being ordinary chat repos. Matched as a substring, the denoiser filter+ # left them with no quants, which withholds the entry and 404s a downloaded model.+ from types import SimpleNamespace+ for name in ("MiniMax-H3-GGUF-mirror", "minimax-h3-gguf-i1", "MiniMax-H3-GGUF-BF16"):+ snapshot = tmp_path / f"models--unsloth--{name}" / "snapshots" / "abc"+ snapshot.mkdir(parents = True)+ (snapshot / "m-Q4_K_M.gguf").write_bytes(b"\0" * 4096)+ (snapshot / "m-Q8_0.gguf").write_bytes(b"\0" * 8192)+ entry = resolver._local_gguf_entry(+ f"unsloth/{name}", SimpleNamespace(path = str(snapshot.parent.parent))+ )+ assert entry is not None, f"{name} disappeared from the index"+ assert set(entry.variants) == {"Q4_K_M", "Q8_0"}, name+++def test_a_broken_alias_helper_costs_the_aliases_and_not_the_model(tmp_path, monkeypatch):+ # _local_gguf_entry answers None on any raise, so an escaping shim would drop the repo.+ monkeypatch.setattr(+ resolver,+ "_legacy_variant_aliases",+ lambda variants: (_ for _ in ()).throw(RuntimeError("renamed private helper")),+ )+ entry = _one_gguf_repo(tmp_path, "shim-broke", ["DeepSeek-R1-BF16-Q4_K_M.gguf"])+ assert entry is None, "a raising helper must not be what deletes the entry"++ # And it swallows its own failure: it reaches into two private names in another module.+ monkeypatch.undo()+ from utils.models import model_config++ def renamed_away(*_args, **_kwargs):+ raise AttributeError("_qualified_variant_name")++ monkeypatch.setattr(model_config, "_qualified_variant_name", renamed_away)+ entry = _one_gguf_repo(tmp_path, "shim-renamed", ["DeepSeek-R1-BF16-Q4_K_M.gguf"])+ assert entry is not None and entry.variants == ("Q4_K_M",)+ assert entry.aliases == ()+++def test_a_repo_the_two_listers_agree_on_carries_no_aliases(tmp_path):+ # The overwhelmingly common case: every alias is a cost, so a repo that needs none+ # carries none, and an id naming no local quant keeps missing.+ entry = _one_gguf_repo(tmp_path, "ordinary", ["m-Q4_K_M.gguf", "m-Q8_0.gguf"])+ assert entry is not None+ assert entry.aliases == ()+ assert resolver._resolve_from_index("org/ordinary:BF16", {"org/ordinary": entry}) is None++def test_local_gguf_entry_rejects_standalone_companions(tmp_path, monkeypatch):# Codex P2: _scan_models_dir's standalone-.gguf pass emits an entry for a# bare mmproj projector (it only filters mmproj inside directory scans). A