ValueError: Tokenizer class LlamaTokenizer does not exist or is not currently imported
Asked Answered
H

1

6

I am trying to run the code from this Hugging Face blog. At first, I had no access to the model so this error: OSError: meta-llama/Llama-2-7b-chat-hf is not a local folder, is now solved and I created an acces token from Hugging Face which works. Now I'm facing a different error when running the following code:

from transformers import AutoTokenizer
import transformers
import torch

model = "meta-llama/Llama-2-7b-chat-hf"

tokenizer = AutoTokenizer.from_pretrained(model, use_auth_token=True)
pipeline = transformers.pipeline(
    "text-generation",
    model=model,
    torch_dtype=torch.float16,
    device_map="auto",
)

sequences = pipeline(
    'I liked "Breaking Bad" and "Band of Brothers". Do you have any recommendations of other shows I might like?\n',
    do_sample=True,
    top_k=10,
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id,
    max_length=200,
)
for seq in sequences:
    print(f"Result: {seq['generated_text']}")

Error:

ValueError: Tokenizer class LlamaTokenizer does not exist or is not currently imported.

The error is not the same as this error: ImportError: cannot import name 'LLaMATokenizer' from 'transformers', because now it is a valuerror. To make sure I use the right version I run this code:

pip install git+https://github.com/huggingface/transformers

After that I checked this issue ValueError: Tokenizer class LLaMATokenizer does not exist or is not currently imported. #22222 . This suggests:

Change the LLaMATokenizer in tokenizer_config.json into lowercase LlamaTokenizer and it works like a charm.

So, I checked the files if it is using LLamaTokenizer instead of LlamaTokenizer like for example here (This is the class in the file):

class LlamaTokenizer(PreTrainedTokenizer):

So I was wondering if anyone knows how to fix this error?

Hexapody answered 30/8, 2023 at 11:11 Comment(2)
If your tokenizers version is 0.13.2 or lower, I recommend upgrading it to 0.13.3. This solves my problem.Barlow
I have the same problem. I have followed the guide here: huggingface.co/blog/llama2 but it doesn't mention anything about tokenizers. News? @AbuShoeb how did you updated? Could you say more about the command/library?Mog
G
9

Just run the following command and you should be good. It will upgrade your transformers library and automatically pull the latest tokenizers for you

pip install --upgrade transformers

Guinna answered 7/11, 2023 at 14:34 Comment(2)
This is also useful for the new gemma tokenizer and model that Google just releasedValenti
I also faced the same issue with NllbTokenizer, and this helped. If HuggingFace could mention in docs the version of transformers required for a given model or tokenizer, that would be very helpful.Vengeful

© 2022 - 2024 — McMap. All rights reserved.