Skip to content

What is the difference between the function of add_tokens() and add_special_tokens() in tokenizer #5940

Closed

Description

@kugwzk

❓ Questions & Help

Details

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?

Activity

  1. sgugger commented on Jul 21, 2020

    @sgugger
    Collaborator

    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!

  2. kugwzk commented on Jul 22, 2020

    @kugwzk
    Author

    Thank a lot for your help. Therefore, if I use the add_tokens(), the tokens may still split?

  3. TobiasJacob commented on Jul 22, 2021

    @TobiasJacob

    I would also appreciate some clarification on the difference between the function and when to use which one.

  4. LukeLIN-web commented on Mar 5, 2025

    @LukeLIN-web

    Thank a lot for your help. Therefore, if I use the add_tokens(), the tokens may still split?

    In my practice yes,

  5. LukeLIN-web commented on Mar 5, 2025

    @LukeLIN-web

    I would also appreciate some clarification on the difference between the function and when to use which one.

    For example if you want your <SEG> as whole, don't split to < , SEG, > three token, you need add_special_tokens()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions