GPTQ › Optimal Brain Quantization

Michael BrenndoerferJanuary 13, 20264 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 320GPTQ › Optimal Brain Quantization
Reading section 4 of 17 · Optimal Brain Quantization · 4 minSection 4 of 17 · 4 min

Book contents

Section 4 of 17 · 4 min read

GPTQ › Optimal Brain QuantizationOne section · Sections in this chapter4 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

GPTQ builds on a framework called Optimal Brain Quantization (OBQ), which itself descends from classical work on neural network pruning. The historical connection is illuminating: pruning and quantization are closely related problems. In pruning, we set certain weights exactly to zero. In quantization, we round weights to the nearest value in a discrete set. Both operations introduce error, and in both cases we want to minimize the impact on the network's output. The mathematics that describes the optimal pruning correction turns out to describe the optimal quantization correction as well.

The core insight of OBQ is that when you quantize one weight, you can compute the optimal adjustment to all remaining weights that minimizes the resulting increase in loss. This is not a heuristic or an approximation: given the quadratic structure of our loss function, there exists a closed-form formula for the best possible compensation. The formula emerges from setting the derivative of the loss (after fixing one weight to its quantized value) to zero with respect to the remaining weights and solving analytically.

Think of OBQ as a chess player who, after being forced to sacrifice a piece, immediately recalculates the optimal position for all remaining pieces. The sacrifice introduces a loss, but the player compensates by repositioning everything else to minimize the damage. In GPTQ, the "sacrifice" is the rounding error introduced when a weight is quantized, and the "repositioning" is the adjustment applied to all remaining weights.

Suppose we have decided to quantize weight wqw_q to value w^q\hat{w}_q. We want to adjust the remaining weights wF\mathbf{w}_F (where FF denotes the set of weights not yet quantized) to minimize the resulting loss. Taking the derivative of the loss with respect to δwF\delta\mathbf{w}_F and setting it to zero yields the optimal adjustment:

δwF=−wq−w^q[H−1]qq⋅[H−1]F,q\delta\mathbf{w}_F = -\frac{w_q - \hat{w}_q}{[\mathbf{H}^{-1}]_{qq}} \cdot [\mathbf{H}^{-1}]_{F,q}

where:

  • δwF\delta\mathbf{w}_F: the optimal adjustment vector for the remaining unquantized weights
  • wq−w^qw_q - \hat{w}_q: the quantization error for the current weight qq, the difference between what we wanted and what we got after rounding
  • [H−1]qq[\mathbf{H}^{-1}]_{qq}: the diagonal element of the inverse Hessian corresponding to weight qq
  • [H−1]F,q[\mathbf{H}^{-1}]_{F,q}: the elements of the qq-th column of the inverse Hessian for all remaining weights FF

Let us unpack this formula carefully to build intuition for what each component contributes. The numerator wq−w^qw_q - \hat{w}_q is the quantization error for weight qq: simply the difference between what we wanted and what we got after rounding. Larger errors require larger compensations.

The denominator [H−1]qq[\mathbf{H}^{-1}]_{qq} is the inverse curvature, measuring how flexible the loss landscape is with respect to weight qq. When this value is large, the curvature of the loss with respect to wqw_q is small, meaning errors in this weight are relatively easy to absorb through small adjustments elsewhere. When this value is small, the curvature is high, meaning errors in this weight are costly and difficult to compensate.

The vector [H−1]F,q[\mathbf{H}^{-1}]_{F,q} determines how this error should be distributed across remaining weights. It acts like a routing map: weights that are strongly correlated with wqw_q through the inverse Hessian receive larger adjustments, because adjusting them has the greatest effect on undoing the damage caused by the rounding error in wqw_q.

The resulting increase in loss from quantizing weight qq (after optimal compensation) is:

ΔLq=(wq−w^q)22[H−1]qq\Delta\mathcal{L}_q = \frac{(w_q - \hat{w}_q)^2}{2[\mathbf{H}^{-1}]_{qq}}

where:

  • ΔLq\Delta\mathcal{L}_q: the unavoidable increase in reconstruction error caused by quantizing weight qq
  • (wq−w^q)2(w_q - \hat{w}_q)^2: the squared quantization error (larger errors cost more)
  • [H−1]qq[\mathbf{H}^{-1}]_{qq}: the inverse curvature, representing the "stiffness" of weight qq; smaller values mean more damage per unit of rounding error

This formula tells us exactly how much each quantization decision costs, accounting for optimal compensation. It has an elegant structure: cost is proportional to the squared rounding error and inversely proportional to the inverse curvature. Weights where [H−1]qq[\mathbf{H}^{-1}]_{qq} is large can be quantized with relatively low cost even if the rounding error is substantial. Conversely, weights with small [H−1]qq[\mathbf{H}^{-1}]_{qq} values are stiff: even small errors cause significant loss increases. This cost formula is exactly what OBQ uses to decide which weight to quantize next, always choosing the one with minimum ΔLq\Delta\mathcal{L}_q.