DeepSeek-R1#

简介#

DeepSeek-R1是由DeepSeek公司开发的高性能混合专家(MoE)大语言模型。它在复杂的逻辑推理、数学问题解决和代码生成方面表现出色。通过动态激活其专家网络,它在保持计算效率的同时提供了卓越的性能。DeepSeek-R1-W8A8是基于R1的完全量化版本。它对权重和激活均采用了8位整数(INT8)量化,这显著减少了模型的内存占用和计算需求,从而能够在资源受限的环境中实现更高效的部署和应用。本文以deepseek- R1-W8A8版本为例,介绍R1系列模型的部署。

支持的特性#

请参阅 支持的特性 获取模型的特性支持矩阵。

请参阅 特性指南 获取特性的配置方法。

环境准备#

模型权重#

  • DeepSeek-R1-W8A8 (量化版本): 需要 1 个 Atlas 800 A3 (64G × 16) 节点或 2 个 Atlas 800 A2 (64G × 8) 节点。下载模型权重

建议将模型权重下载到多个节点的共享目录。

验证多节点通信(可选)#

如果您想部署多节点环境,需要按照验证多节点通信环境来验证多节点通信。

安装#

您可以使用我们官方的docker镜像直接运行 DeepSeek-R1-W8A8

根据您的机器类型选择镜像,并在您的节点上启动docker镜像,请参考使用docker进行设置

# Update --device according to your device (Atlas A2: /dev/davinci[0-7] Atlas A3:/dev/davinci[0-15]).
# Update the vllm-ascend image according to your environment.
# Note you should download the weight to /root/.cache in advance.
# Update the vllm-ascend image
export IMAGE=m.daocloud.io/quay.io/ascend/vllm-ascend:v0.12.0rc1
export NAME=vllm-ascend

# Run the container using the defined variables
# Note: If you are running bridge network with docker, please expose available ports for multiple nodes communication in advance
docker run --rm \
--name $NAME \
--net=host \
--shm-size=1g \
--device /dev/davinci0 \
--device /dev/davinci1 \
--device /dev/davinci2 \
--device /dev/davinci3 \
--device /dev/davinci4 \
--device /dev/davinci5 \
--device /dev/davinci6 \
--device /dev/davinci7 \
--device /dev/davinci_manager \
--device /dev/devmm_svm \
--device /dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /etc/hccn.conf:/etc/hccn.conf \
-v /usr/bin/hccn_tool:/usr/bin/hccn_tool \
-v /usr/local/Ascend/driver/tools/hccn_tool:/usr/local/Ascend/driver/tools/hccn_tool \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-it $IMAGE bash

如果您想部署多节点环境,需要在每个节点上设置环境。

部署#

面向服务的部署#

  • DeepSeek-R1-W8A8: 需要 1 个 Atlas 800 A3 (64G × 16) 节点或 2 个 Atlas 800 A2 (64G × 8) 节点。

#!/bin/sh

# this obtained through ifconfig
# nic_name is the network interface name corresponding to local_ip of the current node
nic_name="xxxx"
local_ip="xxxx"

# AIV
export HCCL_OP_EXPANSION_MODE="AIV"
export HCCL_IF_IP=$local_ip
export GLOO_SOCKET_IFNAME=$nic_name
export TP_SOCKET_IFNAME=$nic_name
export HCCL_SOCKET_IFNAME=$nic_name
export VLLM_ASCEND_ENABLE_MLAPO=1
export VLLM_USE_MODELSCOPE=True

vllm serve vllm-ascend/DeepSeek-R1-W8A8 \
  --host 0.0.0.0 \
  --port 8000 \
  --data-parallel-size 4 \
  --tensor-parallel-size 4 \
  --quantization ascend \
  --seed 1024 \
  --served-model-name deepseek_r1 \
  --enable-expert-parallel \
  --max-num-seqs 16 \
  --max-model-len 16384 \
  --max-num-batched-tokens 4096 \
  --trust-remote-code \
  --gpu-memory-utilization 0.92 \
  --speculative-config '{"num_speculative_tokens":1,"method":"mtp"}' \
  --compilation-config '{"cudagraph_mode":"FULL_DECODE_ONLY"}'

注意: 参数解释如下

  • 设置环境变量 VLLM_ASCEND_ENABLE_MLAPO=1 可以启用一个融合算子,显著提升性能,但需要更多的NPU内存。因此,建议在NPU内存充足的情况下启用此选项。

  • 对于单节点部署,我们建议使用 dp4tp4 而不是 dp2tp8

  • --max-model-len 指定最大上下文长度,即单个请求的输入和输出token之和。对于输入长度为3.5K、输出长度为1.5K的性能测试,设置为 16384 足够,但对于精度测试,请至少设置为 35000

  • --no-enable-prefix-caching表示禁用了前缀缓存。要启用它,请删除此选项。

分别在两个节点上运行以下脚本。

节点 0

#!/bin/sh

# this obtained through ifconfig
# nic_name is the network interface name corresponding to local_ip of the current node
nic_name="xxxx"
local_ip="xxxx"

