使用SFTTrainer,qLoRA 量化微调huggingface上面的大模型,包括mistral,gemma等,微调之后,使用
model.save_pretrained()保存在本地。保存的是.safetensors,看起来没什么问题,但使用llama.cpp的convert-hf-to-gguf.py 要转化为gguf格式时,会报:ValueError: Can not map tensor 'model.layers.0.mlp.down_proj.base_layer.weight' 错误,导致无法转化完成。
重新使用AutoModelForCausalLM.from_pretrained("本地保存目录") 装载时会报warning:
Unused kwargs: ['_load_in_4bit', '_load_in_8bit', 'quant_method']. These kwargs are not used in <class 'transformers.utils.quantization_config.BitsAndBytesConfig'>.
Some weights of the model checkpoint at d:\models\mistral were not used when initializing MistralForCausalLM: ['model.layers.0.mlp.down_proj.base_layer.weight', 'model.layers.0.mlp.down_proj.base_layer.weight.absmax', 'model.layers.0.mlp.down_proj.base_layer.weight.nested_absmax' ...
使用装载后model进行推理时,得到的答案是乱的(毫无逻辑,语言也是乱输出的),跟刚训练完尚未保存时的完全不同。
查看model,发现.base_layer 层的都不见了,是我保存微调的方法有问题吗?我看网上的代码都是使用model.save_pretrained来保存模型的呀。问题出在哪?
大模型微调保存后,重新加载时异常
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
关注让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
问题描述: 使用SFTTrainer,qLoRA 量化微调huggingface上面的大模型,包括mistral,gemma等,微调之后,使用model.save_pretrained()保存在本地。保存的是.safetensors,但使用llama.cpp的convert-hf-to-gguf.py 要转化为gguf格式时,会报:ValueError: Can not map tensor 'model.layers.0.mlp.down_proj.base_layer.weight' 错误,导致无法转化完成。重新使用AutoModelForCausalLM.from_pretrained("本地保存目录") 装载时会报warning: Unused kwargs: ['_load_in_4bit', '_load_in_8bit', 'quant_method']. These kwargs are not used in class 'transformers.utils.quantization_config.BitsAndBytesConfig'. Some weights of the model checkpoint at d:\models\mistral were not used when initializing MistralForCausalLM: ['model.layers.0.mlp.down_proj.base_layer.weight', 'model.layers.0.mlp.down_proj.base_layer.weight.absmax', 'model.layers.0.mlp.down_proj.base_layer.weight.nested_absmax' ...使用装载后model进行推理时,得到的答案是乱的(毫无逻辑,语言也是乱输出的),跟刚训练完尚未保存时的完全不同。查看model,发现.base_layer 层的都不见了,是我保存微调的方法有问题吗?我看网上的代码都是使用model.save_pretrained来保存模型的呀。问题出在哪? 解答:- 报错原因:使用llama.cpp的convert-hf-to-gguf.py转换模型时,报错ValueError: Can not map tensor 'model.layers.0.mlp.down_proj.base_layer.weight'。这是因为SFTTrainer和qLoRA量化微调的模型中,包含了自定义的层,而gguf格式不支持自定义层,因此无法转换。
- 警告原因:使用AutoModelForCausalLM.from_pretrained()装载模型时,会报出Unused kwargs警告。这是因为transformers版本较新,BitsAndBytesConfig已经取代之前的_load_in_4bit、_load_in_8bit等参数,但是AutoModelForCausalLM.from_pretrained()函数中的参数仍使用旧的参数,导致警告。
- 模型推理乱码原因:模型推理乱码的原因可能是微调时使用的参数配置有误,或者使用了过多的量化方法。另外,由于自定义的层没能被保存,也会导致模型推理结果不正确。
- 解决方法:若需要将SFTTrainer和qLoRA量化微调的模型转换为gguf格式,可以使用其他转换方法(如TensorFlow Lite或ONNX等),或者去除自定义层再进行转换。需要注意的是,去除自定义层后,模型性能可能会受到影响,需要重新评估。关于AutoModelForCausalLM.from_pretrained()函数中的参数问题,可以参考transformers文档进行修改。至于模型推理乱码的问题,可以尝试重新微调模型,或者调整量化相关参数进行测试。同时也可以检查模型推理时输入数据的格式是否正确。
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用