一、背景
从modelscope手动下载了bin参数放在本地文件夹,需要加载并运行。
二、问题及解决
错误1:
ValueError: Tokenizer class XXXTokenizer does not exist or is not
currently imported.
解决方案
AutoTokenizer.from_pretrained
和AutoModelForCausalLM.from_pretrained
参数中加上trust_remote_code=True
错误2:
ValueError: The current
device_map
had weights offloaded to the
disk. Please provide anoffload_folder
for them. Alternatively, make
sure you havesafetensors
installed if the model you are using
offers the weights in this format.
解决方案
AutoModelForCausalLM.from_pretrained
的参数中加上offload_folder="offload_folder"
,将模型参数卸载到内存中,时间换空间,解决直接加载权重显存不足问题。(后注:之前没有cuda,可能不能直接gpu加载)
错误3:模型加载到cpu而不是gpu,出现报错RuntimeError: You can't move a model that has some modules offloaded to cpu or disk.
分析:
参考:Huggingface Accelerate文档:超大模型推理方法
Accelerate会自动检测在哪个设备放置模型的哪层参数(自动根据你的硬件资源分配模型参数)。
其规则如下: 首先充分利用GPU上的显存资源
如果GPU上资源不够了,那么就将权重存储到内存
如果内存还不够用了,将会使用内存映射的技术,将剩余的参数存储到硬盘上
因为自己是刚换的主机,忘记装cuda了,自动识别可能出现问题
解决方案
cmd检查nvidia-smi查看cuda支持的最高版本,下载对应的cuda toolkit和cuDnn
相应的pytorch版本也要重新安装成GPU版本(笔主检查了下之前安装的是2.0.1+cpu),由于cuda版本用的是12.0,官网安装低版本的pytorch(2.1.2+cu118),pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
接下来加载模型的时候就可以看到使用显存而不是内存了。
错误4:
发生警告:Xformers is not installed correctly. If you want to use memory_efficient_attention to accelerate training use the following command to install Xformers
解决方案:直接安装
pip install xformers
发现xformers不兼容pytorch版本(需要PyTorch 2.1.2+cu121),遂放弃忽略该问题
错误5:
13b和20b的模型都是多文件,加载的时候都提示问题,于是先尝试下7b量化的跑起来。下载了7b-chat-4bit模型文件,运行时出现需要安装bitsandbytes( CUDA 自定义函数的轻量级包装器),发现低版本CUDA SETUP。
如:CUDA SETUP: PyTorch settings found: CUDA_VERSION=118, Highest Compute Capability: 8.6.
重新安装了pytorch版本为2.0.1-cu118(模型部署统一的环境)
再次安装bitsandbytes出现
The installed version of bitsandbytes was compiled without GPU support.
尝试了pip install bitsandbytes-cuda116,报另一个错误:
OSError: [WinError 193] %1 不是有效的 Win32 应用程序。
解决方案
待更