DeepSeek-R1-Distill-Llama-8B
Usage
Using the reasoning data generated by DeepSeek-R1, DeepSeek AI fine-tuned several dense models that are widely used in the research community, slightly changing their configs and tokenizers. DeepSeek-R1-Distill-Llama-8B is one of them.
权重
权重下载
加载镜像
前往昇腾社区/开发资源下载适配DeepSeek-R1-Distill-Llama-8B的镜像包:1.0.0-800I-A2-py311-openeulsr24.03-lts或1.0.0-300I-Duo-py311-openeuler24.03-lts
完成之后,请使用docker images
命令确认查找具体镜像名称与标签。
镜像中各组件版本配套如下:
组件 | 版本 |
---|---|
MindIE | 1.0.0 |
CANN | 8.0.0 |
PTA | 6.0.0 |
MindStudio | 7.0.0 |
HDK | 24.1.0 |
约束条件
- 部署DeepSeek-R1-Distill-Llama-8B模型至少需要
1台Atlas 800I A2服务器
或者1台插1张Atlas 300I DUO卡的服务器
- 在使用Atlas 300I DUO推理卡部署模型时,需要修改权重目录下的config.json文件,"torch_dtype"字段改为"float16"
- 支持TP=1/2/4/8推理
新建容器
目前提供的MindIE镜像预置了DeepSeek-R1-Distill-Llama-8B模型推理脚本,无需再额外下载模型适配代码,直接新建容器即可。
如果您使用的是root用户镜像(例如从Ascend Hub上取得),并且可以使用特权容器,请使用以下命令启动容器:
docker run -it -d --net=host --shm-size=1g \
--privileged \
--name <container-name> \
--device=/dev/davinci_manager \
--device=/dev/hisi_hdc \
--device=/dev/devmm_svm \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver:ro \
-v /usr/local/sbin:/usr/local/sbin:ro \
-v /path-to-weights:/path-to-weights:ro \
mindie:1.0.0-800I-A2-py311-openeuler24.03-lts bash
如果您希望使用自行构建的普通用户镜像,并且规避容器相关权限风险,可以使用以下命令指定用户与设备:
docker run -it -d --net=host --shm-size=1g \
--name <container-name> \
--device=/dev/davinci_manager \
--device=/dev/hisi_hdc \
--device=/dev/devmm_svm \
--device=/dev/davinci0 \
--device=/dev/davinci1 \
--device=/dev/davinci2 \
--device=/dev/davinci3 \
--device=/dev/davinci4 \
--device=/dev/davinci5 \
--device=/dev/davinci6 \
--device=/dev/davinci7 \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver:ro \
-v /usr/local/sbin:/usr/local/sbin:ro \
-v /path-to-weights:/path-to-weights:ro \
mindie:1.0.0-800I-A2-py311-openeuler24.03-lts bash
更多镜像使用信息请参考官方镜像仓库文档。
进入容器
docker exec -it ${容器名称} bash
量化权重生成
Atlas 800I A2 w8a8量化
生成量化权重依赖msModelSlim工具,安装方式见此README
量化权重统一使用${ATB_SPEED_HOME_PATH}/examples/convert/model_slim/quantifier.py脚本生成,以下提供Llama模型量化权重生成快速启动命令
W8A8量化权重请使用以下指令生成
- 注意该量化方式仅支持在Atlas 800I A2服务器上运行
# 设置CANN包的环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# 关闭虚拟内存
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:False
# 进入atb-models目录
cd ${ATB_SPEED_HOME_PATH}
# DeepSeek-R1-Distill-Llama-8B量化,有回退层,antioutlier使用m1算法配置,使用min-max量化方式,校准数据集使用50条BoolQ数据,在NPU上进行运算
bash examples/models/llama3/generate_quant_weight.sh -src {浮点权重路径} -dst {W8A8量化权重路径} -type llama3.1_8b_w8a8
Atlas 300I DUO 稀疏量化
Step 1 生成W8A8S量化权重
- 注意该量化方式仅支持在Atlas 300I DUO推理卡上运行
- 修改模型权重config.json中
torch_dtype
字段为float16
- 生成量化权重依赖msModelSlim工具,安装方式见此README
- 进入到{msModelSlim工具路径}/msit/msmodelslim/example/Llama的目录
cd msit/msmodelslim/example/Llama
;
# 执行"jq --version"查看是否安装jq,若返回"bash:jq:command not found",则依次执行"apt-get update"和"apt install jq"
jq --version
# 运行量化转换脚本
python3 quant_llama.py --model_path {浮点权重路径} --save_directory {W8A8S量化权重路径} --calib_file ../common/boolq.jsonl --w_bit 4 --a_bit 8 --fraction 0.011 --co_sparse True
Step 2 量化权重切分及压缩
export IGNORE_INFER_ERROR=1
# 进入atb-models目录
cd ${ATB_SPEED_HOME_PATH}
# 运行切分及压缩脚本
torchrun --nproc_per_node {TP数} -m examples.convert.model_slim.sparse_compressor --model_path {W8A8S量化权重路径} --save_directory {W8A8SC量化权重路径}
- TP数为tensor parallel并行个数
- 注意:若权重生成时以TP=2进行切分,则运行时也需以TP=2运行
- 示例
torchrun --nproc_per_node 2 -m examples.convert.model_slim.sparse_compressor --model_path /data1/weights/model_slim/Llama-8b_w8a8s --save_directory /data1/weights/model_slim/Llama-8b_w8a8sc
纯模型推理
对话测试
进入llm_model路径
cd $ATB_SPEED_HOME_PATH
执行对话测试
torchrun --nproc_per_node 2 \
--master_port 20037 \
-m examples.run_pa \
--model_path ${权重路径} \
--input_texts 'What is deep learning?' \
--max_output_length 20 \
性能测试
进入ModelTest路径
cd $ATB_SPEED_HOME_PATH/tests/modeltest/
运行测试脚本
bash run.sh pa_[data_type] performance [case_pair] [batch_size] ([prefill_batch_size]) [model_name] ([is_chat_model]) (lora [lora_data_path]) [weight_dir] ([trust_remote_code]) [chip_num] ([parallel_params]) ([max_position_embedding/max_sequence_length])
具体执行batch=1, 输入长度256, 输出长度256用例的4卡并行性能测试命令如下,
Atlas 800I A2:
bash run.sh pa_bf16 performance [[256,256]] 1 llama ${weight_path} 4
Atlas 300I Duo:
bash run.sh pa_fp16 performance [[256,256]] 1 llama ${weight_path} 4
注:ModelTest为大模型的性能和精度提供测试功能。使用文档请参考
${ATB_SPEED_HOME_PATH}/tests/modeltest/README.md
服务化推理
- 打开配置文件
vim /usr/local/Ascend/mindie/latest/mindie-service/conf/config.json
- 更改配置文件
{
...
"ServerConfig" :
{
...
"port" : 1025, #自定义
"managementPort" : 1026, #自定义
"metricsPort" : 1027, #自定义
...
"httpsEnabled" : false,
...
},
"BackendConfig": {
...
"npuDeviceIds" : [[0,1,2,3]],
...
"ModelDeployConfig":
{
"ModelConfig" : [
{
...
"modelName" : "llama",
"modelWeightPath" : "/data/datasets/DeepSeek-R1-Distill-Llama-8B",
"worldSize" : 4,
...
}
]
},
...
}
}
- 拉起服务化
cd /usr/local/Ascend/mindie/latest/mindie-service/bin
./mindieservice_daemon
- 新建窗口测试(VLLM接口)
curl 127.0.0.1:1025/generate -d '{
"prompt": "What is deep learning?",
"max_tokens": 32,
"stream": false,
"do_sample":true,
"repetition_penalty": 1.00,
"temperature": 0.01,
"top_p": 0.001,
"top_k": 1,
"model": "llama"
}'
注: 服务化推理的更多信息请参考MindIE Service用户指南
常见问题
- ImportError: cannot import name 'shard_checkpoint' from 'transformers.modeling_utils'. 降低transformers版本可解决。
pip install transformers==4.46.3 --force-reinstall
pip install numpy==1.26.4 --force-reinstall