Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove redundant if-condition and improve type correctness for convert_tokens_to_ids #34030

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/transformers/tokenization_utils_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import json
import os
from collections import defaultdict
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union

import tokenizers.pre_tokenizers as pre_tokenizers_fast
from tokenizers import Encoding as EncodingFast
Expand Down Expand Up @@ -326,20 +326,17 @@ def _convert_encoding(

return encoding_dict, encodings

def convert_tokens_to_ids(self, tokens: Union[str, List[str]]) -> Union[int, List[int]]:
def convert_tokens_to_ids(self, tokens: Union[str, Iterable[str]]) -> Union[int, List[int]]:
"""
Converts a token string (or a sequence of tokens) in a single integer id (or a sequence of ids), using the
Converts a token string (or a sequence of tokens) in a single integer id (or a Iterable of ids), using the
vocabulary.

Args:
tokens (`str` or `List[str]`): One or several token(s) to convert to token id(s).
tokens (`str` or `Iterable[str]`): One or several token(s) to convert to token id(s).

Returns:
`int` or `List[int]`: The token id or list of token ids.
"""
if tokens is None:
return None

if isinstance(tokens, str):
return self._convert_token_to_id_with_added_voc(tokens)

Expand Down
Loading