【baichuan2模型部署经验】手把手教你在linux服务器上安装和使用baichuan2-7b-chat模型(模型下载+环境配置+报错分析)
【baichuan2模型部署经验】手把手教你在linux服务器上安装和使用baichuan2-7b-chat模型(模型下载+环境配置+报错分析)
目录
1. 模型下载
baichuan2的github上给出的示例代码很简单,直接使用AutoModelForCausaLLM.from_pretrained(模型名称)这行代码就可以使用了,然而
殊不知在服务器上运行代码,没开代理的话,huggingface.co是连接不上的!因此我们需要把模型下载到本地。如果在huggingface模型库界面一个个文件手动下载效率太低,我在这篇文章中【 从服务器上直接下载huggingface模型,解决huggingface无法连接问题】给出了使用代码下载的方式,可以点击阅读。
2. 环境配置
新建conda虚拟环境,下载配置文件。运行pip install -r requirements.txt
3. 报错分析
本以为就这么简单结束了,结果遇到一堆报错。解决如下
1. AttributeError: module ‘torch.backends.cuda’ has no attribute ‘sdp_kernel’
【原因分析】要使用torch2.0版本才能正常运行,我之前的版本是1.几的。
【解决】重新建一个python=3.10的虚拟环境,安装torch2.0
在这个网站上选择相应版本的torch安装包。我选择的是第一个,即torch2.0版本+cuda11.8+python3.10,并且是linux系统的。(win_amd64指的是windows系统)
右键选择复制链接,然后在之前安装好的conda环境中,输入wget + 链接进行下载。如
1 |
|
下载结束后使用pip install 安装包名字.whl 进行安装
1 |
|
输入python进入python环境,输入torch.__version__进行查询
1 |
|
结果如图所示:
2. AttributeError: ‘BaichuanTokenizer’ object has no attribute ‘sp_model’
【解决方案】使用4.33.3版本的transformers
1 |
|
3. RuntimeError: Failed to import transformers.modeling_utils because of the following error (look up to see its traceback):
1 |
|
因为cuda版本和torch不匹配(之前只下载了torch2.0,但没有配置相应的cuda11.8版本),执行以下代码安装cuda11.8
1 |
|
4. torch.cuda.OutOfMemoryError: CUDA out of memory.
Tried to allocate 192.00 MiB (GPU 0; 31.75 GiB total capacity; 30.58 GiB already allocated; 49.50 MiB free; 30.73 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
【原因分析】之前提交任务的时候只使用了单张内存为32G的V100卡,内存不足,需要指定两张卡来执行
【解决】我这里的是多人GPU服务器,提交任务使用的是LSF调度系统,参考提交任务的run.sh代码为:
1 |
|