开发者社区 > ModelScope模型即服务 > 自然语言处理 > 正文

使用官方帖子里的代码,报错缺sp_model参数

使用代码

import torch
from modelscope import snapshot_download, Model
model_dir = snapshot_download("baichuan-inc/Baichuan-13B-Chat", revision='v1.0.8')
model = Model.from_pretrained(model_dir, device_map="balanced", trust_remote_code=True, torch_dtype=torch.float16)
messages = []
messages.append({"role": "user", "content": "世界上第二高的山峰是哪一座?"})
response = model(messages)
print(response)
messages = response['history'].copy()
messages.append({"role": "user", "content": "世界上第一高的山峰是哪一座?"})
response = model(messages)
print(response)

代码链接

我的环境是:

modelscope 1.9.1
torch 2.1.0

报错如下:

2023-12-07 00:52:35,974 - modelscope - WARNING - ('MODELS', 'text-generation', 'Baichuan-13B-Chat') not found in ast index file
Traceback (most recent call last):
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/modelscope/utils/registry.py", line 210, in build_from_cfg
return obj_cls._instantiate(args)
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/modelscope/models/base/base_model.py", line 67, in _instantiate
return cls(
kwargs)
File "/home/caston/.cache/modelscope/modelscope_modules/Baichuan-13B-Chat/ms_wrapper.py", line 52, in init
self.tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/transformers/models/auto/tokenization_auto.py", line 755, in from_pretrained
return tokenizer_class.from_pretrained(pretrained_model_name_or_path, inputs, **kwargs)
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 2024, in from_pretrained
return cls._from_pretrained(
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 2256, in _from_pretrained
tokenizer = cls(
init_inputs, **init_kwargs)
File "/home/caston/.cache/huggingface/modules/transformers_modules/Baichuan-13B-Chat/tokenization_baichuan.py", line 55, in init
super().init(
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/transformers/tokenization_utils.py", line 367, in init
self._add_tokens(
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/transformers/tokenization_utils.py", line 467, in _add_tokens
current_vocab = self.get_vocab().copy()
File "/home/caston/.cache/huggingface/modules/transformers_modules/Baichuan-13B-Chat/tokenization_baichuan.py", line 89, in get_vocab
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
File "/home/caston/.cache/huggingface/modules/transformers_modules/Baichuan-13B-Chat/tokenization_baichuan.py", line 85, in vocab_size
return self.sp_model.get_piece_size()
AttributeError: 'BaichuanTokenizer' object has no attribute 'sp_model'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/caston/Code/Python/LLM_Api/model_scope.py", line 7, in
model = Model.from_pretrained(model_dir, device_map="balanced", trust_remote_code=True, torch_dtype=torch.float16)
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/modelscope/models/base/base_model.py", line 179, in from_pretrained
model = build_model(model_cfg, task_name=task_name)
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/modelscope/models/builder.py", line 35, in build_model
model = build_from_cfg(
File "/home/caston/Envs/miniconda3/envs/modelscope/lib/python3.8/site-packages/modelscope/utils/registry.py", line 215, in build_from_cfg
raise type(e)(f'{obj_cls.name}: {e}')
AttributeError: Baichuan13BChatTextGeneration: 'BaichuanTokenizer' object has no attribute 'sp_model'

展开
收起
caston 2023-12-07 07:56:19 420 0
1 条回答
写回答
取消 提交回答
  • 面对过去,不要迷离;面对未来,不必彷徨;活在今天,你只要把自己完全展示给别人看。

    这个错误是因为在BaichuanTokenizer类中没有找到sp_model属性。你需要检查BaichuanTokenizer类的实现,确保它包含了sp_model属性。如果需要添加该属性,可以在__init__方法中进行初始化。

    2023-12-07 14:05:00
    赞同 展开评论 打赏

包含命名实体识别、文本分类、分词、关系抽取、问答、推理、文本摘要、情感分析、机器翻译等多个领域

热门讨论

热门文章

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载