【大模型】Llama3-8B报错_sentencepiece.SentencePieceProcessor_LoadFromFileself, arg TypeError: not a string
环境
- Ubuntu22.04
- python3.11
运行模型
Llama3-8B-Chinese-Chat下载地址:
https://hf-mirror.com/shenzhi-wang/Llama3-8B-Chinese-Chat
跑 Llama3-8B-Chinese-Chat 模型:
from transformers import LlamaTokenizer, AutoModelForCausalLM
model_id = "shenzhi-wang/Llama3-8B-Chinese-Chat"
tokenizer = LlamaTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id, torch_dtype="auto", device_map="auto"
)
messages = [
{"role": "user", "content": "写一首诗吧"},
]
input_ids = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=8192,
do_sample=True,
temperature=0.6,
top_p=0.9,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
错误信息
File /usr/local/lib/python3.11/dist-packages/sentencepiece/__init__.py:317, in SentencePieceProcessor.LoadFromFile(self, arg)
315 def LoadFromFile(self, arg):
316 print("debug: arg is ", arg)
--> 317 return _sentencepiece.SentencePieceProcessor_LoadFromFile(self, arg)
TypeError: not a string
分析错误
在 Llama、Llama2 中使用 LlamaTokenizer 加载 Tokenizer,但在 Llama3 中使用 AutoTokenizer 加载 Tokenizer
Llama3 is a different tokenizer and should only be initialized with AutoTokenizer
解决方案
修改代码:
from transformers import AutoTokenizer, AutoModelForCausalLM
model_id = "shenzhi-wang/Llama3-8B-Chinese-Chat"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id, torch_dtype="auto", device_map="auto"
)
messages = [
{"role": "user", "content": "写一首诗吧"},
]
input_ids = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=8192,
do_sample=True,
temperature=0.6,
top_p=0.9,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
测试
再次运行模型正常了