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.

22.1 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

from __future__ import annotations

import re
from dataclasses import dataclass
from typing import Optional, Sequence

from hub.utils.download_manifest import ExpectedFile
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,
    is_gguf_filename,
    is_imatrix_filename,
    is_mmproj_filename,
    is_mtp_drafter_path,
)


@dataclass(frozen = True)
class GgufVariantPlan:
    main_filenames: frozenset[str]
    target_filenames: tuple[str, ...]
    main_hashes: frozenset[str]
    required_hashes: frozenset[str]
    companion_hashes: frozenset[str]
    mmproj_filenames: frozenset[str]
    mmproj_hashes: frozenset[str]
    expected_files: tuple[ExpectedFile, ...]
    main_size_bytes: int
    download_size_bytes: int


def sibling_sha256(sibling) -> Optional[str]:
    lfs = getattr(sibling, "lfs", None)
    if isinstance(lfs, dict):
        value = lfs.get("sha256")
    else:
        value = getattr(lfs, "sha256", None)
    if isinstance(value, str) and value:
        return value
    blob_id = getattr(sibling, "blob_id", None)
    return blob_id if isinstance(blob_id, str) and blob_id else None


def sibling_size(sibling) -> int:
    size = getattr(sibling, "size", 0) or 0
    try:
        return int(size)
    except (TypeError, ValueError):
        return 0


def expected_file_from_sibling(sibling) -> Optional[ExpectedFile]:
    name = getattr(sibling, "rfilename", None)
    if not isinstance(name, str):
        return None
    return ExpectedFile(
        path = name,
        size = sibling_size(sibling),
        sha256 = sibling_sha256(sibling),
    )


def is_companion_gguf_path(path: str) -> bool:
    """Companion (non-main) GGUF downloaded alongside a variant: the vision mmproj or the separate MTP drafter (Gemma 4)."""
    return is_gguf_filename(path) and (is_mmproj_filename(path) or is_mtp_drafter_path(path))


def is_main_gguf_variant_path(path: str, variant: str) -> bool:
    """Whether *path* is one of *variant*'s own weight files. Keyed on :func:`gguf_variant_key`, in lockstep with the listers: a row built under one identity and matched under another produces a variant that can be shown but not downloaded."""
    return (
        is_gguf_filename(path)
        and not is_mmproj_filename(path)
        and not is_mtp_drafter_path(path)
        and not is_imatrix_filename(path)
        # The endian predicate reads a quant TOKEN, so hand it the label: given the qualified key it cannot see a parent-only quant and drops the file, leaving the plan with no main files.
        and not is_big_endian_gguf_path(path, extract_quant_label(path))
        and gguf_variant_key(path).lower() == variant.lower()
    )


def _gguf_rfilename(sibling) -> Optional[str]:
    """The sibling's rfilename when it is a GGUF, else None."""
    name = getattr(sibling, "rfilename", None)
    if isinstance(name, str) and is_gguf_filename(name):
        return name
    return None


def mmproj_siblings(siblings: Sequence) -> list:
    return [s for s in siblings if (name := _gguf_rfilename(s)) and is_mmproj_filename(name)]


def preferred_mmproj_sibling(siblings: Sequence) -> Optional[object]:
    candidates = mmproj_siblings(siblings)
    if not candidates:
        return None
    from utils.models.model_config import mmproj_order, mmproj_order_rank

    order = mmproj_order(*(_gguf_rfilename(s) for s in siblings))
    if order:
        return max(candidates, key = lambda s: mmproj_order_rank(getattr(s, "rfilename"), order))
    return next(
        (s for s in candidates if extract_quant_label(getattr(s, "rfilename")).upper() == "F16"),
        candidates[0],
    )


