You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What is the difference between the function of add_tokens() and add_special_tokens() in tokenizer #5940
When I read the code of tokenizer, I have a problem if I want to use a pretrained model in NMT task, I need to add some tag tokens, such as '2English' or '2French'. I think these tokens are special tokens, so which function should I use: add_tokens() or add_special_tokens(). What is the difference between them?
Reacted by Amogh Mannekote, Bilal Ghanem, Qiyu Chen, Kunyu Shi, LukeLin, ZichengDuan and Mengqi Li
For some reasons those functions do not appear in the documentation, will have a look at why. The docstrings state of add_special_tokens states:
Add a dictionary of special tokens (eos, pad, cls...) to the encoder and link them to class attributes. If special tokens are NOT in the vocabulary, they are added to it (indexed starting from the last index of the current vocabulary).
Using add_special_tokens will ensure your special tokens can be used in several ways:
special tokens are carefully handled by the tokenizer (they are never split)
you can easily refer to special tokens using tokenizer class attributes like tokenizer.cls_token. This makes it easy to develop model-agnostic training and fine-tuning scripts.
Though the second point would not apply in your case. The docstring of add_tokens states:
Add a list of new tokens to the tokenizer class. If the new tokens are not in the vocabulary, they are added to it with indices starting from length of the current vocabulary.
When possible, special tokens are already registered for provided pretrained models (ex: BertTokenizer cls_token is already registered to be '[CLS]' and XLM's one is also registered to be '')
Hope that helps!
Reacted by Minh Quang Nguyen, gugaio, NivinaNull and LukeLin
Activity
sgugger commentedon Jul 21, 2020
For some reasons those functions do not appear in the documentation, will have a look at why. The docstrings state of
add_special_tokensstates:Though the second point would not apply in your case. The docstring of
add_tokensstates:Hope that helps!
kugwzk commentedon Jul 22, 2020
Thank a lot for your help. Therefore, if I use the add_tokens(), the tokens may still split?
TobiasJacob commentedon Jul 22, 2021
I would also appreciate some clarification on the difference between the function and when to use which one.
LukeLIN-web commentedon Mar 5, 2025
In my practice yes,
LukeLIN-web commentedon Mar 5, 2025
For example if you want your
<SEG>as whole, don't split to < , SEG, > three token, you need add_special_tokens()