New Tool Radar

AI Concepts

How to Quantize a Model to 4-Bit GPTQ With AutoGPTQ

Build a calibration set, run a 4-bit GPTQ pass with AutoGPTQ, and save the quantized model.

Schematic with three steps: Set the 4-bit config; Run the quantization pass; Save and check the quantized model.

GPTQ quantizes weights one layer at a time, using a small calibration dataset to figure out how rounding each weight will affect the layer output, then adjusting the remaining weights to compensate for the error. That second-order, error-correcting approach makes GPTQ very good at 4-bit. AutoGPTQ is the standard tool for running it for GPU inference with vLLM, TGI or Transformers, and it needs a calibration dataset of a few hundred representative text samples.

What you need

  • A Hugging Face model id, such as a Llama 3.1 8B instruct model
  • Python with transformers and auto_gptq installed
  • A calibration set of a few hundred representative text samples

Step 1: Set the 4-bit config

Create a quantization config with bits set to 4 and group size set to 128, a common well-tested setting, with desc_act left off. The group size controls how many weights share a scale factor.

Step 2: Run the quantization pass

Load the model with the config, tokenize the calibration samples, and run model.quantize on them. The calibration pass takes minutes to a couple of hours depending on model size, but it only happens once.

Step 3: Save and check the quantized model

Save the quantized model and its tokenizer to a new folder. Load it in vLLM or Transformers like any other model and test it on a real task before using it.

Worked example: synthetic AutoGPTQ run

The table below is a synthetic example. It shows the expected result of a GPTQ run; no model was quantized.

Step Setting Result
Config bits 4, group_size 128 Set
Calibration A few hundred samples Pass completed
Save 4-bit model folder Saved
Load test Loads in Transformers Pass

Expected result (UNTESTED). the 4-bit GPTQ model saves cleanly and loads in Transformers like any other model. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Smaller groups, such as 128, preserve more quality at the cost of slightly more memory for the scale factors.
  • The desc_act option reorders quantization by activation magnitude, which can improve quality but slows inference, so most people leave it off.
  • GPTQ needs a calibration pass; AWQ is faster to apply and tends to preserve quality well, especially for instruction-tuned models.
  • Once saved, the model loads in vLLM or Transformers like any other model, and inference runs at roughly the memory footprint you would expect from 4-bit weights.

Sources

See all