
【干货】DeepSeek-V3和DeepSeek-R1官方中文部署文档(附详细资料和原文链接)
本文对DeepSeek官方英文部署文档进行手动中文翻译,并附上详细资料和原文链接
文章对DeepSeek官方英文部署文档进行了中文手动翻译
官方Deep Seek部署教程:https://github.com/deepseek-ai/DeepSeek-V3
文章目录
DeepSeek-V3本地部署
DeepSeek-V3 可使用以下硬件和开源社区软件在本地部署:
- DeepSeek-Infer 演示: 我们为 FP8 和 BF16 推理提供了一个简单、轻量级的演示。
- SGLang: 在 BF16 和 FP8 推理模式下全面支持 DeepSeek-V3 模型,即将推出多单词预测。
- LMDeploy: 为本地和云部署提供高效的 FP8 和 BF16 推断。
- TensorRT-LLM:目前支持 BF16 推理和 INT4/8 量化,即将支持 FP8。
- vLLM:支持 DeepSeek-V3 模型的 FP8 和 BF16 模式,用于张量并行和流水线并行。
- AMD GPU: 通过 SGLang 以 BF16 和 FP8 模式在 AMD GPU 上运行 DeepSeek-V3 模型。
- 华为 Ascend NPU: 支持在华为 Ascend 设备上运行 DeepSeek-V3。
由于我们的框架原生采用 FP8 训练,因此我们只提供 FP8 权重。如果您需要 BF16 权重进行实验,可以使用提供的转换脚本进行转换。
下面是将 FP8 权重转换为 BF16 的示例:
cd inference
python fp8_cast_bf16.py --input-fp8-hf-path /path/to/fp8_weights --output-bf16-hf-path /path/to/bf16_weights
注意:
Hugging Face’s Transformers 尚未获得直接支持。
DeepSeek 推理(仅演示)
系统要求:
仅限使用 Python 3.10 的 Linux。不支持 Mac 和 Windows。
依赖性:
torch==2.4.1
triton==3.0.0
transformers==4.46.3
safetensors==0.4.5
模型重量和演示代码准备:
首先,克隆我们的 DeepSeek-V3 GitHub 仓库:
git clone https://github.com/deepseek-ai/DeepSeek-V3.git
导航至 inference
文件夹,安装 requirements.txt
中列出的依赖项。最简单的方法是使用 conda
或 uv
等软件包管理器创建新的虚拟环境并安装依赖项。
cd DeepSeek-V3/inference
pip install -r requirements.txt
从 Hugging Face 下载模型权重,并将其放入 /path/to/DeepSeek-V3
文件夹。
模型权重转换
将 Hugging Face 模型权重转换为特定格式:
python convert.py --hf-ckpt-path /path/to/DeepSeek-V3 --save-path /path/to/DeepSeek-V3-Demo --n-experts 256 --model-parallel 16
运行
然后就可以与 DeepSeek-V3 聊天了:
torchrun --nnodes 2 --nproc-per-node 8 --node-rank $RANK --master-addr $ADDR generate.py --ckpt-path /path/to/DeepSeek-V3-Demo --config configs/config_671B.json --interactive --temperature 0.7 --max-new-tokens 200
或对给定文件进行批量推理:
torchrun --nnodes 2 --nproc-per-node 8 --node-rank $RANK --master-addr $ADDR generate.py --ckpt-path /path/to/DeepSeek-V3-Demo --config configs/config_671B.json --input-file $FILE
使用SGLang进行部署(推荐)
SGLang 目前支持 MLA 优化、DP Attention、FP8 (W8A8)、FP8 KV Cache 和 Torch Compile,在开源框架中提供最先进的延迟和吞吐性能。
值得注意的是,SGLang v0.4.1 完全支持在 NVIDIA 和 AMD GPU 上运行 DeepSeek-V3,使其成为一个高度通用和强大的解决方案。
SGLang 还支持多节点张量并行,使您可以在多台联网机器上运行该模型。
多令牌预测 (MTP) 正在开发中,可在优化计划中跟踪进展情况。
以下是 SGLang 团队提供的启动说明: https://github.com/sgl-project/sglang/tree/main/benchmark/deepseek_v3。
SGLang 支持 DeepSeek V3
SGLang和DeepSeek团队通力合作,使DeepSeek V3 FP8从第一天起就能在英伟达和AMD GPU上运行。SGLang还支持MLA优化和DP注意力,使SGLang成为运行DeepSeek模型的最佳开源LLM引擎之一。SGLang 是 DeepSeek 官方团队推荐的推理引擎。
特别感谢Meituan's Search & Recommend Platform
团队,和Baseten
模型性能团队实施该模型,以及 DataCrunch
提供 GPU 资源。
有关 DeepSeek 系列模型在 SGLang 中的优化,请参阅 DeepSeek 模型在 SGLang 中的优化。
硬件推荐
- 8 x NVIDIA H200 GPUs
如果没有足够大内存的 GPU,请尝试多节点张量并行。下面有一个使用 2 个 H20 节点的示例。
在 AMD MI300X 上运行时,请将此作为参考。在单个 NDv5 MI300X 虚拟机上运行 DeepSeek-R1
安装服务
如果在启动服务器时遇到错误,请确保权重已下载完毕。建议提前下载或多次重启,直到下载完所有权重。
使用docker安装(推荐)
# Pull latest image
# https://hub.docker.com/r/lmsysorg/sglang/tags
docker pull lmsysorg/sglang:latest
# Launch
docker run --gpus all --shm-size 32g -p 30000:30000 -v ~/.cache/huggingface:/root/.cache/huggingface --ipc=host --network=host --privileged lmsysorg/sglang:latest \
python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V3 --tp 8 --trust-remote-code --port 30000
如果使用 RDMA,请注意以下几点:
- RDMA 要求使用–network host 和–privileged。如果不需要 RDMA,可以删除它们。
- 如果使用 RoCE,可能需要设置 NCCL_IB_GID_INDEX,例如:export NCCL_IB_GID_INDEX=3。
根据需要添加性能优化选项。
RDMA是一种允许服务器间直接内存访问的技术,而不需要CPU的过多干预。这意味着一个系统可以直接读写另一个系统的内存,大大降低了延迟并提高了带宽。这种技术对于需要快速数据交换的应用场景非常有用,比如高性能计算、大规模数据库和存储网络等。
使用pip安装
# Installation
pip install "sglang[all]>=0.4.3" --find-links https://flashinfer.ai/whl/cu124/torch2.5/flashinfer-python
# Launch
python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V3 --tp 8 --trust-remote-code
根据需要添加性能优化选项。
性能优化选项
MLA optimizations 默认已启用。以下是一些可选的优化功能,可根据需要启用。
- Data Parallelism Attention: 对于高 QPS 场景,添加 --enable-dp-attention 参数可提高吞吐量。
- Torch.compile Optimization : 添加–enable-torch-compile 参数以启用它。这将在服务器启动时花费一些时间。可以使用 --torch-compile-max-bs 控制 torch.compile 优化的最大批量大小。建议将其设置在 1 到 8 之间(例如:—torch-compile-max-bs 8)
示例:以 OpenAI API 规范访问DeepSeek接口
import openai
client = openai.Client(
base_url="http://127.0.0.1:30000/v1", api_key="EMPTY")
# Chat completion
response = client.chat.completions.create(
model="default",
messages=[
{"role": "system", "content": "You are a helpful AI assistant"},
{"role": "user", "content": "List 3 countries and their capitals."},
],
temperature=0,
max_tokens=64,
)
print(response)
示例:用2台8张H20显卡节点部署
例如,有两个 H20 节点,每个节点有 8 个 GPU。第一个节点的 IP 地址是 10.0.0.1,第二个节点的 IP 地址是 10.0.0.2。两条命令都请使用第一个节点的 IP。
如果命令失败,请尝试设置 GLOO_SOCKET_IFNAME 参数。更多信息,请参阅常用环境变量。
如果多节点支持 NVIDIA InfiniBand 并在启动过程中遇到挂起问题,可考虑添加 export NCCL_IB_GID_INDEX=3 参数。更多信息,请参阅此处。
# node 1
python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 10.0.0.1:5000 --nnodes 2 --node-rank 0 --trust-remote-code
# node 2
python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 10.0.0.1:5000 --nnodes 2 --node-rank 1 --trust-remote-code
如果有两个 H100 节点,使用方法与前面提到的 H20 类似。
请注意,此处的启动命令不会启用数据并行性注意事项或 torch.compile 优化。要获得最佳性能,请参阅性能优化选项中的命令选项。
示例:用2台8张H200显卡Docker部署
有两个 H200 节点,每个节点有 8 个 GPU。第一个节点的 IP 地址是 192.168.114.10,第二个节点的 IP 地址是 192.168.114.11。使用 --host 0.0.0.0 和 --port 40000 配置端点,将其暴露给另一个 Docker 容器,并使用 --dist-init-addr 192.168.114.10:20000 设置通信。拥有8台设备的单个H200可以运行DeepSeek V3,双H200设置只是为了演示多节点使用。
# node 1
docker run --gpus all \
--shm-size 32g \
--network=host \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--name sglang_multinode1 \
-it \
--rm \
--env "HF_TOKEN=$HF_TOKEN" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 192.168.114.10:20000 --nnodes 2 --node-rank 0 --trust-remote-code --host 0.0.0.0 --port 40000
另外一台机
# node 2
docker run --gpus all \
--shm-size 32g \
--network=host \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--name sglang_multinode2 \
-it \
--rm \
--env "HF_TOKEN=$HF_TOKEN" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 192.168.114.10:20000 --nnodes 2 --node-rank 1 --trust-remote-code --host 0.0.0.0 --port 40000
为确保功能性,我们包含了一个来自客户端 Docker 容器的测试。
docker run --gpus all \
--shm-size 32g \
--network=host \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--name sglang_multinode_client \
-it \
--rm \
--env "HF_TOKEN=$HF_TOKEN" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.bench_serving --backend sglang --dataset-name random --random-input 1 --random-output 512 --random-range-ratio 1 --num-prompts 1 --host 0.0.0.0 --port 40000 --output-file "deepseekv3_multinode.jsonl"
示例:用4台8张A100显卡节点部署
要在 A100 GPU 上运行 DeepSeek-V3,我们需要先用这里提到的脚本将 FP8 模型检查点转换为 BF16。
由于 BF16 模型超过 1.3 TB,我们需要准备四个 A100 节点,每个节点配备 8 个 80GB GPU。假设第一个节点的 IP 为 10.0.0.1,转换后的模型路径为 /path/to/DeepSeek-V3-BF16,我们可以使用以下命令启动服务器。
# node 1
python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 0 --trust-remote-code --host 0.0.0.0 --port 30000
# node 2
python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 1 --trust-remote-code
# node 3
python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 2 --trust-remote-code
# node 4
python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 3 --trust-remote-code
请注意,此处的启动命令不会启用数据并行性注意事项或 torch.compile 优化。要获得最佳性能,请参阅性能优化选项中的命令选项。
然后,我们可以使用以下示例命令访问第一个节点的外露端口,从而对准确性和延迟进行基准测试。
# bench accuracy
python3 benchmark/gsm8k/bench_sglang.py --num-questions 1319 --host http://10.0.0.1 --port 30000
# bench latency
python3 -m sglang.bench_one_batch_server --model None --base-url http://10.0.0.1:30000 --batch-size 1 --input-len 128 --output-len 128
示例:用云主机或者Kubernetes进行部署
SkyPilot 可帮助在任何云或现有 Kubernetes 集群中找到最便宜的可用 GPU,并只需一条命令即可启动分布式服务。点击此处查看详情。
在多个节点上提供服务:
git clone https://github.com/skypilot-org/skypilot.git
# Serve on 2 H100/H200x8 nodes
sky launch -c r1 llm/deepseek-r1/deepseek-r1-671B.yaml --retry-until-up
# Serve on 4 A100x8 nodes
sky launch -c r1 llm/deepseek-r1/deepseek-r1-671B-A100.yaml --retry-until-up
常见故障
如果在使用 fp16/bf16 检查点时遇到以下错误:
ValueError: Weight output_partition_size = 576 is not divisible by weight quantization block_n = 128.
编辑 config.json,删除 quantization_config 块。例如
"quantization_config": {
"activation_scheme": "dynamic",
"fmt": "e4m3",
"quant_method": "fp8",
"weight_block_size": [128, 128]
},
删除该代码块通常可以解决该错误。有关详细信息,请参阅 sgl-project/sglang#3491 中的讨论。
使用 LMDeploy 进行部署
LMDeploy是专为大型语言模型定制的灵活、高性能推理和服务框架,现在支持DeepSeek-V3。它提供离线管道处理和在线部署功能,可与基于PyTorch的工作流程无缝集成。
有关使用 LMDeploy 运行 DeepSeek-V3 的全面分步说明,请参阅此处: InternLM/lmdeploy#2960
使用 TRT-LLM 进行部署
TensorRT-LLM 现在支持 DeepSeek-V3 模型,提供 BF16 和 INT4/INT8 加权等精度选项。对 FP8 的支持目前正在进行中,很快就会发布。你可以通过以下链接访问专为支持 DeepSeek-V3 而定制的 TRTLLM 分支,直接体验新功能:https://github.com/NVIDIA/TensorRT-LLM/tree/deepseek/examples/deepseek_v3。
使用 vLLM 进行部署
vLLM v0.6.6 支持在英伟达和 AMD GPU 上以 FP8 和 BF16 模式进行 DeepSeek-V3 推理。除标准技术外,vLLM 还提供流水线并行功能,允许您在通过网络连接的多台机器上运行该模型。有关详细指导,请参阅 vLLM 说明。也请随时关注增强计划。
相关官方中文教程:DeepSeek-R1-Distill-Qwen-7B vLLM 部署调用
使用 AMD GPUs 显卡
我们与 AMD 团队合作,使用 SGLang 实现了对 AMD GPU 的 Day-One 支持,并完全兼容 FP8 和 BF16 精度。有关详细指导,请参阅 SGLang 说明。
使用华为 Ascend NPU 显卡
华为 Ascend 社区的 MindIE 框架已成功适配 BF16 版本的 DeepSeek-V3。有关 Ascend NPU 的逐步指导,请按照此处的说明进行。
模型权重
权重下载
- DeepSeek-V3:Modelers/Hugging Face
- DeepSeek-V3-Base:Hugging Face
权重转换(Convert FP8 weights to BF16)
- GPU侧权重转换
git clone https://github.com/deepseek-ai/DeepSeek-V3.git
cd DeepSeek-V3/inferece/
python fp8_cast_bf16.py --input-fp8-hf-path /path/to/DeepSeek-V3 --output-bf16-hf-path /path/to/DeepSeek-V3-bf16
- NPU侧权重转换
目前npu转换脚本不会自动复制tokenizer等文件
git clone https://gitee.com/ascend/ModelZoo-PyTorch.git
cd ModelZoo-PyTorch\MindIE\LLM\DeepSeek\DeepSeek-V2\NPU_inference
python fp8_cast_bf16.py --input-fp8-hf-path /path/to/DeepSeek-V3 --output-bf16-hf-path /path/to/DeepSeek-V3-bf16
注意:
/path/to/DeepSeek-V3
表示DeepSeek-V3原始权重路径,/path/to/DeepSeek-V3-bf16
表示权重转换后的新权重路径- 由于模型权重较大,请确保您的磁盘有足够的空间放下所有权重,例如DeepSeek-V3在转换前权重约为640G左右,在转换后权重约为1.3T左右
- 推理作业时,也请确保您的设备有足够的空间加载模型权重,并为推理计算预留空间
量化权重生成
详情请参考 DeepSeek模型量化方法介绍
目前支持:
- 生成模型w8a16量化权重,使用histogram量化方式,在CPU上进行运算
- 生成模型w8a8混合量化权重,使用histogram量化方式 (MLA:w8a8量化,MOE:w8a8 dynamic pertoken量化)
注意:DeepSeek-R1模型权重较大,量化权重生成时间较久,请耐心等待;具体时间与校准数据集大小成正比,10条数据大概需花费3小时。
加载镜像
DeepSeek-V3的镜像包:mindie_2.0.T3-800I-A2-py311-openeuler24.03-lts-aarch64.tar.gz
镜像加载后的名称:mindie:2.0.T3-800I-A2-py311-openeuler24.03-lts-aarch64
注意:量化需要使用mindie:2.0.T3版本
完成之后,请使用docker images命令确认查找具体镜像名称与标签。
docker load -i mindie:2.0.T3-800I-A2-py311-openeuler24.03-lts-aarch64(下载的镜像名称与标签)
各组件版本配套如下:
硬件要求
部署DeepSeek-V3模型用BF16权重进行推理至少需要4台Atlas 800I A2(864G)服务器,用W8A8量化权重进行推理则至少需要2台Atlas 800I A2 (864G)
容器启动
- 准备模型
目前提供的MindIE镜像预置了DeepSeek-V3模型推理脚本,无需再下载模型代码,也无需参考目录结构。(可跳过至获取模型权重)
下载对应模型代码,可以使用:
git clone https://gitee.com/ascend/ModelZoo-PyTorch.git
目录结构应为如下:
├── DeepSeek-V3
│ ├── README.md
- 获取模型权重
- 本地已有模型权重。从您信任的来源自行获取权重后,放置在从上述下载的模型代码的主目录下,放置后的目录结构应为如下:
- 本地没有模型权重.我们提供模型权重下载脚本,支持HuggingFace,ModelScope以及Modelers来源的模型下载,用法如下。注意:以下引用的
atb_models
路径在DeepSeek-V2
路径下。- 确认
atb_models/build/weights_url.yaml
文件中对应repo_id
,当前已默认配置模型官方认可的下载地址,如您有其他信任来源的repo_id
,可自行修改,默认配置如下: - 执行下载脚本
atb_models/build/download_weights.py
。
- 确认
- 修改模型文件夹属组为1001,执行权限为750,执行:
chown -R 1001:1001 /path-to-weights/DeepSeek-V3
和chmod -R 750 /path-to-weights/DeepSeek-V3
- 本地已有模型权重。从您信任的来源自行获取权重后,放置在从上述下载的模型代码的主目录下,放置后的目录结构应为如下:
- 启动容器
执行以下启动命令(参考):
docker run -itd --privileged --name=容器名称 --net=host \
--shm-size 500g \
--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/hisi_hdc \
--device /dev/devmm_svm \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
-v /usr/local/Ascend/firmware:/usr/local/Ascend/firmware \
-v /usr/local/sbin/npu-smi:/usr/local/sbin/npu-smi \
-v /usr/local/sbin:/usr/local/sbin \
-v /etc/hccn.conf:/etc/hccn.conf \
-v /权重路径:/权重路径 \
mindie:1.0.0-XXX-800I-A2-arm64-py3.11(根据加载的镜像名称修改) \
bash
开启通信环境变量
export ATB_LLM_HCCL_ENABLE=1
export ATB_LLM_COMM_BACKEND="hccl"
export HCCL_CONNECT_TIMEOUT=7200
export WORLD_SIZE=32
export HCCL_EXEC_TIMEOUT=0
权重测试
前置准备
- 修改权重目录下config.json文件:
将 model_type 更改为 deepseekv2 (全小写且无空格)
注意:在本仓实现中,DeepSeek-V3目前沿用DeepSeekV2代码框架
- 检查机器网络情况
# 检查物理链接
for i in {0..7}; do hccn_tool -i $i -lldp -g | grep Ifname; done
# 检查链接情况
for i in {0..7}; do hccn_tool -i $i -link -g ; done
# 检查网络健康情况
for i in {0..7}; do hccn_tool -i $i -net_health -g ; done
# 查看侦测ip的配置是否正确
for i in {0..7}; do hccn_tool -i $i -netdetect -g ; done
# 查看网关是否配置正确
for i in {0..7}; do hccn_tool -i $i -gateway -g ; done
# 检查NPU底层tls校验行为一致性,建议全0
for i in {0..7}; do hccn_tool -i $i -tls -g ; done | grep switch
# NPU底层tls校验行为置0操作
for i in {0..7};do hccn_tool -i $i -tls -s enable 0;done
- 获取每张卡的ip地址
for i in {0..7};do hccn_tool -i $i -ip -g; done
- 参考如下格式,配置
rank_table_file.json
{
"server_count": "...", # 总节点数
# server_list中第一个server为主节点
"server_list": [
{
"device": [
{
"device_id": "...", # 当前卡的本机编号,取值范围[0, 本机卡数)
"device_ip": "...", # 当前卡的ip地址,可通过hccn_tool命令获取
"rank_id": "..." # 当前卡的全局编号,取值范围[0, 总卡数)
},
...
],
"server_id": "...", # 当前节点的ip地址
"container_ip": "..." # 容器ip地址(服务化部署时需要),若无特殊配置,则与server_id相同
},
...
],
"status": "completed",
"version": "1.0"
}
精度测试
- 进入modeltest路径
cd /usr/local/Ascend/llm_model/tests/modeltest/
- 运行测试脚本
# 需在所有机器上同时执行
bash run.sh pa_bf16 [dataset] ([shots]) [batch_size] [model_name] ([is_chat_model]) [weight_dir] [rank_table_file] [world_size] [node_num] [rank_id_start] [master_address]
Example: 在DeepSeek-V3跑CEVAl数据集主节点的命令
bash run.sh pa_bf16 full_CEval 5 16 deepseekv2 /path/to/weights/DeepSeek-V3 /path/to/xxx/ranktable.json 32 4 0 {主节点IP}
# 0 代表从0号卡开始推理,之后的机器依次从8,16,24。
参数说明:
dataset可选full_BoolQ、full_CEval等,部分数据集需要设置shots
model_name为deepseekv2
weight_dir为模型权重路径
rank_table_file为“前置准备”中配置的rank_table_file.json路径
world_size为总卡数
node_num为当前节点编号,即rank_table_file.json的server_list中顺序确定
rank_id_start为当前节点起始卡号,即rank_table_file.json中当前节点第一张卡的rank_id
master_address为主节点ip地址,即rank_table_file.json的server_list中第一个节点的ip
性能测试
- 进入modeltest路径
cd /usr/local/Ascend/llm_model/tests/modeltest/
- 运行测试脚本
# 需在所有机器上同时执行
bash run.sh pa_bf16 performance [case_pair] [batch_size] [model_name] ([is_chat_model]) [weight_dir] [rank_table_file] [world_size] [node_num] [rank_id_start] [master_address]
参数含义同“精度测试”
Example: 在DeepSeek-V3跑性能测试主节点的命令
bash run.sh pa_bf16 performance [[256,256]] 16 deepseekv2 /path/to/weights/DeepSeek-V3 /path/to/xxx/ranktable.json 32 4 0 {主节点IP}
# 0 代表从0号卡开始推理,之后的机器依次从8,16,24。
服务测试
配置服务化环境变量
变量含义:expandable_segments-使能内存池扩展段功能,即虚拟内存特性。更多详情请查看昇腾环境变量参考
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
服务化需要rank_table_file.json
中配置container_ip
字段
所有机器的配置应该保持一致,除了环境变量的MIES_CONTAINER_IP
为本机ip地址。
export MIES_CONTAINER_IP=容器ip地址
export RANKTABLEFILE=rank_table_file.json路径
修改服务化参数
cd /usr/local/Ascend/mindie/latest/mindie-service/
vim conf/config.json
修改以下参数
"httpsEnabled" : false,
...
"multiNodesInferEnabled" : true, # 开启多机推理
...
# 若不需要安全认证,则将以下两个参数设为false
"interCommTLSEnabled" : false,
"interNodeTLSEnabled" : false,
...
"modelName" : "DeepSeek-V3" # 不影响服务化拉起
"modelWeightPath" : "权重路径",
Example:仅供参考,不保证性能
{
"Version" : "1.0.0",
"LogConfig" :
{
"logLevel" : "Info",
"logFileSize" : 20,
"logFileNum" : 20,
"logPath" : "logs/mindie-server.log"
},
"ServerConfig" :
{
"ipAddress" : "改成主节点IP",
"managementIpAddress" : "改成主节点IP",
"port" : 1025,
"managementPort" : 1026,
"metricsPort" : 1027,
"allowAllZeroIpListening" : false,
"maxLinkNum" : 1000, //如果是4机,建议300
"httpsEnabled" : false,
"fullTextEnabled" : false,
"tlsCaPath" : "security/ca/",
"tlsCaFile" : ["ca.pem"],
"tlsCert" : "security/certs/server.pem",
"tlsPk" : "security/keys/server.key.pem",
"tlsPkPwd" : "security/pass/key_pwd.txt",
"tlsCrlPath" : "security/certs/",
"tlsCrlFiles" : ["server_crl.pem"],
"managementTlsCaFile" : ["management_ca.pem"],
"managementTlsCert" : "security/certs/management/server.pem",
"managementTlsPk" : "security/keys/management/server.key.pem",
"managementTlsPkPwd" : "security/pass/management/key_pwd.txt",
"managementTlsCrlPath" : "security/management/certs/",
"managementTlsCrlFiles" : ["server_crl.pem"],
"kmcKsfMaster" : "tools/pmt/master/ksfa",
"kmcKsfStandby" : "tools/pmt/standby/ksfb",
"inferMode" : "standard",
"interCommTLSEnabled" : false,
"interCommPort" : 1121,
"interCommTlsCaPath" : "security/grpc/ca/",
"interCommTlsCaFiles" : ["ca.pem"],
"interCommTlsCert" : "security/grpc/certs/server.pem",
"interCommPk" : "security/grpc/keys/server.key.pem",
"interCommPkPwd" : "security/grpc/pass/key_pwd.txt",
"interCommTlsCrlPath" : "security/grpc/certs/",
"interCommTlsCrlFiles" : ["server_crl.pem"],
"openAiSupport" : "vllm"
},
"BackendConfig" : {
"backendName" : "mindieservice_llm_engine",
"modelInstanceNumber" : 1,
"npuDeviceIds" : [[0,1,2,3,4,5,6,7]],
"tokenizerProcessNumber" : 8,
"multiNodesInferEnabled" : true,
"multiNodesInferPort" : 1120,
"interNodeTLSEnabled" : false,
"interNodeTlsCaPath" : "security/grpc/ca/",
"interNodeTlsCaFiles" : ["ca.pem"],
"interNodeTlsCert" : "security/grpc/certs/server.pem",
"interNodeTlsPk" : "security/grpc/keys/server.key.pem",
"interNodeTlsPkPwd" : "security/grpc/pass/mindie_server_key_pwd.txt",
"interNodeTlsCrlPath" : "security/grpc/certs/",
"interNodeTlsCrlFiles" : ["server_crl.pem"],
"interNodeKmcKsfMaster" : "tools/pmt/master/ksfa",
"interNodeKmcKsfStandby" : "tools/pmt/standby/ksfb",
"ModelDeployConfig" :
{
"maxSeqLen" : 10000,
"maxInputTokenLen" : 2048,
"truncation" : true,
"ModelConfig" : [
{
"modelInstanceType" : "Standard",
"modelName" : "DeepSeek-V3",
"modelWeightPath" : "/home/data/dsv3_base_step178000",
"worldSize" : 8,
"cpuMemSize" : 5,
"npuMemSize" : -1,
"backendType" : "atb",
"trustRemoteCode" : false
}
]
},
"ScheduleConfig" :
{
"templateType" : "Standard",
"templateName" : "Standard_LLM",
"cacheBlockSize" : 128,
"maxPrefillBatchSize" : 8,
"maxPrefillTokens" : 2048,
"prefillTimeMsPerReq" : 150,
"prefillPolicyType" : 0,
"decodeTimeMsPerReq" : 50,
"decodePolicyType" : 0,
"maxBatchSize" : 8,
"maxIterTimes" : 1024,
"maxPreemptCount" : 0,
"supportSelectBatch" : false,
"maxQueueDelayMicroseconds" : 5000
}
}
}
拉起服务化
# 以下命令需在所有机器上同时执行
# 解决权重加载过慢问题
export OMP_NUM_THREADS=1
# 设置显存比
export NPU_MEMORY_FRACTION=0.95
# 拉起服务化
cd /usr/local/Ascend/mindie/latest/mindie-service/
./bin/mindieservice_daemon
执行命令后,首先会打印本次启动所用的所有参数,然后直到出现以下输出:
Daemon start success!
则认为服务成功启动。
来到客户端
进入相同容器,向服务端发送请求。
更多信息可参考官网信息:MindIE Service
常见问题
服务化常见问题
- 若出现out of memory报错,可适当调高
NPU_MEMORY_FRACTION
环境变量(默认值为0.8),适当调低服务化配置文件config.json
中maxSeqLen、maxInputTokenLen、maxPrefillBatchSize、maxPrefillTokens、maxBatchSize
等参数
export NPU_MEMORY_FRACTION=0.96
- 若出现hccl通信超时报错,可配置以下环境变量
export HCCL_CONNECT_TIMEOUT=7200
export HCCL_EXEC_TIMEOUT=0
- 若出现AttributeError:‘IbisTokenizer’ object has no atrribute ‘cache_path’
Step1: 进入环境终端后执行
pip show mies_tokenizer
默认出现类似如下结果,重点查看Location
Name: mies_tokenizer
Version: 0.0.1
Summary: ibis tokenizer
Home-page:
Author:
Author-email:
License:
Location: /usr/local/python3.10.13/lib/python3.10/site-packages
Requires:
Required-by:
Step2: 打开Location路径下的./mies_tokenizer/tokenizer.py文件
vim /usr/local/python3.10.13/lib/python3.10/site-packages/mies_tokenizer/tokenizer.py
Step3: 对以下两个函数代码进行修改
def __del__(self):
- dir_path = file_utils.standardize_path(self.cache_path)
+ cache_path = getattr(self, 'cache_path', None)
+ if cache_path is None:
+ return
+ dir_path = file_utils.standardize_path(cache_path)
file_utils.check_path_permission(dir_path)
all_request = os.listdir(dir_path)
以及
def _get_cache_base_path(child_dir_name):
dir_path = os.getenv("LOCAL_CACHE_DIR", None)
if dir_path is None:
dir_path = os.path.expanduser("~/mindie/cache")
- if not os.path.exists(dir_path):
- os.makedirs(dir_path)
+ os.makedirs(dir_path, exist_ok=True)
os.chmod(dir_path, 0o750)
- 若出现UnicodeEncodeError: ‘ascii’ codec can’t encode character \uff5c in position 301:ordinal not in range(128)
这是因为系统在写入或打印日志ASCII编码deepseek的词表失败,导致报错,不影响服务化正常运行。如果需要规避,需要/usr/local/Ascend/atb-models/atb_llm/runner/model_runner.py
的第145行注释掉:print_log(rank, logger.info, f’init tokenizer done: {self.tokenizer}')
权重路径权限问题
注意保证权重路径是可用的,执行以下命令修改权限,注意是整个父级目录的权限:
chown -R HwHiAiUser:HwHiAiUser /path-to-weights
chmod -R 750 /path-to-weights
DeepSeek-R1本地部署
DeepSeek-R1 Models
请访问DeepSeek-V3GitHub库,了解有关在本地运行DeepSeek-R1的更多信息。
注意:Hugging Face 的 Transformers 尚未得到直接支持。
DeepSeek-R1-Distill Models
DeepSeek-R1-Distill 模型可以以与 Qwen 或 Llama 模型相同的方式使用。
例如,您可以使用 vLLM 轻松启动一项服务:
vllm serve deepseek-ai/DeepSeek-R1-Distill-Qwen-32B --tensor-parallel-size 2 --max-model-len 32768 --enforce-eager
您还可以使用 SGLang 轻松启动服务
python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-R1-Distill-Qwen-32B --trust-remote-code --tp 2
使用建议
我们建议在使用 DeepSeek-R1 系列模型(包括基准测试)时遵循以下配置,以实现预期性能:
-
将
temperature
温度设置在 0.5-0.7 范围内(建议为 0.6),以防止无休止的重复或不连贯的输出。 -
避免添加系统提示
system prompt
;所有指令都应包含在用户提示user prompt
中。 -
对于数学问题,建议在提示中包含一个指令,例如:“请逐步推理,并将您的最终答案放在 \boxed{} 内。”
-
在评估模型性能时,建议进行多次测试并取平均值。
此外,我们观察到 DeepSeek-R1 系列模型在响应某些查询时倾向于绕过思维模式(即输出“<think>\n\n</think>”
),这会对模型的性能产生不利影响。为确保模型进行全面的推理,我们建议强制模型在每次输出的开头以“<think>\n
”开始其响应。
Official Prompts
在DeepSeek官方网页/app中,我们没有使用系统提示,而是设计了两个专门的提示,用于文件上传和网页搜索,以提供更好的用户体验。另外,网页/app中的温度为0.6
上传文件时,请按照模板创建提示,其中 {file_name}, {file_content} 和 {question} 为参数。
file_template = \
"""[file name]: {file_name}
[file content begin]
{file_content}
[file content end]
{question}"""
对于网络搜索,{search_results}、{cur_date} 和 {question} 是参数。
对于中文查询,我们使用提示语:
search_answer_zh_template = \
'''# 以下内容是基于用户发送的消息的搜索结果:
{search_results}
在我给你的搜索结果中,每个结果都是[webpage X begin]...[webpage X end]格式的,X代表每篇文章的数字索引。请在适当的情况下在句子末尾引用上下文。请按照引用编号[citation:X]的格式在答案中对应部分引用上下文。如果一句话源自多个上下文,请列出所有相关的引用编号,例如[citation:3][citation:5],切记不要将引用集中在最后返回引用编号,而是在答案对应部分列出。
在回答时,请注意以下几点:
- 今天是{cur_date}。
- 并非搜索结果的所有内容都与用户的问题密切相关,你需要结合问题,对搜索结果进行甄别、筛选。
- 对于列举类的问题(如列举所有航班信息),尽量将答案控制在10个要点以内,并告诉用户可以查看搜索来源、获得完整信息。优先提供信息完整、最相关的列举项;如非必要,不要主动告诉用户搜索结果未提供的内容。
- 对于创作类的问题(如写论文),请务必在正文的段落中引用对应的参考编号,例如[citation:3][citation:5],不能只在文章末尾引用。你需要解读并概括用户的题目要求,选择合适的格式,充分利用搜索结果并抽取重要信息,生成符合用户要求、极具思想深度、富有创造力与专业性的答案。你的创作篇幅需要尽可能延长,对于每一个要点的论述要推测用户的意图,给出尽可能多角度的回答要点,且务必信息量大、论述详尽。
- 如果回答很长,请尽量结构化、分段落总结。如果需要分点作答,尽量控制在5个点以内,并合并相关的内容。
- 对于客观类的问答,如果问题的答案非常简短,可以适当补充一到两句相关信息,以丰富内容。
- 你需要根据用户要求和回答内容选择合适、美观的回答格式,确保可读性强。
- 你的回答应该综合多个相关网页来回答,不能重复引用一个网页。
- 除非用户要求,否则你回答的语言需要和用户提问的语言保持一致。
# 用户消息为:
{question}'''
对于英语查询,我们使用提示语:
search_answer_en_template = \
'''# The following contents are the search results related to the user's message:
{search_results}
In the search results I provide to you, each result is formatted as [webpage X begin]...[webpage X end], where X represents the numerical index of each article. Please cite the context at the end of the relevant sentence when appropriate. Use the citation format [citation:X] in the corresponding part of your answer. If a sentence is derived from multiple contexts, list all relevant citation numbers, such as [citation:3][citation:5]. Be sure not to cluster all citations at the end; instead, include them in the corresponding parts of the answer.
When responding, please keep the following points in mind:
- Today is {cur_date}.
- Not all content in the search results is closely related to the user's question. You need to evaluate and filter the search results based on the question.
- For listing-type questions (e.g., listing all flight information), try to limit the answer to 10 key points and inform the user that they can refer to the search sources for complete information. Prioritize providing the most complete and relevant items in the list. Avoid mentioning content not provided in the search results unless necessary.
- For creative tasks (e.g., writing an essay), ensure that references are cited within the body of the text, such as [citation:3][citation:5], rather than only at the end of the text. You need to interpret and summarize the user's requirements, choose an appropriate format, fully utilize the search results, extract key information, and generate an answer that is insightful, creative, and professional. Extend the length of your response as much as possible, addressing each point in detail and from multiple perspectives, ensuring the content is rich and thorough.
- If the response is lengthy, structure it well and summarize it in paragraphs. If a point-by-point format is needed, try to limit it to 5 points and merge related content.
- For objective Q&A, if the answer is very brief, you may add one or two related sentences to enrich the content.
- Choose an appropriate and visually appealing format for your response based on the user's requirements and the content of the answer, ensuring strong readability.
- Your answer should synthesize information from multiple relevant webpages and avoid repeatedly citing the same webpage.
- Unless the user requests otherwise, your response should be in the same language as the user's question.
# The user's message is:
{question}'''
更多推荐
所有评论(0)