Skip to content

artubss/SKILLS-CLAUDE-CODE / skills/skills/ai-research/optimization-awq

awq-quantization

Verified

Quantização de pesos consciente da ativação para compressão de LLM de 4-bit com aceleração 3x e perda mínima de acurácia. Use ao implantar modelos grandes (7B-70B) em memória GPU limitada, quando precisa de inferência mais rápida que GPTQ com melhor preservação de acurácia, ou para modelos com instruções ajustadas e multimodais. Vencedor do Prêmio Melhor Artigo MLSys 2024.

AWQ (Activation-aware Weight Quantization)

Quantização de 4-bit que preserva pesos salientes com base em padrões de ativação, alcançando aceleração 3x com perda mínima de acurácia.

Quando usar AWQ

Use AWQ quando:

  • Precisar de quantização de 4-bit com <5% de perda de acurácia
  • Implantando modelos com instruções ajustadas ou chat (AWQ generaliza melhor)
  • Quiser aceleração de ~2,5-3x na inferência em relação a FP16
  • Usando vLLM para serving em produção
  • Tiver GPUs Ampere+ (A100, H100, RTX 40xx) para suporte a kernel Marlin

Use GPTQ em vez disso quando:

  • Precisar de compatibilidade máxima de ecossistema (mais ferramentas suportam GPTQ)
  • Trabalhando especificamente com backend ExLlamaV2
  • Tiver GPUs mais antigas sem suporte a Marlin

Use bitsandbytes em vez disso quando:

  • Precisar de zero overhead de calibração (quantizar na hora)
  • Quiser ajustar com QLoRA
  • Preferir integração mais simples

Início rápido

Instalação

# Padrão (kernels Triton)
pip install autoawq

# Com kernels CUDA otimizados + Flash Attention
pip install autoawq[kernels]

# Otimização Intel CPU/XPU
pip install autoawq[cpu]

Requisitos: Python 3.8+, CUDA 11.8+, Compute Capability 7.5+

Carregar modelo pré-quantizado

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))

Quantizar seu próprio modelo

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")

Tempo: ~10-15 min para modelos 7B, ~1 hora para modelos 70B.

AWQ vs GPTQ vs bitsandbytes

RecursoAWQGPTQbitsandbytes
Aceleração (4-bit)~2,5-3x~2x~1,5x
Perda de acurácia<5%~5-10%~5-15%
CalibraçãoMínima (128-1K tokens)Mais extensaNenhuma
Risco de overfittingBaixoMais altoN/A
Melhor paraInferência em produçãoInferência GPUIntegração fácil
Suporte vLLMNativoSimLimitado

Insight principal: AWQ assume que nem todos os pesos são igualmente importantes. Ele protege ~1% de pesos salientes identificados por padrões de ativação, reduzindo erro de quantização sem overhead de precisão mista.

Backends de kernel

GEMM (padrão, inferência em lote)

quant_config = {
    "zero_point": True,
    "q_group_size": 128,
    "w_bit": 4,
    "version": "GEMM"  # Best for batch sizes > 1
}

GEMV (geração de token único)

quant_config = {
    "version": "GEMV"  # 20% faster for batch_size=1
}

Limitação: Apenas tamanho de lote 1, não é bom para contexto grande.

Marlin (GPUs Ampere+)

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
)

Requisitos: Compute Capability 8.0+ (A100, H100, RTX 40xx)

ExLlamaV2 (compatível com AMD)

config = AwqConfig(
    bits=4,
    version="exllama"  # Faster prefill, AMD GPU support
)

Integração HuggingFace Transformers

Carregamento direto

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")

Módulos fundidos (recomendado)

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
)

Nota: Módulos fundidos não podem ser combinados com FlashAttention2.

Integração 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)

Benchmarks de desempenho

Redução de memória

ModeloFP16AWQ 4-bitRedução
Mistral 7B14 GB5,5 GB2,5x
Llama 2-13B26 GB10 GB2,6x
Llama 2-70B140 GB35 GB4x

Velocidade de inferência (RTX 4090)

ModeloPrefill (tok/s)Decode (tok/s)Memória
Mistral 7B GEMM3.8971145,55 GB
TinyLlama 1B GEMV5.1794312,10 GB
Llama 2-13B GEMM2.2797410,28 GB

Acurácia (perplexity)

ModeloFP16AWQ 4-bitDegradação
Llama 3 8B8,208,48+3,4%
Mistral 7B5,255,42+3,2%
Qwen2 72B4,854,95+2,1%

Calibração personalizada

# 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)

Implantação multi-GPU

model = AutoAWQForCausalLM.from_quantized(
    "TheBloke/Llama-2-70B-AWQ",
    device_map="auto",  # Auto-split across GPUs
    max_memory={0: "40GB", 1: "40GB"}
)

Modelos suportados

35+ arquiteturas incluindo:

  • Família Llama: Llama 2/3, Code Llama, Mistral, Mixtral
  • Qwen: Qwen, Qwen2, Qwen2.5-VL
  • Outros: Falcon, MPT, Phi, Yi, DeepSeek, Gemma
  • Multimodal: LLaVA, LLaVA-Next, Qwen2-VL

Problemas comuns

CUDA OOM durante quantização:

# Reduce batch size
model.quantize(tokenizer, quant_config=quant_config, max_calib_samples=64)

Inferência lenta:

# Enable fused layers
model = AutoAWQForCausalLM.from_quantized(model_name, fuse_layers=True)

Suporte GPU AMD:

# Use ExLlama backend
config = AwqConfig(bits=4, version="exllama")

Aviso de descontinuação

AutoAWQ está oficialmente descontinuado. Para novos projetos, considere:

Modelos quantizados existentes continuam utilizáveis.

Referências

slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 196kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 192kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 192kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 192kUpdated Claude CodeCodexVerifiedInstall