PigeonColumba compressa

Lesson 10 of 10

GPTQ's error-compensation loop

Round-to-nearest quantizes each weight independently. GPTQ notices that what we care about is the layer's output on real inputs X, and that the error from rounding one column can be partly cancelled by nudging the columns that haven't been quantized yet.

With H = 2 X^T X / N and U the upper Cholesky factor of H^-1 (so H^-1 = U^T U), process columns j = 0, 1, ... of W (shape out x in):

  1. q = round_to_grid(W[:, j])
  2. err = (W[:, j] - q) / U[j, j]
  3. W[:, j+1:] -= outer(err, U[j, j+1:])
  4. store q as column j of the result.

Step 3 is the Optimal Brain Surgeon update: the change to the remaining weights that best compensates the error, given the input statistics.

You get quantize_column(col, scale, qmax) and the Cholesky factor. Implement gptq(W, X, bits) with one scale per output row computed from the original W. The test checks that your output error on X is well below plain rounding's.

Your code

Where this is used