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.

11.5 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
import gc
import os

os.environ.setdefault("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "python")

import transformers

# Same accessor as unsloth.tokenizer_utils: the legacy sentencepiece_model_pb2 breaks on protobuf >= 4.
from transformers.convert_slow_tokenizer import import_protobuf

sentencepiece_model_pb2 = import_protobuf()

from unsloth.tokenizer_utils import fix_sentencepiece_tokenizer


NORMAL, CONTROL = 1, 3


def _spm_bytes(pieces):
    m = sentencepiece_model_pb2.ModelProto()
    for piece, score, typ in pieces:
        p = m.pieces.add()
        p.piece = piece
        p.score = score
        p.type = typ
    return m.SerializeToString()


def _read_pieces(path):
    m = sentencepiece_model_pb2.ModelProto()
    with open(path, "rb") as f:
        m.ParseFromString(f.read())
    return [p.piece for p in m.pieces]


class _FakeTokenizer:
    """Stand-in slow tokenizer; save_pretrained writes tokenizer.model, which the fix reads back."""

    def __init__(
        self,
        name,
        spm_bytes = None,
        vocab = None,
    ):
        self.name = name
        self.eos_token = "</s>"
        self.pad_token = "<pad>"
        self._spm_bytes = spm_bytes
        self._vocab = vocab or {}
        self.saved_to = []

    def save_pretrained(self, location):
        self.saved_to.append(location)
        os.makedirs(location, exist_ok = True)
        if self._spm_bytes is not None:
            with open(os.path.join(location, "tokenizer.model"), "wb") as f:
                f.write(self._spm_bytes)

    def __call__(
        self,
        texts,
        add_special_tokens = False,
    ):
        class _Encoded:
            pass

        encoded = _Encoded()
        encoded.input_ids = [[self._vocab[text]] for text in texts]
        return encoded


def _tokenizers():
    pieces = [("<s>", 0.0, CONTROL), ("a", -1.0, NORMAL), ("</s>", 0.0, CONTROL)]
    old = _FakeTokenizer("old", spm_bytes = _spm_bytes(pieces), vocab = {"</s>": 2})
    new = _FakeTokenizer("new")
    return old, new


class _ReloadedTokenizer:
    """Weakref-able stand-in for the tokenizer AutoTokenizer.from_pretrained returns."""

    def __init__(self, location):
        self.location = location


def _stub_auto_tokenizer(monkeypatch):
    """Stubs AutoTokenizer: the reload needs a full tokenizer on disk, which these tests do not build."""
    loaded = []

    class _StubAutoTokenizer:
        @staticmethod
        def from_pretrained(location, **kwargs):
            loaded.append(location)
            return _ReloadedTokenizer(location)

    monkeypatch.setattr(transformers, "AutoTokenizer", _StubAutoTokenizer)
    return loaded


def test_old_tokenizer_is_saved_so_its_model_can_be_read(tmp_path, monkeypatch):
    """The guard must not skip on a fresh scratch dir: tokenizer.model appears only after save_pretrained"""
    _stub_auto_tokenizer(monkeypatch)
    old, new = _tokenizers()
    location = str(tmp_path / "_unsloth_sentencepiece_temp")

    fix_sentencepiece_tokenizer(old, new, {"</s>": "<|im_end|>"}, temporary_location = location)

    assert old.saved_to, "old tokenizer was never saved: the body did not run"


def test_token_mapping_is_applied_to_the_sentencepiece_model(tmp_path, monkeypatch):
    loaded = _stub_auto_tokenizer(monkeypatch)
    old, new = _tokenizers()
    location = str(tmp_path / "_unsloth_sentencepiece_temp")

    # Hold the tokenizer so its scratch dir survives until we read it.
    tok = fix_sentencepiece_tokenizer(old, new, {"</s>": "<|im_end|>"}, temporary_location = location)

    assert "<|im_end|>" in _read_pieces(f"{loaded[-1]}/tokenizer.model")
    assert tok is not None


def test_tokenizer_without_a_sentencepiece_model_is_returned_untouched(tmp_path, monkeypatch):
    """A fast-only tokenizer writes no tokenizer.model, so the guard returns new_tokenizer unchanged."""
    _stub_auto_tokenizer(monkeypatch)
    old = _FakeTokenizer("old", spm_bytes = None)
    new = _FakeTokenizer("new")
    location = str(tmp_path / "_unsloth_sentencepiece_temp")

    result = fix_sentencepiece_tokenizer(
        old, new, {"</s>": "<|im_end|>"}, temporary_location = location
    )

    assert result is new
    assert not any(
        name.startswith("tokenizer_") for name in os.listdir(location)
    ), "the fast-only scratch dir was not reclaimed"


def test_each_call_uses_a_fresh_isolated_subdirectory(tmp_path, monkeypatch):
    """Each call gets a fresh subdirectory, so calls never share scratch files or delete the caller's."""
    loaded = _stub_auto_tokenizer(monkeypatch)
    location = str(tmp_path / "_unsloth_sentencepiece_temp")
    os.makedirs(location, exist_ok = True)

    marker = os.path.join(location, "leftover.json")
    with open(marker, "w") as f:
        f.write("{}")

    old1, new1 = _tokenizers()
    old2, new2 = _tokenizers()
    tok1 = fix_sentencepiece_tokenizer(
        old1, new1, {"</s>": "<|im_end|>"}, temporary_location = location
    )
    tok2 = fix_sentencepiece_tokenizer(
        old2, new2, {"</s>": "<|im_end|>"}, temporary_location = location
    )

    work1, work2 = loaded[0], loaded[1]
    assert work1 != work2, "two calls reused the same directory"
    assert os.path.dirname(work1) == location and os.path.dirname(work2) == location
    assert os.path.isdir(work1) and os.path.isdir(work2)
    assert os.path.isfile(marker), "a pre-existing scratch file was deleted"
    assert not os.path.isfile(os.path.join(work1, "leftover.json"))
    assert not os.path.isfile(os.path.join(work2, "leftover.json"))
    assert tok1 is not None and tok2 is not None


def test_sentencepiece_scratch_dir_is_reclaimed_once_the_tokenizer_is_gone(tmp_path, monkeypatch):
    """The scratch dir must outlive the returned tokenizer, whose vocab_file points into it, and be
    reclaimed once the tokenizer is garbage collected."""
    loaded = _stub_auto_tokenizer(monkeypatch)
    old, new = _tokenizers()
    location = str(tmp_path / "_unsloth_sentencepiece_temp")

    tok = fix_sentencepiece_tokenizer(old, new, {"</s>": "<|im_end|>"}, temporary_location = location)
    work = loaded[-1]
    assert os.path.isdir(work), "scratch dir vanished while the tokenizer was alive"

    del tok
    gc.collect()
    assert not os.path.isdir(work), "scratch dir was not reclaimed after the tokenizer was freed"


class _CopyFromSubdirTokenizer:
    """Source vocab may live outside the work dir; save_pretrained copies it in, as HF slow
    tokenizers do."""

    def __init__(self, source_model_path):
        self.eos_token = "</s>"
        self.pad_token = "<pad>"
        self._source_model_path = source_model_path

    def save_pretrained(self, location):
        os.makedirs(location, exist_ok = True)
        if os.path.isfile(self._source_model_path):
            with open(self._source_model_path, "rb") as src:
                data = src.read()
            with open(os.path.join(location, "tokenizer.model"), "wb") as dst:
                dst.write(data)

    def __call__(
        self,
        texts,
        add_special_tokens = False,
    ):
        class _Encoded:
            pass

        encoded = _Encoded()
        encoded.input_ids = [[2] for _ in texts]
        return encoded


def test_source_vocab_outside_the_work_directory_is_not_disturbed(tmp_path, monkeypatch):
    """A source vocab outside the work dir is copied in and patched there; the original is left alone."""
    loaded = _stub_auto_tokenizer(monkeypatch)
    location = str(tmp_path / "_unsloth_sentencepiece_temp")
    subdir = os.path.join(location, "some_model")
    os.makedirs(subdir, exist_ok = True)

    pieces = [("<s>", 0.0, CONTROL), ("a", -1.0, NORMAL), ("</s>", 0.0, CONTROL)]
    source_model = os.path.join(subdir, "tokenizer.model")
    with open(source_model, "wb") as f:
        f.write(_spm_bytes(pieces))

    old = _CopyFromSubdirTokenizer(source_model)
    new = _FakeTokenizer("new")
    tok = fix_sentencepiece_tokenizer(old, new, {"</s>": "<|im_end|>"}, temporary_location = location)

    assert _read_pieces(source_model) == [
        "<s>",
        "a",
        "</s>",
    ], "the original source vocab was modified"
    assert "<|im_end|>" in _read_pieces(f"{loaded[-1]}/tokenizer.model")
    assert tok is not None


def test_swap_mapping_swaps_both_pieces_without_duplicating(tmp_path, monkeypatch):
    """A swap of eos and stop_word in the fast JSON must map both directions; a one-way mapping
    would leave two stop_word pieces and no eos."""
    loaded = _stub_auto_tokenizer(monkeypatch)
    location = str(tmp_path / "_unsloth_sentencepiece_temp")

    pieces = [("<s>", 0.0, CONTROL), ("<|im_end|>", -1.0, NORMAL), ("</s>", 0.0, CONTROL)]
    old = _FakeTokenizer("old", spm_bytes = _spm_bytes(pieces), vocab = {"</s>": 2, "<|im_end|>": 1})
    new = _FakeTokenizer("new")

    tok = fix_sentencepiece_tokenizer(
        old, new, {"</s>": "<|im_end|>", "<|im_end|>": "</s>"}, temporary_location = location
    )

    result = _read_pieces(f"{loaded[-1]}/tokenizer.model")
    assert result.count("<|im_end|>") == 1 and result.count("</s>") == 1, result
    assert tok is not None


def test_only_applied_mappings_are_patched(tmp_path, monkeypatch):
    """Only mappings actually applied may be passed, or a skipped rename duplicates an existing target."""
    loaded = _stub_auto_tokenizer(monkeypatch)
    location = str(tmp_path / "_unsloth_sentencepiece_temp")

    pieces = [
        ("<s>", 0.0, CONTROL),
        ("aa", -1.0, NORMAL),
        ("bb", -1.0, NORMAL),
        ("X", -1.0, NORMAL),
    ]
    old = _FakeTokenizer("old", spm_bytes = _spm_bytes(pieces), vocab = {"aa": 1, "bb": 2})
    new = _FakeTokenizer("new")

    # The caller skipped aa->X (X exists), so only bb->Y is passed.
    tok = fix_sentencepiece_tokenizer(old, new, {"bb": "Y"}, temporary_location = location)

    result = _read_pieces(f"{loaded[-1]}/tokenizer.model")
    assert result.count("X") == 1 and "Y" in result and "aa" in result, result
    assert tok is not None


def test_a_scratch_directory_created_by_another_process_is_reused(tmp_path, monkeypatch):
    """Two processes sharing a working directory can both see the scratch dir missing, so the second
    makedirs must not raise; simulated by reporting it absent after creation."""
    import unsloth.tokenizer_utils as tokenizer_utils

    _stub_auto_tokenizer(monkeypatch)
    old, new = _tokenizers()
    location = str(tmp_path / "_unsloth_sentencepiece_temp")
    os.makedirs(location)
    real_exists = os.path.exists
    monkeypatch.setattr(
        tokenizer_utils.os.path,
        "exists",
        lambda path: False if os.fspath(path) == location else real_exists(path),
    )

    fix_sentencepiece_tokenizer(old, new, {"</s>": "<|im_end|>"}, temporary_location = location)

    assert old.saved_to


def test_every_scratch_makedirs_tolerates_an_existing_directory():
    """convert_to_fast_tokenizer shares the pattern but needs a real slow tokenizer to reach
    it, so pin the call shape for every makedirs in the module instead."""
    import ast
    import inspect

    import unsloth.tokenizer_utils as tokenizer_utils

    calls = [
        node
        for node in ast.walk(ast.parse(inspect.getsource(tokenizer_utils)))
        if isinstance(node, ast.Call)
        and isinstance(node.func, ast.Attribute)
        and node.func.attr == "makedirs"
    ]
    assert calls, "no makedirs call found; update this test"
    racy = [
        node.lineno
        for node in calls
        if not any(
            kw.arg == "exist_ok" and isinstance(kw.value, ast.Constant) and kw.value.value is True
            for kw in node.keywords
        )
    ]
    assert not racy, f"makedirs without exist_ok = True at tokenizer_utils.py lines {racy}"