mcpField
A
awq-quantization

awq-quantization

github.com/orchestra-research开发14.1k966 次安装19 周前更新
代码仓库

关于 awq-quantization

面向开发者的 AWQ 语言模型量化指南,通过利用激活模式,在 4-bit 量化时保留重要权重。内容介绍模型加载、校准、内核选择和服务集成;AutoAWQ 已弃用,但现有量化模型仍可使用。

主要功能

  1. 014-bit AWQ 量化与模型加载
  2. 02自定义校准数据
  3. 03GEMM、GEMV、Marlin 等内核选项
  4. 04HuggingFace Transformers 与 vLLM 集成
  5. 05多 GPU 部署说明

适用场景

  1. 01降低模型推理的显存占用
  2. 02为生产推理量化聊天模型
  3. 03使用 vLLM 服务 AWQ 模型

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/orchestra-research/ai-research-skills --skill awq-quantization

安装后在 Claude Code 中输入/awq-quantization确认已加载。

附带文件references/advanced-usage.mdreferences/troubleshooting.md

详细介绍

AWQ(Activation-aware Weight Quantization)

一种 4-bit 量化方法,根据激活模式保留显著权重,在准确率损失极小的情况下实现 3 倍加速。

何时使用 AWQ

适合使用 AWQ 的情况:

  • 需要准确率损失低于 5% 的 4-bit 量化
  • 部署经过指令微调或聊天模型(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 对比

特性AWQGPTQbitsandbytes
加速比(4-bit)~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)

性能基准

内存缩减
模型FP16AWQ 4-bit缩减比
Mistral 7B14 GB5.5 GB2.5 倍
Llama 2-13B26 GB10 GB2.6 倍
Llama 2-70B140 GB35 GB4 倍
推理速度(RTX 4090)
模型Prefill(tok/s)Decode(tok/s)内存
Mistral 7B GEMM3,8971145.55 GB
TinyLlama 1B GEMV5,1794312.10 GB
Llama 2-13B GEMM2,2797410.28 GB
准确率(困惑度)
模型FP16AWQ 4-bit性能下降
Llama 3 8B8.208.48+3.4%
Mistral 7B5.255.42+3.2%
Qwen2 72B4.854.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 已正式弃用。新项目可考虑:

现有的量化模型仍可继续使用。

参考资料

评论

支持 Markdown