def preferred_mtp_sibling(siblings: Sequence) -> Optional[object]:
    """Choose the root MTP drafter used by normal llama.cpp discovery, or the loader-compatible nested sidecar required by Qwen3.8 Flash Next."""
    from utils.models.drafters import (
        is_published_drafter_filename,
        split_listing_is_complete,
    )

    gguf_names = [name for sibling in siblings if (name := _gguf_rfilename(sibling))]

    def _complete(name: str) -> bool:
        # Match detect_mtp_file launchability gate; a half-published family must step aside before ranking.
        return split_listing_is_complete(gguf_names, name)

    # Root-level only: the MTP/ subdir copies now share the mtp- prefix too.
    candidates = sorted(
        (
            s
            for s in siblings
            if (name := _gguf_rfilename(s))
            and "/" not in name
            and is_published_drafter_filename(name, kind = "mtp", allow_legacy_suffix = False)
            and _complete(name)
        ),
        key = lambda s: getattr(s, "rfilename"),
    )
    if candidates:
        return candidates[0]

    # Remote planner cannot read the GGUF header to prove qwen4exp; match the family token.
    flash_next = any(
        re.search(r"(?:^|[/_-])qwen3\.8-flash-next(?:$|[/_.-])", name, re.IGNORECASE)
        for sibling in siblings
        if (name := _gguf_rfilename(sibling)) and not is_mtp_drafter_path(name)
    )
    if not flash_next:
        return None

    from utils.models.drafters.preference import mtp_preference_key

    nested = [
        sibling
        for sibling in siblings
        if (name := _gguf_rfilename(sibling))
        and "/" in name.replace("\\", "/")
        and is_mtp_drafter_path(name)
        and is_published_drafter_filename(name.replace("\\", "/").rsplit("/", 1)[-1], kind = "mtp")
        and _complete(name)
    ]
    return (
        min(nested, key = lambda sibling: mtp_preference_key(sibling.rfilename)) if nested else None
    )


def mtp_plan_files(siblings: Sequence) -> tuple[ExpectedFile, ...]:
    """Every shard of the MTP sidecar selected by the loader-compatible preference rule."""
    from utils.models.drafters import (
        is_published_drafter_filename,
        split_listing_is_complete,
    )

    selected = preferred_mtp_sibling(siblings)
    selected_name = _gguf_rfilename(selected) if selected is not None else None
    if selected_name is None:
        return ()
    family = gguf_variant_family(selected_name)
    nested = "/" in selected_name.replace("\\", "/")
    files = tuple(
        sorted(
            (
                file
                for sibling in siblings
                if (name := _gguf_rfilename(sibling))
                and gguf_variant_family(name) == family
                and is_mtp_drafter_path(name)
                and is_published_drafter_filename(
                    name.replace("\\", "/").rsplit("/", 1)[-1],
                    kind = "mtp",
                    allow_legacy_suffix = nested,
                )
                and (file := expected_file_from_sibling(sibling)) is not None
            ),
            key = lambda file: file.path,
        )
    )
    if not files or not split_listing_is_complete([file.path for file in files], selected_name):
        return ()
    return files


def preferred_dflash_sibling(
    siblings: Sequence,
    weight_name: Optional[str] = None,
    other_weight_names: Sequence[str] = (),
) -> Optional[object]:
    """The DFlash sidecar to fetch alongside ``weight_name``. Root level only, like preferred_mtp_sibling: detect_dflash_file never offers a nested ``quants/dflash-*.gguf``, and a listing cannot read a header, so matching the basename would plan a whole ordinary weight nothing could reject in time. Ordered by dflash_repo_preference_key, as the download, snapshot reuse and offline cache are, so the manifest promises the file the loader launches."""
    from utils.models.drafters import dflash_repo_preference_key

    candidates = [
        s
        for s in siblings
        if (name := _gguf_rfilename(s)) and "/" not in name and name.lower().startswith("dflash-")
    ]
    if not candidates:
        return None
    return min(
        candidates,
        key = lambda s: dflash_repo_preference_key(
            getattr(s, "rfilename"), weight_name, other_weight_names
        ),
    )


