ChatGLM3:AttributeError_ can‘t set attribute ‘eos_token‘

文章描述了用户在使用ChatGLM3-6b模型进行微调后,在验证阶段遇到关于`eos_token`、`pad_token`和`unk_token`的AttributeError。解决方法是找到tokenizer_config.json文件,删除这些键值并确保无逗号,然后重新运行inference_hf.py脚本。
摘要由CSDN通过智能技术生成

最近在微调 ChatGLM3-6b 时,训练好模型之后,调用inference_hf.py函数验证模型的时候报了如下错误,下面是解决方案。

我在训练时使用的是ptuning_v2.yaml配置文件,训练运行代码如下:

CUDA_VISIBLE_DEVICES=1 python finetune_hf.py  data/AdvertiseGen_fix/ ../chatglm3-6b configs/ptuning_v2.yaml

错误信息

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /home/admin/ChatGLM3-6B/ChatGLM3/finetune_demo/inference_hf.py:50 in main                        │
│                                                                                                  │
│   47 │   │   model_dir: Annotated[str, typer.Argument(help='')],                                 │
│   48 │   │   prompt: Annotated[str, typer.Option(help='')],                                      │
│   49 ):                                                                                          │
│ ❱ 50 │   model, tokenizer = load_model_and_tokenizer(model_dir)                                  │
│   51 │   response, _ = model.chat(tokenizer, prompt)                                             │
│   52 │   print(response)                                                                         │
│   53                                                                                             │
│                                                                                                  │
│ /home/admin/ChatGLM3-6B/ChatGLM3/finetune_demo/inference_hf.py:30 in load_model_and_tokenizer    │
│                                                                                                  │
│   27 def load_model_and_tokenizer(model_dir: Union[str, Path]) -> tuple[ModelType, TokenizerT    │
│   28 │   model_dir = _resolve_path(model_dir)                                                    │
│   29if (model_dir / 'adapter_config.json').exists():                                        │
│ ❱ 30 │   │   model = AutoPeftModelForCausalLM.from_pretrained(                                   │
│   31 │   │   │   model_dir, trust_remote_code=True, device_map='auto'                            │
│   32 │   │   )                                                                                   │
│   33 │   │   tokenizer_dir = model.peft_config['default'].base_model_name_or_path                │
│                                                                                                  │
│ /home/admin/anaconda3/envs/chatglm/lib/python3.10/site-packages/peft/auto.py:123 in              │
│ from_pretrained                                                                                  │
│                                                                                                  │
│   120 │   │   │   )                                                                              │
│   121 │   │                                                                                      │
│   122 │   │   if tokenizer_exists:                                                               │
│ ❱ 123 │   │   │   tokenizer = AutoTokenizer.from_pretrained(                                     │
│   124 │   │   │   │   pretrained_model_name_or_path, trust_remote_code=kwargs.get("trust_remot   │
│   125 │   │   │   )                                                                              │
│   126 │   │   │   base_model.resize_token_embeddings(len(tokenizer))                             │
│                                                                                                  │
│ /home/admin/anaconda3/envs/chatglm/lib/python3.10/site-packages/transformers/models/auto/tokeniz │
│ ation_auto.py:810 in from_pretrained                                                             │
│                                                                                                  │
│   807 │   │   │   _ = kwargs.pop("code_revision", None)                                          │
│   808 │   │   │   if os.path.isdir(pretrained_model_name_or_path):                               │
│   809 │   │   │   │   tokenizer_class.register_for_auto_class()                                  │
│ ❱ 810 │   │   │   return tokenizer_class.from_pretrained(                                        │
│   811 │   │   │   │   pretrained_model_name_or_path, *inputs, trust_remote_code=trust_remote_c   │
│   812 │   │   │   )                                                                              │
│   813 │   │   elif config_tokenizer_class is not None:                                           │
│                                                                                                  │
│ /home/admin/anaconda3/envs/chatglm/lib/python3.10/site-packages/transformers/tokenization_utils_ │
│ base.py:2048 in from_pretrained                                                                  │
│                                                                                                  │
│   2045 │   │   │   else:                                                                         │
│   2046 │   │   │   │   logger.info(f"loading file {file_path} from cache at {resolved_vocab_fil  │
│   2047 │   │                                                                                     │
│ ❱ 2048 │   │   return cls._from_pretrained(                                                      │
│   2049 │   │   │   resolved_vocab_files,                                                         │
│   2050 │   │   │   pretrained_model_name_or_path,                                                │
│   2051 │   │   │   init_configuration,                                                           │
│                                                                                                  │
│ /home/admin/anaconda3/envs/chatglm/lib/python3.10/site-packages/transformers/tokenization_utils_ │
│ base.py:2287 in _from_pretrained                                                                 │
│                                                                                                  │
│   2284 │   │                                                                                     │
│   2285 │   │   # Instantiate the tokenizer.                                                      │2286 │   │   try:                                                                              │
│ ❱ 2287 │   │   │   tokenizer = cls(*init_inputs, **init_kwargs)                                  │
│   2288 │   │   except OSError:                                                                   │
│   2289 │   │   │   raise OSError(                                                                │
│   2290 │   │   │   │   "Unable to load vocabulary from file. "                                   │
│                                                                                                  │
│ /root/.cache/huggingface/modules/transformers_modules/checkpoint-3000/tokenization_chatglm.py:10 │
│ 8 in __init__                                                                                    │
│                                                                                                  │
│   105 │   │   │   "<pad>": self.tokenizer.pad_id                                                 │
│   106 │   │   }                                                                                  │
│   107 │   │   self.encode_special_tokens = encode_special_tokens                                 │
│ ❱ 108 │   │   super().__init__(padding_side=padding_side, clean_up_tokenization_spaces=clean_u   │
│   109 │   │   │   │   │   │    encode_special_tokens=encode_special_tokens,                      │
│   110 │   │   │   │   │   │    **kwargs)                                                         │
│   111                                                                                            │
│                                                                                                  │
│ /home/admin/anaconda3/envs/chatglm/lib/python3.10/site-packages/transformers/tokenization_utils. │
│ py:363 in __init__                                                                               │
│                                                                                                  │
│    360 │   │   self._added_tokens_encoder: Dict[str, int] = {k.content: v for v, k in self._add  │
│    361 │   │                                                                                     │
│    362 │   │   # 4 init the parent class                                                         │
│ ❱  363 │   │   super().__init__(**kwargs)                                                        │
│    364 │   │                                                                                     │
│    365 │   │   # 4. If some of the special tokens are not part of the vocab, we add them, at th  │366 │   │   # the order of addition is the same as self.SPECIAL_TOKENS_ATTRIBUTES following   │
│                                                                                                  │
│ /home/admin/anaconda3/envs/chatglm/lib/python3.10/site-packages/transformers/tokenization_utils_ │
│ base.py:1603 in __init__                                                                         │
│                                                                                                  │
│   1600 │   │   # Stores a Jinja template that formats chat histories into tokenizable strings    │1601 │   │   self.chat_template = kwargs.pop("chat_template", None)                            │
│   1602 │   │                                                                                     │
│ ❱ 1603 │   │   super().__init__(**kwargs)                                                        │
│   1604 │                                                                                         │
│   1605 │   @property                                                                             │
│   1606 │   def max_len_single_sentence(self) -> int:                                             │
│                                                                                                  │
│ /home/admin/anaconda3/envs/chatglm/lib/python3.10/site-packages/transformers/tokenization_utils_ │
│ base.py:861 in __init__                                                                          │
│                                                                                                  │
│    858 │   │   │   │   │   ), "One of the tokens is not a string or an AddedToken"               │
│    859 │   │   │   │   │   setattr(self, key, value)                                             │
│    860 │   │   │   │   elif isinstance(value, (str, AddedToken)):                                │
│ ❱  861 │   │   │   │   │   setattr(self, key, value)                                             │
│    862 │   │   │   │   else:                                                                     │
│    863 │   │   │   │   │   raise TypeError(f"Special token {key} has to be either str or AddedT  │
│    864                                                                                           │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
AttributeError: can't set attribute 'eos_token'

解决办法

  1. 找到模型保存文件夹中的 tokenizer_config.json。路径为:../output/checkpoint-xxxx/tokenizer_config.json

image.png

  1. 编辑tokenizer_config.json,删除其中的eos_tokenpad_tokenunk_token就可以了

image.png

注意:删除这三项后,要删除最后一行最后面的逗号!

重新运行代码,即可运行模型:

python inference_hf.py your_finetune_path --prompt your prompt
  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗的西瓜瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值