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

[ConvertSlow] make sure the order is preserved for addedtokens #31902

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Changes from 4 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
19 changes: 11 additions & 8 deletions src/transformers/convert_slow_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,16 +622,19 @@ def decoder(self, replacement, add_prefix_space):
def converted(self) -> Tokenizer:
tokenizer = self.tokenizer(self.proto)

# control tokens are special
# user defined symbols are not
# both user and control tokens are AddedTokens
# Add user defined symbols (type == 4) from sentnecepiece (https://github.com/google/sentencepiece/blob/6225e08edb2577757163b3f5dbba4c0b670ef445/src/sentencepiece_model.proto#L299C29-L299C33)
user_defined_symbols = [
AddedToken(token, normalized=False, special=False)
for token in [p.piece for p in self.proto.pieces if p.type == 4]
]
control_symbols = [
AddedToken(token, normalized=False, special=True) for token in self.proto.trainer_spec.control_symbols
]

tokenizer.add_tokens(user_defined_symbols + control_symbols)
all_added_tokens = {
id: AddedToken(token, normalized=False, special=special)
for id, token, special in [
(id, p.piece, p.type == 3) for id, p in enumerate(self.proto.pieces) if p.type in [3, 4]
]
}

tokenizer.add_tokens([k for _, k in sorted(all_added_tokens.items(), key=lambda x: x[0])])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fast fix! Is the sorting here necessary or is it already sorted from enumerate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not sure that the dict is an ordered dict by default for all python version! just for safety!


# Tokenizer assemble
normalizer = self.normalizer(self.proto)
Expand Down
Loading