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.

7.7 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 types

from hub.utils.snapshot_filters import (
    resolve_snapshot_ignore_patterns_for_files,
    snapshot_download_siblings,
    snapshot_download_size,
)


def _siblings(sizes: dict[str, int]) -> list:
    return [types.SimpleNamespace(rfilename = name, size = size) for name, size in sizes.items()]


def _kept(sizes: dict[str, int]) -> set[str]:
    return {s.rfilename for s in snapshot_download_siblings(_siblings(sizes))}


GPT_OSS = {
    "config.json": 2,
    "tokenizer.json": 27,
    "model.safetensors.index.json": 36,
    "model-00000-of-00002.safetensors": 4_792,
    "model-00001-of-00002.safetensors": 4_798,
    "model-00002-of-00002.safetensors": 4_170,
    "original/config.json": 1,
    "original/dtypes.json": 13,
    "original/model.safetensors": 13_761,
    "metal/model.bin": 13_750,
}

# bigscience/bloom: underscore safetensors shards plus the same 72 shards as .bin.
BLOOM = {
    "config.json": 2,
    "tokenizer.json": 14,
    "model_00001-of-00072.safetensors": 4_900,
    "model_00002-of-00072.safetensors": 4_900,
    "model.safetensors.index.json": 1,
    "pytorch_model_00001-of-00072.bin": 4_900,
    "pytorch_model_00002-of-00072.bin": 4_900,
    "pytorch_model.bin.index.json": 1,
}

WHISPER = {
    "config.json": 2,
    "tokenizer.json": 2,
    "training_args.bin": 3,
    "model.safetensors": 967,
    "pytorch_model.bin": 967,
    "tf_model.h5": 968,
    "flax_model.msgpack": 967,
}


def test_gpt_oss_downloads_only_the_root_safetensors():
    assert snapshot_download_size(_siblings(GPT_OSS)) == 2 + 27 + 36 + 4_792 + 4_798 + 4_170


def test_whisper_downloads_only_the_safetensors_copy():
    assert snapshot_download_size(_siblings(WHISPER)) == 2 + 2 + 3 + 967
    assert "training_args.bin" in _kept(WHISPER)


def test_sharded_bin_checkpoint_beside_safetensors_is_skipped():
    kept = _kept(
        {
            "model.safetensors": 100,
            "pytorch_model-00001-of-00002.bin": 60,
            "pytorch_model-00002-of-00002.bin": 60,
            "pytorch_model.bin.index.json": 1,
            "rust_model.ot": 100,
            "coreml/model.mlpackage/weights.bin": 100,
            "tokenizer.bin": 5,
            "adapter_model.bin": 7,
            "2_Dense/pytorch_model.bin": 9,
        }
    )
    assert kept == {
        "model.safetensors",
        "tokenizer.bin",
        "adapter_model.bin",
        "2_Dense/pytorch_model.bin",
    }


def test_sibling_formats_are_kept_without_root_safetensors():
    bin_only = {
        "config.json": 2,
        "pytorch_model.bin": 967,
        "tf_model.h5": 968,
        "flax_model.msgpack": 967,
        "original/consolidated.00.pth": 900,
        "metal/model.bin": 900,
    }
    assert _kept(bin_only) == set(bin_only)

    nested_only = {
        "unet/diffusion_pytorch_model.safetensors": 100,
        "adapter_model.safetensors": 10,
        "pytorch_model.bin": 50,
        "original/model.safetensors": 50,
    }
    assert _kept(nested_only) == set(nested_only)
    assert "pytorch_model*.bin" not in resolve_snapshot_ignore_patterns_for_files(nested_only)


def test_underscore_sharded_safetensors_still_skip_the_bin_copy():
    assert snapshot_download_size(_siblings(BLOOM)) == 2 + 14 + 4_900 + 4_900 + 1
    assert _kept(BLOOM) == {
        "config.json",
        "tokenizer.json",
        "model_00001-of-00072.safetensors",
        "model_00002-of-00072.safetensors",
        "model.safetensors.index.json",
    }


def test_a_bin_only_dtype_variant_survives():
    # model.safetensors cannot serve a variant="fp16" load, so the fp16 bin is not a duplicate.
    bin_only_variant = {
        "config.json": 2,
        "model.safetensors": 500,
        "pytorch_model.bin": 500,
        "pytorch_model.fp16.bin": 250,
    }
    kept = _kept(bin_only_variant)
    assert "pytorch_model.bin" not in kept
    assert "pytorch_model.fp16.bin" in kept

    both = dict(bin_only_variant, **{"model.fp16.safetensors": 250})
    assert "pytorch_model.fp16.bin" not in _kept(both)


