背景:
在对大语言模型(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