Search

Search papers and lessons

GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transfor...

Elias Frantar, Saleh Ashkboos, Torsten Hoefler et al. · 2022

GPTQ shrinks a large language model's weights to 3 or 4 bits each, without retraining, by rounding one weight at a time and correcting the rest to cancel the error.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Neural network basics
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

GPTQ stores a trained language model's weights with 3 or 4 bits each instead of 16, and it does this without any retraining. It works one layer at a time: it rounds one weight, then adjusts the weights that are not yet rounded so they cancel part of the damage, using a matrix built from 128 calibration segments of 2048 tokens each. The authors compressed the 175-billion-parameter OPT model in about four GPU hours on one A100, with almost no loss in perplexity.

Builds on: Quantization, Hessian Matrix, Cholesky Decomposition, Matrix Inverse, Linear Regression, Transformer · Leads to: Post-Training Quantization

Why it mattered

Before GPTQ, the accurate ways to shrink a network needed retraining, and the cheap way (rounding each weight to the nearest level) broke down at 3 bits on models this large. GPTQ showed that a careful one-shot method could handle models with hundreds of billions of parameters. The paper reports that OPT-175B fits on a single 80 GB A100 at 3 bits, where the FP16 version needs five such GPUs. The authors released a reference implementation.

The path to it ran through older work. Post-training quantization methods such as AdaRound were built for vision networks with millions of parameters. Optimal Brain Quantization (OBQ) gave a strong second-order method, but its cost was too high for language models. When open models with tens of billions of parameters appeared in 2022, practical work such as LLM.int8() used simple rounding, which the GPTQ authors showed fails at 3 bits. GPTQ was posted to arXiv in October 2022. It combines OBQ's error compensation with a fixed column order, lazy block updates and a Cholesky reformulation, which together make the method fast enough for 175-billion-parameter models.

The problem

A linear layer multiplies its input by a weight matrix W\mathbf{W}. Quantization replaces each weight with a value from a small grid, for example the 16 levels of a 4-bit code. The obvious approach, round-to-nearest (RTN), ignores what the layer does with its inputs, so a weight can be rounded in the direction that hurts the outputs most.

The better methods look at the layer's output error. OBQ does this well, but its cost grows with the cube of the layer width. For a weight matrix with drowd_{\text{row}} rows and dcold_{\text{col}} columns it takes time proportional to drowdcol3d_{\text{row}} d_{\text{col}}^3, which is far too slow for a model with 175 billion weights.

Two things pull against each other. The layer's output error depends on the weights only through a matrix built from the inputs, the Hessian, so that is the right quantity to minimise. But a model with billions of weights leaves almost no time for per-weight linear algebra on that matrix.

How it works

Take one output row of a layer, with two weights and two sample inputs. The goal is to choose rounded weights that keep both outputs close to the originals. The inputs are summarised in a 2 by 2 matrix HH, and its inverse is computed once for the layer.

The toy setup: w=(0.6,0.6)w = (0.6, 0.6) is the full-precision row, the grid is the integers (rounding means going to the nearest whole number), and the inverse Hessian is H−1=(0.5−0.25−0.250.25)H^{-1} = \begin{pmatrix} 0.5 & -0.25 \\ -0.25 & 0.25 \end{pmatrix}. Its inverse, the Hessian itself, is H=(4448)H = \begin{pmatrix} 4 & 4 \\ 4 & 8 \end{pmatrix}, and the layer's squared output error for a row with error ee is 12e⊤He\tfrac{1}{2} e^\top H e.

  1. Baseline: round every weight. RTN sends 0.6 to 1 and 0.6 to 1, giving (1,1)(1, 1). The layer's squared output error on the samples is 1.6.
  2. Quantize column 1 first. Round w1=0.6w_1 = 0.6 to 1. The error on this weight is 0.6−1=−0.40.6 - 1 = -0.4. The weight is now fixed.
  3. Spread the error to column 2. Column 2 is still in full precision, so move it to cancel part of the damage. Compute E=(w1−quant(w1))/H11−1=−0.4/0.5=−0.8E = (w_1 - \mathrm{quant}(w_1)) / H^{-1}_{11} = -0.4 / 0.5 = -0.8, then move w2w_2 by −E⋅H21−1=−(−0.8)(−0.25)=−0.2-E \cdot H^{-1}_{21} = -(-0.8)(-0.25) = -0.2. So w2w_2 becomes 0.6−0.2=0.40.6 - 0.2 = 0.4.
  4. Quantize column 2. Round 0.40.4 to 0. The result is (1,0)(1, 0), and the squared error on the samples is 0.80. That is half the RTN error, on the same grid.

Step 3 is the whole method in miniature: rounding w1w_1 is not free, but part of its damage can be paid back by the weights still in full precision.

