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

#32184 save total_vocab_size #32240

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions src/transformers/tokenization_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,17 @@ def get_added_vocab(self) -> Dict[str, int]:

def __len__(self):
"""
Size of the full vocabulary with the added tokens. Counts the `keys` and not the `values` because otherwise if
there is a hole in the vocab, we will add tokenizers at a wrong index.
Size of the full vocabulary with the added tokens.
"""
return len(set(self.get_vocab().keys()))
return self.total_vocab_size

def _update_total_vocab_size(self):
"""
Update the size of the full vocabulary with the added tokens. Counts the `keys` and not the `values` because
otherwise if there is a hole in the vocab, we will add tokenizers at a wrong index. This operation is slow and
is only updated when adding tokens.
"""
self.total_vocab_size = len(self.get_vocab())

def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int:
"""
Expand Down Expand Up @@ -574,6 +581,7 @@ def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_to
logger.info(f"Adding {token} to the vocabulary")

self._update_trie()
self._update_total_vocab_size()
return added_tokens

def _update_trie(self, unique_no_split_tokens: Optional[str] = []):
Expand Down
Loading