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: download only one copy of a GGUF quant (#11966)

Nilay17398fbc176c
diff --git a/studio/backend/hub/utils/gguf.py b/studio/backend/hub/utils/gguf.py
index 97d90564e82..109dea25579 100644
--- a/studio/backend/hub/utils/gguf.py
+++ b/studio/backend/hub/utils/gguf.py
@@ -72,7 +72,7 @@ GGUF_QUANT_PREFERENCE = [
"F32",
]
-_GGUF_SPLIT_SUFFIX_RE = re.compile(r"-\d{3,}-of-\d{3,}", re.IGNORECASE)
+_GGUF_SPLIT_SUFFIX_RE = re.compile(r"-\d{3,}-of-(\d{3,})", re.IGNORECASE)
_GGUF_QUANT_RE = re.compile(
r"(UD-)?"
r"(MXFP[0-9]+(?:_[A-Z0-9]+)*"
@@ -357,6 +357,12 @@ def gguf_variant_family(filename: str) -> str:
return _unknown_gguf_variant_key(filename)
+def gguf_shard_set(filename: str) -> tuple[str, int]:
+ # A quant shipped both whole and split is two copies of one checkpoint, not one set of shards.
+ split = _GGUF_SPLIT_SUFFIX_RE.search(filename.rsplit("/", 1)[-1])
+ return gguf_variant_family(filename), int(split.group(1)) if split else 0
+
+
def gguf_checkpoint_family(filename: str) -> Optional[str]:
"""the exact checkpoint name shared by quant variants, or ``None`` when unnamed."""
path = filename.replace("\\", "/")
@@ -792,12 +798,12 @@ def _apply_gguf_display_labels(variants: list[GgufVariantInfo]) -> None:
def group_gguf_variant_files(entries) -> dict[str, tuple[str, int]]:
- """``variant key -> (first filename, size of that variant's shard family)``. *entries* is an iterable of ``(path, size)`` for main GGUFs only, already filtered of mmproj, drafters and big-endian builds. Sizes are summed across the shards of ONE family, never across families: a repo that ships the same quant twice (``BF16/QwQ-32B-BF16-*`` beside ``BF16/QwQ-32B.BF16-*``) therefore advertises what a load would actually read rather than the total of both copies. The family kept is the one holding the lexicographically first file, which is the shard the lister and the loader open."""
- families: dict[str, dict[str, list[tuple[str, int]]]] = {}
+ """``variant key -> (first filename, summed size of ONE shard set)``: a quant shipped twice (``QwQ-32B-BF16-*`` beside ``QwQ-32B.BF16-*``, or whole beside split) keeps only the set holding the first file, the one the loader opens."""
+ families: dict[str, dict[tuple[str, int], list[tuple[str, int]]]] = {}
for path, size in entries:
- families.setdefault(gguf_variant_key(path), {}).setdefault(
- gguf_variant_family(path), []
- ).append((path, int(size or 0)))
+ families.setdefault(gguf_variant_key(path), {}).setdefault(gguf_shard_set(path), []).append(
+ (path, int(size or 0))
+ )
grouped: dict[str, tuple[str, int]] = {}
for key, by_family in families.items():
chosen = min(by_family.values(), key = lambda members: min(path for path, _ in members))
diff --git a/studio/backend/hub/utils/gguf_plan.py b/studio/backend/hub/utils/gguf_plan.py
index f7e3c3a6d8c..ebaf4552a3a 100644
--- a/studio/backend/hub/utils/gguf_plan.py
+++ b/studio/backend/hub/utils/gguf_plan.py
@@ -12,6 +12,7 @@ from hub.utils.gguf import (
bare_quant_alias,
drop_shadowed_appledouble_siblings,
extract_quant_label,
+ gguf_shard_set,
gguf_variant_family,
gguf_variant_key,
is_big_endian_gguf_path,
@@ -358,12 +359,12 @@ def plan_for_variant(plans: dict[str, GgufVariantPlan], variant: str) -> Optiona
def _one_shard_family(main_files: Sequence[ExpectedFile]) -> tuple[ExpectedFile, ...]:
- """Narrow a variant's weight files to the single shard family a load would read. A repo can ship one quant twice under names that share a variant key, the same BF16 as ``QwQ-32B-BF16-*`` and ``QwQ-32B.BF16-*``, or one Q6_K under both ``Q6_K/`` and ``<model>-Q6_K/``; fetching both doubles the download and leaves the variant permanently short of its expected bytes, because the loader only ever opens one. Keep the family holding the lexicographically first file, the shard the lister advertises and the loader opens. A genuinely split GGUF is one family, so all of its shards survive untouched."""
+ """Narrow a variant's weight files to the one shard set the loader opens (see ``group_gguf_variant_files``); a genuine split keeps every shard."""
if len(main_files) < 2:
return tuple(main_files)
- families: dict[str, list[ExpectedFile]] = {}
+ families: dict[tuple[str, int], list[ExpectedFile]] = {}
for file in main_files:
- families.setdefault(gguf_variant_family(file.path), []).append(file)
+ families.setdefault(gguf_shard_set(file.path), []).append(file)
if len(families) < 2:
return tuple(main_files)
chosen = min(families.values(), key = lambda group: min(file.path for file in group))
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index 1d4dd57818a..d80ad6e1886 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -4808,16 +4808,14 @@ def _main_variant_gguf_label(rel_path: str) -> Optional[str]:
def _one_shard_family_of(entries: list) -> list:
- """*entries* narrowed to the single shard family the loader would open, as ``(rel, path, size)`` triples.
- Same rule as ``hub.utils.gguf.group_gguf_variant_files``: every shard of one split GGUF shares a family,
- two files that do not are two checkpoints, and the family kept is the one holding the first file."""
+ """``(rel, path, size)`` *entries* narrowed to the one shard set the loader opens, as ``hub.utils.gguf.group_gguf_variant_files``."""
if len(entries) < 2:
return list(entries)
- from hub.utils.gguf import gguf_variant_family
+ from hub.utils.gguf import gguf_shard_set
- families: dict[str, list] = {}
+ families: dict[tuple[str, int], list] = {}
for entry in entries:
- families.setdefault(gguf_variant_family(entry[0]), []).append(entry)
+ families.setdefault(gguf_shard_set(entry[0]), []).append(entry)
if len(families) < 2:
return list(entries)
return min(families.values(), key = lambda group: min(e[0] for e in group))
diff --git a/studio/backend/tests/test_gguf_variant_rows.py b/studio/backend/tests/test_gguf_variant_rows.py
index 1812ffc7d74..02db53adcce 100644
--- a/studio/backend/tests/test_gguf_variant_rows.py
+++ b/studio/backend/tests/test_gguf_variant_rows.py
@@ -463,6 +463,31 @@ def test_a_genuine_split_keeps_every_shard_in_the_plan():
assert plan.download_size_bytes == 30
+def test_a_quant_shipped_whole_and_split_plans_only_the_set_the_loader_opens():
+ """Qwen/Qwen2.5-Coder-7B-Instruct-GGUF ships Q4_K_M both as one file and as two shards."""
+ from routes.models import _one_shard_family_of
+ from utils.models.model_config import _group_gguf_variant_files
+
+ split = [
+ ("qwen2.5-coder-7b-instruct-q4_k_m-00001-of-00002.gguf", 3_993_201_376),
+ ("qwen2.5-coder-7b-instruct-q4_k_m-00002-of-00002.gguf", 689_872_288),
+ ]
+ files = [*split, ("qwen2.5-coder-7b-instruct-q4_k_m.gguf", 4_683_073_536)]
+ opened = _gguf_files_for_variant([path for path, _ in files], "Q4_K_M")
+ assert opened[: len(split)] == [path for path, _ in split]
+
+ plan = build_gguf_variant_plans([_Sibling(path, size) for path, size in files])["q4_k_m"]
+ assert set(plan.target_filenames) == plan.main_filenames == {path for path, _ in split}
+ assert plan.download_size_bytes == plan.main_size_bytes == 4_683_073_664
+
+ row = (split[0][0], 4_683_073_664)
+ assert group_gguf_variant_files(files) == {"q4_k_m": row}
+ assert _group_gguf_variant_files([(p, "q4_k_m", s) for p, s in files]) == {"q4_k_m": row}
+ assert [e[0] for e in _one_shard_family_of([(p, None, s) for p, s in files])] == [
+ path for path, _ in split
+ ]
+
+
def test_a_qualified_key_is_an_explicit_checkpoint_request():
"""``resolve_local_gguf`` falls back to the first local variant for a ``:tag`` that
names no quant, which is right for ``:latest`` and wrong for one of our own rows. The
diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.py
index 8b0179ddc07..443b93cdbcf 100644
--- a/studio/backend/utils/models/model_config.py
+++ b/studio/backend/utils/models/model_config.py
@@ -2759,7 +2759,7 @@ _GGUF_KNOWN_QUANT_RE = re.compile(
_FLOAT_PRECISION_QUANTS = frozenset({"BF16", "F16", "F32"})
-_GGUF_SPLIT_SUFFIX_RE = re.compile(r"-\d{3,}-of-\d{3,}", re.IGNORECASE)
+_GGUF_SPLIT_SUFFIX_RE = re.compile(r"-\d{3,}-of-(\d{3,})", re.IGNORECASE)
def _select_known_quant_match(text: str):
@@ -2820,6 +2820,12 @@ def _gguf_variant_family(filename: str) -> str:
return f"{parents}/{stem}" if parents and stem else stem or "gguf"
+# MIRROR of ``hub.utils.gguf.gguf_shard_set``.
+def _gguf_shard_set(filename: str) -> tuple[str, int]:
+ split = _GGUF_SPLIT_SUFFIX_RE.search(filename.rsplit("/", 1)[-1])
+ return _gguf_variant_family(filename), int(split.group(1)) if split else 0
+
+
# MIRROR of ``hub.utils.gguf._GGUF_BPW_SUFFIX_RE``. Applied with ``match`` against the text that
# follows the token, never ``search``: only a modifier IMMEDIATELY after the quant qualifies it,
# so ``flux1-dev-Q8_0-fp32-08.577bpw`` keeps the bare ``Q8_0``.
@@ -3116,19 +3122,10 @@ def list_gguf_variants(
def _group_gguf_variant_files(entries: list[tuple[str, str, int]]) -> dict[str, tuple[str, int]]:
- """``quant -> (first filename, size of that quant's shard family)``.
-
- MIRROR of ``hub.utils.gguf.group_gguf_variant_files`` over ``(name, quant, size)`` triples.
- Sizes are summed across the shards of ONE family, never across families: a repo shipping the
- same quant twice (QwQ-32B's BF16 as ``QwQ-32B-BF16-*`` beside ``QwQ-32B.BF16-*``) would
- otherwise charge both copies to a row the loader only ever opens one of, and
- ``routes/inference.py`` bills this ``size_bytes`` to the VRAM guard, which then refuses a load
- that fits. The family kept is the one holding the lexicographically first file, which is the
- shard this lister advertises and the loader opens.
- """
- families: dict[str, dict[str, list[tuple[str, int]]]] = {}
+ """MIRROR of ``hub.utils.gguf.group_gguf_variant_files`` over ``(name, quant, size)`` triples; ``routes/inference.py`` bills this size to the VRAM guard."""
+ families: dict[str, dict[tuple[str, int], list[tuple[str, int]]]] = {}
for name, quant, size in entries:
- families.setdefault(quant, {}).setdefault(_gguf_variant_family(name), []).append(
+ families.setdefault(quant, {}).setdefault(_gguf_shard_set(name), []).append(
(name, int(size or 0))
)
grouped: dict[str, tuple[str, int]] = {}