flowchart LR
  A["Layer weights and sample inputs"] --> B["Hessian H = 2 X X^T"]
  B --> C["Inverse, then Cholesky factor"]
  C --> D["For each column j in order"]
  D --> E["Round column j to the grid"]
  E --> F["Spread its error onto columns after j"]
  F --> D
  D --> G["Rounded layer"]

The paper's full procedure (its Algorithm 1) adds three ideas to the toy:

  • Fixed order. OBQ picks the weight with the smallest error next, which differs per row. GPTQ goes through the columns in one fixed order for every row, so the same inverse serves all rows. The authors found this costs little accuracy on large layers.
  • Lazy batches. Updates are applied to blocks of 128 columns at a time, so the GPU does large matrix operations instead of many small memory-bound ones. The updates to later columns are applied once per block.
  • Cholesky form. On large matrices, round-off can make the inverse indefinite, which sends the updates in wrong directions. GPTQ computes the Cholesky factor of the inverse once per layer and reads the rows it needs from it. A small dampening term, 1 percent of the average diagonal value, is added to HH as a safeguard.

The calibration data is 128 random segments of 2048 tokens from the C4 web-text corpus. Layers are quantized in turn, and the inputs to each block come from the already-quantized blocks before it, so earlier errors are included in the calibration.

The math

Layer objective. Let W\mathbf{W} be the weights of a linear layer, X\mathbf{X} the matrix of mm sample inputs (one column per sample), and W^\widehat{\mathbf{W}} the quantized weights. The goal is

min⁡W^  ∥WX−W^X∥22\min_{\widehat{\mathbf{W}}} \; \lVert \mathbf{W}\mathbf{X} - \widehat{\mathbf{W}}\mathbf{X} \rVert_2^2

with every entry of W^\widehat{\mathbf{W}} restricted to the quantization grid. Working one output row w\mathbf{w} at a time, the error of a candidate row w^\widehat{\mathbf{w}} is a quadratic form. With H=2XX⊤\mathbf{H} = 2\mathbf{X}\mathbf{X}^\top,

(w−w^)⊤XX⊤(w−w^)=12 (w−w^)⊤H (w−w^).(\mathbf{w} - \widehat{\mathbf{w}})^\top \mathbf{X}\mathbf{X}^\top (\mathbf{w} - \widehat{\mathbf{w}}) = \tfrac{1}{2}\,(\mathbf{w} - \widehat{\mathbf{w}})^\top \mathbf{H}\,(\mathbf{w} - \widehat{\mathbf{w}}).

Step 2 in symbols (the greedy update). Quantizing one weight wqw_q to quant(wq)\mathrm{quant}(w_q) is ranked by (quant(wq)−wq)2/[H−1]qq(\mathrm{quant}(w_q) - w_q)^2 / [\mathbf{H}^{-1}]_{qq}, where [H−1]qq[\mathbf{H}^{-1}]_{qq} is the diagonal entry for weight qq. The other unquantized weights, the set FF, then move by

δF=−wq−quant(wq)[H−1]qq (H−1):,q.\bm{\delta}_F = -\frac{w_q - \mathrm{quant}(w_q)}{[\mathbf{H}^{-1}]_{qq}} \, (\mathbf{H}^{-1})_{:,q}.

Here (H−1):,q(\mathbf{H}^{-1})_{:,q} is column qq of the inverse.

Step 3 in symbols (the toy numbers). Here [H−1]11=0.5[\mathbf{H}^{-1}]_{11} = 0.5 and (H−1)21=−0.25(\mathbf{H}^{-1})_{21} = -0.25. The error is w1−quant(w1)=0.6−1=−0.4w_1 - \mathrm{quant}(w_1) = 0.6 - 1 = -0.4, so the move on weight 2 is

δ2=−−0.40.5⋅(−0.25)=−0.2,\delta_2 = -\frac{-0.4}{0.5} \cdot (-0.25) = -0.2,

and w2=0.6−0.2=0.4w_2 = 0.6 - 0.2 = 0.4, as in the walkthrough.

Block version. When a block of BB columns is quantized together, the move on the later weights is

δF=−(wQ−quant(wQ))([H−1]QQ)−1(H−1):,Q,\bm{\delta}_F = -\left(\mathbf{w}_Q - \mathrm{quant}(\mathbf{w}_Q)\right)\left([\mathbf{H}^{-1}]_{QQ}\right)^{-1} (\mathbf{H}^{-1})_{:,Q},

where QQ is the set of weights in the block. Only the B×BB \times B block of the inverse is inverted, which is why this is cheap.

Runtime. Doing the updates weight by weight costs O(drow dcol3)O(d_{\text{row}} \, d_{\text{col}}^3). The inverse depends only on the inputs, not on the weights, so it is updated once per column rather than once per weight. The total cost becomes O(max⁡{drowdcol2,dcol3})O(\max\{d_{\text{row}} d_{\text{col}}^2, d_{\text{col}}^3\}), a saving of a factor min⁡{drow,dcol}\min\{d_{\text{row}}, d_{\text{col}}\} over OBQ. For the very large layers of GPT-style models that difference is several orders of magnitude.

