llm-compressor 量化指南#

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

支持的 llm-compressor 量化类型#

支持 CompressedTensorsW8A8 静态权重

权重:每通道、int8、对称;激活:每张量、int8、对称。

支持 CompressedTensorsW8A8Dynamic 动态权重

权重:每通道、int8、对称;激活:每 token、int8、对称、动态。

安装 llm-compressor#

要量化模型,您应该安装 llm-compressor。它是一个统一的库,用于创建压缩模型以实现 vLLM 更快的推理。

安装 llm-compressor

pip install llmcompressor

生成 W8A8 权重#

cd examples/quantization/llm-compressor

python3 w8a8_int8_dynamic.py

更多详情,请参阅 官方示例

运行模型#

现在,您可以使用 vLLM Ascend 运行量化模型。下面提供了在线和离线推理的示例

离线推理#

import torch

from vllm import LLM, SamplingParams

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

llm = LLM(model="{quantized_model_save_path}",
          max_model_len=2048,
          trust_remote_code=True)

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}")

在线推理#

使用 vLLM Ascend 启动量化模型;无需修改启动命令。