跳转至

量化指南

模型量化是一种通过降低权重和激活值的数值精度来减小模型尺寸和计算开销的技术,从而节省内存并提升推理速度。

vLLM Ascend 支持多种量化方法。本指南提供了使用不同量化工具以及在 vLLM Ascend 上运行量化模型的说明。

注意

您可以选择自行转换模型,或使用我们上传的量化模型。 参见 https://www.modelscope.cn/models/vllm-ascend/Kimi-K2-Instruct-W8A8。 在量化模型之前,请确保有足够的可用 RAM。

量化工具

vLLM Ascend 支持由两种主要工具量化的模型:ModelSlim 和 LLM-Compressor。

1. ModelSlim(推荐)

ModelSlim 是一个对 Ascend 友好的压缩工具,专注于加速,采用压缩技术,并专为 Ascend 硬件构建。它包含一系列推理优化技术,如量化和压缩,旨在加速大语言稠密模型、MoE 模型、多模态理解模型、多模态生成模型等。

安装

要使用 ModelSlim 进行模型量化,请从其 Git 仓库 安装:

# Install 26.0.0 version, this is currently the latest stable branch
git clone https://gitcode.com/Ascend/msmodelslim.git -b 26.0.0

cd msmodelslim

bash install.sh

模型量化

以下示例展示了如何为 Qwen3-MoE 模型 生成 W8A8 量化权重。

量化脚本:

cd example/Qwen3-MOE

# Support multi-card quantization
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:False

# Set model and save paths
export MODEL_PATH="/path/to/your/model"
export SAVE_PATH="/path/to/your/quantized_model"

# Run quantization script
python3 quant_qwen_moe_w8a8.py --model_path $MODEL_PATH \
--save_path $SAVE_PATH \
--anti_dataset ../common/qwen3-moe_anti_prompt_50.json \
--calib_dataset ../common/qwen3-moe_calib_prompt_50.json \
--trust_remote_code True

量化完成后,输出目录将包含量化后的模型文件。

更多示例,请参考 官方示例。

2. LLM-Compressor

LLM-Compressor 是一个统一的压缩模型库,用于加速 vLLM 推理。

安装

pip install llmcompressor

模型量化

LLM-Compressor 提供了多种量化方案示例。

稠密量化

为稠密模型生成 W8A8 动态量化权重的示例:

# Navigate to LLM-Compressor examples directory
cd examples/quantization/llm-compressor

# Run quantization script
python3 w8a8_int8_dynamic.py
MoE 量化

为 MoE 模型生成 W8A8 动态量化权重的示例:

# Navigate to LLM-Compressor examples directory
cd examples/quantization/llm-compressor

# Run quantization script
python3 w8a8_int8_dynamic_moe.py

更多内容,请参考 官方示例。

LLM-Compressor 当前支持的量化类型可在 vllm_ascend/quantization/compressed_tensors_config.py 文件中查看。

3. 原生 FP8 检查点(无需量化步骤)

Many models are now published directly in FP8, for example Qwen/Qwen3.8-27B-FP8 and zai-org/GLM-5.3-Flash. Their config.json carries "quant_method": "fp8" together with a weight_block_size, meaning every quantized weight is stored as float8_e4m3fn plus one float32 scale per weight block. vLLM Ascend detects these checkpoints and, on hardware with an FP8 matmul, serves them as published, so no offline re-quantization is required:

vllm serve /path/to/Qwen3.8-27B-FP8 --trust-remote-code
vllm serve /path/to/GLM-5.3-Flash --tensor-parallel-size 8 --trust-remote-code

权重的执行方式取决于硬件:

  • 950PR&950DT Products: the block scales are re-grouped into MXFP8 at load time, so weights stay at one byte per element and the native FP8 matmul is used.
  • Other Ascend generations: A2/A3 and Atlas 300I DUO have no FP8 matmul, so such a checkpoint is rejected at startup. Dequantizing it into the model dtype instead would double the weight footprint to run an unquantized GEMM, which is not what the checkpoint promises. Serve the BF16 release of the model there, or re-quantize it with ModelSlim or LLM-Compressor.

检查点中未量化的层(例如多模态模型上的 visual.merger.*)会列在 ignored_layers 或 modules_to_not_convert 中,并以未量化方式提供。

仅支持块级(block-wise)形式。没有 weight_block_size(逐张量或逐通道缩放因子)的原生 FP8 检查点目前没有 Ascend 执行路径;请改用 ModelSlim 或 LLM-Compressor 对其进行重新量化。

运行量化模型

一旦您拥有了由 ModelSlim 生成的量化模型,您可以通过指定 --quantization ascend 参数来使用 vLLM Ascend 进行推理,以启用量化功能;而对于由 LLM-Compressor 量化的模型,则无需添加此参数。

离线推理

import torch

from vllm import LLM, SamplingParams

prompts = [
    "Hello, my name is",
    "The future of AI is",
]
# Set sampling parameters
sampling_params = SamplingParams(temperature=0.6, top_p=0.95, top_k=40)

llm = LLM(model="/path/to/your/quantized_model",
          max_model_len=4096,
          trust_remote_code=True,
          # Set appropriate TP and DP values
          tensor_parallel_size=2,
          data_parallel_size=1,
          # Set an unused port
          port=8000,
          # Set serving model name
          served_model_name="quantized_model",
          # Specify `quantization="ascend"` to enable quantization for models quantized by ModelSlim
          quantization="ascend")

outputs = llm.generate(prompts, sampling_params)
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

在线推理

# Corresponding to offline inference
python -m vllm.entrypoints.api_server \
    --model /path/to/your/quantized_model \
    --max-model-len 4096 \
    --port 8000 \
    --tensor-parallel-size 2 \
    --data-parallel-size 1 \
    --served-model-name quantized_model \
    --trust-remote-code 

参考资料