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 GGUF variants: reuse the request's Hub listing for the MLX fallback (#17279)

Daniel Hane172bd324f0c
diff --git a/studio/backend/hub/services/models/gguf_variants.py b/studio/backend/hub/services/models/gguf_variants.py
index 5c9fbfa6feb..eb539e88f94 100644
--- a/studio/backend/hub/services/models/gguf_variants.py
+++ b/studio/backend/hub/services/models/gguf_variants.py
@@ -39,6 +39,7 @@ from hub.utils.hf_cache_state import (
repo_cache_dir_name,
)
from hub.utils.gguf import (
+ HUB_MODEL_INFO_SINK,
GgufVariantInfo,
extract_quant_label,
gguf_variant_family,
@@ -1151,9 +1152,11 @@ async def _mlx_precision_variants_answer(
hf_token: Optional[str],
offline: bool,
cache_readable: bool = True,
+ info = None,
) -> Optional[VariantsAnswer]:
"""The precision folders of an MLX repo (``4-bit/``, ``6-bit/``, ``8bit/``) as its variants,
- else None. Each folder is a whole checkpoint, so a pick downloads and loads that folder alone."""
+ else None. Each folder is a whole checkpoint, so a pick downloads and loads that folder alone.
+ *info* is this request's own ``model_info`` (same token, files_metadata), reused if given."""
if is_local_path(repo_id) or not _is_valid_repo_id(repo_id):
return None
from hub.utils.mlx_variants import cached_precision_folder, looks_like_mlx_repo
@@ -1170,25 +1173,27 @@ async def _mlx_precision_variants_answer(
if not offline:
from huggingface_hub import HfApi
try:
- info = call_with_anonymous_retry(
- lambda token: HfApi(token = token).model_info(
- repo_id, files_metadata = True, timeout = 5.0
- ),
- hf_token,
- )
+ hub_info = info
+ if hub_info is None:
+ hub_info = call_with_anonymous_retry(
+ lambda token: HfApi(token = token).model_info(
+ repo_id, files_metadata = True, timeout = 5.0
+ ),
+ hf_token,
+ )
except Exception as exc: # noqa: BLE001 - fall back to the cached copy below
logger.debug("MLX precision folders for %s unavailable: %s", repo_id, exc)
else:
- tags = {str(tag).lower() for tag in (info.tags or [])}
+ tags = {str(tag).lower() for tag in (hub_info.tags or [])}
if (
"mlx" not in tags
- and str(info.library_name or "").lower() != "mlx"
+ and str(hub_info.library_name or "").lower() != "mlx"
and not looks_like_mlx_repo(repo_id)
):
return None
- sizes = {s.rfilename: int(s.size or 0) for s in (info.siblings or [])}
- hashes = {s.rfilename: sibling_sha256(s) for s in (info.siblings or [])}
- has_vision = str(info.pipeline_tag or "").lower() in _VISION_PIPELINE_TAGS
+ sizes = {s.rfilename: int(s.size or 0) for s in (hub_info.siblings or [])}
+ hashes = {s.rfilename: sibling_sha256(s) for s in (hub_info.siblings or [])}
+ has_vision = str(hub_info.pipeline_tag or "").lower() in _VISION_PIPELINE_TAGS
if not sizes:
# A caller refused this repo's cache learns nothing from it.
if not cache_readable:
@@ -1646,6 +1651,7 @@ async def get_gguf_variants_answer(
cache_authorized = [True]
variant_context_sources: dict[str, str] = {}
hub_listing = None
+ hub_model_info: list = []
# The downloaded rows a Hub-less answer proved complete from disk, see _locally_resolved.
locally_proven: list[Optional[frozenset]] = [None]
@@ -2169,7 +2175,11 @@ async def get_gguf_variants_answer(
try:
if hub_listing is None:
- hub_listing = list_gguf_variants(repo_id, hf_token = hf_token)
+ sink_token = HUB_MODEL_INFO_SINK.set(hub_model_info)
+ try:
+ hub_listing = list_gguf_variants(repo_id, hf_token = hf_token)
+ finally:
+ HUB_MODEL_INFO_SINK.reset(sink_token)
variants, has_vision, siblings = hub_listing
except Exception:
# Ungated: _cache_fallback_response already refuses an unauthorized caller.
@@ -2736,7 +2746,11 @@ async def get_gguf_variants_answer(
# Only after an empty GGUF listing, so GGUF answers never change.
if not answer.response.variants and not local_path:
mlx_answer = await _mlx_precision_variants_answer(
- repo_id, hf_token, offline, cache_readable = answer.cache_authorized
+ repo_id,
+ hf_token,
+ offline,
+ cache_readable = answer.cache_authorized,
+ info = hub_model_info[-1] if hub_model_info and not offline else None,
)
if mlx_answer is not None:
return mlx_answer
diff --git a/studio/backend/hub/utils/gguf.py b/studio/backend/hub/utils/gguf.py
index afa8ac32228..2f5a5b2006e 100644
--- a/studio/backend/hub/utils/gguf.py
+++ b/studio/backend/hub/utils/gguf.py
@@ -5,6 +5,7 @@
from __future__ import annotations
+import contextvars
import os
import re
from dataclasses import dataclass
@@ -1336,6 +1337,12 @@ def _ready_cached_variants(cached: tuple) -> tuple[list[GgufVariantInfo], bool,
return whole or variants, has_vision, None
+# A caller's list here receives the model_info list_gguf_variants fetched, for reuse in the same request.
+HUB_MODEL_INFO_SINK: contextvars.ContextVar[Optional[list]] = contextvars.ContextVar(
+ "gguf_hub_model_info_sink", default = None
+)
+
+
def list_gguf_variants(
repo_id: str, hf_token: Optional[str] = None
) -> tuple[list[GgufVariantInfo], bool, Optional[list]]:
@@ -1374,6 +1381,9 @@ def list_gguf_variants(
return _ready_cached_variants(cached)
raise
+ sink = HUB_MODEL_INFO_SINK.get()
+ if sink is not None:
+ sink.append(info)
has_vision = False
main_files: list[tuple[str, int]] = []
diff --git a/studio/backend/tests/test_gguf_variants_one_listing.py b/studio/backend/tests/test_gguf_variants_one_listing.py
new file mode 100644
index 00000000000..1a782fba51b
--- /dev/null
+++ b/studio/backend/tests/test_gguf_variants_one_listing.py
@@ -0,0 +1,167 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A repo with no GGUF files is listed once per request: the MLX precision fallback reuses the
+model_info the GGUF listing already fetched, with the same answer as a second fetch gave."""
+
+from __future__ import annotations
+
+import asyncio
+from types import SimpleNamespace
+
+import pytest
+
+from hub.services.models import gguf_variants
+from hub.utils import hf_tokens
+
+GB = 1_000_000_000
+
+
+def _folder(folder: str, size: int) -> dict[str, int]:
+ return {
+ f"{folder}/config.json": 4_000,
+ f"{folder}/model.safetensors": size,
+ }
+
+
+_MLX = (
+ "mlx-community/Qwen3-8B-MLX",
+ ("mlx", "safetensors"),
+ "mlx",
+ {**_folder("4-bit", 5 * GB), **_folder("8-bit", 9 * GB), "README.md": 1},
+)
+_SAFETENSORS = (
+ "org/Qwen3-8B",
+ ("safetensors",),
+ "transformers",
+ {"config.json": 4_000, "model.safetensors": 16 * GB, "README.md": 1},
+)
+
+
+def _fake_hub(
+ monkeypatch,
+ tmp_path,
+ tags,
+ library,
+ sizes,
+ refuse_token = False,
+):
+ calls = []
+
+ class _Api:
+ def __init__(self, token = None):
+ self.token = token
+
+ def model_info(
+ self,
+ repo_id,
+ files_metadata = False,
+ timeout = None,
+ **kwargs,
+ ):
+ calls.append((repo_id, self.token, files_metadata, timeout, tuple(sorted(kwargs))))
+ if refuse_token and self.token:
+ raise RuntimeError("401 refused")
+ return SimpleNamespace(
+ tags = list(tags),
+ library_name = library,
+ pipeline_tag = "text-generation",
+ siblings = [
+ SimpleNamespace(rfilename = name, size = size, lfs = {"sha256": f"sha-{name}"})
+ for name, size in sizes.items()
+ ],
+ )
+
+ monkeypatch.setattr("huggingface_hub.HfApi", _Api)
+ monkeypatch.setattr("utils.utils.hf_cache_snapshot_dir_for_repo", lambda repo: None)
+ monkeypatch.setenv("HF_HUB_CACHE", str(tmp_path / "hub"))
+ if refuse_token:
+ monkeypatch.setattr(
+ hf_tokens,
+ "is_rejected_credential_error",
+ lambda exc, token: "401" in str(exc) and bool(token),
+ )
+ return calls
+
+
[email protected]("case", [_MLX, _SAFETENSORS], ids = ["mlx", "safetensors"])
[email protected]("token", [None, "hf_test"], ids = ["anonymous", "token"])
+def test_a_repo_without_gguf_is_listed_once(monkeypatch, tmp_path, case, token):
+ repo, tags, library, sizes = case
+ calls = _fake_hub(monkeypatch, tmp_path, tags, library, sizes)
+ answer = asyncio.run(gguf_variants.get_gguf_variants_answer(repo, hf_token = token))
+ assert len(calls) == 1, calls
+ assert calls[0][2] is True
+ if repo == _MLX[0]:
+ assert answer.response.variant_format == "mlx"
+ assert [v.quant for v in answer.response.variants] == ["4-bit", "8-bit"]
+ else:
+ assert not answer.response.variants
+
+
+def test_a_refused_token_reuses_the_anonymous_listing(monkeypatch, tmp_path):
+ repo, tags, library, sizes = _MLX
+ calls = _fake_hub(monkeypatch, tmp_path, tags, library, sizes, refuse_token = True)
+ answer = asyncio.run(gguf_variants.get_gguf_variants_answer(repo, hf_token = "hf_bad"))
+ assert [token for _, token, *_ in calls] == ["hf_bad", False]
+ assert answer.response.variant_format == "mlx"
+
+
+def test_without_a_hub_listing_the_mlx_fallback_still_fetches(monkeypatch, tmp_path):
+ repo, tags, library, sizes = _MLX
+ calls = _fake_hub(monkeypatch, tmp_path, tags, library, sizes)
+ empty = gguf_variants.VariantsAnswer(gguf_variants.GgufVariantsResponse(repo_id = repo), None)
+
+ async def _scan(
+ key,
+ compute,
+ offline = False,
+ ):
+ return empty
+
+ monkeypatch.setattr(gguf_variants, "_shared_variants_scan", _scan)
+ answer = asyncio.run(gguf_variants.get_gguf_variants_answer(repo))
+ assert len(calls) == 1
+ assert answer.response.variant_format == "mlx"
+
+
+def test_a_failed_listing_is_not_reused(monkeypatch, tmp_path):
+ repo, tags, library, sizes = _MLX
+ calls = _fake_hub(monkeypatch, tmp_path, tags, library, sizes)
+
+ def _boom(*args, **kwargs):
+ raise RuntimeError("hub down")
+
+ monkeypatch.setattr(gguf_variants, "list_gguf_variants", _boom)
+ try:
+ asyncio.run(gguf_variants.get_gguf_variants_answer(repo))
+ except Exception:
+ pass
+ assert all(call[2] is True for call in calls)
+
+
+def test_offline_never_reaches_the_hub(monkeypatch, tmp_path):
+ repo, tags, library, sizes = _MLX
+ calls = _fake_hub(monkeypatch, tmp_path, tags, library, sizes)
+ try:
+ asyncio.run(gguf_variants.get_gguf_variants_answer(repo, offline = True))
+ except Exception:
+ pass
+ assert calls == []
+
+
+def test_list_gguf_variants_hands_back_its_model_info(monkeypatch, tmp_path):
+ from hub.utils.gguf import HUB_MODEL_INFO_SINK, list_gguf_variants
+
+ repo, tags, library, sizes = _MLX
+ calls = _fake_hub(monkeypatch, tmp_path, tags, library, sizes)
+ monkeypatch.delenv("HF_HUB_OFFLINE", raising = False)
+ sink: list = []
+ reset = HUB_MODEL_INFO_SINK.set(sink)
+ try:
+ variants, _, siblings = list_gguf_variants(repo, hf_token = "hf_x")
+ finally:
+ HUB_MODEL_INFO_SINK.reset(reset)
+ assert variants == [] and len(sink) == 1 and len(calls) == 1
+ assert [s.rfilename for s in sink[0].siblings] == [s.rfilename for s in siblings]
+ assert list_gguf_variants(repo, hf_token = "hf_x")[0] == [] and len(sink) == 1