开始使用华为昇腾(Atlas 800T A2)#

在华为Ascend设备上使用lmdeploy与在CUDA上使用PytorchEngine的lmdeploy几乎相同。 在阅读本教程之前,请先阅读原始的Get Started指南。

这里是支持的模型列表

安装#

我们强烈建议用户构建一个Docker镜像以简化环境设置。

Git克隆lmdeploy的源代码,Dockerfile位于docker目录中:

git clone https://github.com/InternLM/lmdeploy.git
cd lmdeploy

环境准备#

Docker 版本应不低于 18.03。并且应按照 官方指南 安装 Ascend Docker Runtime

[!注意] 如果出现错误信息 libascend_hal.so: cannot open shared object file,这意味着 Ascend Docker Runtime 没有正确安装!

Ascend 驱动程序、固件和CANN#

目标机器需要安装不低于23.0.3版本的华为驱动程序和固件,请参考 CANN驱动和固件安装下载资源

并且CANN(版本8.0.RC2.beta1)软件包也应从Ascend资源下载中心自行下载。确保将Ascend-cann-kernels-910b*.runAscend-cann-nnal_*.runAscend-cann-toolkit*-aarch64.run放置在lmdeploy源代码的根目录下。

构建Docker镜像#

在lmdeploy的根目录下运行以下命令来构建镜像:

DOCKER_BUILDKIT=1 docker build -t lmdeploy-aarch64-ascend:latest \
    -f docker/Dockerfile_aarch64_ascend .

Dockerfile_aarch64_ascend 已在鲲鹏 CPU 上测试。对于英特尔 CPU,请尝试 此 dockerfile(尚未完全测试)

如果以下命令执行时没有任何错误,则表示环境设置成功。

docker run -e ASCEND_VISIBLE_DEVICES=0 --rm --name lmdeploy -t lmdeploy-aarch64-ascend:latest lmdeploy check_env

有关在Ascend设备上运行Docker客户端的更多信息,请参阅指南

离线批量推理#

[!提示] Atlas 800T A2 已支持图模式。 用户可以设置 eager_mode=False 来启用图模式,或者设置 eager_mode=True 来禁用图模式。 (在启用图模式之前,请先执行 /usr/local/Ascend/nnal/atb/set_env.sh

LLM推理#

PytorchEngineConfig中设置device_type="ascend"

from lmdeploy import pipeline
from lmdeploy import PytorchEngineConfig
if __name__ == "__main__":
    pipe = pipeline("internlm/internlm2_5-7b-chat",
                    backend_config=PytorchEngineConfig(tp=1, device_type="ascend", eager_mode=True))
    question = ["Shanghai is", "Please introduce China", "How are you?"]
    response = pipe(question)
    print(response)

VLM推理#

PytorchEngineConfig中设置device_type="ascend"

from lmdeploy import pipeline, PytorchEngineConfig
from lmdeploy.vl import load_image
if __name__ == "__main__":
    pipe = pipeline('OpenGVLab/InternVL2-2B',
                    backend_config=PytorchEngineConfig(tp=1, device_type='ascend', eager_mode=True))
    image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
    response = pipe(('describe this image', image))
    print(response)

在线服务#

[!提示] Atlas 800T A2 已支持图模式。 在线服务中默认启用图模式。用户可以添加 --eager-mode 来禁用图模式。 (在启用图模式之前,请先执行 /usr/local/Ascend/nnal/atb/set_env.sh

服务一个LLM模型#

在服务命令中添加 --device ascend

lmdeploy serve api_server --backend pytorch --device ascend --eager-mode internlm/internlm2_5-7b-chat

服务一个VLM模型#

在服务命令中添加 --device ascend

lmdeploy serve api_server --backend pytorch --device ascend --eager-mode OpenGVLab/InternVL2-2B

使用命令行界面进行推理#

在服务命令中添加 --device ascend

lmdeploy chat internlm/internlm2_5-7b-chat --backend pytorch --device ascend --eager-mode

在启动容器后,运行以下命令以启动lmdeploy聊天:

docker exec -it lmdeploy_ascend_demo \
    bash -i -c "lmdeploy chat --backend pytorch --device ascend --eager-mode internlm/internlm2_5-7b-chat"

量化#

w4a16 AWQ#

运行以下命令以在Atlas 800T A2上量化权重。

lmdeploy lite auto_awq $HF_MODEL --work-dir $WORK_DIR --device npu

在使用此功能之前,请检查supported_models

int8 KV缓存量化#

Ascend 后端已支持在 eager 模式下进行离线 int8 KV-cache 量化。

请参考此文档以获取详细信息。