使用AutoTrain微调的Mistral模型在Colab加载时遇AttributeError错误求助
问题描述
我使用Hugging Face AutoTrain对Mistral大语言模型进行了微调,模型参数和检查点已保存至Hugging Face模型仓库。尝试在Google Colab中加载测试,使用代码如下:
from transformers import AutoModelForCausalLM, AutoTokenizer model_path = "Layla321/llm_finetuning-0" tokenizer = AutoTokenizer.from_pretrained(model_path) model = AutoModelForCausalLM.from_pretrained( model_path, device_map="auto", torch_dtype='auto' ).eval() messages = [ {"role": "user", "content": "hi"} ] input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, add_generation_prompt=True, return_tensors='pt') output_ids = model.generate(input_ids.to('cuda')) response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True) print(response)
运行后出现错误:
AttributeError: 'TrainingArguments' object has no attribute 'values'
已更新transformers库并清除缓存,但错误仍存在。
解决思路
- 清理模型仓库中的
training_args.bin文件:AutoTrain导出模型时可能附带了该文件,其中的TrainingArguments对象与当前transformers版本不兼容。登录Hugging Face模型仓库页面,删除这个文件后重新加载模型。 - 对齐transformers版本:确保Colab中安装的transformers版本与AutoTrain微调时使用的版本一致。例如微调时用的是4.35.2版本,执行以下命令安装对应版本:
!pip install transformers==4.35.2 --force-reinstall - 加载模型时跳过参数检查:在
from_pretrained中添加ignore_mismatched_sizes=True参数,强制跳过不兼容的参数检查:model = AutoModelForCausalLM.from_pretrained( model_path, device_map="auto", torch_dtype='auto', ignore_mismatched_sizes=True ).eval() - 重新导出模型:如果上述方法无效,回到AutoTrain重新导出模型,选择仅导出模型和tokenizer相关文件,避免包含训练参数文件。
内容的提问来源于stack exchange,提问作者layla Bitar
相关产品推荐
相关产品推荐

