{"id":"awq","name":"awq-quantization","summary":"4ビットLLM圧縮のためのアクティベーション認識型ウェイト量子化で、3倍の高速化と最小限の精度損失を実現。","body":"# AWQ (Activation-aware Weight Quantization)\n\n4-bit quantization that preserves salient weights based on activation patterns, achieving 3x speedup with minimal accuracy loss.\n\n## When to use AWQ\n\n**Use AWQ when:**\n- Need 4-bit quantization with <5% accuracy loss\n- Deploying instruction-tuned or chat models (AWQ generalizes better)\n- Want ~2.5-3x inference speedup over FP16\n- Using vLLM for production serving\n- Have Ampere+ GPUs (A100, H100, RTX 40xx) for Marlin kernel support\n\n**Use GPTQ instead when:**\n- Need maximum ecosystem compatibility (more tools support GPTQ)\n- Working with ExLlamaV2 backend specifically\n- Have older GPUs without Marlin support\n\n**Use bitsandbytes instead when:**\n- Need zero calibration overhead (quantize on-the-fly)\n- Want to fine-tune with QLoRA\n- Prefer simpler integration\n\n## Quick start\n\n### Installation\n\n```bash\n# Default (Triton kernels)\npip install autoawq\n\n# With optimized CUDA kernels + Flash Attention\npip install autoawq[kernels]\n\n# Intel CPU/XPU optimization\npip install autoawq[cpu]\n```\n\n**Requirements**: Python 3.8+, CUDA 11.8+, Compute Capability 7.5+\n\n### Load pre-quantized model\n\n```python\nfrom awq import AutoAWQForCausalLM\nfrom transformers import AutoTokenizer\n\nmodel_name = \"TheBloke/Mistral-7B-Instruct-v0.2-AWQ\"\n\nmodel = AutoAWQForCausalLM.from_quantized(\n    model_name,\n    fuse_layers=True  # Enable fused attention for speed\n)\ntokenizer = AutoTokenizer.from_pretrained(model_name)\n\n# Generate\ninputs = tokenizer(\"Explain quantum computing\", return_tensors=\"pt\").to(\"cuda\")\noutputs = model.generate(**inputs, max_new_tokens=200)\nprint(tokenizer.decode(outputs[0], skip_special_tokens=True))\n```\n\n### Quantize your own model\n\n```python\nfrom awq import AutoAWQForCausalLM\nfrom transformers import AutoTokenizer\n\nmodel_path = \"mistralai/Mistral-7B-Instruct-v0.2\"\n\n# Load model and tokenizer\nmodel = AutoAWQForCausalLM.from_pretrained(model_path)\ntokenizer = AutoTokenizer.from_pretrained(model_path)\n\n# Quantization config\nquant_config = {\n    \"zero_point\": True,      # Use zero-point quantization\n    \"q_group_size\": 128,     # Group size (128 recommended)\n    \"w_bit\": 4,              # 4-bit weights\n    \"version\": \"GEMM\"        # GEMM for batch, GEMV for single-token\n}\n\n# Quantize (uses pileval dataset by default)\nmodel.quantize(tokenizer, quant_config=quant_config)\n\n# Save\nmodel.save_quantized(\"mistral-7b-awq\")\ntokenizer.save_pretrained(\"mistral-7b-awq\")\n```\n\n**Timing**: ~10-15 min for 7B, ~1 hour for 70B models.\n\n## AWQ vs GPTQ vs bitsandbytes\n\n| Feature | AWQ | GPTQ | bitsandbytes |\n|---------|-----|------|--------------|\n| **Speedup (4-bit)** | ~2.5-3x | ~2x | ~1.5x |\n| **Accuracy loss** | <5% | ~5-10% | ~5-15% |\n| **Calibration** | Minimal (128-1K tokens) | More extensive | None |\n| **Overfitting risk** | Low | Higher | N/A |\n| **Best for** | Production inference | GPU inference | Easy integration |\n| **vLLM support** | Native | Yes | Limited |\n\n**Key insight**: AWQ assumes not all weights are equally important. It protects ~1% of salient weights identified by activation patterns, reducing quantization error without mixed-precision overhead.\n\n## Kernel backends\n\n### GEMM (default, batch inference)\n\n```python\nquant_config = {\n    \"zero_point\": True,\n    \"q_group_size\": 128,\n    \"w_bit\": 4,\n    \"version\": \"GEMM\"  # Best for batch sizes > 1\n}\n```\n\n### GEMV (single-token generation)\n\n```python\nquant_config = {\n    \"version\": \"GEMV\"  # 20% faster for batch_size=1\n}\n```\n\n**Limitation**: Only batch size 1, not good for large context.\n\n### Marlin (Ampere+ GPUs)\n\n```python\nfrom transformers import AwqConfig, AutoModelForCausalLM\n\nconfig = AwqConfig(\n    bits=4,\n    version=\"marlin\"  # 2x faster on A100/H100\n)\n\nmodel = AutoModelForCausalLM.from_pretrained(\n    \"TheBloke/Mistral-7B-AWQ\",\n    quantization_config=config\n)\n```\n\n**Requirements**: Compute Capability 8.0+ (A100, H100, RTX 40xx)\n\n### ExLlamaV2 (AMD compatible)\n\n```python\nconfig = AwqConfig(\n    bits=4,\n    version=\"exllama\"  # Faster prefill, AMD GPU support\n)\n```\n\n## HuggingFace Transformers integration\n\n### Direct loading\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\nmodel = AutoModelForCausalLM.from_pretrained(\n    \"TheBloke/zephyr-7B-alpha-AWQ\",\n    device_map=\"auto\"\n)\ntokenizer = AutoTokenizer.from_pretrained(\"TheBloke/zephyr-7B-alpha-AWQ\")\n```\n\n### Fused modules (recommended)\n\n```python\nfrom transformers import AwqConfig, AutoModelForCausalLM\n\nconfig = AwqConfig(\n    bits=4,\n    fuse_max_seq_len=512,  # Max sequence length for fusing\n    do_fuse=True           # Enable fused attention/MLP\n)\n\nmodel = AutoModelForCausalLM.from_pretrained(\n    \"TheBloke/Mistral-7B-OpenOrca-AWQ\",\n    quantization_config=config\n)\n```\n\n**Note**: Fused modules cannot combine with FlashAttention2.\n\n## vLLM integration\n\n```python\nfrom vllm import LLM, SamplingParams\n\n# vLLM auto-detects AWQ models\nllm = LLM(\n    model=\"TheBloke/Llama-2-7B-AWQ\",\n    quantization=\"awq\",\n    dtype=\"half\"\n)\n\nsampling = SamplingParams(temperature=0.7, max_tokens=200)\noutputs = llm.generate([\"Explain AI\"], sampling)\n```\n\n## Performance benchmarks\n\n### Memory reduction\n\n| Model | FP16 | AWQ 4-bit | Reduction |\n|-------|------|-----------|-----------|\n| Mistral 7B | 14 GB | 5.5 GB | 2.5x |\n| Llama 2-13B | 26 GB | 10 GB | 2.6x |\n| Llama 2-70B | 140 GB | 35 GB | 4x |\n\n### Inference speed (RTX 4090)\n\n| Model | Prefill (tok/s) | Decode (tok/s) | Memory |\n|-------|-----------------|----------------|--------|\n| Mistral 7B GEMM | 3,897 | 114 | 5.55 GB |\n| TinyLlama 1B GEMV | 5,179 | 431 | 2.10 GB |\n| Llama 2-13B GEMM | 2,279 | 74 | 10.28 GB |\n\n### Accuracy (perplexity)\n\n| Model | FP16 | AWQ 4-bit | Degradation |\n|-------|------|-----------|-------------|\n| Llama 3 8B | 8.20 | 8.48 | +3.4% |\n| Mistral 7B | 5.25 | 5.42 | +3.2% |\n| Qwen2 72B | 4.85 | 4.95 | +2.1% |\n\n## Custom calibration data\n\n```python\n# Use custom dataset for domain-specific models\nmodel.quantize(\n    tokenizer,\n    quant_config=quant_config,\n    calib_data=\"wikitext\",       # Or custom list of strings\n    max_calib_samples=256,       # More samples = better accuracy\n    max_calib_seq_len=512        # Sequence length\n)\n\n# Or provide your own samples\ncalib_samples = [\n    \"Your domain-specific text here...\",\n    \"More examples from your use case...\",\n]\nmodel.quantize(tokenizer, quant_config=quant_config, calib_data=calib_samples)\n```\n\n## Multi-GPU deployment\n\n```python\nmodel = AutoAWQForCausalLM.from_quantized(\n    \"TheBloke/Llama-2-70B-AWQ\",\n    device_map=\"auto\",  # Auto-split across GPUs\n    max_memory={0: \"40GB\", 1: \"40GB\"}\n)\n```\n\n## Supported models\n\n35+ architectures including:\n- **Llama family**: Llama 2/3, Code Llama, Mistral, Mixtral\n- **Qwen**: Qwen, Qwen2, Qwen2.5-VL\n- **Others**: Falcon, MPT, Phi, Yi, DeepSeek, Gemma\n- **Multimodal**: LLaVA, LLaVA-Next, Qwen2-VL\n\n## Common issues\n\n**CUDA OOM during quantization**:\n```python\n# Reduce batch size\nmodel.quantize(tokenizer, quant_config=quant_config, max_calib_samples=64)\n```\n\n**Slow inference**:\n```python\n# Enable fused layers\nmodel = AutoAWQForCausalLM.from_quantized(model_name, fuse_layers=True)\n```\n\n**AMD GPU support**:\n```python\n# Use ExLlama backend\nconfig = AwqConfig(bits=4, version=\"exllama\")\n```\n\n## Deprecation notice\n\nAutoAWQ is officially deprecated. For new projects, consider:\n- **vLLM llm-compressor**: https://github.com/vllm-project/llm-compressor\n- **MLX-LM**: For Mac devices with Apple Silicon\n\nExisting quantized models remain usable.\n\n## References\n\n- **Paper**: AWQ: Activation-aware Weight Quantization (arXiv:2306.00978) - MLSys 2024 Best Paper\n- **GitHub**: https://github.com/casper-hansen/AutoAWQ\n- **MIT Han Lab**: https://github.com/mit-han-lab/llm-awq\n- **Models**: https://huggingface.co/models?library=awq","author":"@Orchestra-Research","ownerProfile":null,"authorContacts":null,"sourceUrl":"https://github.com/Orchestra-Research/AI-Research-SKILLs/tree/main/10-optimization/awq","license":"MIT","category":"coding","lang":"en","tokens":2318,"stars":0,"calls30d":1,"claimed":false,"visibility":"public","origin":"crawler","version":"0.1.0","createdAt":"2026-08-22","updatedAt":"2026-08-22","files":[{"path":"references/advanced-usage.md","size":7983,"sha256":"0630d5b6c3f5c8a5fcd38898f305b5b732ac8cc63580023cebcd067a698db4a7"},{"path":"references/troubleshooting.md","size":7733,"sha256":"14187391e2025b0d428977f33caaea7ccf5388aee6f879309ef49f5fc95a6d5d"}],"requires":{"mcp":[],"tools":[]},"safety":{"flags":[],"scannedAt":"2026-08-22","hasScripts":false,"networkEndpoints":["discuss.huggingface.co","download.pytorch.org","huggingface.co"]}}