关于 awq-quantization
AWQ 是用于 LLM 4-bit 量化的技能,面向需要降低模型内存占用并进行推理部署的开发者。它依据激活模式保留重要权重,并支持多种内核后端及 Transformers、vLLM 集成;AutoAWQ 已弃用。
主要功能
- 01基于激活模式保护重要权重
- 02支持 4-bit 模型量化与加载
- 03提供 GEMM、GEMV、Marlin 等后端
- 04集成 Transformers 和 vLLM
- 05支持自定义校准数据与多 GPU 部署
适用场景
- 01量化模型以降低推理内存占用
- 02用 vLLM 部署 AWQ 模型
- 03为特定领域模型设置校准数据
- 04在多 GPU 上加载大型量化模型
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ kalarislabs/ research-agent-skills --skill awq-quantization安装后在 Claude Code 中输入/awq-quantization确认已加载。
附带文件references/advanced-usage.mdreferences/troubleshooting.md
详细介绍
AWQ(激活感知权重量化)
基于激活模式保留显著权重的 4 位量化技术,可在精度损失极小的情况下实现 3 倍加速。
何时使用 AWQ
以下情况适合使用 AWQ:
- 需要精度损失 <5% 的 4 位量化
- 部署经过指令微调或聊天模型(AWQ 的泛化能力更强)
- 希望相比 FP16 获得约 2.5-3 倍的推理加速
- 使用 vLLM 进行生产环境服务
- 使用 Ampere 及更新架构的 GPU(A100、H100、RTX 40xx),以支持 Marlin 内核
以下情况适合改用 GPTQ:
- 需要最大的生态兼容性(支持 GPTQ 的工具更多)
- 专门使用 ExLlamaV2 后端
- 使用不支持 Marlin 的旧款 GPU
以下情况适合改用 bitsandbytes:
- 需要零校准开销(即时量化)
- 希望使用 QLoRA 进行微调
- 更倾向于简单的集成方式
快速开始
安装
# Default (Triton kernels)
pip install autoawq
# With optimized CUDA kernels + Flash Attention
pip install autoawq[kernels]
# Intel CPU/XPU optimization
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 # Enable fused attention for speed
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Generate
inputs = tokenizer("Explain quantum computing", 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"
# Load model and tokenizer
model = AutoAWQForCausalLM.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
# Quantization config
quant_config = {
"zero_point": True, # Use zero-point quantization
"q_group_size": 128, # Group size (128 recommended)
"w_bit": 4, # 4-bit weights
"version": "GEMM" # GEMM for batch, GEMV for single-token
}
# Quantize (uses pileval dataset by default)
model.quantize(tokenizer, quant_config=quant_config)
# Save
model.save_quantized("mistral-7b-awq")
tokenizer.save_pretrained("mistral-7b-awq")
耗时:7B 模型约需 10-15 分钟,70B 模型约需 1 小时。
AWQ、GPTQ 与 bitsandbytes 对比
| 特性 | AWQ | GPTQ | bitsandbytes |
|---|---|---|---|
| 加速比(4 位) | 约 2.5-3 倍 | 约 2 倍 | 约 1.5 倍 |
| 精度损失 | <5% | 约 5-10% | 约 5-15% |
| 校准 | 极少(128-1K 个 token) | 更多 | 无 |
| 过拟合风险 | 低 | 较高 | 不适用 |
| 最适合 | 生产环境推理 | GPU 推理 | 简单集成 |
| vLLM 支持 | 原生支持 | 支持 | 有限 |
关键要点:AWQ 假设并非所有权重都同等重要。它会保护约 1% 根据激活模式识别出的显著权重,在不增加混合精度开销的情况下减少量化误差。
内核后端
GEMM(默认,批量推理)
quant_config = {
"zero_point": True,
"q_group_size": 128,
"w_bit": 4,
"version": "GEMM" # Best for batch sizes > 1
}
GEMV(单 token 生成)
quant_config = {
"version": "GEMV" # 20% faster for batch_size=1
}
限制:仅支持批量大小为 1,不适用于较大的上下文。
Marlin(Ampere 及更新架构的 GPU)
from transformers import AwqConfig, AutoModelForCausalLM
config = AwqConfig(
bits=4,
version="marlin" # 2x faster on A100/H100
)
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" # Faster prefill, AMD GPU support
)
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, # Max sequence length for fusing
do_fuse=True # Enable fused attention/MLP
)
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/Mistral-7B-OpenOrca-AWQ",
quantization_config=config
)
注意:融合模块无法与 FlashAttention2 一起使用。
vLLM 集成
from vllm import LLM, SamplingParams
# vLLM auto-detects AWQ models
llm = LLM(
model="TheBloke/Llama-2-7B-AWQ",
quantization="awq",
dtype="half"
)
sampling = SamplingParams(temperature=0.7, max_tokens=200)
outputs = llm.generate(["Explain AI"], sampling)
性能基准
内存缩减
| 模型 | FP16 | AWQ 4 位 | 缩减比 |
|---|---|---|---|
| Mistral 7B | 14 GB | 5.5 GB | 2.5 倍 |
| Llama 2-13B | 26 GB | 10 GB | 2.6 倍 |
| Llama 2-70B | 140 GB | 35 GB | 4 倍 |
推理速度(RTX 4090)
| 模型 | 预填充(token/s) | 解码(token/s) | 内存 |
|---|---|---|---|
| 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% |
自定义校准数据
# Use custom dataset for domain-specific models
model.quantize(
tokenizer,
quant_config=quant_config,
calib_data="wikitext", # Or custom list of strings
max_calib_samples=256, # More samples = better accuracy
max_calib_seq_len=512 # Sequence length
)
# Or provide your own samples
calib_samples = [
"Your domain-specific text here...",
"More examples from your use case...",
]
model.quantize(tokenizer, quant_config=quant_config, calib_data=calib_samples)
多 GPU 部署
model = AutoAWQForCausalLM.from_quantized(
"TheBloke/Llama-2-70B-AWQ",
device_map="auto", # Auto-split across GPUs
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):
# Reduce batch size
model.quantize(tokenizer, quant_config=quant_config, max_calib_samples=64)
推理速度慢:
# Enable fused layers
model = AutoAWQForCausalLM.from_quantized(model_name, fuse_layers=True)
AMD GPU 支持:
# Use ExLlama backend
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
Agent 操作流程
- 检查环境。 检查 GPU 类型、内存、驱动程序/CUDA 版本(
nvidia-smi)、框架版本,以及数据集的位置和大小。 - 明确输入。 根据数据或用户确认格式、标识符和参数。对于任何会影响结果的值,不要猜测,应先询问。
- 先运行小规模版本。 进行冒烟测试:使用小型模型或数据子集,运行少量步骤,并确认损失下降且检查点已保存。
- 按照上述说明和参考资料执行完整任务。
- 验证结果。 在留出数据上跟踪指标,与基线进行比较,并记录随机种子、配置和硬件。
- 报告。 说明运行了什么(版本、命令、参数)、检查了什么,以及仍有哪些不确定之处。
| 如果发生这种情况 | 请这样做 |
|---|---|
| CUDA 显存不足 | 减小批次大小,启用梯度累积/检查点或混合精度,或者对模型进行分片。 |
| 损失为 NaN 或发散 | 降低学习率,检查数据是否包含无效值,并启用梯度裁剪。 |
| 这些说明中提到的函数、标志或端点在已安装版本中不存在 | 查看已安装版本自身的文档(help()、--help、官方文档),调整用法并告知用户。绝不编造 API。 |
| 必需的输入、标识符或参数含糊不清 | 询问用户,或在运行前明确说明所作的假设。 |
诚信准则
- 绝不捏造结果、参数、标识符、引用或统计数据。如果无法运行或验证某项内容,请直言说明。
- 没有日志时,绝不声称取得了训练结果;启动大型任务前,先估算计算成本。
- 此处与特定版本相关的细节可能已经过时:请根据已安装版本的官方文档进行确认。
- 对于会产生费用、占用共享 GPU 或云配额、涉及个人或患者数据,或无法撤销的操作,必须先征得用户同意。
相关技能
quantizing-models-bitsandbytes:将 LLM 量化为 8 位或 4 位,在精度损失极小的情况下减少 50-75% 的内存占用。gptq:使用 AutoGPTQ 和 transformers,通过分组量化(默认组大小为 128)将 LLM 量化为 4 位(也支持 3 位),采用 GPTQ 方法。serving-llms-vllm:使用 vLLM 的 PagedAttention 和连续批处理,以高吞吐量提供 LLM 服务。
评论
支持 Markdown