跳转至

量化指南

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

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

注意

You can choose to convert the model yourself or use the quantized model we uploaded. See https://www.modelscope.cn/models/vllm-ascend/Kimi-K2-Instruct-W8A8. Before you quantize a model, ensure sufficient RAM is available.

量化工具

vLLM Ascend 支持由两种主要工具量化的模型:ModelSlimLLM-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 文件中查看。

运行量化模型

一旦您拥有了由 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 

参考资料