|
| 1 | +# Copyright 2026 ModelCloud.ai |
| 2 | +# Copyright 2026 qubitium@modelcloud.ai |
| 3 | +# Contact: qubitium@modelcloud.ai, x.com/qubitium |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import unittest |
| 18 | +from unittest.mock import Mock, patch |
| 19 | + |
| 20 | +from huggingface_hub.errors import StrictDataclassClassValidationError |
| 21 | + |
| 22 | +from tokenicer import Tokenicer |
| 23 | + |
| 24 | + |
| 25 | +class TestMistralRegexNormalization(unittest.TestCase): |
| 26 | + @patch("tokenicer.tokenicer.AutoTokenizer.from_pretrained") |
| 27 | + def test_mistral_regex_fix_is_enabled_by_default(self, from_pretrained): |
| 28 | + expected = object() |
| 29 | + from_pretrained.return_value = expected |
| 30 | + |
| 31 | + tokenizer = Tokenicer._load_tokenizer("poolside/Laguna-S-2.1", trust_remote_code=True) |
| 32 | + |
| 33 | + self.assertIs(tokenizer, expected) |
| 34 | + from_pretrained.assert_called_once_with( |
| 35 | + "poolside/Laguna-S-2.1", |
| 36 | + trust_remote_code=True, |
| 37 | + fix_mistral_regex=True, |
| 38 | + ) |
| 39 | + |
| 40 | + @patch("tokenicer.tokenicer.AutoTokenizer.from_pretrained") |
| 41 | + def test_explicit_mistral_regex_setting_is_preserved(self, from_pretrained): |
| 42 | + from_pretrained.return_value = object() |
| 43 | + |
| 44 | + Tokenicer._load_tokenizer("poolside/Laguna-S-2.1", fix_mistral_regex=False) |
| 45 | + |
| 46 | + from_pretrained.assert_called_once_with( |
| 47 | + "poolside/Laguna-S-2.1", |
| 48 | + fix_mistral_regex=False, |
| 49 | + ) |
| 50 | + |
| 51 | + @patch("tokenicer.tokenicer.Tokenicer._resolve_tokenizer_class") |
| 52 | + @patch("tokenicer.tokenicer.tokenizer_class_name", return_value="FallbackTokenizer") |
| 53 | + @patch("tokenicer.tokenicer.tokenizer_special_token_overrides", return_value={"bos_token": "<s>"}) |
| 54 | + @patch( |
| 55 | + "tokenicer.tokenicer.AutoTokenizer.from_pretrained", |
| 56 | + side_effect=StrictDataclassClassValidationError( |
| 57 | + validator="validate_layer_type", |
| 58 | + cause=ValueError("legacy model config"), |
| 59 | + ), |
| 60 | + ) |
| 61 | + def test_mistral_regex_fix_is_preserved_on_fallback( |
| 62 | + self, |
| 63 | + auto_from_pretrained, |
| 64 | + special_token_overrides, |
| 65 | + tokenizer_class_name, |
| 66 | + resolve_tokenizer_class, |
| 67 | + ): |
| 68 | + fallback_from_pretrained = Mock(return_value=object()) |
| 69 | + resolve_tokenizer_class.return_value = Mock(from_pretrained=fallback_from_pretrained) |
| 70 | + |
| 71 | + Tokenicer._load_tokenizer("/tmp/Laguna-S-2.1", trust_remote_code=True) |
| 72 | + |
| 73 | + auto_from_pretrained.assert_called_once_with( |
| 74 | + "/tmp/Laguna-S-2.1", |
| 75 | + trust_remote_code=True, |
| 76 | + fix_mistral_regex=True, |
| 77 | + ) |
| 78 | + special_token_overrides.assert_called_once_with("/tmp/Laguna-S-2.1") |
| 79 | + tokenizer_class_name.assert_called_once_with("/tmp/Laguna-S-2.1") |
| 80 | + resolve_tokenizer_class.assert_called_once_with("/tmp/Laguna-S-2.1", "FallbackTokenizer") |
| 81 | + fallback_from_pretrained.assert_called_once_with( |
| 82 | + "/tmp/Laguna-S-2.1", |
| 83 | + trust_remote_code=True, |
| 84 | + fix_mistral_regex=True, |
| 85 | + bos_token="<s>", |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + unittest.main() |
0 commit comments