跳到内容

AutoAWQ

⚠️ 警告:AutoAWQ 库已被弃用。此功能已由 vLLM 项目在 llm-compressor 中采用。有关推荐的量化工作流程,请参阅 llm-compressor 中的 AWQ 示例。关于弃用的更多详细信息,请参阅原始的 AutoAWQ 仓库

要创建新的 4-bit 量化模型,您可以利用 AutoAWQ。量化将模型的精度从 BF16/FP16 降低到 INT4,从而有效地减少了模型的总内存占用。其主要优势是降低了延迟和内存使用量。

您可以通过安装 AutoAWQ 或从 Huggingface 上的 6500 多个模型中选择一个来量化您自己的模型。

pip install autoawq

安装 AutoAWQ 后,您就可以开始量化模型了。请参阅 AutoAWQ 文档以获取更多详细信息。以下是如何量化 mistralai/Mistral-7B-Instruct-v0.2 的示例:

代码
from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer

model_path = "mistralai/Mistral-7B-Instruct-v0.2"
quant_path = "mistral-instruct-v0.2-awq"
quant_config = {"zero_point": True, "q_group_size": 128, "w_bit": 4, "version": "GEMM"}

# Load model
model = AutoAWQForCausalLM.from_pretrained(
    model_path,
    low_cpu_mem_usage=True,
    use_cache=False,
)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)

# Quantize
model.quantize(tokenizer, quant_config=quant_config)

# Save quantized model
model.save_quantized(quant_path)
tokenizer.save_pretrained(quant_path)

print(f'Model is quantized and saved at "{quant_path}"')

要在 vLLM 中运行 AWQ 模型,您可以使用 TheBloke/Llama-2-7b-Chat-AWQ 并执行以下命令:

python examples/offline_inference/llm_engine_example.py \
    --model TheBloke/Llama-2-7b-Chat-AWQ \
    --quantization awq

AWQ 模型也直接通过 LLM 入口点提供支持:

代码
from vllm import LLM, SamplingParams

# Sample prompts.
prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
# Create a sampling params object.
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

# Create an LLM.
llm = LLM(model="TheBloke/Llama-2-7b-Chat-AWQ", quantization="AWQ")
# Generate texts from the prompts. The output is a list of RequestOutput objects
# that contain the prompt, generated text, and other information.
outputs = llm.generate(prompts, sampling_params)
# Print the outputs.
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")