awq-quantization について
AWQで言語モデルを量子化するための開発者向けガイドです。活性化パターンを利用して4-bit量子化時に重要な重みを保護し、モデルの読み込み、キャリブレーション、カーネル選択、サービング連携を解説します。AutoAWQは非推奨ですが、既存の量子化モデルは引き続き利用できます。
主な機能
- 014-bit AWQ量子化とモデル読み込み
- 02カスタムキャリブレーションデータ
- 03GEMM、GEMV、Marlinなどのカーネル選択
- 04HuggingFace TransformersとvLLMの連携
- 05マルチGPUデプロイの説明
活用シーン
- 01モデル推論のメモリ使用量削減
- 02本番推論向けチャットモデルの量子化
- 03vLLMによる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ビット量子化で、精度低下を最小限に抑えながら3倍の高速化を実現します。
AWQ を使うべき場合
AWQ を使うべき場合:
- 精度低下が5%未満の4ビット量子化が必要な場合
- 指示チューニング済みモデルやチャットモデルをデプロイする場合(AWQ はより良く汎化します)
- FP16 と比べて約2.5〜3倍の推論高速化が欲しい場合
- 本番環境での提供に vLLM を使用する場合
- Marlin カーネルをサポートする Ampere 以降の GPU(A100、H100、RTX 40xx)を持っている場合
代わりに 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+、Compute Capability 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トークン) | より大規模 | なし |
| 過学習のリスク | 低 | 高い | 該当なし |
| 最適な用途 | 本番推論 | 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(単一トークン生成)
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
)
要件: Compute Capability 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)
| モデル | プリフィル(tok/s) | デコード(tok/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 Best Paper
- GitHub: https://github.com/casper-hansen/AutoAWQ
- MIT Han Lab: https://github.com/mit-han-lab/llm-awq
- モデル: https://huggingface.co/models?library=awq