DeepSeek-R1#

简介#

DeepSeek-R1 是由深度求索公司开发的高性能混合专家(MoE)大语言模型。它在复杂逻辑推理、数学问题求解和代码生成方面表现出色。通过动态激活其专家网络,它在保持计算效率的同时提供了卓越的性能。基于 R1,DeepSeek-R1-W8A8 是该模型的完全量化版本。它对权重和激活均采用 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.18.0
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_BALANCE_SCHEDULING=1
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
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 \
  --async-scheduling \
  --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":3,"method":"mtp"}' \
  --compilation-config '{"cudagraph_capture_sizes":[4,16,32,48,64], "cudagraph_mode": "FULL_DECODE_ONLY"}'

注意: 参数解释如下:

  • 设置环境变量 VLLM_ASCEND_BALANCE_SCHEDULING=1 可启用均衡调度。这可能有助于在 v1 调度器中提高输出吞吐量并降低 TPOT。然而,在某些场景下 TTFT 可能会下降。此外,在 PD 分离的场景中不建议启用此功能。

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

  • --max-model-len 指定最大上下文长度——即单个请求的输入和输出令牌总数。对于输入长度为 3.5K 和输出长度为 1.5K 的性能测试,16384 的值已足够,但对于精度测试,请将其设置为至少 35000

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

  • 如果您使用 w4a8 权重,将有更多内存分配给 kvcache,您可以尝试增加系统吞吐量以实现更大的吞吐量。

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

节点 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"

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 OMP_PROC_BIND=false
export OMP_NUM_THREADS=1
export HCCL_BUFFSIZE=200
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export VLLM_ASCEND_BALANCE_SCHEDULING=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 \
  --async-scheduling \
  --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":3,"method":"mtp"}' \
  --compilation-config '{"cudagraph_capture_sizes":[4,16,32,48,64], "cudagraph_mode": "FULL_DECODE_ONLY"}'

节点 1

#!/bin/sh

# this is 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

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 OMP_PROC_BIND=false
export OMP_NUM_THREADS=1
export HCCL_BUFFSIZE=200
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export VLLM_ASCEND_BALANCE_SCHEDULING=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 \
  --async-scheduling \
  --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":3,"method":"mtp"}' \
  --compilation-config '{"cudagraph_capture_sizes":[4,16,32,48,64], "cudagraph_mode": "FULL_DECODE_ONLY"}'

Prefill-Decode 解耦#

我们推荐使用 DeepSeek-V3.1 进行部署:DeepSeek-V3.1

此解决方案已经过测试,并展现出优异的性能。

功能验证#

一旦您的服务器启动,您就可以使用输入提示词查询模型:

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

精度评估#

这里有两种精度评估方法。

使用 AISBench#

  1. 详情请参考使用 AISBench

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

    数据集

    版本

    指标

    模式

    vllm-api-general-chat

    aime2024dataset

    -

    准确率

    gen

    80.00

    gpqadataset

    -

    准确率

    gen

    72.22

使用 Language Model Evaluation Harness#

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 ./
    
  3. 执行后,您可以获得结果。

性能#

使用 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-prompts 200 --request-rate 1 --save-result --result-dir ./

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