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
Tasks
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.
System Info
transformersversion: 4.38.2Who can help?
@ArthurZucker
Information
Tasks
examplesfolder (such as GLUE/SQuAD, ...)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:
Expected behavior
It should be consistent tokenization results between fast and slow tokenizers for those added tokens.