使用Unsloth的save_pretrained_merged保存微调模型时出现AttributeError错误
Unsloth的save_pretrained_merged保存微调模型时出现AttributeError错误
看起来你在使用Unsloth微调并保存unsloth/Llama-3.2-11B-Vision-Instruct模型时遇到了临时文件处理相关的错误,这个AttributeError: 'NoneType' object has no attribute 'name'通常和目录权限、磁盘空间不足或者Unsloth的临时文件逻辑有关,我整理了几个可行的解决思路:
1. 检查保存目录的权限与磁盘空间
首先排查最基础的文件系统问题:
- 确认你指定的
"unsloth_finetune"目录当前用户有读写权限,无权限会导致Unsloth无法创建临时文件,进而返回None对象。 - 合并保存16bit的11B多模态模型需要至少22GB左右的磁盘空间,检查剩余空间是否充足。
- 可以先手动创建目标目录,避免自动创建失败:
import os os.makedirs("unsloth_finetune", exist_ok=True)
2. 显式指定临时文件目录
错误发生在临时文件创建环节,你可以通过temporary_location参数指定一个你确认有权限、有空间的目录:
model.save_pretrained_merged( "unsloth_finetune", tokenizer, save_method = "merged_16bit", # 替换成你有权限的目录,比如系统临时目录或自定义目录 temporary_location = "/tmp/unsloth_temp" )
记得提前创建这个临时目录:os.makedirs("/tmp/unsloth_temp", exist_ok=True)
3. 先保存PEFT格式再合并
如果直接合并保存有问题,可以拆分步骤:先保存轻量化的PEFT模型,再加载合并:
# 第一步:保存PEFT微调权重 model.save_pretrained("unsloth_finetune_peft") tokenizer.save_pretrained("unsloth_finetune_peft") # 第二步:加载PEFT模型并合并保存 from unsloth import FastVisionModel model, tokenizer = FastVisionModel.from_pretrained( "unsloth_finetune_peft", load_in_4bit=False, ) model.save_pretrained_merged( "unsloth_finetune_merged", tokenizer, save_method="merged_16bit" )
4. 升级Unsloth到最新版本
这个错误有可能是Unsloth旧版本的bug,升级到最新版可以修复已知的文件处理问题:
!pip install --upgrade unsloth
如果以上方法都不行,你可以尝试用save_method="pytorch"先保存原始格式,再手动转换,或者检查是否在加载模型时有没有遗漏多模态相关的参数,但从错误栈来看,优先解决文件系统和临时目录的问题应该能解决这个报错。
备注:内容来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

