Skip to content

Llama tokenizers behaviours different with special tokens. #29872

Description

@ShomyLiu

System Info

  • transformers version: 4.39.1
  • Platform: Linux-3.10.0-1062.9.1.el7.x86_64-x86_64-with-glibc2.35
  • Python version: 3.10.12
  • Huggingface_hub version: 0.19.4
  • Safetensors version: 0.4.1
  • Accelerate version: 0.25.0
  • Accelerate config: not found
  • PyTorch version (GPU?): 2.1.2+cu121 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed

Who can help?

@ArthurZucker

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

the code sample is :

from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("./llama2", use_fast=False)

# add three extra different type tokens:
tok.add_tokens(['|<im_start1>|'], special_tokens=False)
tok.add_tokens(['|<im_start2>|'], special_tokens=True)
tok.add_special_tokens({"pad_token": '|<im_start3>|'})

print(tok)
# output:
"""
LlamaTokenizer(name_or_path='./llama2/', vocab_size=32000, model_max_length=1000000000000000019884624838656, is_fast=False, padding_side='right', truncation_side='right', special_tokens={'bos_token': '<s>', 'eos_token': '</s>', 'unk_token': '<unk>', 'pad_token': '|<im_start3>|', 'additional_special_tokens': ['|<im_start2>|']}, clean_up_tokenization_spaces=False),  added_tokens_decoder={
        0: AddedToken("<unk>", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
        1: AddedToken("<s>", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
        2: AddedToken("</s>", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
        32000: AddedToken("|<im_start1>|", rstrip=False, lstrip=False, single_word=False, normalized=True, special=False),
        32001: AddedToken("|<im_start2>|", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
        32002: AddedToken("|<im_start3>|", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
}
"""

print(tok.tokenize("|<im_start1>|system"))
# ['▁', '|<im_start1>|', 'system']

print(tok.tokenize("|<im_start2>|system"))
# ['|<im_start2>|', 'system']

print(tok.tokenize("|<im_start3>|system"))
# ['|<im_start3>|', 'system']

print(tok.decode(tok("|<im_start1>|system").input_ids))
# '<s>  |<im_start1>| system'

print(tok.decode(tok("|<im_start2>|system").input_ids))
# '<s> |<im_start2>| system'

print(tok.decode(tok("|<im_start3>|system").input_ids))
# '<s>|<im_start3>|system'

We can see that only the last example of using '|<im_start3>|' could recover the original input, and there will be a space in the other two examples.
So it is confusing, and I don't know what is right? (maybe the |<im_start3>| is the expected behavior?)

  • what is the difference of the following two methods:
   tok.add_tokens(['|<im_start2>|'], special_tokens=True)
   tok.add_special_tokens({"pad_token": '|<im_start3>|'})
  • Why there are extra space in the above three tokens?
  • How to change the behaviors of '|<im_start1>|' and '|<im_start2>|' to the same as '|<im_start3>|'?

Would you mind taking care of this issue? @ArthurZucker
Thanks a lot.

Expected behavior

I expect the behaviors of '|<im_start3>|'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Core: TokenizationInternals of the library; Tokenization.Should FixThis has been identified as a bug and should be fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions