在前面的文章里一文吃透模型量化,解锁DeepSeek轻量级部署密码手把手教你玩转DeepSeek-R1量化模型,我们了解了什么是模型量化、量化的工具以及如何获取和部署DeepSeek量化模型。目前DeepSeek的量化模型可在魔乐社区下载(或点击魔乐社区直达DeepSeek量化模型专区):

  • https://modelers.cn/models/State_Cloud/DeepSeek-V3-w8a8
  • https://modelers.cn/models/State_Cloud/DeepSeek-R1-W8A8
  • https://modelers.cn/models/State_Cloud/DeepSeek-R1-bf16-hfd-w8a8

接下来,让我们继续了解DeepSeek模型量化的最佳实践。

DeepSeek模型当前已验证的量化方法

  • W8A8量化:DeepSeek-V2-Lite-Chat-16B, DeepSeek-V2-Chat-236B, DeepSeek-V3, DeepSeek-R1

  • W8A16量化:DeepSeek-V2-Lite-Chat-16B, DeepSeek-V2-Chat-236B

此模型仓已适配的模型版本
  • Deepseek-V2-Chat(https://huggingface.co/deepseek-ai/DeepSeek-V2-Chat)
  • Deepseek-V3(https://huggingface.co/deepseek-ai/DeepSeek-V3,https://modelers.cn/models/State_Cloud/DeepSeek-V3)
  • Deepseek-R1(https://huggingface.co/deepseek-ai/DeepSeek-R1,https://modelers.cn/models/State_Cloud/DeepSeek-R1-origin)

环境配置

  • 有关DeepSeek的量化,需要下载安装最新版CANN包及msmodelslim(br_noncom_MindStudio_8.0.0_POC_20251231分支),环境配置以及量化工具安装请参考使用说明的方式二安装。(https://gitee.com/ascend/msit/blob/br_noncom_MindStudio_8.0.0_POC_20251231/msmodelslim/README.md)

  • 如果使用mindie镜像的话,需要下载安装msmodelslim(br_noncom_MindStudio_8.0.0_POC_20251231分支),安装方式参考使用说明的方式二安装。(https://gitee.com/ascend/msit/blob/br_noncom_MindStudio_8.0.0_POC_20251231/msmodelslim/README.md)

运行前必检

在运行脚本前,请根据以下必检项对相关内容进行更改。

1、昇腾不支持flash_attn库,运行时需要注释掉权重文件夹中modeling_deepseek.py中的部分代码。

image.png

2、需安装更新transformers版本(>=4.48.2)。

3、当前昇腾设备不支持FP8格式加载,需要将权重文件夹中config.json中的以下字段删除:

image.png

4、修改quant_deepseek_w8a8.py脚本,增加卡数,eg: max_memory={0: "50GiB", 1: "50GiB", "cpu": "1500GiB"}, 可以根据自己的卡数以及显存情况设置,以防OOM显存不够报错。

image.png

量化权重生成

量化权重可使用quant_deepseek.py和quant_deepseek_w8a8.py脚本生成,以下提供DeepSeek模型量化权重生成快速启动命令。

quant_deepseek.py量化参数说明

image.png

quant_deepseek_w8a8.py量化参数说明

image.png

更多参数配置要求,请参考:

  • 量化过程中配置的参数 QuantConfig(https://gitee.com/ascend/msit/blob/dev/msmodelslim/docs/Python-API%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E/%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%8E%8B%E7%BC%A9%E6%8E%A5%E5%8F%A3/%E5%A4%A7%E6%A8%A1%E5%9E%8B%E9%87%8F%E5%8C%96%E6%8E%A5%E5%8F%A3/PyTorch/QuantConfig.md)

  • 量化参数配置类 Calibrator(https://gitee.com/ascend/msit/blob/dev/msmodelslim/docs/Python-API%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E/%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%8E%8B%E7%BC%A9%E6%8E%A5%E5%8F%A3/%E5%A4%A7%E6%A8%A1%E5%9E%8B%E9%87%8F%E5%8C%96%E6%8E%A5%E5%8F%A3/PyTorch/Calibrator.md)

使用案例

  • 请将{浮点权重路径}和{量化权重路径}替换为用户实际路径。

  • 如果需要使用npu多卡量化,请先配置环境变量,支持多卡量化:

    export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7export PYTORCH_NPU_ALLOC_CONF=expandable_segments:False

DeepSeek-V2/V3/R1 模型量化

1、DeepSeek-V2 w8a16量化

生成DeepSeek-V2模型w8a16量化权重,使用histogram量化方式,在CPU上进行运算

python3 quant_deepseek.py --model_path {浮点权重路径} --save_directory {W8A16量化权重路径} --device_type cpu --act_method 2 --w_bit 8 --a_bit 16

2、DeepSeek-V2 w8a8 Dynamic量化

生成DeepSeek-V2模型 w8a8 dynamic量化权重,使用histogram量化方式,在CPU上进行运算

python3 quant_deepseek.py --model_path {浮点权重路径} --save_directory {W8A16量化权重路径} --device_type cpu --act_method 2 --w_bit 8 --a_bit 16

3、DeepSeek-V2/V3/R1 w8a8混合量化(MLA:w8a8量化,MOE:w8a8 dynamic量化)

注:当前量化只支持输入bfloat16格式模型

生成DeepSeek-V2/V3/R1模型 w8a8 混合量化权重

python3 quant_deepseek_w8a8.py --model_path {浮点权重路径} --save_path {W8A8量化权重路径}

4、DeepSeek-V2/V3/R1 w4a16 per-group量化(MLA:float,MOE:w4a16)

注:当前量化只支持输入bfloat16格式模型。

生成DeepSeek-V2/V3/R1模型 w4a16 量化权重​​​​​​​

python3 quant_deepseek.py \--model_path {浮点权重路径} \--save_directory {W4A16量化权重路径} \--w_bit 4 \--a_bit 16 \--group_size 64 \--is_lowbit True \--open_outlier False \--device "npu" \--calib_file ""

DeepSeek量化QA

Q1:报错 This modeling file requires the following packages that were not found in your environment:flash_attn. Run 'pip install      flash_attn'

A: 当前环境中缺少flash_attn库且昇腾不支持该库,运行时需要注释掉权重文件夹中modeling_deepseek.py中的部分代码。

image.png

Q2:modeling_utils.py报错 if metadata.get("format") not in ["pt",      "tf", "flax", "mix"]: AttributeError:  "NoneType" object has no attribute 'get';

A:说明输入的的权重中缺少metadata字段,需安装更新transformers版本(>=4.48.2)。

Q3:报错 Unknown quantization type,got fp8 - supported types are:['awq', 'bitsandbytes_4bit','bitsandbytes_8bit', 'gptq', 'aqlm', 'quanto', 'eetq', 'hqq','fbgemm_fp8']。

A: 由于当前昇腾设备不支持FP8格式加载,需要将权重文件夹中config.json中的以下字段删除:

image.png

Q4: 遇到OOM显存不够报错。

A:修改quant_deepseek_w8a8.py脚本,增加卡数,eg: max_memory={0: "50GiB", 1: "50GiB", "cpu": "1500GiB"}, 可以根据自己的卡数以及显存情况设置。

image.png

--END--

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