AutoAWQ Documentation

repository·main·Indexed 25 days ago

https://github.com/casper-hansen/autoawq

A package for 4-bit quantized Large Language Models (LLMs) using the Activation-aware Weight Quantization (AWQ) algorithm. AutoAWQ reduces memory requirements and increases speed by up to 3x compared to FP16 models. It supports NVIDIA GPUs (Compute Capability 7.5+), AMD GPUs via ROCm, and Intel CPUs/XPUs. Key features include fused modules for inference speedup, support for vision-language models like LLaVA, and options for GEMM and GEMV quantization.

Tokens
6.7K
Snippets
16
Records
24
Agent score
79%

What's inside AutoAWQ

  1. How fused modules work in AutoAWQ

    main

    Fused modules combine multiple layers into a single operation to increase efficiency. They are activated by setting fuse_layers=True during AutoAWQForCausalLM.from_quantized.

    Important Constraints when using Fused Modules:

    • Linux Only: The primary accelerator (FasterTransformer) is only compatible with Linux.
    • Fixed Cache: A custom cache is used that preallocates based on batch_size and max_seq_len. You cannot change the sequence length after the model is created.
    • Initialization: Use AutoAWQForCausalLM.from_quantized(max_seq_len=seq_len, batch_size=batch_size) to set these parameters.
    • Dummy Values: The past_key_values returned by model.generate() are dummy values and cannot be used for subsequent generation steps.
  2. Implement a Custom Quantizer

    main

    For specialized models (like Qwen2-VL or MiniCPM3), you can extend the AwqQuantizer class to define custom logic for initialization or weight clipping.

    When using a custom quantizer, pass the class instance to the quantizer_cls argument in the .quantize() method. This allows you to override how calibration data is processed or how the best clipping values are computed.

  3. Choosing between GEMM and GEMV quantization

    main

    AutoAWQ supports two versions of quantization based on how matrix multiplication is executed:

    • GEMV (quantized): Best for batch size 1. It is approximately 20% faster than GEMM but is not suitable for large contexts.
    • GEMM (quantized): Best for batch sizes below 8 and large contexts. It is much faster than FP16 in these scenarios.

    Note on Throughput: If you require the highest possible throughput, it is recommended to use vLLM with non-quantized FP16 models.

  4. Prerequisites for AutoAWQ

    main

    Before installing, ensure your hardware meets these requirements:

    • NVIDIA GPUs: Must be Compute Capability 7.5 or later (Turing architecture and newer). CUDA version must be 11.8 or later.
    • AMD GPUs: ROCm version must be compatible with Triton.
    • Intel CPU/GPU:
      • For optimized performance, use torch and intel_extension_for_pytorch version 2.4 or later.
      • Alternatively, use Triton kernels by installing intel-xpu-backend-for-triton along with compatible torch and transformers versions.
  5. Perform Basic AWQ Quantization

    main

    AWQ performs zero-point quantization down to 4-bit precision. You can specify other bit rates (e.g., 3-bit), but kernels for inference may be limited.

    Important Compatibility Notes:

    • Falcon models: Only compatible with q_group_size: 64.
    • Marlin version: To use the Marlin backend, you must set zero_point: False and version: "Marlin" in your configuration.

    To perform basic quantization, use AutoAWQForCausalLM.from_pretrained() to load the model, call .quantize() with a configuration dictionary, and then .save_quantized() to export the results.

    from awq import AutoAWQForCausalLM
    from transformers import AutoTokenizer
    
    model_path = 'mistralai/Mistral-7B-Instruct-v0.2'
    quant_path = 'mistral-instruct-v0.2-awq'
    quant_config = { "zero_point": True, "q_group_size": 128, "w_bit": 4, "version": "GEMM" }
    
    # Load model
    model = AutoAWQForCausalLM.from_pretrained(model_path)
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    
    # Quantize
    model.quantize(tokenizer, quant_config=quant_config)
    
    # Save quantized model
    model.save_quantized(quant_path)
    tokenizer.save_pretrained(quant_path)
  6. Export AWQ Weights to GGUF Format

    main

    To use AWQ-scaled weights in other frameworks like llama.cpp, follow these steps:

    1. Quantize with compatibility mode: Call .quantize() with export_compatible=True. This computes AWQ scales and applies them to the weights but skips the actual weight packing, resulting in an FP16 model with AWQ scales applied.
    2. Save the model: Use .save_quantized() to save the FP16 model.
    3. Convert to GGUF: Use llama.cpp's convert.py to convert the HuggingFace FP16 weights to GGUF FP16.
    4. GGUF Quantization: Use llama.cpp's quantize tool to perform the final quantization (e.g., 4-bit) on the GGUF file.
  7. Optimize Quantization for Long-Context Models

    main

    To avoid Out-of-Memory (OOM) errors when quantizing models with long contexts or large datasets, adjust the following parameters in the .quantize() method:

    • n_parallel_calib_samples: When set to an integer, samples are offloaded to system RAM to save GPU VRAM. This helps prevent OOM but requires sufficient system memory.
    • max_calib_samples: Limits the number of calibration samples used. For AWQ, 128-256 samples are typically sufficient.
    • max_calib_seq_len: Limits the sequence length of the calibration samples.
    model.quantize(
        tokenizer,
        quant_config=quant_config,
        calib_data=load_cosmopedia(),
        n_parallel_calib_samples=32,
        max_calib_samples=128,
        max_calib_seq_len=4096
    )
  8. Run inference on CPU using IPEX

    main

    To run inference on a CPU, you should install intel_extension_for_pytorch (IPEX).

    Requirements:

    • Install IPEX: pip install intel_extension_for_pytorch.
    • Ensure your torch version matches the version IPEX was built with (e.g., IPEX 2.4 requires torch 2.4).
    • If building IPEX from source, ensure torch version consistency.

    When loading the model, set use_ipex=True.

    model = AutoAWQForCausalLM.from_quantized(
        ...,
        use_ipex=True
    )
  9. Run performance benchmarks with AutoAWQ

    main

    You can benchmark the speed (prefill and decoding) and memory usage of models using the provided benchmark.py script.

    GPU Benchmarking To benchmark on a GPU, use the following command: python examples/benchmark.py --model_path <hf_model> --batch_size 1

    CPU Benchmarking To benchmark on a CPU using the Hugging Face generator, use: python examples/benchmark.py --model_path <hf_model> --batch_size 1 --generator hf

    Note that performance is highly dependent on GPU memory bandwidth and CPU single-core clock speed.

  10. Install AutoAWQ

    main

    AutoAWQ can be installed via PyPI using different options depending on your hardware and performance requirements:

    1. Default: Uses Triton for inference and includes no external kernels.
      pip install autoawq
    2. With Kernels: Installs AutoAWQ_kernels. Requires matching the latest Torch version used during the kernel build.
      pip install autoawq[kernels]
    3. Intel CPU/XPU Optimized: Requires at least torch 2.4.0.
      pip install autoawq[cpu]
    pip install autoawq
  11. Run inference on AMD GPUs

    main

    For AMD GPUs, inference runs through ExLlamaV2 kernels without fused layers. When loading a quantized model using AutoAWQForCausalLM.from_quantized, you must explicitly disable fused layers and enable ExLlamaV2.

    model = AutoAWQForCausalLM.from_quantized(
        ...,
        fuse_layers=False,
        use_exllama_v2=True
    )