基于华为昇腾910B,实战InternVL2-8B模型推理
本文将带领大家基于启智平台,使用 LMDeploy 推理框架在华为昇腾 910B 上实现 InternVL2-8B 模型的推理。
https://github.com/OpenGVLab/InternVL
https://github.com/InternLM/lmdeploy
1.登录启智平台

2.创建云脑任务
我们新建云脑任务

目前启智平台提供 4 种 AI 训练任务(调试任务、训练任务、在线推理、通用任务),这里我们选择调试任务。
所属项目,我们选择一个已经有的项目,没有项目,可以新建,这个就不详细展开。(可以点击右上角)


接下就是选择算力平台,启智平台目前提供好几个厂商的算力平台。(英伟达、昇腾NPU、遂源GCU、寒武纪MLU、海光DCU、天数智芯GPGPU、沐曦GPGPU),这里我们选华为昇腾NPU。
资源规格栏, 我们选择 D910B 的显卡。(显存 64GB 、CPU24 、内存 192GB)

镜像栏会随着我们选择的显卡会出现相应的模型镜像,这里我们选择 openmind_cann8。


点击新建任务等待服务器创建新任务,创建完成系统会进入服务器资源分配等待列表中。

当分配资源完成后,状态变成运行状态,这个时候右边操作会出现调试按钮。
3.模型调试
我们点击调试按钮,进入 jupyterlab 调试代码界面。

3.1 下载模型
我们在 jupyterlab 界面编写下载模型代码脚本。
Shell
sh#!/bin/bash
#conda install git-lfs -y
pip install -U openi -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install c2net
model_download.sh
Shell
#!/bin/bash
# 定义变量
model_user_name="FoundationModel/OpenGVLab"
model_name="InternVL2-8B"
save_path="/home/ma-user/work/pretrainmodel/InternVL2-8B"
# 检查保存路径是否存在,如果不存在则创建
if [ ! -d "$save_path" ]; then
mkdir -p "$save_path"
fi
# 调用 openi 命令下载模型
openi model download $model_user_name $model_name --save_path $save_path
Python
from c2net.context import prepare
#初始化导入数据集和预训练模型到容器内
c2net_context = prepare()
#获取预训练模型路径
InternVL2_8B_v1_path = c2net_context.pretrain_model_path+"/"+"InternVL2-8B"
print(InternVL2_8B_v1_path)
我们可以将脚本和代码上传到 jupyterlab 调试界面里面。
分别执行以上脚本和代码,先执行 start.sh,再执行 model_download.sh。
bash start.sh


再执行 model_download.sh 下载模型
bash model_download.sh

当模型下载完成后,我们执行 checkmodel.py 检查模型并初始化 代码目录、数据集、模型输出、模型预训练目录。
python checkmodel.py

刷新一下当前目录,我们会看到左边代码区 code、dataset、output、pretrainmodel 4个文件夹,顾名思义,code 是放代码的;dataset 是放数据集的;output 是模型训练或者微调输出目录;pretrainmodel 是模型挂载的目录。
我们进入 pretrainmodel:
Shell
cd /home/ma-user/work/pretrainmodel/InternVL2-8B

我们已经可以看到下载好的模型权重文件了。
3.2 支持华为 ascend 国产硬件接入大模型推理框架 dlinfer
这里我们借助一下开源项目 dlinfer,项目地址 https://github.com/DeepLink-org/dlinfer。
目前它支持 LMDeploy 部分模型推理,见下表:

安装参考:https://pypi.org/project/dlinfer-ascend/
Shell
pip install dlinfer-ascend==0.1.0.post1

3.3 LMDeploy 推理框架安装
接下来我们需要再 code 代码目录下面下载 LMDeploy 推理程序并安装。 安装之前我们需要下载 LMDeploy 源码,
源码中需要删除 LMDeploy 推理框架中 requirements/runtime.txt 中关于 triton 依赖包。
因为 Triton 是 NVIDIA 推出的一款开源推理服务软件,旨在简化深度学习模型在生产环境中的部署和执行。它属于 NVIDIA AI 平台的一部分,能够在基于 GPU 或 CPU 的基础设施(如云、数据中心或边缘设备)上运行。我们在华为平台上部署也用不到它。(如果你安装大概率你是安装不上的,不信你试一试。)
我们修改 requirements/runtime.txt:

Shell
cd code
git clone https://github.com/InternLM/lmdeploy.git
# 如果网络慢可以使用下面代理
git clone -b v0.6.1 https://mirror.ghproxy.com//https://github.com/InternLM/lmdeploy.git
cd lmdeploy
pip install -e .
# 升级pillow 到11.0.0 版本
pip install --upgrade pillow
# 使用transformers4.44.1 版本,不要升级到最新版本
pip install transformers==4.44.1 -U

检查一下 LMDeploy
Shell
pip show lmdeploy

注意:这里我们需要注意 LMDeploy 版本必须要大于 0.6.0 版本。
3.4 LMDeploy 推理
接下来我们编写推理代码实现模型推理。
Python
from lmdeploy import pipeline, PytorchEngineConfig
from lmdeploy.vl import load_image
if name == "__main__":
pipe = pipeline('/home/ma-user/work/pretrainmodel/InternVL2-8B',
backend_config=PytorchEngineConfig(tp=1, device_type='ascend'))
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe(('describe this image', image))
print(response)
执行如下命令
Shell
cd code
touch inference.py
复制以上代码到 inference.py 代码中

执行推理代码
Shell
python inference.py

我们查看一下这张原图
https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg

从模型推理来看回答的是正确的。通过简单的推理代码就可以完成多模态模型的推理,效果还是不错的。
我们查看一下显存情况
Shell
npu-smi info

LMDeploy 默认使用 0.9 的缓存参数设置,这里我们没有修改默认参数,所以模型推理后剩余的显存都分配给模型缓存使用了,所以占用的显存比较大。这里就不详细展开技术细节了。想了解这块知识可以参加书生大模型实战营,学习关于模型量化相关的知识。
我们使用 lmdeploy chat 来推理。
Shell
lmdeploy chat /home/ma-user/work/pretrainmodel/InternVL2-8B --backend pytorch --device ascend

我们输入问题测试下推理效果:

4.总结
在 0.6.0这个版本上开始支持华为昇腾NPU,本文主要讲述了如何使用 LMDeploy 推理框架在华为昇腾 910B 上实现 InternVL2-8B 模型的推理。
LMDeploy 目前支持 InternVL1.5 和 InternVL2 系列的多模态模型,因为时间关系我这里只做了 2B 和 8B 模型的推理,其他模型没有做过测试。感兴趣的小伙伴也可以根据文档实现其它模型的推理和适配,我们也期待 LMDeploy推理框架支持更多更好用的多模态大模型。