# 使用vllm单机部署DeepSeek蒸馏模型（Linux）
#### 应用场景
蒸馏是一种通过大型基础模型生成训练语料数据来训练较小模型的技术，适用于需要更小、更高效的场景，而不会显著的损失精度。华为云为用户提供使用vllm来部署DeepSeek蒸馏系列模型的详细步骤，帮助客户快速部署。
#### 方案架构
图1基于vllm部署DeepSeek蒸馏模型示意图（Linux）   
![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002641442714.png "点击放大")
#### 方案优势
使用vllm在conda虚环境上从0开始部署DeepSeek蒸馏模型，深刻理解模型运行依赖，使用少量的资源，快速高效接入用户服务用于生产，实现更精细的性能和成本控制。
#### 资源和成本规划
表1资源和成本规划 
| 资源       | 资源说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | 成本说明                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|
| 虚拟私有云VPC | VPC网段：192.168.0.0/16                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | 免费                                                                                                                                                                                                                                                                                                                                                            |
| 虚拟私有云子网  | - 可用区：可用区1  - 子网网段：192.168.0.0/24                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | 免费                                                                                                                                                                                                                                                                                                                                                            |
| 安全组      | 入方向规则： - 优先级：1  - 策略：允许  - 类型：IPv4  - 协议端口：TCP: 80  - 源地址：0.0.0.0/0                                                                                                                                                                                                                                                                                                              | 免费                                                                                                                                                                                                                                                                                                                                                            |
| 弹性云服务器   | - 计费模式：包年/包月  - 可用区：可用区1  - 规格：见[表2]  - 镜像：使用驱动为535，CUDA为12.2的镜像。  - 系统盘：200G  - 弹性公网IP：需要购买  - 线路：全动态BGP  - 公网带宽：按流量计费  - 带宽大小：100 Mbit/s   | ECS涉及以下几项费用： - 云服务器  - 云硬盘  - 弹性公网IP   具体的计费方式及标准请参考[计费模式概述](https://support.huaweicloud.com/price-ecs/ecs_billing_2001.html)。 |
   
 表2DeepSeek-R1/V3运行可选的GPU弹性云服务规格 
| 编号 | 模型名称                                                                                | 最小所需规格         | GPU卡         |
|:---|:---|:---|:---|
| 0  | DeepSeek-R1-Distill-Qwen-7B DeepSeek-R1-Distill-Llama-8B | p2s.2xlarge.8  | V100(32G)\*1 |
| 0  | DeepSeek-R1-Distill-Qwen-7B DeepSeek-R1-Distill-Llama-8B | p2v.4xlarge.8  | V100(16G)\*2 |
| 0  | DeepSeek-R1-Distill-Qwen-7B DeepSeek-R1-Distill-Llama-8B | pi2.4xlarge.4  | T4(16G)\*2   |
| 0  | DeepSeek-R1-Distill-Qwen-7B DeepSeek-R1-Distill-Llama-8B | g6.18xlarge.7  | T4(16G)\*2   |
| 1  | DeepSeek-R1-Distill-Qwen-14B                                                        | p2s.4xlarge.8  | V100(32G)\*2 |
| 1  | DeepSeek-R1-Distill-Qwen-14B                                                        | p2v.8xlarge.8  | V100(16G)\*4 |
| 1  | DeepSeek-R1-Distill-Qwen-14B                                                        | pi2.8xlarge.4  | T4(16G)\*4   |
| 2  | DeepSeek-R1-Distill-Qwen-32B                                                        | p2s.8xlarge.8  | V100(32G)\*4 |
| 2  | DeepSeek-R1-Distill-Qwen-32B                                                        | p2v.16xlarge.8 | V100(16G)\*8 |
| 3  | DeepSeek-R1-Distill-Llama-70B                                                       | p2s.16xlarge.8 | V100(32G)\*8 |
   
![](https://support.huaweicloud.com/bestpractice-ecs/public_sys-resources/note_3.0-zh-cn.png)
请联系华为云技术支持选择最适合您部署的GPU卡资源服务器。
#### 基于vllm手动部署DeepSeek蒸馏模型步骤
Linux实例基于vllm手动部署DeepSeek蒸馏模型操作步骤如下：
1. [创建GPU弹性云服务器。]
2. [驱动和CUDA的前置条件检查。]
3. [创建conda虚环境。]
4. [安装vllm等依赖包。]
5. [下载大模型文件。]
6. [启动vllm.server运行大模型。]
7. [调用模型接口测试。]
 
#### 实施步骤
1. 创建GPU弹性云服务器。
   
   1. 镜像选择：建议选择华为云提供的GPU实例的未安装驱动的Huawei Cloud EulerOS 2.0/Ubuntu 22.04公共镜像，勾选自动安装GPU驱动，选择驱动版本535.161.08，CUDA版本12.2.0。
      图2GPU实例镜像选择和自动安装GPU驱动选择   
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002302538996.png "点击放大") 
   
   2. 需要创建EIP，方便依赖环境下载，方便调用模型接口进行推理。
   
   
   
   
2. 驱动和CUDA的前置条件检查。
   
   执行以下命令，查看显卡基本信息。
   ```
   nvidia-smi
   ```
   如果使用的不是推荐的镜像和驱动版本，请参考[手动安装GPU加速型ECS的Tesla驱动](https://support.huaweicloud.com/usermanual-ecs/ecs_03_0174.html)方法更新驱动到535版本，CUDA更新到12.2。
   
   
3. 创建conda虚环境。
   
   1. 执行以下命令，下载[miniconda安装包](https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh)。
      ```
      wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
      ```
      
   
   2. 执行以下命令，安装miniconda。
      ```
      bash Miniconda3-latest-Linux-x86_64.sh
      ```
      
   
   3. 执行以下命令，将conda环境变量添加到启动文件中。
      ```
      echo 'export PATH="$HOME/miniconda3/bin:$PATH"' >> ~/.bashrc 
      source ~/.bashrc
      ```
      
   
   4. 执行以下命令，创建基于python3.10的虚环境。
      ```
      conda create -n vllm-ds python=3.10
      conda activate vllm-ds
      conda install numpy
      ```
      
   
   
   
   
4. 安装vllm等依赖包。
   
   1. 执行以下命令，更新升级pip。
      ```
      python -m pip install --upgrade pip -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
      ```
      
   
   2. 执行以下命令，安装vllm。
      ```
      pip install vllm -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
      ```
      使用**vllm --version**可以查看当前安装的vllm版本。
      
   
   3. 执行以下命令，安装modelscope。
      ```
      pip install modelscope -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
      ```
      ![](https://support.huaweicloud.com/bestpractice-ecs/public_sys-resources/note_3.0-zh-cn.png)
      modelscope是国内模型开源社区，国内下载会更快，如果在国外，请使用hugging Face进行下载。
      
   
   
   
   
5. 下载大模型文件。
   
   1. 执行以下命令，创建下载模型脚本。
      ```
      vim download_models.py
      ```
      插入以下内容：
      ```
      from modelscope import snapshot_download
      model_dir = snapshot_download('deepseek-ai/DeepSeek-R1-Distill-Qwen-7B', cache_dir='/root', revision='master')
      ```
      模型名称以*DeepSeek-R1-Distill-Qwen-7B* 为例，可参考[表2]更换需要的模型，模型存储的本地路径以*/root*为例，可以更换自己的目录。
      
   
   2. 执行以下命令，下载模型。
      ```
      python3 download_models.py
      ```
      等待模型下载完成。
      
   
   
   
   
6. 启动vllm.server运行大模型。
   
   执行以下命令，运行大模型。
   ```
   python -m vllm.entrypoints.openai.api_server --model /root/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B --served-model-name DeepSeek-R1-Distill-Qwen-7B --max-model-len=2048 &
   ```
   ![](https://support.huaweicloud.com/bestpractice-ecs/public_sys-resources/caution_3.0-zh-cn.png)
   1. 如果GPU实例是多卡机器，添加-tp *${卡数}*，比如2卡就是 -tp 2。
   
   2. V100/T4显卡不能使用BF16精度，只能使用float16，需要加上参数**--dtype float16** 。
      ```
      python -m vllm.entrypoints.openai.api_server --model /root/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B --served-model-name DeepSeek-R1-Distill-Qwen-7B --max-model-len=2048 --dtype float16 -tp 2 &
      ```
      
   
   3. 模型加载完成后，报显存不足可以添加**--enforce-eager** 使用eager模式，停用CUDA graph减少显存使用。
      ```
      python -m vllm.entrypoints.openai.api_server --model /root/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B --served-model-name DeepSeek-R1-Distill-Qwen-7B --max-model-len=2048 –dtype float16 –enforce-eager &
      ```
      
   
   4. 如果显存不够，请更换大规格GPU实例，请参考[表2]。
   
   5. 模型启动后，默认监听端口是8000。
    
   
   
7. 调用模型接口测试。
   
   1. 调用以下API查看当前运行的模型。
      ```
      curl http://localhost:8000/v1/models
      ```
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002336538097.png "点击放大")
      
   
   2. 调用以下API进行聊天对话。
      ```
      curl http://localhost:8000/v1/chat/completions \
      -H "Content-Type: application/json" \
      -d '{
        "model": "DeepSeek-R1-Distill-Qwen-7B",
        "messages": [{"role": "user", "content": "hello\n"}]
      }'
      ```
      ![](https://support.huaweicloud.com/bestpractice-ecs/zh-cn_image_0000002302698716.png "点击放大")
      
   
   
   至此模型就部署完毕并验证正常，您可以在本地postman或者自己的服务里面使用EIP调用对应的API接口来对话。
   
   
 
