name: awq-quantization description: 激活感知权重量化,用于4位LLM压缩,达到3倍加速和最小准确度损失。在部署大型模型(7B-70B)到有限GPU内存时使用,当需要比GPTQ更快的推理并更好保持准确性时,或用于指令调优和多模态模型。MLSys 2024最佳论文奖获得者。 version: 1.0.0 author: Orchestra Research license: MIT tags: [Optimization, AWQ, Quantization, 4-Bit, Activation-Aware, Memory Optimization, Fast Inference, vLLM Integration, Marlin Kernels] dependencies: [autoawq, transformers>=4.45.0, torch>=2.0.0]
AWQ (Activation-aware Weight Quantization)
4位量化,基于激活模式保护重要权重,实现3倍加速和最小准确度损失。
何时使用 AWQ
使用 AWQ 当:
- 需要4位量化且准确度损失<5%
- 部署指令调优或聊天模型(AWQ泛化能力更好)
- 希望比FP16有~2.5-3倍的推理加速
- 使用vLLM进行生产服务
- 拥有Ampere+ GPU(A100、H100、RTX 40xx)以支持Marlin内核
使用 GPTQ 替代当:
- 需要最大生态系统兼容性(更多工具支持GPTQ)
- 专门使用ExLlamaV2后端
- 拥有旧GPU没有Marlin支持
使用 bitsandbytes 替代当:
- 需要零校准开销(动态量化)
- 想用QLoRA进行微调
- 更喜欢简单集成
快速开始
安装
# 默认(Triton内核)
pip install autoawq
# 带优化CUDA内核 + Flash Attention
pip install autoawq[kernels]
# Intel CPU/XPU优化
pip install autoawq[cpu]
要求: Python 3.8+, CUDA 11.8+, 计算能力7.5+
加载预量化模型
from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer
model_name = "TheBloke/Mistral-7B-Instruct-v0.2-AWQ"
model = AutoAWQForCausalLM.from_quantized(
model_name,
fuse_layers=True # 启用融合注意力以加速
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 生成
inputs = tokenizer("解释量子计算", return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=200)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
量化你自己的模型
from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer
model_path = "mistralai/Mistral-7B-Instruct-v0.2"
# 加载模型和分词器
model = AutoAWQForCausalLM.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
# 量化配置
quant_config = {
"zero_point": True, # 使用零点量化
"q_group_size": 128, # 组大小(推荐128)
"w_bit": 4, # 4位权重
"version": "GEMM" # GEMM用于批处理,GEMV用于单令牌
}
# 量化(默认使用pileval数据集)
model.quantize(tokenizer, quant_config=quant_config)
# 保存
model.save_quantized("mistral-7b-awq")
tokenizer.save_pretrained("mistral-7b-awq")
时间: ~10-15分钟用于7B模型,~1小时用于70B模型。
AWQ vs GPTQ vs bitsandbytes
| 特性 | AWQ | GPTQ | bitsandbytes |
|---|---|---|---|
| 加速(4位) | ~2.5-3x | ~2x | ~1.5x |
| 准确度损失 | <5% | ~5-10% | ~5-15% |
| 校准 | 最小(128-1K令牌) | 更广泛 | 无 |
| 过拟合风险 | 低 | 较高 | 不适用 |
| 最佳用于 | 生产推理 | GPU推理 | 简单集成 |
| vLLM支持 | 原生 | 是 | 有限 |
关键洞察: AWQ假设并非所有权重同等重要。它保护约1%的重要权重,这些权重由激活模式识别,减少量化误差,无需混合精度开销。
内核后端
GEMM(默认,批处理推理)
quant_config = {
"zero_point": True,
"q_group_size": 128,
"w_bit": 4,
"version": "GEMM" # 批处理大小>1时最佳
}
GEMV(单令牌生成)
quant_config = {
"version": "GEMV" # 批处理大小=1时快20%
}
限制: 仅批处理大小1,不适合大上下文。
Marlin(Ampere+ GPU)
from transformers import AwqConfig, AutoModelForCausalLM
config = AwqConfig(
bits=4,
version="marlin" # 在A100/H100上快2倍
)
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/Mistral-7B-AWQ",
quantization_config=config
)
要求: 计算能力8.0+(A100、H100、RTX 40xx)
ExLlamaV2(AMD兼容)
config = AwqConfig(
bits=4,
version="exllama" # 更快预填充,AMD GPU支持
)
HuggingFace Transformers 集成
直接加载
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/zephyr-7B-alpha-AWQ",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("TheBloke/zephyr-7B-alpha-AWQ")
融合模块(推荐)
from transformers import AwqConfig, AutoModelForCausalLM
config = AwqConfig(
bits=4,
fuse_max_seq_len=512, # 融合的最大序列长度
do_fuse=True # 启用融合注意力/MLP
)
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/Mistral-7B-OpenOrca-AWQ",
quantization_config=config
)
注意: 融合模块不能与FlashAttention2结合使用。
vLLM 集成
from vllm import LLM, SamplingParams
# vLLM 自动检测AWQ模型
llm = LLM(
model="TheBloke/Llama-2-7B-AWQ",
quantization="awq",
dtype="half"
)
sampling = SamplingParams(temperature=0.7, max_tokens=200)
outputs = llm.generate(["解释AI"], sampling)
性能基准
内存减少
| 模型 | FP16 | AWQ 4位 | 减少 |
|---|---|---|---|
| Mistral 7B | 14 GB | 5.5 GB | 2.5x |
| Llama 2-13B | 26 GB | 10 GB | 2.6x |
| Llama 2-70B | 140 GB | 35 GB | 4x |
推理速度(RTX 4090)
| 模型 | 预填充(令牌/秒) | 解码(令牌/秒) | 内存 |
|---|---|---|---|
| Mistral 7B GEMM | 3,897 | 114 | 5.55 GB |
| TinyLlama 1B GEMV | 5,179 | 431 | 2.10 GB |
| Llama 2-13B GEMM | 2,279 | 74 | 10.28 GB |
准确度(困惑度)
| 模型 | FP16 | AWQ 4位 | 退化 |
|---|---|---|---|
| Llama 3 8B | 8.20 | 8.48 | +3.4% |
| Mistral 7B | 5.25 | 5.42 | +3.2% |
| Qwen2 72B | 4.85 | 4.95 | +2.1% |
自定义校准数据
# 使用自定义数据集用于领域特定模型
model.quantize(
tokenizer,
quant_config=quant_config,
calib_data="wikitext", # 或自定义字符串列表
max_calib_samples=256, # 更多样本 = 更好准确度
max_calib_seq_len=512 # 序列长度
)
# 或提供你自己的样本
calib_samples = [
"你的领域特定文本这里...",
"来自你使用案例的更多例子...",
]
model.quantize(tokenizer, quant_config=quant_config, calib_data=calib_samples)
多GPU部署
model = AutoAWQForCausalLM.from_quantized(
"TheBloke/Llama-2-70B-AWQ",
device_map="auto", # 自动跨GPU分割
max_memory={0: "40GB", 1: "40GB"}
)
支持模型
35+架构包括:
- Llama家族: Llama 2/3, Code Llama, Mistral, Mixtral
- Qwen: Qwen, Qwen2, Qwen2.5-VL
- 其他: Falcon, MPT, Phi, Yi, DeepSeek, Gemma
- 多模态: LLaVA, LLaVA-Next, Qwen2-VL
常见问题
CUDA OOM在量化期间:
# 减少批处理大小
model.quantize(tokenizer, quant_config=quant_config, max_calib_samples=64)
推理慢:
# 启用融合层
model = AutoAWQForCausalLM.from_quantized(model_name, fuse_layers=True)
AMD GPU支持:
# 使用ExLlama后端
config = AwqConfig(bits=4, version="exllama")
弃用通知
AutoAWQ 已正式弃用。对于新项目,考虑:
- vLLM llm-compressor: https://github.com/vllm-project/llm-compressor
- MLX-LM: 用于具有Apple Silicon的Mac设备
现有量化模型仍可使用。
参考文献
- 论文: AWQ: Activation-aware Weight Quantization (arXiv:2306.00978) - MLSys 2024最佳论文
- GitHub: https://github.com/casper-hansen/AutoAWQ
- MIT Han Lab: https://github.com/mit-han-lab/llm-awq
- 模型: https://huggingface.co/models?library=awq