def dflash_plan_files(
    siblings: Sequence,
    weight_name: Optional[str] = None,
    other_weight_names: Sequence[str] = (),
    *,
    max_bytes: int = 0,
) -> tuple[ExpectedFile, ...]:
    """Whole DFlash shard family; bounded by max_bytes, and an unknown size stays out."""
    from utils.models.drafters import dflash_repo_preference_key, split_listing_is_complete

    families: dict[str, list[ExpectedFile]] = {}
    for sibling in siblings:
        name = _gguf_rfilename(sibling)
        if not name or "/" in name or not name.lower().startswith("dflash-"):
            continue
        file = expected_file_from_sibling(sibling)
        if file is not None:
            families.setdefault(gguf_variant_family(name), []).append(file)

    eligible: dict[str, tuple[ExpectedFile, ...]] = {}
    for family, files in families.items():
        shards = tuple(sorted(files, key = lambda file: file.path))
        if not split_listing_is_complete([f.path for f in shards], shards[0].path):
            continue
        total = sum(max(0, int(file.size or 0)) for file in shards)
        if not total or max_bytes <= 0 or total >= max_bytes:
            continue
        eligible[family] = shards
    if not eligible:
        return ()
    best = min(
        eligible,
        key = lambda family: dflash_repo_preference_key(
            eligible[family][0].path, weight_name, other_weight_names
        ),
    )
    return eligible[best]


def _audio_cpp_package_plans(siblings: Sequence) -> Optional[dict[str, GgufVariantPlan]]:
    """Shared files of every mix are companions, so deleting one mix never counts them as its weights."""
    try:
        from core.inference.audio_cpp_models import package_variant_files
    except Exception:
        return None
    by_name = {
        name: sibling
        for sibling in siblings
        if isinstance(name := getattr(sibling, "rfilename", None), str)
    }
    packages = package_variant_files(by_name)
    if not packages:
        return None
    shared = set.intersection(*(set(files) for files in packages.values()))
    plans: dict[str, GgufVariantPlan] = {}
    for key, files in packages.items():
        expected = tuple(
            file
            for name in files
            if (file := expected_file_from_sibling(by_name[name])) is not None
        )
        main_files = tuple(
            file for file in expected if is_gguf_filename(file.path) and file.path not in shared
        )
        companions = tuple(file for file in expected if file.path in shared)
        plans[key.lower()] = GgufVariantPlan(
            main_filenames = frozenset(file.path for file in main_files),
            target_filenames = tuple(file.path for file in expected),
            main_hashes = frozenset(file.sha256 for file in main_files if file.sha256),
            required_hashes = frozenset(file.sha256 for file in expected if file.sha256),
            companion_hashes = frozenset(file.sha256 for file in companions if file.sha256),
            mmproj_filenames = frozenset(),
            mmproj_hashes = frozenset(),
            expected_files = expected,
            main_size_bytes = sum(max(0, int(file.size or 0)) for file in main_files),
            download_size_bytes = sum(max(0, int(file.size or 0)) for file in expected),
        )
    return plans


def _with_audio_cpp_extras(plan: GgufVariantPlan, siblings: Sequence) -> GgufVariantPlan:
    """*plan* plus what audio.cpp reads beside its GGUF: the voice embeddings in ``<dir>/embeddings/``
    (PocketTTS) and a companion model in the same repo (MioTTS's MioCodec). Otherwise unchanged."""
    dirs = {name.rpartition("/")[0] for name in plan.main_filenames}
    if len(dirs) != 1:
        return plan
    prefix = f"{next(iter(dirs))}/embeddings/".lstrip("/")
    by_name = {
        name: sibling
        for sibling in siblings
        if isinstance(name := getattr(sibling, "rfilename", None), str)
    }
    wanted = {
        name
        for name in by_name
        if name.startswith(prefix) and name.lower().endswith(".safetensors")
    }
    try:
        from core.inference.audio_cpp_models import companion_files
    except Exception:
        companion_files = None
    if companion_files is not None:
        wanted.update(companion_files(min(plan.main_filenames), by_name))
    extras = tuple(
        file
        for name, sibling in by_name.items()
        if name in wanted and (file := expected_file_from_sibling(sibling)) is not None
    )
    if not extras:
        return plan
    hashes = frozenset(file.sha256 for file in extras if file.sha256)
    from dataclasses import replace

    return replace(
        plan,
        target_filenames = (*plan.target_filenames, *(file.path for file in extras)),
        required_hashes = plan.required_hashes | hashes,
        companion_hashes = plan.companion_hashes | hashes,
        expected_files = (*plan.expected_files, *extras),
        download_size_bytes = plan.download_size_bytes
        + sum(max(0, int(file.size or 0)) for file in extras),
    )