def test_an_indexless_variant_does_not_authorise_dropping_its_bins():
    # Sharded fp16 safetensors without an fp16 index are unloadable, so the fp16 bins stay.
    indexless_variant = {
        "config.json": 2,
        "model.safetensors": 500,
        "model.fp16-00001-of-00002.safetensors": 250,
        "pytorch_model.bin": 500,
        "pytorch_model.fp16-00001-of-00002.bin": 250,
        "pytorch_model.fp16-00002-of-00002.bin": 250,
    }
    kept = _kept(indexless_variant)
    assert "pytorch_model.bin" not in kept
    assert "pytorch_model.fp16-00001-of-00002.bin" in kept

    indexed = dict(indexless_variant, **{"model.safetensors.index.fp16.json": 1})
    assert not any(n.startswith("pytorch_model") for n in _kept(indexed))

    # transformers writes model.safetensors.index.fp16.json; the other spelling is not findable.
    wrong_index = dict(indexless_variant, **{"model.fp16.safetensors.index.json": 1})
    assert "pytorch_model.fp16-00001-of-00002.bin" in _kept(wrong_index)


def test_both_variant_shard_layouts_are_recognised():
    # transformers writes the counter first; both orders are accepted.
    counter_first = {
        "config.json": 2,
        "model.safetensors": 500,
        "model-00001-of-00002.fp16.safetensors": 250,
        "model-00002-of-00002.fp16.safetensors": 250,
        "model.safetensors.index.fp16.json": 1,
        "pytorch_model.bin": 500,
        "pytorch_model-00001-of-00002.fp16.bin": 250,
        "pytorch_model-00002-of-00002.fp16.bin": 250,
    }
    kept = _kept(counter_first)
    assert not any(n.startswith("pytorch_model") for n in kept), sorted(kept)
    assert "model-00001-of-00002.fp16.safetensors" in kept


def test_a_variant_index_never_outlives_its_shards():
    # openai/whisper-large-v3 shape: bin shards go, and their index must go too.
    whisper = {
        "config.json": 2,
        "model.safetensors": 3_090,
        "model.fp32-00001-of-00002.safetensors": 3_000,
        "model.fp32-00002-of-00002.safetensors": 3_000,
        "model.safetensors.index.fp32.json": 1,
        "pytorch_model.bin": 3_090,
        "pytorch_model.fp32-00001-of-00002.bin": 3_000,
        "pytorch_model.fp32-00002-of-00002.bin": 3_000,
        "pytorch_model.bin.index.fp32.json": 1,
    }
    kept = _kept(whisper)
    assert not any(name.startswith("pytorch_model") for name in kept), sorted(kept)
    assert "model.fp32-00001-of-00002.safetensors" in kept
    assert "model.safetensors.index.fp32.json" in kept


def test_indexless_shards_do_not_open_the_gate():
    # Numbered shards need the index; without it the .bin checkpoint is the only loadable one.
    indexless = {
        "config.json": 2,
        "model-00001-of-00002.safetensors": 500,
        "model-00002-of-00002.safetensors": 500,
        "pytorch_model.bin": 990,
    }
    assert _kept(indexless) == set(indexless)
    assert "pytorch_model*.bin" not in resolve_snapshot_ignore_patterns_for_files(indexless)

    indexed = dict(indexless, **{"model.safetensors.index.json": 1})
    assert "pytorch_model.bin" not in _kept(indexed)

    unsharded = {"config.json": 2, "model.safetensors": 500, "pytorch_model.bin": 500}
    assert "pytorch_model.bin" not in _kept(unsharded)


def test_a_non_ascii_shard_number_does_not_open_the_gate():
    # Python's \d matches non-ASCII digits but JS's does not; both sides use [0-9] to agree.
    arabic_indic = {
        "config.json": 2,
        "model-٠١-of-٠٢.safetensors": 500,
        "pytorch_model.bin": 500,
    }
    assert _kept(arabic_indic) == set(arabic_indic)
    assert "pytorch_model*.bin" not in resolve_snapshot_ignore_patterns_for_files(arabic_indic)