# AIV
export HCCL_OP_EXPANSION_MODE="AIV"
export HCCL_IF_IP=$local_ip
export GLOO_SOCKET_IFNAME=$nic_name
export TP_SOCKET_IFNAME=$nic_name
export HCCL_SOCKET_IFNAME=$nic_name
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export VLLM_ASCEND_ENABLE_MLAPO=1
export HCCL_INTRA_PCIE_ENABLE=1
export HCCL_INTRA_ROCE_ENABLE=0
export VLLM_USE_MODELSCOPE=True

vllm serve vllm-ascend/DeepSeek-R1-W8A8 \
  --host 0.0.0.0 \
  --port 8000 \
  --data-parallel-size 4 \
  --data-parallel-size-local 2 \
  --data-parallel-address $local_ip \
  --data-parallel-rpc-port 13389 \
  --tensor-parallel-size 4 \
  --quantization ascend \
  --seed 1024 \
  --served-model-name deepseek_r1 \
  --enable-expert-parallel \
  --max-num-seqs 16 \
  --max-model-len 16384 \
  --max-num-batched-tokens 4096 \
  --trust-remote-code \
  --gpu-memory-utilization 0.94 \
  --speculative-config '{"num_speculative_tokens":1,"method":"mtp"}' \
  --compilation-config '{"cudagraph_mode":"FULL_DECODE_ONLY"}' 

节点 1

#!/bin/sh

# this obtained through ifconfig
# nic_name is the network interface name corresponding to local_ip of the current node
nic_name="xxxx"
local_ip="xxxx"
node0_ip="xxxx" # same as the local_IP address in node 0

# AIV
export HCCL_OP_EXPANSION_MODE="AIV"
export HCCL_IF_IP=$local_ip
export GLOO_SOCKET_IFNAME=$nic_name
export TP_SOCKET_IFNAME=$nic_name
export HCCL_SOCKET_IFNAME=$nic_name
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export VLLM_ASCEND_ENABLE_MLAPO=1
export HCCL_INTRA_PCIE_ENABLE=1
export HCCL_INTRA_ROCE_ENABLE=0
export VLLM_USE_MODELSCOPE=True

vllm serve vllm-ascend/DeepSeek-R1-W8A8 \
  --host 0.0.0.0 \
  --port 8000 \
  --headless \
  --data-parallel-size 4 \
  --data-parallel-size-local 2 \
  --data-parallel-start-rank 2 \
  --data-parallel-address $node0_ip \
  --data-parallel-rpc-port 13389 \
  --tensor-parallel-size 4 \
  --quantization ascend \
  --seed 1024 \
  --served-model-name deepseek_r1 \
  --enable-expert-parallel \
  --max-num-seqs 16 \
  --max-model-len 16384 \
  --max-num-batched-tokens 4096 \
  --trust-remote-code \
  --gpu-memory-utilization 0.94 \
  --speculative-config '{"num_speculative_tokens":1,"method":"mtp"}' \
  --compilation-config '{"cudagraph_mode":"FULL_DECODE_ONLY"}'

Prefill-Decode 分离#

我们推荐使用Mooncake进行部署:Mooncake

该方案经过测试,表现出卓越的性能。

功能验证#

服务器启动后,您可以用输入提示查询模型

curl http://<node0_ip>:<port>/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "deepseek_r1",
        "prompt": "The future of AI is",
        "max_tokens": 50,
        "temperature": 0
    }'

精度评估#

这里有两种准确率评估方法。

使用 AISBench#

  1. 详情请参阅使用 AISBench

  2. 执行后,您将获得结果,以下是 DeepSeek-R1-W8A8vllm-ascend:0.11.0rc2 中的结果,仅供参考。

数据集

version

指标

模式

vllm-api-general-chat

aime2024dataset

-

accuracy

gen

80.00

gpqadataset

-

accuracy

gen

72.22

使用语言模型评估框架#

例如,以 gsm8k 数据集作为测试数据集,在在线模式下运行 DeepSeek-R1-W8A8 的准确率评估。

  1. 关于 lm_eval 的安装,请参考 使用lm_eval

  2. 运行 lm_eval 以执行准确性评估。

lm_eval \
  --model local-completions \
  --model_args model=path/DeepSeek-R1-W8A8,base_url=http://<node0_ip>:<port>/v1/completions,tokenized_requests=False,trust_remote_code=True \
  --tasks gsm8k \
  --output_path ./
  1. 执行后,您将获得结果。

性能#

使用 AISBench#

详情请参阅使用 AISBench 进行性能评估

使用 vLLM Benchmark#

DeepSeek-R1-W8A8 为例,运行性能评估。

更多详情请参阅 vllm benchmark

有三个 vllm bench 子命令

  • latency:对单批请求的延迟进行基准测试。

  • serve:对在线服务吞吐量进行基准测试。

  • throughput:对离线推理吞吐量进行基准测试。

serve 为例。运行代码如下。

export VLLM_USE_MODELSCOPE=true
vllm bench serve --model path/DeepSeek-R1-W8A8  --dataset-name random --random-input 200 --num-prompt 200 --request-rate 1 --save-result --result-dir ./

大约几分钟后,您就可以得到性能评估结果。