大模型训练时,使用bitsandbytes报错的解决方法

在对大语言模型如LLaMa或Chat-GLM进行微调时,为减少显存占用,会使用特定的加载方法。然而,遇到bitsandbytes库没有GPU支持的问题,导致编译错误。解决方法包括安装bitsandbytes库后,手动编辑其源码,根据CUDA版本调整相关路径,从而避免加载错误。此方案可以成功解决模型加载时的报错问题。

前言
在对大语言模型(LLaMa、Chat-GLM等)进行微调时,考虑到减少显存占用,会使用如下方式加载模型。

from transformers import AutoModel

model = AutoModel.from_pretrained(
    model_path,
    trust_remote_code=True,
    load_in_8bit=True,
    torch_dtype=torch.float16,
    device_map='auto',
)
1
2
3
4
5
6
7
8
9
为了使用上述功能,需要安装bitsandbytes库,但在使用时,会提示UserWarning: The installed version of bitsandbytes was compiled without GPU support.。
进而,在模型加载时会报有关"libsbitsandbytes_cpu.so"的编译错误。

解决方案
可通过如下方式解决上述问题:

使用pip install bitsandbytes正常安装库
切换到bitsandbytes所在lib目录,例如: xxx/venv/lib/python3.9/site-packages/bitsandbytes/cuda_setup
使用vim指令或其他方式编辑main.py文件
定位到if not torch.cuda.is_available(): return 'libsbitsandbytes_cpu.so', None, None, None, None,将其替换为if torch.cuda.is_available(): return 'libbitsandbytes_cuda116.so', None, None, None, None 。(并非一定要使用cuda116,只需大于等于自身显卡cuda版本即可)
定位到self.lib = ct.cdll.LoadLibrary(binary_path),会找到两处,把两处都替换为self.lib = ct.cdll.LoadLibrary(str(binary_path))
然后使用之前的模型加载代码则不会报错了。

参考资料:
https://github.com/oobabooga/text-generation-webui/issues/147#issuecomment-1456040134
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值