Numerics Explained
← /learn · 07

GPTQ

Quantise one column, push its error into the columns still to come, weighted by the inverse Hessian: second-order error compensation, column by column.

Check yourself ↓ three questions and what to remember, at the end

Loading the animation…

Concept

Rounding each weight to its nearest grid point (round-to-nearest, RTN) minimises the error in the weights. What matters is the error in the layer's output, ∥WX−W^X∥2\| W X - \hat W X \|^2 over real inputs XX, and there the errors of different weights can cancel. GPTQ (Frantar et al.) quantises a layer one input column at a time and, after each column, adjusts the columns not yet quantised to compensate for the error just made, using second-order information about the inputs: the Hessian H=2XX⊤\mathbf{H} = 2 X X^\top of the output error.

Watch the animation: a column turns into its quantised values, and the weights to its right shift a little (the middle panel), each by an amount set by how strongly its input is correlated with the one just rounded. The bottom panel tracks the layer's output error with the columns done so far quantised. Plain rounding of the same columns climbs steadily; GPTQ climbs more slowly, because each step's compensation undoes part of the damage. On this layer (8 outputs, 16 inputs, 64 calibration inputs) GPTQ ends at 1.267 against 2.57 for INT4 round-to-nearest, 2.029 times smaller; at INT3, 11.47 against 15.78.

GPTQ's contribution was making this affordable at scale: the paper quantises 175-billion-parameter models to 3 or 4 bits per weight in about four GPU hours. It processes every row in the same column order (which turns out to cost little accuracy), batches the updates so the GPU stays busy, and works from a Cholesky factor of H−1\mathbf{H}^{-1} for numerical stability.

Maths

GPTQ descends from Optimal Brain Quantization (OBQ), itself from Optimal Brain Surgeon. For one output row ww with the quadratic loss (w−w^)⊤H(w−w^)/2(w - \hat w)^\top \mathbf{H} (w - \hat w)/2, quantising coordinate jj to qjq_j and letting the free coordinates FF re-optimise gives the update

δF=−wj−qj[HF−1]jj (HF−1):,j.\delta_F = -\frac{w_j - q_j}{[\mathbf{H}_F^{-1}]_{jj}} \, (\mathbf{H}_F^{-1})_{:,j}.

After removing jj, the inverse Hessian of the remaining coordinates is one step of Gaussian elimination on HF−1\mathbf{H}_F^{-1}. Going through the columns in a fixed order, those successive eliminations are exactly the rows of the upper Cholesky factor UU of H−1\mathbf{H}^{-1}, so the whole algorithm needs one Cholesky decomposition, not one matrix update per weight. The dampening term (here 1% of the mean diagonal added to H\mathbf{H}) keeps the factorisation stable when inputs are nearly collinear.

Code

The inner loop, cut from src/lib/num/model.ts (U is the upper Cholesky factor of H−1\mathbf{H}^{-1}; the scales are fixed per output row from the original weights):

for (let j = 0; j < d; j++) {
  const errs: number[] = [];
  for (let r = 0; r < rows; r++) {
    const w = Wc[r]![j]!;
    const q = dequantInt(quantInt(w, params[r]!), params[r]!);
    Q[r]![j] = q;
    const e = (w - q) / U[j]![j]!;
    errs.push(e);
    for (let k = j + 1; k < d; k++) Wc[r]![k]! -= e * U[j]![k]!;
  }

The Python reference is checked against a numpy implementation of the same algorithm, and this TypeScript port against the Python, bit for bit.

Check yourself

Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.

Q1On this layer at INT4, how many times smaller is GPTQ's output error than round-to-nearest's?
Reveal the answer

Answer: 2.029 (counted right within 10%)

2.029 times: each column's rounding error is partly undone by adjusting the columns not yet quantised.

GPTQ, column by column →

Commit to a number, then reveal.

Q2What does GPTQ minimise that round-to-nearest does not?
Reveal the answer

Answer: The error in the layer's output over real inputs

The output error ∥WX−W^X∥2\|WX - \hat W X\|^2. Round-to-nearest minimises each weight's own error; in the output, errors of correlated inputs can cancel.

Weights against outputs →

Pick one, then reveal.

Q3After a column is quantised, how do the weights to its right change?
Reveal the answer

Answer: Each by an amount set by how strongly its input correlates with the column just rounded

The Hessian H=2XX⊤\mathbf{H} = 2XX^\top of the output error says how the inputs correlate; the update spreads each column's error over the correlated columns not yet done.

GPTQ, column by column →

Pick one, then reveal.