Skip to main content

Overview

GPTQ (Generalized Post-Training Quantization) is a weight quantization method that compresses model weights to 4-bit or 8-bit integers. Qwen uses AutoGPTQ for GPTQ quantization, achieving near-lossless compression with significant memory savings.

Benefits

Memory Reduction

Int4: ~4x smaller than BF16
Int8: ~2x smaller than BF16

Speed Improvement

Int4: Up to 40% faster
Int8: Similar or slightly slower

Quality Preservation

Less than 2% accuracy drop on most benchmarks

Easy to Use

Load pre-quantized models directly or quantize your own

Using Pre-Quantized Models

Installation

Install the required packages:
AutoGPTQ depends on specific versions of PyTorch and CUDA. See version compatibility below.

Version Compatibility

If you encounter installation issues with auto-gptq, check the official repository for pre-compiled wheels matching your environment.

Loading Quantized Models

Load and use a pre-quantized model:
The API is identical to the full-precision model. Simply change the model name to the Int4 or Int8 variant.

Available Models

All Qwen chat models are available in Int4 and Int8 variants:

Quantizing Your Own Models

Quantize a fine-tuned or custom Qwen model using the provided run_gptq.py script.

Prerequisites

  1. A fine-tuned model (or base model)
  2. Calibration data in JSON format
  3. GPU with sufficient memory
If you fine-tuned with LoRA, merge the adapter weights before quantization. Q-LoRA models are already quantized and do not need this step.

Calibration Data Format

Prepare calibration data in the same format as fine-tuning data:
You can reuse your fine-tuning data or create a representative sample of diverse prompts (100-1000 samples recommended).

Running Quantization

Use the run_gptq.py script from the source repository:

Script Parameters

Quantization requires GPU and may take several hours depending on model size and calibration data. For Qwen-7B with 1000 samples, expect ~2-3 hours on an A100 GPU.

Post-Quantization Steps

After quantization completes:
  1. Copy support files to the output directory:
  2. Update config.json by copying from the corresponding official quantized model:
  3. Rename the checkpoint:

Testing the Quantized Model

Load and test your quantized model:

Quantization Configuration

The run_gptq.py script uses the following GPTQ configuration:
desc_act=False significantly speeds up inference with minimal perplexity increase. Keep this setting unless accuracy is critical.

Performance Impact

GPTQ quantization maintains excellent accuracy:

Benchmark Comparison (Qwen-7B-Chat)

Key Findings:
  • Int8 preserves 99% of BF16 quality with 34% memory reduction
  • Int4 achieves 52% memory reduction with minimal quality loss on most tasks
  • HumanEval (code generation) sees larger degradation with Int4

Speed Considerations

Known Issue: Models loaded via AutoModelForCausalLM.from_pretrained run ~20% slower than models loaded directly through AutoGPTQ. This is a known issue reported to the HuggingFace team.

Troubleshooting

Solution: Check your auto-gptq version and ensure compatibility with your PyTorch version:
Refer to the version compatibility table above.
Solution: Reduce calibration data size or use a GPU with more memory:
Quantization requires more memory than inference.
Solution: Ensure you completed all post-quantization steps:
  1. Copied all .py, .cu, .cpp files
  2. Updated config.json from official model
  3. Renamed gptq.safetensors to model.safetensors
Solution: This is a known issue with models loaded via Transformers. The model should still be faster than BF16 in most cases. For optimal speed, consider using vLLM for deployment.

Best Practices

1

Choose the right precision

  • Int8 for production systems requiring high accuracy
  • Int4 for memory-constrained environments or experimentation
2

Use diverse calibration data

Include samples representative of your use case (100-1000 samples)
3

Validate after quantization

Test the quantized model on your evaluation set before deployment
4

Consider use case

Code generation tasks may see larger quality drops with Int4

Next Steps

KV Cache Quantization

Further reduce memory usage with KV cache quantization

Performance Benchmarks

Detailed performance analysis and optimization tips

Fine-tuning

Fine-tune Qwen models before quantization

Deployment

Deploy quantized models in production