大模型训练时关于bitsandbytes安装的问题

背景:

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

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',
)

这需要安装bitsandbytes库,然而,安装完成后可能会出现如下问题:
1) PermissionError: [Errno 13] Permission denied: 'XXXXX'.

2) UserWarning: The installed version of bitsandbytes was compiled without GPU support.

3) module 'bitsandbytes' has no attribute 'nn'.

解决方案:

先确认该模型是否指定bitsandbytes版本,如果指定了版本,请看方案
方案一:

打开源码,找到文件“/home/user (自己位置)/anaconda3/envs/环境名字/lib/python3.11/site-packages/bitsandbytes/cuda_setup/main.py”
找到remove_non_existent_dirs,调换except PermissionError as pex和except OSError as exc:的位置,如下所示,因为PermissionError 是 OSError的子类。

def remove_non_existent_dirs(candidate_paths: Set[Path]) -> Set[Path]:
    existent_directories: Set[Path] = set()
    for path in candidate_paths:
        try:
            if path.exists():
                existent_directories.add(path)
        except PermissionError as pex:
            pass
        except OSError as exc:
            if exc.errno != errno.ENAMETOOLONG:
                raise exc

没有指定版本,则尝试方案二和方案三:

方案二:

重装或许有效果
step1:pip uninstall bitsandbytes

step2:pip install bitsandbytes

方案三:

使用pip install bitsandbytes正常安装库
找到文件“/home/user (自己位置)/anaconda3/envs/环境名字/lib/python3.11/site-packages/bitsandbytes/cuda_setup/main.py”
step1:找到

if not torch.cuda.is_available(): return 'libsbitsandbytes_cpu.so', None, None, None, None

step2:将其替换为

if torch.cuda.is_available(): return 'libbitsandbytes_cudaXXX(自己cuda版本).so', None, None, None, None 


step3:定位到

self.lib = ct.cdll.LoadLibrary(binary_path)

会找到两处。
step4:把两处都替换为

self.lib = ct.cdll.LoadLibrary(str(binary_path))

 参考:
https://github.com/TimDettmers/bitsandbytes/pull/622

Support for LLaMA models · Issue #147 · oobabooga/text-generation-webui · GitHub

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值