Results

The paper evaluates perplexity on WikiText-2 (lower is better), plus PTB and C4, and a set of zero-shot tasks. The main numbers for the two largest open models:

  • Accuracy at 4 bits. On OPT-175B, GPTQ gives 8.37 against 8.34 for FP16 and 10.54 for RTN. On BLOOM-176B, GPTQ gives 8.21 against 8.11 for FP16 and 8.37 for RTN. RTN on the 175B model is worse than the 10 times smaller FP16 13B model (perplexity 10.13).
  • Accuracy at 3 bits. RTN collapses on the large models (7.3e3 on OPT-175B). GPTQ gives 8.68 on OPT-175B and 8.64 on BLOOM-176B, and keeps reasonable perplexity across the OPT and BLOOM families.
  • Grouping. Group size 128 brings OPT-175B at 3 bits to 8.45, about 0.1 from FP16, at a cost of about 0.15 extra bits per weight.
  • Extreme regimes. At about 2.2 bits per weight (group size 128), the perplexity increase is under 1.5 points. Ternary weights with group size 8 reach 9.20 on OPT-175B.
  • Speed. A 3-bit OPT-175B runs at 71 ms per token on one A100, against 230 ms for FP16 on five GPUs, a 3.24 times speedup. On two A6000 GPUs it runs at 130 ms against 589 ms for FP16 on eight GPUs, a 4.5 times latency reduction.

To read the 4-bit row as a number: the RTN gap to FP16 is 10.54−8.34=2.2010.54 - 8.34 = 2.20 perplexity points, and the GPTQ gap is 8.37−8.34=0.038.37 - 8.34 = 0.03 points. The GPTQ increase is 0.03/8.340.03 / 8.34, about 0.36 percent. The stored weights are four times smaller than FP16 at 4 bits.

What the method does not do

  • It is tempting to think GPTQ retrains the model. It runs no gradient steps: it does forward passes over calibration tokens to collect layer inputs, then solves a least-squares-style problem for each layer.
  • It is tempting to think each weight should be rounded as close as possible to its original value. The error that matters is the output error, and a weight can be rounded in an apparently worse direction if the others compensate.
  • It is tempting to think the greedy order is best. It is a heuristic. On the toy row, the rounding (0,1)(0, 1) gives output error 0.40, which beats the 0.80 that GPTQ's fixed order reaches. The paper argues the order matters little on average for large layers, not that it is optimal.
  • It is tempting to think 3-bit weights make the model three times faster. The speedup comes from moving fewer bytes, not from cheaper multiplications. The paper says its method does not yet speed up the multiplications themselves, because mainstream hardware lacks fast mixed-precision operands, and it quantizes weights only.
  • Group size adds bits. A "3-bit" model with groups is slightly larger than three bits per weight.

The main limits: results cover perplexity and zero-shot tasks on OPT and BLOOM only, the C4 calibration data makes the C4 perplexity not fully zero-shot, and activation quantization is left to other techniques.

Reading guide

Check yourself

QuestionWhy does GPTQ need the inverse Hessian rather than just the weights?Show answer
The layer's output error depends on the weights only through the inputs. The Hessian H=2XX⊤\mathbf{H} = 2\mathbf{X}\mathbf{X}^\top summarises how the weights interact through those inputs, and its inverse says how to move the unrounded weights to cancel the error from a rounded one.
QuestionA layer has H−1\mathbf{H}^{-1} with entries [H−1]11=0.5[\mathbf{H}^{-1}]_{11} = 0.5 and (H−1)21=−0.25(\mathbf{H}^{-1})_{21} = -0.25, and w=(0.4,0.4)\mathbf{w} = (0.4, 0.4) on the integer grid. Round w1w_1 with GPTQ's first step and find the new w2w_2.Show answer
The error on w1w_1 is 0.4−0=0.40.4 - 0 = 0.4. The move is −(0.4)/0.5⋅(−0.25)=+0.2-(0.4)/0.5 \cdot (-0.25) = +0.2, so w2w_2 becomes 0.4+0.2=0.60.4 + 0.2 = 0.6.
QuestionWhy does a fixed column order help GPTQ on large models, compared with OBQ's per-row greedy order?Show answer
A fixed order means every row uses the same unquantized set and the same inverse matrix, so the inverse is updated once per column rather than once per weight. The paper's experiments show that for large layers the choice of order changes the final error only slightly.
QuestionA model has 175 billion weights. Compared with FP16, how many times smaller are the quantized weights at 3 bits, ignoring the embedding layers?Show answer
FP16 uses 16 bits per weight, so 16/3≈5.316 / 3 \approx 5.3. The 3-bit weights are about five times smaller, ignoring the embeddings and the scale factors.

Sources

Read the full paper

Elias Frantar, Saleh Ashkboos, Torsten Hoefler et al., 2022. arXiv:2210.17323