使用DeepGPU-LLM镜像构建模型的推理环境

更新时间:
复制为 MD 格式

GPU实例上配置DeepGPU-LLM容器镜像后,可以帮助您快速构建大语言模型(例如Llama模型、ChatGLM模型、百川Baichuan模型或千问Qwen模型)的推理环境,主要应用在智能对话系统、文本分析、编程辅助等自然语言处理业务场景,您无需深入了解底层的硬件优化细节,镜像拉取完成后,无需额外配置即可开箱即用。本文为您介绍如何在GPU实例上使用DeepGPU-LLM容器镜像构建大语言模型的推理服务。

说明

DeepGPU-LLM是阿里云研发的基于GPU云服务器的大语言模型(Large Language Model,LLM)推理引擎,可以帮助您实现大语言模型在GPU上的高性能推理优化功能。更多信息,请参见什么是推理引擎DeepGPU-LLM

准备工作

获取DeepGPU-LLM容器镜像详细信息,以便您在GPU实例上部署该容器镜像时使用。例如,创建GPU实例时需要提前了解容器镜像适用的GPU实例类型,拉取容器镜像时需要提前获取镜像地址等信息。

  1. 登录容器镜像服务控制台

  2. 在左侧导航栏,单击制品中心

  3. 仓库名称搜索框,搜索deepgpu选择目标镜像egs/deepgpu-llm

    DeepGPU-LLM容器镜像大概每3个月内更新一次。镜像详情如下所示:

    镜像名称

    组件信息

    镜像地址

    适用的GPU实例

    DeepGPU-LLM

    • DeepGPU-LLM:24.3

    • Python:3.10

    • PyTorch:2.1.0

    • CUDA:12.1.1

    • cuDNN:8.9.0.131

    • 基础镜像:Ubuntu 22.04

    egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/deepgpu-llm:24.3-pytorch2.1-cuda12.1-cudnn8-ubuntu22.04

    DeepGPU-LLM镜像仅支持以下GPU实例选择,更多信息,请参见GPU计算型(gn/ebm/scc系列)

    • gn6e、ebmgn6e

    • gn7i、ebmgn7i、ebmgn7ix

    • gn7e、ebmgn7e、ebmgn7ex

操作步骤

本操作以Ubuntu 20.04操作系统的gn7i实例规格为例。

  1. 创建GPU实例并安装Tesla驱动。

    具体操作,请参见创建GPU实例

    GPU实例上使用DeepGPU-LLM镜像,需要提前在该实例上安装Tesla驱动且驱动版本为535或更高,建议您通过ECS控制台购买GPU实例,并选中安装GPU驱动

    说明

    GPU实例创建完成后,会同时自动安装Tesla驱动、CUDA、cuDNN库等,相比手动安装方式更快捷。

    镜像选择 公共镜像 > Ubuntu 20.04 64,勾选后将自动安装 CUDA 12.1.1、Driver 535.154.05、CUDNN 8.9.7.29

  2. 远程连接GPU实例。

    具体操作,请参见使用Workbench登录Linux实例

  3. 执行以下命令,安装Docker环境。

    sudo apt-get update
    sudo apt-get -y install ca-certificates curl
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    sudo apt-get install -y docker-ce docker-ce-cli containerd.io
  4. 执行以下命令,检查Docker是否安装成功。

    docker -v

    如下回显信息所示,表示Docker已安装成功。

    docker -v
    Docker version 27.1.1, build 6312585
  5. 执行以下命令,安装nvidia-container-toolkit。

    若安装失败,请参考解决方案
    curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
      && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
        sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
        sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
    sudo apt-get update
    sudo apt-get install -y nvidia-container-toolkit
  6. 设置Docker开机自启动并重启Docker服务。

    sudo systemctl enable docker 
    sudo systemctl restart docker
  7. 执行以下命令,查看Docker是否已启动。

    sudo systemctl status docker

    如以下回显所示,表示Docker已启动。

    sudo systemctl status docker
    ● docker.service - Docker Application Container Engine
         Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
         Active: active (running) since Tue 2024-07-09 16:37:54 CST; 2min 9s ago
    TriggeredBy: ● docker.socket
           Docs: https://docs.docker.com
       Main PID: 6987 (dockerd)
          Tasks: 20
         Memory: 31.9M
         CGroup: /system.slice/docker.service
                 └─6987 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  8. 执行以下命令,拉取DeepGPU-LLM镜像。

    sudo docker pull egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/deepgpu-llm:24.3-pytorch2.1-cuda12.1-cudnn8-ubuntu22.04
  9. 执行以下命令,运行DeepGPU-LLM容器。

    sudo docker run -d -t --net=host --gpus all \
     --privileged \
     --ipc=host \
     --name deepgpu \
     -v /root:/root \
     egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/deepgpu-llm:24.3-pytorch2.1-cuda12.1-cudnn8-ubuntu22.04

