You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 00:02:14