@@ -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 :
0 commit comments