测试验证

本测试以千问-7B-Chat模型为例,您可以通过DeepGPU-LLM安装包自带的可运行代码实例体验模型运行效果,展示使用DeepGPU-LLM的推理性能。更多信息,请参见安装并使用DeepGPU-LLM

  1. 执行以下命令,进入DeepGPU-LLM容器。

    sudo docker exec -it deepgpu bash
  2. 执行以下命令,下载modelscope格式的千问-7B-Chat模型。

    git-lfs clone https://modelscope.cn/qwen/Qwen-7B-Chat.git
  3. 执行以下命令,将modelscope格式的千问模型转换为DeepGPU-LLM支持的格式。

    #qwen-7b weight convert
    huggingface_qwen_convert \
        -in_file /workspace/Qwen-7B-Chat \
        -saved_dir /workspace/Qwen-7B-Chat-converted \
        -infer_gpu_num 1 \
        -weight_data_type fp16 \
        -model_name qwen-7b
    说明
    • in_file参数后的/workspace/Qwen-7B-Chat请替换为您实际待转换的原始模型所在路径。

    • saved_dir参数后的/workspace/Qwen-7B-Chat-converted请替换为您实际转换后的模型所存放的路径。

  4. 执行以下命令,运行DeepGPU-LLM安装包自带的推理代码,测试模型的推理效果。

    qwen_cli -i /workspace/Qwen-7B-Chat-converted/1-gpu/ -t /workspace/Qwen-7B-Chat/

    运行完成后,您可以输入内容和ChatGLM模型进行问答对话。例如:

    root@xxx:~# qwen_cli -i /workspace/Qwen-7B-Chat-converted/1-gpu/ -t /Qwen-7B-Chat/
    ============== Argument ==============
    model_dir: /workspace/Qwen-7B-Chat-converted/1-gpu/
    tp_size: 1
    precision: fp16
    tokenizer_dir: /Qwen-7B-Chat/
    =======================================
    running in fp16 mode, cb is False
    欢迎使用 DeepGPU 加速版 Qwen 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序
    用户: 西安特产介绍
    西安有许多著名的特产,包括著名的肉夹馍、烤鸭、豆汁、羊肉泡馍等。此外,西安还有许多传统的手工艺品,如剪纸、泥塑、丝织品等,都是极具特色的地方文化产品
    Cost time: 1.79 s
    Throughput: 29.66 tokens/s
    用户: introduce about shaanxi
    Shaanxi Province, also known as Xi'an, is a province located in the central part of China. It borders Gansu to the west, Shaanxi and Inner Mongolia h an area of 166,700 square kilometers, it is the third largest provincial administrative region in China.
    The capital of Shaanxi is Xi'an, which is famous for its ancient city walls, Terracotta Warriors, and various historical sites. Xi'an is also one of The city has been continuously inhabited since the Western Zhou Dynasty (1046-771 BC) and has played an important role in Chinese history and cultu In addition to Xi'an, other major cities in Shaanxi include Yangzhou, Lanzhou, Weinan, and Ankang. These cities are home to a diverse range of attra Overall, Shaanxi Province is a rich and culturally significant region that offers visitors a unique glimpse into China's past and present.