在新手在消费级GPU上本地部署chatGLM-6B-CSDN博客完成chatGLM-6B(FP16)的部署后,尝试对模型进行loRA微调,期间遇到并解决了一些bug,分享给大家,希望大家少走弯路!
参考LLM - ChatGLM-6B Lora 微调与推理_chatglm2-6b训练lora-CSDN博客在运用LoRA对ChatGLM-6B(FP16)大语音模型时,遇到了bug1:ValueError: Attempting to unscale FP16 gradients. 和 bug2: AttributeError: 'ChatGLMTokenizer' object has no attribute 'sp_tokenizer'. Did you mean: '_tokenize'? ,经过一波挫折后,终于解决,把解决的途径总结分享给大家。
在运行LoRA微调程序Chatglm_lora_tuning.py时,出现bug1:
ValueError: Attempting to unscale FP16 gradients.
为解决该问题,通过查询,参考https://github.com/huggingface/transformers/issues/23165 网页里面的建议,将peft、transformer更新至新新版本peft==0.7.1,transformer==4.37.1。代码为:
pip install --upgrade peft
pip install --upgrade transformers
再次运行LoRA微调程序,bug1:ValueError: Attempting to unscale FP16 gradients. 问题消失。但是随即,报出了bug2: AttributeError: 'ChatGLMTokenizer' object has no attribute 'sp_tokenizer'. Did you mean: '_tokenize'? 。
针对bug2,参考文章https://huggingface.co/THUDM/chatglm2-6b/discussions/87 网页中建议,将THUDM/chatglm-6b文件夹下面的tokenization_chatglm.py中的
self.sp_tokenizer = SPTokenizer(vocab_file, num_image_tokens=num_image_tokens),移动到super().init前面。
保存后,重新运行LoRA微调程序Chatglm_lora_tuning.py,bug1:ValueError: Attempting to unscale FP16 gradients. 再次出现!!!(老板们,当时心态都咋了!这不是bug循环坑嘛!)
平复心态后,继续找bug1的解决方案,看了这篇文章https://github.com/huggingface/peft/issues/341 ,大概感觉是peft的版本问题太高了。不管三七二十一,先试试,于是将peft的版本降到0.3.0,代码如下:
pip install peft==0.3.0
然后,奇迹就出现了,程序跑通了。哈哈哈哈哈哈哈哈哈!
顺带还有个福利是,解决了这两个bug后,之前在运行Chatglm_lora_tuning.py时出现的GPU(3090,显存24G)内存溢出(torch.OutOfMemoryError:CUDA out of Memory. Tried to allocate ......)的问题也解决了。太舒爽了。
希望大家少走弯路,也拜托大家多多点赞、收藏,谢谢!
参考文献:
新手在消费级GPU上本地部署chatGLM-6B-CSDN博客
LLM - ChatGLM-6B Lora 微调与推理_chatglm2-6b训练lora-CSDN博客
https://github.com/huggingface/transformers/issues/23165
https://huggingface.co/THUDM/chatglm2-6b/discussions/87
https://github.com/huggingface/peft/issues/341