def build_gguf_variant_plans(siblings: Sequence) -> dict[str, GgufVariantPlan]:
    # Family grouping keeps the family holding the lexicographically first name, which is the "._" one, so the plan fetched the sidecar and marked the variant complete, leaving header-based local discovery no main GGUF to load.
    siblings = drop_shadowed_appledouble_siblings(list(siblings))
    package_plans = _audio_cpp_package_plans(siblings)
    if package_plans is not None:
        return package_plans
    main: dict[str, list] = {}
    all_mmproj = mmproj_siblings(siblings)
    all_mmproj_filenames = frozenset(
        getattr(s, "rfilename")
        for s in all_mmproj
        if isinstance(getattr(s, "rfilename", None), str)
    )
    all_mmproj_hashes = frozenset(h for h in (sibling_sha256(s) for s in all_mmproj) if h)
    companion = preferred_mmproj_sibling(siblings)
    companion_expected = expected_file_from_sibling(companion) if companion is not None else None
    mtp_expected = mtp_plan_files(siblings)
    common_companions_expected = (companion_expected,) if companion_expected is not None else ()

    for sibling in siblings:
        name = _gguf_rfilename(sibling)
        if name is None:
            continue
        # Keep companions out of the quant grouping so a drafter never lands in a variant's main files: the root mtp-*.gguf carries a quant label. An imatrix leaves entirely rather than joining companions_expected, since no variant needs llama-quantize's calibration data downloaded.
        if is_mmproj_filename(name) or is_mtp_drafter_path(name) or is_imatrix_filename(name):
            continue
        quant = gguf_variant_key(name).lower()
        # The endian predicate reads a quant TOKEN, so a qualified key would make it misread the path and drop the file from every plan.
        if is_big_endian_gguf_path(name, extract_quant_label(name)):
            continue
        main.setdefault(quant, []).append(sibling)

    plans: dict[str, GgufVariantPlan] = {}
    # Every weight in the listing, so the ranking can tell a sidecar naming a neighbouring family from one naming this variant's.
    all_weight_names = [
        name.rsplit("/", 1)[-1]
        for quant_siblings in main.values()
        for sibling in quant_siblings
        if (name := _gguf_rfilename(sibling))
    ]
    for quant, target_main_siblings in main.items():
        main_expected = tuple(
            file
            for sibling in target_main_siblings
            if (file := expected_file_from_sibling(sibling)) is not None
        )
        # Per variant, unlike mmproj and the MTP drafter: ranked against the weight being fetched, and against the family plan_from_expected_files KEEPS, or a two-family variant key pairs the wrong sidecar.
        kept_main = _one_shard_family(main_expected)
        target_weight_name = (
            min(file.path for file in kept_main).rsplit("/", 1)[-1] if kept_main else None
        )
        dflash_expected = dflash_plan_files(
            siblings,
            target_weight_name,
            [n for n in all_weight_names if n != target_weight_name],
            max_bytes = sum(max(0, int(file.size or 0)) for file in kept_main),
        )
        expected_files = (
            *main_expected,
            *common_companions_expected,
            *mtp_expected,
            *dflash_expected,
        )
        plans[quant] = _with_audio_cpp_extras(
            plan_from_expected_files(
                quant,
                expected_files,
                all_mmproj_filenames = all_mmproj_filenames,
                all_mmproj_hashes = all_mmproj_hashes,
            ),
            siblings,
        )
    return plans


def plan_for_variant(plans: dict[str, GgufVariantPlan], variant: str) -> Optional[GgufVariantPlan]:
    """A bare quant matches at lookup only when exactly one plan carries it; map keys stay path-based."""
    wanted = (variant or "").strip().lower()
    if not wanted:
        return None
    exact = plans.get(wanted)
    if exact is not None:
        return exact
    # PATH-qualified keys only, not is_qualified_gguf_variant_key: an H3 root stem's bare quant names both partitions, and picking either would load a different task.
    matches = [key for key in plans if "/" in key and bare_quant_alias(key).lower() == wanted]
    return plans[matches[0]] if len(matches) == 1 else None


