AutoAWQ
::: warning 注意
请注意,vLLM中的AWQ(Approximate Weight Quantization,近似权重量化)支持目前尚未优化。我们建议使用模型的非量化版本以获得更高的准确性和吞吐量。目前,您可以使用AWQ作为一种减少内存占用的方法。截至目前,它更适合于低延迟推理,且并发请求数量较少的场景。vLLM的AWQ实现的吞吐量低于非量化版本。 :::
为了创建一个新的4位量化模型,您可以利用AutoAWQ。量化将模型的精度从FP16降低到INT4,这有效地将文件大小减少了约70%。主要的好处是降低了延迟和内存使用。
您可以通过安装AutoAWQ或在Huggingface上选择400多个模型中的一个来量化您自己的模型。
pip install autoawq
安装AutoAWQ之后,您就可以准备量化模型了。以下是量化Vicuna 7B v1.5:
from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer
model_path = 'lmsys/vicuna-7b-v1.5'
quant_path = 'vicuna-7b-v1.5-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})
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)
要使用vLLM运行AWQ模型,您可以使用TheBloke/Llama-2-7b-Chat-AWQ,并使用以下命令:
python examples/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}")