GPTQ post-training quantization technique implemented by GPT-QModel

GPT-QModel, a Python package named gptqmodel, implements the GPTQ post-training quantization technique.

Verified

On this page

Definition

The GPT-QModel project is a Python package named gptqmodel, and it implements the GPTQ algorithm. GPTQ is a post-training quantization technique in which each row of the weight matrix is quantized independently. The goal of this row-by-row quantization is to find a version of the weights that minimizes the error.

GPT-QModel is the actively maintained backend for GPTQ in Transformers. AutoGPTQ is no longer supported in Transformers.

At a glance

In GPTQ, each row of the weight matrix is quantized independently, and the weights are quantized to int4 and restored to fp16 during inference.

Diagram: Each row of weights is independently quantized to int4 to find a version of the weights that minimizes the error. > During inference, the int4 weights are restored to fp16 on the fly.1Each row of weights is independently quantizedto int4 to find a version of the weights thatminimizes the error.2During inference, the int4 weights are restored to fp16 on the fly.

How it works

The documentation says to install Accelerate, Transformers and Optimum first. Then run the documented command to install GPT-QModel. Next, a GPTQConfig class is created with the number of bits to quantize to, a dataset to calibrate the weights for quantization, and a tokenizer to prepare the dataset.

The dataset can be your own list of strings, but the documentation highly recommends the same dataset used in the GPTQ paper. To quantize a model, pass GPTQConfig to from_pretrained when loading the model. Setting device_map to auto automatically offloads the model to a CPU to help fit it in memory, and it lets model modules move between the CPU and GPU for quantization.

If memory runs out because a dataset is too large, the documentation suggests passing the max_memory parameter to allocate memory on the GPU and CPU, and disk offloading is not supported. Quantizing a model from scratch takes time, with the duration depending on the hardware. Before quantizing a model, the documentation says it is a good idea to check the Hub for an existing GPTQ-quantized version of that model.

Marlin is a 4-bit only CUDA GPTQ kernel that the documentation describes as highly optimized for the NVIDIA A100 GPU (Ampere) architecture. The documentation says Marlin's loading, dequantization and execution of post-dequantized weights are highly parallelized, offering a substantial inference improvement versus the original CUDA GPTQ kernel. Marlin is only available for quantized inference and does not support model quantization.

Marlin inference can be activated with the backend parameter in GPTQConfig. GPT-QModel was originally forked from AutoGPTQ. The documentation says GPT-QModel provides asymmetric quantization, which may lower quantization errors compared to symmetric quantization.

GPT-QModel is not backward compatible with legacy AutoGPTQ checkpoints, and not all kernels support asymmetric quantization, with Marlin named as one of them. The documentation also says the Marlin kernels are updated for A100 GPUs, and other kernels are updated to include auto-padding for legacy models and models with non-uniform in/out-features.

Example

The documentation's example flow starts from a GPTQConfig that sets the number of bits, a calibration dataset and a tokenizer. The model to quantize is loaded with GPTQConfig passed to from_pretrained. With device_map set to auto, the model is automatically offloaded to a CPU, and its modules can move between the CPU and GPU for quantization.

In the documentation's timing note, quantizing the facebook/opt-350m model takes about 5 minutes on a free-tier Google Colab GPU, while quantizing a 175B parameter model takes about 4 hours on a NVIDIA A100. The documentation points to a GPTQ quantization with PEFT notebook for a hands-on experience. Once the model is quantized, push_to_hub can push the model and tokenizer to the Hub.

save_pretrained saves the quantized model locally. If the model was quantized with the device_map parameter, the entire model must be moved to a GPU or CPU before saving. A quantized model can be reloaded with from_pretrained.

From the official source
python
from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig

tokenizer = AutoTokenizer.from_pretrained("facebook/opt-125m")
gptq_config = GPTQConfig(bits=4, dataset="c4", tokenizer=tokenizer)
From the official source
python
quantized_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m", device_map="auto", quantization_config=gptq_config)
From the official source
python
quantized_model.push_to_hub("opt-125m-gptq")
tokenizer.push_to_hub("opt-125m-gptq")
From the official source
python
quantized_model.save_pretrained("opt-125m-gptq")
tokenizer.save_pretrained("opt-125m-gptq")

# if quantized with device_map set
quantized_model.to("cpu")
quantized_model.save_pretrained("opt-125m-gptq")
From the official source
python
from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto")

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.