def precision_folder_plans(siblings: Sequence) -> dict[str, GgufVariantPlan]:
    """Each MLX precision folder (``6-bit/``) as a variant plan over every file in the folder."""
    from hub.utils.snapshot_filters import precision_variant_subfolders

    by_name = {getattr(s, "rfilename", ""): s for s in siblings}
    return {
        folder.lower(): _precision_folder_plan(
            tuple(expected_file_from_sibling(by_name[name]) for name in sorted(names))
        )
        for folder, names in precision_variant_subfolders(by_name).items()
    }


def _precision_folder_plan(expected: tuple[ExpectedFile, ...]) -> GgufVariantPlan:
    hashes = frozenset(file.sha256 for file in expected if file.sha256)
    size = sum(max(0, int(file.size or 0)) for file in expected)
    return GgufVariantPlan(
        main_filenames = frozenset(file.path for file in expected),
        target_filenames = tuple(file.path for file in expected),
        main_hashes = hashes,
        required_hashes = hashes,
        companion_hashes = frozenset(),
        mmproj_filenames = frozenset(),
        mmproj_hashes = frozenset(),
        expected_files = expected,
        main_size_bytes = size,
        download_size_bytes = size,
    )


def resolve_variant_plan(siblings: Sequence, variant: str) -> Optional[GgufVariantPlan]:
    """A GGUF variant's plan, else the MLX precision folder of that name."""
    siblings = list(siblings)
    plan = plan_for_variant(build_gguf_variant_plans(siblings), variant)
    if plan is not None:
        return plan
    return precision_folder_plans(siblings).get((variant or "").strip().lower())


def _one_shard_family(main_files: Sequence[ExpectedFile]) -> tuple[ExpectedFile, ...]:
    """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[tuple[str, int], list[ExpectedFile]] = {}
    for file in main_files:
        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))
    return tuple(chosen)


def plan_from_expected_files(
    variant: str,
    expected_files: Sequence[ExpectedFile],
    *,
    all_mmproj_filenames: frozenset[str] | None = None,
    all_mmproj_hashes: frozenset[str] | None = None,
) -> GgufVariantPlan:
    expected = tuple(expected_files)
    all_main = tuple(file for file in expected if is_main_gguf_variant_path(file.path, variant))
    from hub.utils.snapshot_filters import PRECISION_FOLDER_RE

    folder = (variant or "").strip()
    if (
        not all_main
        and expected
        and PRECISION_FOLDER_RE.fullmatch(folder)
        and all(file.path.lower().startswith(f"{folder.lower()}/") for file in expected)
    ):
        return _precision_folder_plan(expected)
    main_files = _one_shard_family(all_main)
    # A discarded family has to leave the plan ENTIRELY: target_filenames, required_hashes and download_size_bytes are what the worker fetches, so leaving the copy there downloaded it, then reclaim deleted it as not-ours (absent from main_hashes) and the job fetched it again.
    kept = {file.path for file in main_files}
    expected = tuple(file for file in expected if file not in all_main or file.path in kept)
    companion_files = tuple(file for file in expected if is_companion_gguf_path(file.path))
    # companion_files also holds the MTP drafter, so keep an mmproj-only view for the manifest-resume fallback.
    mmproj_files = tuple(file for file in companion_files if is_mmproj_filename(file.path))
    main_hashes = frozenset(file.sha256 for file in main_files if file.sha256)
    companion_hashes = frozenset(file.sha256 for file in companion_files if file.sha256)
    required_hashes = frozenset(file.sha256 for file in expected if file.sha256)
    main_size = sum(max(0, int(file.size or 0)) for file in main_files)
    download_size = sum(max(0, int(file.size or 0)) for file in expected)
    return GgufVariantPlan(
        main_filenames = frozenset(file.path for file in main_files),
        target_filenames = tuple(file.path for file in expected),
        main_hashes = main_hashes,
        required_hashes = required_hashes,
        companion_hashes = companion_hashes,
        mmproj_filenames = (
            all_mmproj_filenames
            if all_mmproj_filenames is not None
            else frozenset(file.path for file in mmproj_files)
        ),
        mmproj_hashes = (
            all_mmproj_hashes
            if all_mmproj_hashes is not None
            else frozenset(file.sha256 for file in mmproj_files if file.sha256)
        ),
        expected_files = expected,
        main_size_bytes = main_size,
        download_size_bytes = download_size,
    )