Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd5bcad

Browse files
authored
throw error when conversion required (#45078)
* throw error when conversion required * fix * typo * comment * add test without sentencepiece and add PreTrainedTokenizerFast to class list
1 parent aba6e22 commit cd5bcad

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎src/transformers/models/auto/tokenization_auto.py‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ def from_pretrained(
704704
else:
705705
tokenizer_auto_map = tokenizer_config["auto_map"].get("AutoTokenizer", None)
706706

707-
# if there is a config, we can check that the tokenizer class != than model class and can thus assume we need to use TokenizersBackend
708-
# Skip this early exit if auto_map is present (custom tokenizer with trust_remote_code)
707+
# if there is a config, we can check that the tokenizer class != than model class.
708+
# Use the config class if it's a specialized tokenizer, otherwise fall back to TokenizersBackend.
709709
if (
710710
tokenizer_auto_map is None
711711
and tokenizer_config_class is not None
@@ -715,15 +715,20 @@ def from_pretrained(
715715
and (TOKENIZER_MAPPING_NAMES.get(config_model_type).removesuffix("Fast"))
716716
!= (tokenizer_config_class.removesuffix("Fast"))
717717
):
718-
# new model, but we ignore it unless the model type is the same
718+
tokenizer_class = tokenizer_class_from_name(tokenizer_config_class)
719+
if tokenizer_class is not None and tokenizer_class.__name__ not in (
720+
"TokenizersBackend",
721+
"PythonBackend",
722+
"PreTrainedTokenizerFast",
723+
):
724+
return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
725+
719726
if TokenizersBackend is not None:
720-
try:
721-
return TokenizersBackend.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
722-
except Exception as e:
723-
logger.debug(f"Failed to use TokenizersBackend: {e}")
727+
return TokenizersBackend.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
724728

725-
return tokenizer_class_from_name(tokenizer_config_class).from_pretrained(
726-
pretrained_model_name_or_path, *inputs, **kwargs
729+
raise ValueError(
730+
f"Tokenizer class '{tokenizer_config_class}' specified in the tokenizer config was not found. "
731+
f"The tokenizer may need to be converted or re-saved."
727732
)
728733

729734
if "_commit_hash" in tokenizer_config:

‎tests/models/auto/test_tokenization_auto.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
SMALL_MODEL_IDENTIFIER,
5959
CaptureLogger,
6060
RequestCounter,
61+
require_sentencepiece,
6162
require_tokenizers,
6263
slow,
6364
)
@@ -746,3 +747,21 @@ def __init__(self, **kwargs):
746747
self.assertTrue(tokenizer.special_attribute_present)
747748
finally:
748749
os.chdir(prev_dir)
750+
751+
@require_tokenizers
752+
@require_sentencepiece
753+
def test_mismatched_model_type_uses_config_tokenizer_class_with_sentencepiece(self):
754+
tokenizer = AutoTokenizer.from_pretrained(
755+
"facebook/nllb-200-distilled-600M",
756+
revision="f8d333a098d19b4fd9a8b18f94170487ad3f821d",
757+
)
758+
self.assertEqual(tokenizer.__class__.__name__, "NllbTokenizer")
759+
760+
@require_tokenizers
761+
def test_mismatched_model_type_uses_config_tokenizer_class_without_sentencepiece(self):
762+
with mock.patch("transformers.models.auto.tokenization_auto.is_sentencepiece_available", return_value=False):
763+
tokenizer = AutoTokenizer.from_pretrained(
764+
"facebook/nllb-200-distilled-600M",
765+
revision="f8d333a098d19b4fd9a8b18f94170487ad3f821d",
766+
)
767+
self.assertEqual(tokenizer.__class__.__name__, "NllbTokenizer")

0 commit comments

Comments
 (0)