Repository: huggingface/transformers
Pull request: fix(tokenizer): Only strip Fast from class names in AutoTokenizer if used as a suffix

### What does this PR do?

The following failing use case was identified and fixed in this PR:

→ [TOKENIZER_MAPPING_NAMES](https://github.com/huggingface/transformers/blob/main/src/transformers/models/auto/tokenization_auto.py#L63-L338) doesn't contain any class names where "Fast" appears in the middle of the name, and the canonical pattern is that "Fast" is appended to the end of the class name. But in the current version, the code uses `.replace(.)` (so if a model were named `FastTextTokenizerFast`, it would give `TextTokenizer`), which doesn't seem quite right. Resultingly in the current set of models, `FastSpeech2ConformerTokenizer` instantiation fails; this change fixes that by using `endswith(.)` instead, which should follow the convention more closely :)

cc: @Rocketknight1

Fixes #44442.

**Current Output:**

<img width="500" height="200" alt="Image" src="https://github.com/user-attachments/assets/532044d0-a936-4acc-ba22-e6b0525dd793" /><br>

**Output After the Fix:**

<img width="600" height="300" alt="4" src="https://github.com/user-attachments/assets/3dc2d496-23bf-4fda-8de8-94b177ebd697" />

### Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [x] Did you fix any necessary existing tests?

Changed files:
filename: src/transformers/models/auto/tokenization_auto.py
patch: @@ -680,8 +680,8 @@ def from_pretrained(
             and config_model_type is not None
             and config_model_type != ""
             and TOKENIZER_MAPPING_NAMES.get(config_model_type) is not None
-            and TOKENIZER_MAPPING_NAMES.get(config_model_type).replace("Fast", "")
-            != tokenizer_config_class.replace("Fast", "")
+            and (TOKENIZER_MAPPING_NAMES.get(config_model_type).removesuffix("Fast"))
+            != (tokenizer_config_class.removesuffix("Fast"))
         ):
             # new model, but we ignore it unless the model type is the same
             if TokenizersBackend is not None:
@@ -697,8 +697,8 @@ def from_pretrained(
         if "_commit_hash" in tokenizer_config:
             kwargs["_commit_hash"] = tokenizer_config["_commit_hash"]
 
-        if tokenizer_config_class:
-            tokenizer_config_class = tokenizer_config_class.replace("Fast", "")
+        if tokenizer_config_class and tokenizer_config_class.endswith("Fast"):
+            tokenizer_config_class = tokenizer_config_class[:-4]
 
         has_remote_code = tokenizer_auto_map is not None
         has_local_code = type(config) in TOKENIZER_MAPPING or (
@@ -749,8 +749,8 @@ def from_pretrained(
             return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
         elif getattr(config, "tokenizer_class", None):
             _class = config.tokenizer_class
-            if "PreTrainedTokenizerFast" not in _class:
-                _class = _class.replace("Fast", "")
+            if "PreTrainedTokenizerFast" not in _class and _class.endswith("Fast"):
+                _class = _class[:-4]
             tokenizer_class = tokenizer_class_from_name(_class)
             return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
 
@@ -775,7 +775,7 @@ def from_pretrained(
         # Fallback: try tokenizer_class from tokenizer_config.json
         tokenizer_config_class = tokenizer_config.get("tokenizer_class", None)
         if tokenizer_config_class is not None:
-            if tokenizer_config_class != "TokenizersBackend" and "Fast" in tokenizer_config_class:
+            if tokenizer_config_class != "TokenizersBackend" and tokenizer_config_class.endswith("Fast"):
                 tokenizer_config_class = tokenizer_config_class[:-4]
             tokenizer_class = tokenizer_class_from_name(tokenizer_config_class)
             if tokenizer_class is None and not tokenizer_config_class.endswith("Fast"):