注:本来还想实测一下2台K100AI跑MP16的,奈何1台都这么慢了,就先不测了。
还请官方多关注一下K100AI的推理优化!
以下是参考了论坛帖子(https://forum.sourcefind.cn/thread/c5cfdcfa-3fd5-11f1-bde9-0242ac150003)
和魔搭评论区(https://modelscope.cn/models/FlagRelease/DeepSeek-V4-Flash-hygon-FlagOS/feedback)
的方法,实测的DeepSeek-V4-Flash效果
# 1 部署deepseek-v4-flash版在1台K100AI上(8卡)
## 1.1 基础环境
### 1.1.1 下载权重
modelscope download --model deepseek-ai/DeepSeek-V4-Flash --local_dir /data/models/DeepSeek-V4-Flash
### 1.1.2 下载docker镜像
docker pull harbor.baai.ac.cn/flagrelease-public/flagrelease-hygon-deepseek-v4-flash:202604242039
### 1.1.3 启动镜像
```
#!/bin/bash
docker run -it \
--shm-size 60g \
--network=host \
--name flagos_ds_v4 \
--privileged \
--device=/dev/kfd \
--device=/dev/dri \
--device=/dev/mkfd \
--group-add video \
--cap-add=SYS_PTRACE \
--security-opt seccomp=unconfined \
-v /opt/hyhal/:/opt/hyhal/:ro \
-v /home/xxx/:/workspace \
-v /data/:/data/ \
-v /data1/:/data1/ \
--ipc=host \
harbor.baai.ac.cn/flagrelease-public/flagrelease-hygon-deepseek-v4-flash:202604242039
```
## 1.2 权重转化
参考 https://github.com/flagos-ai/DeepSeek-V4-FlagOS/blob/main/README_cn.md
### 1.2.1 fp4/fp8 -> bf16
方式二的第一步
python3 convert_weight.py \
--input-fp4-hf-path /data/models/DeepSeek-V4-Flash \
--output-bf16-hf-path /data/models/DeepSeek-V4-Flash-convert-out-BF16
### 1.2.2 将专家权重从 BF16 量化为 INT8-MP8
方式三的第一步
python3 quantize_int8_moe.py \
--input_dir /data/models/DeepSeek-V4-Flash-convert-out-BF16 \
--output_dir /data/models/DeepSeek-V4-Flash-convert-out-INT8 \
--config_path /workspace/code/deepseek_v4/my_run/config_flash_v4_int8.json
config_flash_v4_int8.json的内容从魔搭评论区获取(https://modelscope.cn/models/FlagRelease/DeepSeek-V4-Flash-hygon-FlagOS/feedback):
```
{
"vocab_size": 129280,
"dim": 4096,
"moe_inter_dim": 2048,
"n_layers": 43,
"n_hash_layers": 3,
"n_heads": 64,
"n_routed_experts": 256,
"n_shared_experts": 1,
"n_activated_experts": 6,
"score_func": "sqrtsoftplus",
"route_scale": 1.5,
"swiglu_limit": 10.0,
"q_lora_rank": 1024,
"head_dim": 512,
"rope_head_dim": 64,
"o_groups": 8,
"o_lora_rank": 1024,
"window_size": 128,
"original_seq_len": 65536,
"rope_theta": 10000,
"rope_factor": 16,
"beta_fast": 32,
"beta_slow": 1,
"index_n_heads": 64,
"index_head_dim": 128,
"index_topk": 512,
"hc_mult": 4,
"hc_sinkhorn_iters": 20,
"dtype": "fp8",
"scale_fmt": "ue8m0",
"expert_dtype": "fp4",
"compress_rope_theta": 160000,
"compress_ratios": [0, 0, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 128, 4, 0],
"quantization_config": {
"quant_method": "linear_int8",
"target": "moe_experts",
"scheme": "per_channel_symmetric",
"bits": 8,
"scale_suffix": ".scale"
}
}
```
### 1.2.3 分片用于模型并行
方式三的第二步
export MP=8
python convert.py \
--hf-ckpt-path /data/models/DeepSeek-V4-Flash-convert-out-INT8 \
--save-path /data/models/DeepSeek-V4-Flash-convert-out-INT8-MP8 \
--n-experts 256 \
--model-parallel ${MP} \
--expert-dtype int8 \
--o-groups 8
## 1.3 运行
### 1.3.1 输入为文件
```
export USE_FLAGGEMS=1
export GEMS_VENDOR=hygon
torchrun --nproc-per-node 8 generate.py \
--max-new-tokens 28 \
--config /workspace/code/deepseek_v4/my_run/config_flash_v4_int8.json \
--ckpt-path /data/models/DeepSeek-V4-Flash-convert-out-INT8-MP8 \
--input-file prompt.txt
```
#### 实测prompt.txt:
从模型加载,到运行出结果,到模型释放,耗时4分20秒

### 1.3.2 交互式
启动模型大约耗时1分40秒
```
export USE_FLAGGEMS=1
export GEMS_VENDOR=hygon
stdbuf -o0 torchrun --nproc-per-node 8 generate.py \
--max-new-tokens 8192 \
--config /workspace/code/deepseek_v4/my_run/config_flash_v4_int8.json \
--ckpt-path /data/models/DeepSeek-V4-Flash-convert-out-INT8-MP8 \
--interactive
```
#### 输入为:你好,介绍一下你自己
输出如图:(问答耗时大约14分钟)

#### 输入为: 1+1等于几?
(耗时28秒)
#### 接着问:2+3等于几?
报错编解码问题

#### 解决连续提问的编解码问题后,继续问2+3等于几?
解决方式是在原有指令的基础上,增加“stdbuf -o0”,即stdbuf -o0 torchrun xxx
```
stdbuf -o0 强制将程序的 stdout(标准输出)缓冲设为 0(无缓冲),避免模型生成的输出(含特殊 token 的残余字节)残留在缓冲区中污染后续的 stdin(标准输入) 读取操作,从而防止 UnicodeDecodeError
```

(回答2+3等于几,该问题耗时1分54秒)
bitsandbytes 库为节省显存而设计的方案,引入了额外的计算开销,反而拖慢了速度。
linear_int8是保证了精度,损失了速度。和单机的linear_int8量化方式相比,双机K100-AI跑BF-16的速度反而要快的多,当然这种所谓的快,也就是每秒1-2个token而已!