Skip to content

mT5 additional_special_tokens seems not work #9747

Closed

Description

@PiggyFan

I want add some special tokens such as <CON_START> . But T5tokenizer/MT5tokenizer both can't tokenize correctly after using additional_special_tokens parameter. It still split these special tokens to subwords.
截圖 2021-01-22 下午5 18 00

It works when using OpenAIGPTTokenizer additional_special_tokens parameter. It's clear that after declare additional_special_tokens parameter, OpenAIGPTTokenizer tokenize as one word rather split it.
截圖 2021-01-22 下午5 54 57
截圖 2021-01-22 下午5 55 10

The version of transformers is 4.2.2
And I'm not sure this problem is related with issue624 in T5 which talk about SentencePiece extra vocab.

Thank you for your feedback

Activity

  1. LysandreJik commented on Jan 25, 2021

    @LysandreJik
    Member

    Hi! Could you either post a link to your notebook or your code as actual code? Images makes it impossible to copy/paste or for others to search with similar issues. Thanks.

  2. PiggyFan commented on Jan 28, 2021

    @PiggyFan
    Author

    Hi! Could you either post a link to your notebook or your code as actual code? Images makes it impossible to copy/paste or for others to search with similar issues. Thanks.

    Here is colab notebook link. Thanks for your reply.

    https://colab.research.google.com/drive/1fbp7VvnUvbf5r8CSitOg2pDZyM47y9xj?usp=sharing

  3. LysandreJik commented on Jan 28, 2021

    @LysandreJik
    Member

    Hi! Indeed, this is a bit misleading. Special tokens are considered as those that were in the pre-training, that is: unknown tokens, bos tokens, eos tokens, etc.

    If you want to use special tokens that you use as special tokens, I would argue it is better to define them as simple tokens. Therefore doing the following:

    >>> from transformers import MT5Tokenizer, MT5ForConditionalGeneration
    ... import torch
    ... special_tokens = ['<POS>', '<NEG>','<CON_START>','<START>','<END>'] # Set the special tokens
    ... mt5_add_tokenizer = MT5Tokenizer.from_pretrained("google/mt5-small")
    ... mt5_add_tokenizer.add_tokens(special_tokens)
    ... print(mt5_add_tokenizer.tokenize("<POS> <CON_START> the biscuits and gravy were . <START>"))

    You'll get the following output:

    ['<POS>', '<CON_START>', '▁the', '▁b', 'iscuit', 's', '▁and', '▁grav', 'y', '▁were', '▁', '.', '<START>']
    

    Let me know if that makes sense.

  4. PiggyFan commented on Jan 29, 2021

    @PiggyFan
    Author

    Thank you very much, it works well.
    I was confused by issue5940 which mentioned "special tokens are carefully handled by the tokenizer (they are never split)". If use add_token() method, the additional simple tokens may still split?
    In addition, the code in Colab notebook as above link, OpenaiTokenizer should use add_tokens method rather than add_special_tokens (define them as a simple tokens) ?

  5. LysandreJik commented on Jan 29, 2021

    @LysandreJik
    Member

    For tokens that cannot be identified as being either:

    • eos_token
    • bos_token
    • cls_token
    • unk_token
    • pad_token
    • sep_token
    • mask_token

    Then I would recommend using the add_token method. These tokens shouldn't split either:

    >>> from transformers import BertTokenizer
    >>> tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
    >>> tokenizer.add_tokens(["<CON_START>", "<CON_ST"])
    2
    >>> tokenizer.tokenize("<CON_START>")
    ['<CON_START>']
    >>> tokenizer.tokenize("<CON_STAR>")
    ['<CON_ST', 'AR', '>']
  6. PiggyFan commented on Feb 1, 2021

    @PiggyFan
    Author

    Thank for your help and recommend.

  7. Victordongy commented on Apr 6, 2022

    @Victordongy

    For tokens that cannot be identified as being either:

    • eos_token
    • bos_token
    • cls_token
    • unk_token
    • pad_token
    • sep_token
    • mask_token

    Then I would recommend using the add_token method. These tokens shouldn't split either:

    >>> from transformers import BertTokenizer
    >>> tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
    >>> tokenizer.add_tokens(["<CON_START>", "<CON_ST"])
    2
    >>> tokenizer.tokenize("<CON_START>")
    ['<CON_START>']
    >>> tokenizer.tokenize("<CON_STAR>")
    ['<CON_ST', 'AR', '>']

    Hi it seems add_tokens not working with AutoTokenizer , but work with specific-defined tokenizer like BertTokenizer,

    special_tokens = ["-Title-"]
    #tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
    tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
    tokenizer.add_tokens(special_tokens)
    tokenizer.tokenize("-Title-")
    # ['-', 'title', '-']
  8. ArthurZucker commented on Jun 1, 2023

    @ArthurZucker
    Collaborator

    He! Thanks for reporting. This should be fixed by #23909

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