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

Nit-added-tokens #26538

Merged
merged 16 commits into from
Oct 3, 2023
Merged
8 changes: 8 additions & 0 deletions src/transformers/tokenization_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,14 @@ def added_tokens_decoder(self) -> Dict[int, AddedToken]:
"""
return dict(sorted(self._added_tokens_decoder.items(), key=lambda item: item[0]))

@property
def added_tokens_encoder(self) -> Dict[str, int]:
"""
Returns the sorted mapping from string to index. The added tokens encoder is cached for performance
optimisation in `self._added_tokens_encoder` for the slow tokenizers.
"""
return {k.content: v for v, k in sorted(self._added_tokens_decoder.items(), key=lambda item: item[0])}

@added_tokens_decoder.setter
def added_tokens_decoder(self, value: Dict[int, Union[AddedToken, str]]) -> Dict[int, AddedToken]:
# Always raise an error if string because users should define the behavior
Expand Down
38 changes: 23 additions & 15 deletions src/transformers/tokenization_utils_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,15 +846,26 @@ def __init__(self, verbose=True, **kwargs):
# We directly set the hidden value to allow initialization with special tokens
# which are not yet in the vocabulary. Necessary for serialization/de-serialization
# TODO clean this up at some point (probably by switching to fast tokenizers)

for key, value in kwargs.items():
if value is None:
continue
if key in self.SPECIAL_TOKENS_ATTRIBUTES:
if key == "additional_special_tokens":
# TODO THIS IS NASTY! Will always reset tokens to default rstrip and lstrip because self.set_attr on strings
# will not check the addedtokens decoder. WILL FIX TOMORROW
assert isinstance(value, (list, tuple)), f"Value {value} is not a list or tuple"
assert all(
isinstance(t, (str, AddedToken)) for t in value
), "One of the tokens is not a string or an AddedToken"
if hasattr(self, "added_tokens_encoder"):
extended_token = []
for token in value:
if isinstance(token, str) and str(token) in self.added_tokens_encoder:
extended_token.append(self.added_tokens_decoder[self.added_tokens_encoder[str(token)]])
else:
extended_token.append(token)
value = extended_token
setattr(self, key, value)
elif isinstance(value, (str)):
value = AddedToken(value, normalized=False, special=True)
Expand Down Expand Up @@ -1674,14 +1685,6 @@ def _set_processor_class(self, processor_class: str):
"""Sets processor class as an attribute."""
self._processor_class = processor_class

@property
def added_tokens_encoder(self) -> Dict[str, int]:
"""
Returns the sorted mapping from string to index. The added tokens encoder is cached for performance
optimisation in `self._added_tokens_encoder` for the slow tokenizers.
"""
return {k.content: v for v, k in sorted(self._added_tokens_decoder.items(), key=lambda item: item[0])}

@property
def added_tokens_decoder(self) -> Dict[int, AddedToken]:
raise NotImplementedError()
Expand Down Expand Up @@ -2196,9 +2199,13 @@ def _from_pretrained(
for idx, token in init_kwargs["added_tokens_decoder"].items():
if isinstance(token, dict):
token = AddedToken(**token)

if isinstance(token, AddedToken):
added_tokens_decoder[int(idx)] = token
if str(token) in additional_special_tokens:
# at this point the token is in `additional_special_tokens` as an str, let's add the AddedToken info
additional_special_tokens.remove(str(token))
if token.special and token not in additional_special_tokens:
additional_special_tokens.append(token)
Comment on lines +2204 to +2208
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @LysandreJik here

else:
raise ValueError(
f"Found a {token.__class__} in the saved `added_tokens_decoder`, should be a dictionary."
Expand Down Expand Up @@ -2228,10 +2235,11 @@ def _from_pretrained(
with open(added_tokens_file, encoding="utf-8") as added_tokens_handle:
added_tok_encoder = json.load(added_tokens_handle)
# legacy: we have to init with (rstrip=True, lstrip=True)
strip = True if "Fast" not in cls.__name__ else False
added_tokens_decoder = {
index: AddedToken(token, rstrip=strip, lstrip=strip) for token, index in added_tok_encoder.items()
}
rstrip = lstrip = True if "Fast" not in cls.__name__ else False
for token, index in added_tok_encoder.items():
if index not in added_tokens_decoder:
rstrip = lstrip = False
added_tokens_decoder = {index: AddedToken(token, rstrip=rstrip, lstrip=lstrip)}
# end legacy

# slow -> fast, non-legacy: we need to make sure the `added_tokens_decoder` is used to add tokens if the `fast` was not properly saved!
Expand Down Expand Up @@ -2382,8 +2390,8 @@ def save_pretrained(
tokenizer_config = copy.deepcopy(self.init_kwargs)

# TODO: Ensure the modified attributes (those are also in the __init__ kwargs) will give identical tokenizers
# target_keys = self.init_kwargs.keys()
target_keys = ["model_max_length", "clean_up_tokenization_spaces", "additional_special_tokens"]
target_keys = list(self.init_kwargs.keys())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when saving; we should overwrite the init_kwargs with the content of self. Don't know why it was not the case before

target_keys += ["model_max_length", "clean_up_tokenization_spaces", "additional_special_tokens"]
for k in target_keys:
if hasattr(self, k):
tokenizer_config[k] = getattr(self, k)
Expand Down
8 changes: 8 additions & 0 deletions src/transformers/tokenization_utils_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ def get_vocab(self) -> Dict[str, int]:
def vocab(self) -> Dict[str, int]:
return self.get_vocab()

@property
def added_tokens_encoder(self) -> Dict[str, int]:
"""
Returns the sorted mapping from string to index. The added tokens encoder is cached for performance
optimisation in `self._added_tokens_encoder` for the slow tokenizers.
"""
return {k.content: v for v, k in sorted(self.added_tokens_decoder.items(), key=lambda item: item[0])}

@property
def added_tokens_decoder(self) -> Dict[int, AddedToken]:
"""
Expand Down