Skip to content

Inconsistent tokenization between fast and slow tokenizers for sentencepiece user-defined tokens. #29868

Description

@TissueC

System Info

  • transformers version: 4.38.2
  • Platform: Linux-5.4.0-125-generic-x86_64-with-glibc2.29
  • Python version: 3.8.10
  • Huggingface_hub version: 0.21.4
  • Safetensors version: 0.4.2
  • Accelerate version: not installed
  • Accelerate config: not found
  • PyTorch version (GPU?): 2.1.0a0+fe05266 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?:
  • Using distributed or parallel set-up in script?:

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

Fast tokenizers cannot view the added token as a whole but split it as many small parts.
I found the similar issue in the huggingface discussion (but with no solution) : https://discuss.huggingface.co/t/sentencepiece-user-defined-symbols-and-fast-tokenizers/52208
Here is a script to reproduce this issue:

from transformers import AutoTokenizer
import transformers.utils.sentencepiece_model_pb2 as sp_model
import json

proto = sp_model.ModelProto()

llama_orgin_tokenizer_path = 'Llama-2-7b-hf' # this a local path
llama_tokenizer_with_added_token_path = 'Llama-2-7b-hf-with-added-token'

with open(f'{llama_orgin_tokenizer_path}/tokenizer.model', 'rb') as fin:
    proto_str = fin.read()
    proto.ParseFromString(proto_str)

# append a user-defined token
adding_token = 'aaabbbcc'
proto.pieces.append(sp_model.ModelProto.SentencePiece(piece=adding_token, score=0.0, type=4)) #type 4 means user defined
with open(f'{llama_tokenizer_with_added_token_path}/tokenizer.model', 'wb') as fout:
    fout.write(proto.SerializeToString())


with open(f'{llama_tokenizer_with_added_token_path}/tokenizer_config.json','w', encoding='utf-8') as fout:
    tokenizer_config = {
        "model_max_length":99999999999999,
        "tokenizer_class": "LlamaTokenizer"
    }
    json.dump(tokenizer_config, fout)



tokenizer_fast = AutoTokenizer.from_pretrained(llama_tokenizer_with_added_token_path, use_fast=True)
tokenizer_slow = AutoTokenizer.from_pretrained(llama_tokenizer_with_added_token_path, use_fast=False)

text = 'I love aaabbbcc.'
print('fast:', tokenizer_fast.tokenize(text))
print('slow:', tokenizer_slow.tokenize(text))

# The outputs:
# fast: ['▁I', '▁love', '▁aa', 'ab', 'bb', 'cc', '.']
# slow: ['▁I', '▁love', '▁', 'aaabbbcc', '.']

Expected behavior

It should be consistent tokenization results between fast and slow tokenizers for those added tokens.

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