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.

1.9 KB
Raw Blame
# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Regression test: the IGNORED_TOKENIZER_NAMES guard must match case-insensitively."""

import types
from unittest.mock import patch

import unsloth.tokenizer_utils as tu


class _Tok:
    def __init__(self, kind):
        self.kind = kind


def _fake_auto_tokenizer():
    # Distinguishable slow and fast stand-ins show which one is returned.
    def from_pretrained(name, **kwargs):
        return _Tok("slow" if kwargs.get("from_slow") else "fast")

    return types.SimpleNamespace(from_pretrained = from_pretrained)


def test_ignored_tokenizer_name_matched_case_insensitively():
    name = "unsloth/Qwen2.5-Coder-7B-Instruct"  # name.lower() is in IGNORED_TOKENIZER_NAMES
    assert name.lower() in tu.IGNORED_TOKENIZER_NAMES

    with (
        patch.object(tu, "AutoTokenizer", _fake_auto_tokenizer()),
        patch.object(tu, "assert_same_tokenization", lambda a, b: False),
        patch.object(tu, "convert_to_fast_tokenizer", lambda slow, **k: _Tok("converted")),
    ):
        result = tu._load_correct_tokenizer(name, fix_tokenizer = True)

    assert result.kind == "fast", (
        "an ignored tokenizer (matched case-insensitively) must be returned unchanged, "
        f"but it fell through to conversion (got {result.kind!r})"
    )


if __name__ == "__main__":
    test_ignored_tokenizer_name_matched_case_insensitively()
    print("ok")