Ran Wei/ Maths for AI and LLMs
中文
Maths for AI and LLMs · Numerical computing

Module 8Quantisation and measuring numerical error

Quantise a tiny weight vector and bound the effect of rounding on its dot product.

70 minYou need Vectors, absolute value and Module 7

In this module you will

  • Compute quantised integer codes and dequantised values
  • Identify when clipping invalidates the usual half-step error bound
  • Compare an approximation with a reference calculation
1

Convert between floats and integer codes

For a simple symmetric quantiser choose a scale s>0s>0 and an integer limit K>0.K>0\mathclose{\text{.}} Store q=clip⁡(round⁡(x/s),−K,K)q=\operatorname{clip}(\operatorname{round}(x/s),-K,K) and reconstruct x^=sq.\hat x=sq\mathclose{\text{.}} Specify a tie rule for rounding. For example, Python’s round-to-nearest sends an exact tie to the even integer. With K=127,K=127\mathclose{\text{,}} the integer codes fit in a signed 8-bit field.

If rounding alone produces a code in the allowed range, the absolute reconstruction error is at most s/2.s/2\mathclose{\text{.}} If clipping changes the code, the error can exceed that bound. Fixing a narrow representable range improves resolution for small values at a given number of codes and can clip more outliers. Allowing per-group scales can adapt the resolution to groups with different magnitudes. Every scale also needs to be stored or recovered.

Example 8.1Clip and round✓ Checked

With s=1/4s=1/4 and K=3,K=3\mathclose{\text{,}} an input of 0.300.30 maps to code 11 and reconstructs as 0.25.0.25\mathclose{\text{.}} An input of 1.201.20 maps to code 33 after clipping and reconstructs as 0.75,0.75\mathclose{\text{,}} with error 0.45.0.45\mathclose{\text{.}}

2

Bound the effect on a prediction

Let ww be a weight vector, w^\hat w its reconstruction, and xx an input vector. With no clipping and common scale s,s\mathclose{\text{,}} the triangle inequality gives

∣xTw^−xTw∣≤∑i∣xi∣ ∣w^i−wi∣≤s2∥x∥1.\left|x^\mathsf T\hat w-x^\mathsf T w\right| \le\sum_i |x_i|\,|\hat w_i-w_i| \le \frac{s}{2}\lVert x\rVert_1.

This is an absolute bound in exact arithmetic, for quantised weights and original inputs. Actual implementation may introduce more error during multiplication or accumulation. A very small original dot product can result in large relative error, even with a small absolute error.

Compute itCompare exact rational reference values

For weights (3/10,−11/20,1/5),(3/10,-11/20,1/5)\mathclose{\text{,}} inputs (2,−1,4)(2,-1,4) and scale 1/4,1/4\mathclose{\text{,}} compare the quantised dot product with the unquantised dot product.

Python
from fractions import Fraction as F

weights = [F(3, 10), F(-11, 20), F(1, 5)]
inputs = [2, -1, 4]
scale, limit = F(1, 4), 3
codes = [max(-limit, min(limit, round(w / scale))) for w in weights]
reference = sum(x * w for x, w in zip(inputs, weights))
approximation = sum(x * q * scale for x, q in zip(inputs, codes))
print(codes)
print(reference, approximation, abs(reference - approximation))
print(scale * sum(map(abs, inputs)) / 2)
Output
[1, -2, 1]
39/20 2 1/20
7/8

To evaluate quantisation over a set of inputs, report the scale rule, tie rule, reference precision, absolute error and the downstream metric. Check that the held-out evaluation matches across runs. In the toy example the absolute error is 1/20,1/20\mathclose{\text{,}} well within the bound of 7/8.7/8\mathclose{\text{.}} Real input distributions can have very different errors from one chosen vector.

3

Exercises

Exercise 8.1★Reconstruct a weight

At scale 1/10,1/10\mathclose{\text{,}} reconstruct the integer code −7.-7\mathclose{\text{.}}

Show solution✓ Checked

Multiply the code by the scale, giving −7/10.-7/10\mathclose{\text{.}}

Exercise 8.2★★An exact rounding tie

At scale 1/4,1/4\mathclose{\text{,}} which code does 5/85/8 get with ties to even and K=3?K=3\mathclose{\text{?}}

Show solution✓ Checked

The scaled input is 5/2.5/2\mathclose{\text{.}} Its nearest even integer is 2.2\mathclose{\text{.}} The reconstructed value is 1/21/2 and the absolute error is 1/8.1/8\mathclose{\text{.}}

Exercise 8.3★★★Bound a dot product

For input x=(−1,2,−2)x=(-1,2,-2) and scale 1/10,1/10\mathclose{\text{,}} what absolute error bound follows when no weights clip?

Show solution✓ Checked

The one-norm is 5.5\mathclose{\text{.}} Multiply by half of 1/101/10 to obtain 1/4.1/4\mathclose{\text{.}}

4

Check yourself

1
What is the no-clipping absolute error limit per coordinate?
2
What can clipping do to that bound?
3
Which quantity multiplies s/2s/2 in the derived dot-product bound?
Finished this module?